extras/net: Fixes for the net library

This change fixes two problems with the net library in extras/.

The h_addr field of the hostent structure (see gethostbyname(3)) is
deprecated.  gethostbyname can return multiple addresses in a list in the
field h_addr_list.  I've updated code to use the first item in this list.
Note that use of gethostbyname is also discouraged and getaddrinfo(3) is
recommended.  In a later change I intend to update this library to use
getaddrinfo.

The library used a macro error() to retrieve the error either from errno, or
on Windows from WSAGetLastError.  WSAGetLastError is a function however the
parentheses were missing from the macro causing it to be returned as a value
rather than called.

extras/net/sockets.m:
extras/net/tcp.m:
    As above.
This commit is contained in:
Paul Bone
2014-09-03 14:01:37 +10:00
parent ca19aeecd4
commit 95e48fce93
2 changed files with 5 additions and 5 deletions

View File

@@ -70,7 +70,7 @@
#ifdef MR_WIN32
#include <winsock.h>
#define error() WSAGetLastError
#define error() WSAGetLastError()
#else /* !MR_WIN32 */
@@ -184,7 +184,7 @@
} else {
addr = MR_GC_NEW(struct sockaddr_in);
MR_memcpy(&(addr->sin_addr), host->h_addr, host->h_length);
MR_memcpy(&(addr->sin_addr), host->h_addr_list[0], host->h_length);
addr->sin_family = host->h_addrtype;
addr->sin_port = htons(Port);
@@ -215,7 +215,7 @@
Success = MR_NO;
} else {
addr = MR_GC_NEW(struct sockaddr_in);
MR_memcpy(&(addr->sin_addr), host->h_addr, host->h_length);
MR_memcpy(&(addr->sin_addr), host->h_addr_list[0], host->h_length);
addr->sin_family = host->h_addrtype;
addr->sin_port = service->s_port;
SA = (MR_Word) addr;

View File

@@ -274,7 +274,7 @@ void ML_tcp_init(void)
} else {
addr = MR_GC_NEW(struct sockaddr_in);
MR_memset(addr, 0, sizeof(struct sockaddr_in));
MR_memcpy(&(addr->sin_addr), host->h_addr, host->h_length);
MR_memcpy(&(addr->sin_addr), host->h_addr_list[0], host->h_length);
addr->sin_family = host->h_addrtype;
addr->sin_port = htons(Port);
/*
@@ -334,7 +334,7 @@ socket_fd(Tcp) = socket_fd_c(Tcp ^ handle).
} else {
addr = MR_GC_NEW(struct sockaddr_in);
MR_memset(addr, 0, sizeof(struct sockaddr_in));
MR_memcpy(&(addr->sin_addr), host->h_addr, host->h_length);
MR_memcpy(&(addr->sin_addr), host->h_addr_list[0], host->h_length);
addr->sin_family = host->h_addrtype;
addr->sin_port = htons(Port);