From owner-freebsd-net@FreeBSD.ORG Mon Jul 18 20:47:42 2011 Return-Path: Delivered-To: freebsd-net@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id DF3C8106566C; Mon, 18 Jul 2011 20:47:42 +0000 (UTC) (envelope-from rea@codelabs.ru) Received: from 0.mx.codelabs.ru (0.mx.codelabs.ru [144.206.177.45]) by mx1.freebsd.org (Postfix) with ESMTP id 5C5838FC08; Mon, 18 Jul 2011 20:47:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=codelabs.ru; s=two; h=Sender:In-Reply-To:Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date; bh=sWXTlsCYLdFETDrReVLOeKJ90E8G+oDcJxLauJJ8d/I=; b=C1GTLWoNxgJcVMGnftbXINVDbcPUQId3lSvxNfu4ULnOwAhwsIYb0ASolz1yNQzLfrdqV0Pe+8W8XE5ynVjBhJanHASXMKY/tmCj0CHgieW6/WX2OY2e1pIsrtEKswEpNKl/dRuE4663Oc5ocFFiVXxcMUkyRMBNyzWOS/9w9MvWR/h60P57e7LzomZySYm3WNXgJRnbYYMX6FEqXFypCazZI7u6M7+wjyJFOPQmQYTeTUAZPLromtFysDIsbEBHIF+PKMeMnJtdx8Ah9fs3OFr3/PiAUnpJkXrs/cXZ1b2OcsIfR9lT/bceEdYzG1CnGbOxpY1Tv0OsW9n6jVA6VA==; Received: from MacBook-Eygene-Ryabinkin.local (ppp91-77-162-104.pppoe.mtu-net.ru [91.77.162.104]) by 0.mx.codelabs.ru with esmtpsa (TLSv1:AES256-SHA:256) id 1QiuUE-000DnR-2u; Tue, 19 Jul 2011 00:32:18 +0400 Date: Tue, 19 Jul 2011 00:32:15 +0400 From: Eygene Ryabinkin To: Daniel Braniss Message-ID: <20110718203215.GM54929@MacBook-Eygene-Ryabinkin.local> References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="McpcKDxJRrEJVmOH" Content-Disposition: inline In-Reply-To: Sender: rea@codelabs.ru Cc: freebsd-net@freebsd.org, freebsd-hackers@freebsd.org Subject: Re: broadcast oddity X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 18 Jul 2011 20:47:43 -0000 --McpcKDxJRrEJVmOH Content-Type: multipart/mixed; boundary="UeXZ3FjlYZvuln/G" Content-Disposition: inline --UeXZ3FjlYZvuln/G Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable 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? > 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? > 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. 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. --=20 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--