0

I want to use raw socket to send a packet.

if I create a IP packet(icmp, tcp or udp packet), then the source and destination IP is in the IP packet. now I want to send it via the following lines:

    memset(&sin, 0, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = inet_addr(dst_ip);
    sendto(sd, packet, 60, 0, (struct sockaddr *)&sin, sizeof(sin));

of course, the dst_ip should be the same as the destination IP in the packet.

I'm wondering what happens if the dst_ip is not the same as the destination IP in the packet. It seems to me that the dst_ip is used to decide for which NIC should be used to send the packet. while the destination IP in the crafted packet is not changed by dst_ip and will be processed by in-network routers.

is my understanding correct or not? are there any other differences?

thanks!

misteryes
  • 2,167
  • 4
  • 32
  • 58

1 Answers1

0

The answer for BSD is a comment in the source code for rip_output():1 "If the user handed us a complete packet, use it. Otherwise, allocate an mbuf for a header and fill it in" [i.e., from the address provided to sendto()].

For other systems you're going to have to read some kernel source code.

  1. G.R. Wright & W.R. Stevens, TCP/IP Illustrated, vol II: The Implementation, pp.1057-1062.
user207421
  • 305,947
  • 44
  • 307
  • 483