c - 在 C 中,如何将接收到的 rtp 数据包存储到 .pcap 文件中 - UDP

标签 c sockets rtp packets

我需要将使用 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 是否需要添加任何额外的内容?

最佳答案

您必须添加以下 header 才能将数据包写入 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;

记录(数据包) header :

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示例代码片段。

关于c - 在 C 中,如何将接收到的 rtp 数据包存储到 .pcap 文件中 - UDP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26581858/

相关文章:

sip - Asterisk 直接媒体和 NAT

java - 如何使用 Xuggler 在 RTP 数据包中编码媒体文件

streaming - 流媒体服务器中的 RTP 或 RTSP 有什么区别?

c - 无限期地 sleep

C 代码,随机骰子游戏未给出正确答案需要帮助

Java 字节数组和带有套接字的图像

multithreading - Indy 10 IdTCPClient 使用单独的线程读取数据?

c# - 在 C# 中广播到多个 IP 地址

c - 将 FILE* 结构映射到缓冲区?

c - 检查所有可能值的递归方法(C)