dev9
dev9

Reputation: 31

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..???

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. and only this send and recv.

Upvotes: 0

Views: 946

Answers (2)

Manos
Manos

Reputation: 2186

Using the socket API, you do not need to create the IP/UDP header. 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. At the receiver side, when you use recvfrom(), your buffer will contain only the UDP payload, which is the RTP packet in your case.

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. For example for the source 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.

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249153

You can find it using a packet capture tool like tcpdump or 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. But a packet capture on either the sending or the receiving end will show you the headers.

Upvotes: 1

Related Questions