简体   繁体   English

我如何找到由套接字SOCK_DGRAM选项创建的ip / udp标头

[英]how can i find ip/udp header while it created by socket SOCK_DGRAM option

can anyone tell how can i find ip/udp header while it created by socket type SOCK_DGARM option.in my VOIP application while this option set sendto() function send only RTP data not whole buffer of IP/UDP/RTP header and data.that's why i want to find that where IP/UDP header are created.so can anyone tell at which point i find it..??? 谁能告诉我如何通过套接字类型SOCK_DGARM选项创建的ip/udp标头。在我的VOIP应用程序中,同时此选项设置sendto()函数仅发送RTP数据,而不发送IP/UDP/RTP标头和数据的整个缓冲区。为什么我要找到在哪里创建IP/UDP标头的人,所以谁能告诉我在哪一点找到它。

error = sendto (sockfd, (char*)m->b_rptr, (int) (m->b_wptr - m->b_rptr),
         0,destaddr,destlen);

here, m->b_rptr is point out rtp header and data. 在这里, m->b_rptr指出rtp标头和数据。 and only this send and recv. 并且仅此发送和接收。

You can find it using a packet capture tool like tcpdump or Wireshark. 您可以使用数据包捕获工具(如tcpdump或Wireshark)找到它。 You can't access the low-level protocol details directly via the sendto() function call--the OS crafts the headers for you and normally you wouldn't need to see them. 您无法直接通过sendto()函数调用访问底层协议的详细信息-操作系统会为您精心制作标头,通常您无需查看它们。 But a packet capture on either the sending or the receiving end will show you the headers. 但是在发送端或接收端捕获的数据包都会向您显示标头。

Using the socket API, you do not need to create the IP/UDP header. 使用套接字API,您无需创建IP / UDP标头。 The OS will automatically fill the appropriate headers. 操作系统将自动填充适当的标题。 All that you need, is to create your RTP packet and encapsulate it inside a UDP packet. 您所需要做的就是创建RTP数据包并将其封装在UDP数据包内。 At the receiver side, when you use recvfrom() , your buffer will contain only the UDP payload, which is the RTP packet in your case. 在接收方,当您使用recvfrom() ,缓冲区将仅包含UDP有效负载,在您的情况下为RTP数据包。

If you want to retrieve other information too, like the source IP or source port of a UDP packet received you have too do some more steps. 如果您还想检索其他信息,例如接收到的UDP数据包的源IP或源端口,您也需要执行更多步骤。 For example for the source IP: 例如,对于源IP:

struct sockaddr         src_addr;
socklen_t               src_addr_len;
struct sockaddr_in      *src_addr_in;

/* Receive a UDP packet. Error Checking is omitted */
memset(&src_addr, 0, sizeof(struct sockaddr));
recvfrom(sockfd, buffer, MSG_MAX_LEN, 0, &src_addr, &src_addr_len));

/* Now lets retrieve the IP of the sender */
/* Cast first the sockaddr struct to another more appropriate struct */
src_addr_in = (struct sockaddr_in *) &src_addr;

/* And now use inet_ntoa to print the source IP in a human readable form */
printf("Source IP: %s\n", inet_ntoa(src_addr_in->sin_addr));

In a similar way you can retrieve the the source port of the packet. 您可以通过类似的方式检索数据包的源端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM