From owner-freebsd-hackers@FreeBSD.ORG Tue Jul 19 07:40:13 2011 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id D70581065670; Tue, 19 Jul 2011 07:40:13 +0000 (UTC) (envelope-from danny@cs.huji.ac.il) Received: from kabab.cs.huji.ac.il (kabab.cs.huji.ac.il [132.65.16.84]) by mx1.freebsd.org (Postfix) with ESMTP id 2CCD48FC14; Tue, 19 Jul 2011 07:40:13 +0000 (UTC) Received: from pampa.cs.huji.ac.il ([132.65.80.32]) by kabab.cs.huji.ac.il with esmtp id 1Qj4uZ-0005nH-SR; Tue, 19 Jul 2011 10:40:11 +0300 X-Mailer: exmh version 2.7.2 01/07/2005 with nmh-1.2 To: Eygene Ryabinkin In-reply-to: <20110718203215.GM54929@MacBook-Eygene-Ryabinkin.local> References: <20110718203215.GM54929@MacBook-Eygene-Ryabinkin.local> Comments: In-reply-to Eygene Ryabinkin message dated "Tue, 19 Jul 2011 00:32:15 +0400." Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Tue, 19 Jul 2011 10:40:11 +0300 From: Daniel Braniss Message-ID: Cc: freebsd-net@freebsd.org, freebsd-hackers@freebsd.org Subject: Re: broadcast oddity X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 19 Jul 2011 07:40:14 -0000 Hi Eygene, > Daniel, good day. > > Mon, Jul 18, 2011 at 05:04:27PM +0300, Daniel Braniss wrote: > > this code behaves correctly when run from a diskless host which > > booted via PXE, but fails on a host that was booted from disk. > > hint: the non working sends a packet with a non ethernet broadcast > > address and an ip address of 255.255.255.255, > > And that non-broadcast ethernet address is the MAC of your > default router? yes. > > > the working version sets the ethernet address to 0xffffffff and the > > ip to the network broadcast address. > > What's your routing table (netstat -rn) for the PXE-booted host? it's ok, same in both cases. it's picked up via DHCP, in the diskless case by the boot/loader in the second via dhcpclient. > > > what am I doing wrong? > >=20 > > danny > > PS: what is the correct way to obtain the network broadcast address? > > You nailed it: you should send packets to the network's broadcast, > not to the 0xffffffff. > but I'm at the user/ip level!, have no way to set mac/ethernet address. still, the question is why it works in one case, and failes in the other. > And in order to find the broadcast address for the interface (or > network at that interface), you should use getifaddrs(), like in the > attached example. It is very quick and dirty one and it has some > limitations (e.g., it takes the first broadcast address from the > interface), but it should be a good starting point. thanks, danny > Eygene Ryabinkin ,,,^..^,,, > [ Life's unfair - but root password helps! | codelabs.ru ] > [ 82FE 06BC D497 C0DE 49EC 4FF0 16AF 9EAE 8152 ECFB | freebsd.org ] > > --UeXZ3FjlYZvuln/G > Content-Type: text/plain; charset=us-ascii > Content-Disposition: attachment; filename="bcast.c" > > #include > #include > #include > #include > #include > #include > #include > #include > #include > > void > bcast(in_addr_t addr) > { > int so, on; > char msg[BUFSIZ]; > struct timespec t2; > struct sockaddr_in soin; > > if((so = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { > perror("socket"); > exit(-1); > } > on = 1; > if(setsockopt(so, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on))) { > perror("setsockopt"); > exit(-1); > } > > bzero(&soin, sizeof(struct sockaddr_in)); > soin.sin_len = sizeof(struct sockaddr_in); > soin.sin_family = AF_INET; > soin.sin_addr.s_addr = addr; > soin.sin_port = htons(12345); > while(1) { > clock_gettime(CLOCK_REALTIME, &t2); > sprintf(msg, "0x%016x", t2.tv_sec); > if(sendto(so, msg, strlen(msg)+1, > 0, (struct sockaddr *)&soin, sizeof(struct sockaddr)) < 0) { > perror("sendto"); > break; > } > sleep(10); > } > } > > main(int argc, char *argv[]) > { > struct ifaddrs *ifap, *ifa, *our_if; > struct sockaddr_in *sin; > > if (argc < 2) > errx(1, "No arguments"); > if (getifaddrs(&ifap) != 0) > perror("getifaddrs"); > our_if = NULL; > for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { > if (strcmp(argv[1], ifa->ifa_name) == 0) { > sin = (struct sockaddr_in *)ifa->ifa_broadaddr; > if (!(ifa->ifa_flags & IFF_BROADCAST) || > sin == NULL || > sin->sin_addr.s_addr == 0) > continue; > our_if = ifa; > break; > } > } > if (!our_if) > errx(1, "Can't find broadcast-able interface '%s'", argv[1]); > bcast(sin->sin_addr.s_addr); > freeifaddrs(ifap); > } > > --UeXZ3FjlYZvuln/G-- > > --McpcKDxJRrEJVmOH > Content-Type: application/pgp-signature > > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.11 (Darwin) > > iF0EAREIAAYFAk4kmE8ACgkQFq+eroFS7PvE+QD/ZL0cpFaKvLA+ZWWFH/QlA5Xb > hqKEG+XY90zdya2/twEA9R3xcK8wwRtOiLf7Tb9SHviukeMsrxwufSWhdapJfj0= > =Iujs > -----END PGP SIGNATURE----- > > --McpcKDxJRrEJVmOH--