[Vm-dev] unix nameToAddr() function appears to not work on Raspbian?

Bert Freudenberg bert at freudenbergs.de
Wed Sep 23 13:31:04 UTC 2015


> On 23.09.2015, at 04:22, David T. Lewis <lewis at mail.msen.com> wrote:
> 
> 
> On Tue, Sep 22, 2015 at 10:29:33AM +0200, Bert Freudenberg wrote:
>> 
>> 'xcept by connecting a UDP socket no actual data is sent yet, so the port doesn???t matter. In the python example you can use 0 for the port and it still works (have not tried C but I suspect it???s pretty much the same). The beauty of this approach is that we get the OS to consult its routing tables and pick the right interface and give us its address.
>> 
>> I suspect that our logic is tuned to TCP sockets and wont properly handle UDP (SOCK_DGRAM) ones for this use case.
> 
> In Squeak, the localAddress of a connected UDP socket does not seem to be able to find its local address:
> 
>  addr := NetNameResolver addressForName: 'ntp.ubuntu.com'.
>  s := Socket newUDP. "==> a Socket[connected]"
>  s sendData: '!' toHost: serverAddr port: 13.
>  local := s localAddress.
>  s close.
>  local ==> #[0 0 0 0]

Marcel and I just tried this on Windows, also returns #[0 0 0 0]. Perhaps unsurprisingly, since the Windows VM uses the Posix API too, instead of the native Windows functions.

> But a connected TCP socket does know its local address:
> 
>  addr := NetNameResolver addressForName: 'squeak.org'.
>  s := Socket newTCP connectTo: addr port: 80. " a Socket[connected]"
>  local := s localAddress.
>  s close.
>  local ==> #[172 16 0 10]
> 
> So for a TCP connection, we can establish an outbound TCP session and ask
> the socket for the local IP address that was used to establish that session.
> 
> For a UDP datagram, I would have guessed that the local IP address would be
> known after data was first sent. On the other hand, these are datagrams, and
> there is no inherent reason that they would need to be sent through any one
> interface (although in practice that would surely be the case).
> 
> But if it works in Python, then it should work in Squeak.

Yep. Works in plain C too:

#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <strings.h>

int main(int argc, char**argv)
{
   int sockfd;
   struct sockaddr_in remote_addr, local_addr;
   socklen_t local_addr_len;
   char *local_name;

   sockfd = socket(AF_INET, SOCK_DGRAM, 0);

   bzero(&remote_addr, sizeof(remote_addr));
   remote_addr.sin_family = AF_INET;
   remote_addr.sin_addr.s_addr = inet_addr("8.8.8.8");
   remote_addr.sin_port = htons(53);

   connect(sockfd, (struct sockaddr *)&remote_addr, sizeof(remote_addr));

   local_addr_len = sizeof(local_addr);
   getsockname(sockfd, (struct sockaddr *)&local_addr, &local_addr_len);

   local_name = inet_ntoa(local_addr.sin_addr);
   printf("local address: %s\n", local_name);
}


- Bert -



-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4115 bytes
Desc: not available
Url : http://lists.squeakfoundation.org/pipermail/vm-dev/attachments/20150923/dca3358a/smime.bin


More information about the Vm-dev mailing list