0

let's two machines are directly connected on a Point-to-point link:

A -192.168.4.1/24---------------------------192.168.4.2/24--B

How A can send an IP packet to B through broadcast only ?

if A cooks up the packet with Dest mac = ff:ff:ff:ff:ff:ff and dest ip : 192.168.4.255, Would this make the packet destined to B ? If yes, Can somebody explains how this works ?

Abhishek Sagar
  • 1,189
  • 4
  • 20
  • 44
  • 1
    Don't setup the IP header manually. Use a standard socket function like `sendto()` to send a packet to the broadcast IP of A's subnet, which you can determine by masking A's IP with its subnet mask using a bitwise `AND` operator, and then adding the inverse of the mask using a bitwise `OR` operator (ie `((192.168.4.1 and 255.255.255.0) or 0.0.0.255) = 192.168.4.255`). Or, use a system function, like `getifaddrs()` or equivalent, to query A's network adapter directly for its broadcast IP. Either way, if B is connected to and listening on the same subnet, it will receive the packet, yes. – Remy Lebeau Jan 30 '18 at 21:49
  • if you still want to create the packet from scratch, I would recommend scapy, for example: `send(Ether()/IP(dst='192.168.4.255')/ICMP()/"test packet")` – EmilioPeJu Jan 30 '18 at 22:01
  • @RemyLebeau That what i was trying. not preparing the IP hdr manually, and using sendto() API to send packet to B with dest ip = 192.168.4.255. But send to returning -1 - that is packet is not even sent out. When i just replace 192.168.4.255 with 192.168.4.2, packet is sent and received without any issues. `errno` is being set to 13(Permission denied) – Abhishek Sagar Jan 31 '18 at 04:43
  • 1
    Ok, i figured out, i need to set broadcast privileges on my socket using 'setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)); – Abhishek Sagar Jan 31 '18 at 04:54

1 Answers1

1

I figured out - we can send the packet with dest IP = 192.168.4.255. In addition, set the broadcast privileges on socket using

int on=1;
setsockopt(igmp_sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));

It will work.

Abhishek Sagar
  • 1,189
  • 4
  • 20
  • 44