-// -----------------------------------
-int startNetwork (struct ngadmin *nga) {
-
- struct ifreq ifr;
- int ret;
-
-
- memset(&nga->local, 0, sizeof(struct sockaddr_in));
- nga->local.sin_family=AF_INET;
- nga->local.sin_port=htons(CLIENT_PORT);
-
- memset(&ifr, 0, sizeof(struct ifreq));
- strncpy(ifr.ifr_name, nga->iface, IFNAMSIZ-1);
-
- if ( (nga->sock=socket(AF_INET, SOCK_DGRAM, 0))<0 ) {
- perror("socket");
- return nga->sock;
- }
-
- // get the interface IP address
- if ( (ret=ioctl(nga->sock, SIOCGIFADDR, &ifr))<0 ) {
- perror("ioctl(SIOCGIFADDR)");
- close(nga->sock);
- return ret;
- }
- /*
- Here we have a problem: when you have multiple interfaces, sending a packet to
- 255.255.255.255 may not send it to the interface you want. If you bind() to
- the address of the interface, you will not be able to receive broadcasts.
- The only solution I have found yet is in this case to use
- setsockopt(SO_BINDTODEVICE) but this requires root priviledges.
- */
- //local.sin_addr=(*(struct sockaddr_in*)&ifr.ifr_addr).sin_addr; // FIXME
-
- // get the interface MAC address
- if ( (ret=ioctl(nga->sock, SIOCGIFHWADDR, &ifr))<0 ) {
- perror("ioctl(SIOCGIFHWADDR)");
- close(nga->sock);
- return ret;
- }
- memcpy(&nga->localmac, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
-
- // bind
- if ( (ret=bind(nga->sock, (struct sockaddr*)&nga->local, sizeof(struct sockaddr_in)))<0 ) {
- perror("bind");
- close(nga->sock);
- return ret;
- }
-
- // allow broadcasting
- ret=1;
- if ( (ret=setsockopt(nga->sock, SOL_SOCKET, SO_BROADCAST, &ret, sizeof(ret)))<0 ) {
- perror("setsockopt(SO_BROADCAST)");
- return ret;
- }
-
- // prevent unicast packets from being routed
- ret=1;
- if ( (ret=setsockopt(nga->sock, IPPROTO_IP, IP_TTL, &ret, sizeof(ret)))<0 ) {
- perror("setsockopt(IP_TTL)");
- return ret;
- }
-
-
- return 0;
-
-}