【问题标题】:In C, How can i store received rtp packets into .pcap file - UDP在 C 中,我如何将接收到的 rtp 数据包存储到 .pcap 文件中 - UDP
【发布时间】:2014-10-27 05:53:29
【问题描述】:

我需要将使用 recvfrom() 接收的 rtp 数据包存储到 .pcap 文件中。

我试图打开一个扩展名为 .pcap 的文件并直接 fwrite 我收到的任何内容(即 void *buf),但是当我尝试使用wireshark 打开该文件时,它给出的错误语句为“无法理解” 我应该怎么做才能让它变得容易理解??

我使用的代码:

recvfrom(sockfd, buf, len, flags, (struct sockaddr *)&src_addr->ss, &src_addr->len);
fp=fopen("test.pcap","a+");
fprintf(fp, "%s",buf);

在 UDP 连接中,我如何接收 rtp 数据包并将其存储到可理解的 .pcap 文件中? 收到的 rtp pcakets 有什么需要补充的吗?

【问题讨论】:

  • 不要为此使用fprintf。使用fwrite
  • 您可以阅读libpcapFileFormat。 .pcap 文件需要一些文件头才能被wireshark 理解。
  • 我试过 fprintf 和 fwrite,都没有工作@Qix

标签: c sockets rtp packets


【解决方案1】:

您必须添加以下标头才能将数据包写入 pcap 文件。

全局标题

This header starts the libpcap file and will be followed by the first packet header:

   typedef struct pcap_hdr_s {
            guint32 magic_number;   /* magic number */
            guint16 version_major;  /* major version number */
            guint16 version_minor;  /* minor version number */
            gint32  thiszone;       /* GMT to local correction */
            guint32 sigfigs;        /* accuracy of timestamps */
            guint32 snaplen;        /* max length of captured packets, in octets */
            guint32 network;        /* data link type */
    } pcap_hdr_t;

记录(包)头:

Each captured packet starts with (any byte alignment possible):

typedef struct pcaprec_hdr_s {
        guint32 ts_sec;         /* timestamp seconds */
        guint32 ts_usec;        /* timestamp microseconds */
        guint32 incl_len;       /* number of octets of packet saved in file */
        guint32 orig_len;       /* actual length of packet */
} pcaprec_hdr_t;

查看link 以了解标题的解释。

查看link 获取代码示例 sn-p。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-08
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多