--- net.c.orig Wed Mar 14 19:34:11 2007 +++ net.c Wed Mar 14 19:58:54 2007 @@ -71,13 +71,7 @@ #endif #endif -#ifdef SPOOF #include -#include -#endif /* SPOOF */ - -//#define SIOCGIFGENERIC _IOWR('i', 58, struct ifreq) /* generic IF get op */ -//#define SIOCGWAVELAN SIOCGIFGENERIC #include @@ -335,7 +329,6 @@ return (-1); } -#ifdef SPOOF if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on)) < 0) { perror("SO_REUSEPORT failed"); @@ -347,7 +340,6 @@ perror("IP_RECVIF failed"); return (-1); } -#endif /* SPOOF */ for (on = bufspace; ; on -= 1024) { @@ -504,10 +496,6 @@ * Wrapper for sendto(2) */ -#ifdef SPOOF -static u_int16_t ip_id = 0; -#endif /* SPOOF */ - ssize_t olsr_sendto(int s, const void *buf, @@ -516,96 +504,42 @@ const struct sockaddr *to, socklen_t tolen) { -#ifdef SPOOF - /* IPv4 for now! */ - - libnet_t *context; - char errbuf[LIBNET_ERRBUF_SIZE]; - libnet_ptag_t udp_tag, ip_tag, ether_tag; - unsigned char enet_broadcast[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; - int status; - struct sockaddr_in *to_in = (struct sockaddr_in *) to; - u_int32_t destip; - struct interface *iface; - - udp_tag = ip_tag = ether_tag = 0; - destip = to_in->sin_addr.s_addr; - iface = if_ifwithsock (s); - - /* initialize libnet */ - context = libnet_init(LIBNET_LINK, iface->int_name, errbuf); - if (context == NULL) - { - OLSR_PRINTF (1, "libnet init: %s\n", libnet_geterror (context)) - return (0); - } + struct sockaddr_in *psin = (struct sockaddr_in *)to; + int err; - /* initialize IP ID field if necessary */ - if (ip_id == 0) + /* Check if 255.255.255.255 and use BSD IP_ONESBCAST if so. */ + if (psin->sin_family == AF_INET && psin->sin_addr.s_addr == INADDR_BROADCAST) { - ip_id = (u_int16_t) (arc4random () & 0xffff); - } - - udp_tag = libnet_build_udp (698, /* src port */ - 698, /* dest port */ - LIBNET_UDP_H + len, /* length */ - 0, /* checksum */ - buf, /* payload */ - len, /* payload size */ - context, /* context */ - udp_tag); /* pblock */ - if (udp_tag == -1) - { - OLSR_PRINTF (1, "libnet UDP header: %s\n", libnet_geterror (context)) - return (0); - } - - ip_tag = libnet_build_ipv4 (LIBNET_IPV4_H + LIBNET_UDP_H + len, /* len */ - 0, /* TOS */ - ip_id++, /* IP id */ - 0, /* IP frag */ - 1, /* IP TTL */ - IPPROTO_UDP, /* protocol */ - 0, /* checksum */ - libnet_get_ipaddr4 (context), /* src IP */ - destip, /* dest IP */ - NULL, /* payload */ - 0, /* payload len */ - context, /* context */ - ip_tag); /* pblock */ - if (ip_tag == -1) - { - OLSR_PRINTF (1, "libnet IP header: %s\n", libnet_geterror (context)) - return (0); - } + struct interface *iface; + int on; - ether_tag = libnet_build_ethernet (enet_broadcast, /* ethernet dest */ - libnet_get_hwaddr (context), /* ethernet source */ - ETHERTYPE_IP, /* protocol type */ - NULL, /* payload */ - 0, /* payload size */ - context, /* libnet handle */ - ether_tag); /* pblock tag */ - if (ether_tag == -1) - { - OLSR_PRINTF (1, "libnet ethernet header: %s\n", libnet_geterror (context)) - return (0); - } - - status = libnet_write (context); - if (status == -1) - { - OLSR_PRINTF (1, "libnet packet write: %s\n", libnet_geterror (context)) - return (0); - } - - libnet_destroy (context); - - return (len); + on = 1; + if (setsockopt(s, IPPROTO_IP, IP_ONESBCAST, &on, sizeof(on)) < 0) + { + perror("IP_ONESBCAST failed"); + return (-1); + } + + /* Get broadcast address of ifnet for use with IP_ONESBCAST; it will + * be turned into 255.255.255.255 by ip_output(). */ + iface = if_ifwithsock (s); + + err = sendto(s, buf, len, flags, (struct sockaddr *)&iface->int_broadaddr, + sizeof(struct sockaddr_in)); + + on = 0; + if (setsockopt(s, IPPROTO_IP, IP_ONESBCAST, &on, sizeof(on)) < 0) + { + perror("IP_ONESBCAST failed"); + return (-1); + } + + } else { + /* destinations other than 255.255.255.255 */ + err = sendto(s, buf, len, flags, to, tolen); + } -#else - return sendto(s, buf, len, flags, to, tolen); -#endif + return ((err == 0) ? len : err) ; } @@ -621,7 +555,6 @@ struct sockaddr *from, socklen_t *fromlen) { -#if SPOOF struct msghdr mhdr; struct iovec iov; struct cmsghdr *cm; @@ -673,15 +606,6 @@ iname); return (count); - -#else /* SPOOF */ - return recvfrom(s, - buf, - len, - 0, - from, - fromlen); -#endif /* SPOOF */ } /**