tags:

views:

51

answers:

2

I have built a udp server that broadcasts a message every so often. It broadcasts without a problem. But if I turn on my vpn, then sendto will start returning -1 even though I am sending to INADDR_BROADCAST.

I am running this on a mac btw if that changes anything. I really have no idea why this would happen, I am really wondering how could a udp broadcast fail?

A: 

You should check errno global variable for more detailed error description. Possible values for this call described at ERRORS section of sendto(2) manual page.

For example:

[EACCES] The SO_BROADCAST option is not set on the socket and a broadcast address is given as the destination.

[EHOSTUNREACH] The destination address specifies an unreachable host.

[ENETUNREACH] No route to the network is present.

AlexD
A: 

my errno is 227 (I think) which reads

sendto: Can't assign requested address

I think it can't find a broadcasting port when my vpn gets turned on, here is an example when I turn on my vpn and turn it back off

2010-06-25 04:05:55.022 SwypeSendForMac2[63687:6203] wrote 187 bytes to socked 6 2010-06-25 04:05:56.023 SwypeSendForMac2[63687:6203] wrote 187 bytes to socked 6 2010-06-25 04:05:57.024 SwypeSendForMac2[63687:6203] wrote 187 bytes to socked 6 sendto: Can't assign requested address sendto: Can't assign requested address sendto: Can't assign requested address sendto: Can't assign requested address sendto: Can't assign requested address sendto: Can't assign requested address sendto: Can't assign requested address 2010-06-25 04:06:05.027 SwypeSendForMac2[63687:6203] wrote 187 bytes to socked 6 2010-06-25 04:06:06.031 SwypeSendForMac2[63687:6203] wrote 187 bytes to socked 6

the middle of course is with the vpn on

here is my roouting table without the vpn:

34:~ samuelw$ netstat -r Routing tables

Internet: Destination Gateway Flags Refs Use Netif Expire default 192.168.1.1 UGSc 63 0 en1 127 localhost UCS 0 0 lo0 localhost localhost UH 1 180317 lo0 169.254 link#5 UCS 1 0 en1 cronos.local link#5 UHLW 0 1 en1 192.168.1 link#5 UCS 4 0 en1 192.168.1.1 90:84:d:d3:e2:54 UHLWI 69 1260 en1 1123 192.168.1.98 0:11:24:a2:41:6d UHLWI 0 324 en1 1112 192.168.1.152 0:21:e9:76:fe:6b UHLWI 0 0 en1 657 192.168.1.153 localhost UHS 0 118 lo0 192.168.1.154 0:21:5c:52:e0:11 UHLWI 1 2662 en1 1134

Internet6: Destination Gateway Flags Netif Expire localhost localhost UH lo0 fe80::%lo0 localhost Uc lo0 localhost link#1 UHL lo0 fe80::%en1 link#5 UC en1 samuelmacbook.loca 0:1e:c2:a6:64:98 UHL lo0 cronos.local 0:21:5c:52:e0:11 UHLW en1 ff01:: localhost Um lo0 ff02:: localhost UmC lo0 ff02:: link#5 UmC en1

and with my vpn

34:~ samuelw$ netstat -r Routing tables

Internet: Destination Gateway Flags Refs Use Netif Expire default 172.16.20.1 UGSc 8 0 ppp0 default 192.168.1.1 UGScI 1 0 en1 127 localhost UCS 0 0 lo0 localhost localhost UH 1 180323 lo0 169.254 link#5 UCS 1 0 en1 cronos.local link#5 UHLW 0 1 en1 172.16.20.1 34.108.131.216.cli UH 9 0 ppp0 192.168.1 link#5 UCS 5 0 en1 192.168.1.1 90:84:d:d3:e2:54 UHLWI 3 1346 en1 1104 192.168.1.98 0:11:24:a2:41:6d UHLWI 2 378 en1 1093 192.168.1.152 0:21:e9:76:fe:6b UHLWI 0 0 en1 637 192.168.1.153 localhost UHS 0 118 lo0 192.168.1.154 0:21:5c:52:e0:11 UHLWI 1 2665 en1 1114 192.168.1.255 ff:ff:ff:ff:ff:ff UHLWbI 0 2 en1 strong-sf1.reliabl 192.168.1.1 UGHS 25 25 en1 216.131.108 ppp0 USc 0 0 ppp0

Internet6: Destination Gateway Flags Netif Expire localhost localhost UH lo0 fe80::%lo0 localhost Uc lo0 localhost link#1 UHL lo0 fe80::%en1 link#5 UC en1 samuelmacbook.loca 0:1e:c2:a6:64:98 UHL lo0 cronos.local 0:21:5c:52:e0:11 UHLW en1 ff01:: localhost Um lo0 ff02:: localhost UmC lo0 ff02:: link#5 UmC en1

and here is my code split into 2 functions

  • (int)startService { broadcast = 1;

    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); exit(1); }

    // this call is what allows broadcast packets to be sent: if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof broadcast) == -1) { perror("setsockopt (SO_BROADCAST)"); exit(1); }

    their_addr.sin_family = AF_INET; // host byte order

    their_addr.sin_port = htons(SERVERPORT); // short, network byte order

    their_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);

    memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);

    return 0; }

  • (void)publishServiceLoop { NSAutoreleasePool * autoreleasePool = [[NSAutoreleasePool alloc] init]; NSString * message;

    while (alive) { while ((message = [AdyNetServer encodeToXML:server]) == nil) { [NSThread sleepForTimeInterval:3.0]; } const char * msgString = [message cStringUsingEncoding:NSUTF8StringEncoding]; if ((numbytes=sendto(sockfd, msgString, [message length], 0, (struct sockaddr *)&their_addr, sizeof their_addr)) == -1) { perror("sendto"); if ([[AdyErrorAlerter sharedInstance] delegate]) { [[[AdyErrorAlerter sharedInstance] delegate] receiveErrorMessage:[NSString stringWithFormat: @"unable to broadcast to socket %d, with message %@",sockfd,msgString]]; }

        }
    else {
        NSLog(@"wrote %d bytes to socked %d",numbytes,sockfd);
    }
    
    
    //printf("sent %d bytes to %s\n", numbytes,
    //     inet_ntoa(their_addr.sin_addr));
    [NSThread sleepForTimeInterval:1.0];
    [autoreleasePool release];
    autoreleasePool = [[NSAutoreleasePool alloc] init];
    

    } [autoreleasePool release]; if (thread != nil) { thread = nil; [thread release]; } }