From: Alexis Laferrière Date: Sun, 5 Jul 2015 17:54:22 +0000 (-0400) Subject: lib/socket: revamp services of `NativeSocketAddrIn` X-Git-Tag: v0.7.7~19^2~6 X-Git-Url: http://nitlanguage.org lib/socket: revamp services of `NativeSocketAddrIn` Signed-off-by: Alexis Laferrière --- diff --git a/lib/socket/socket.nit b/lib/socket/socket.nit index f318dcc..515f368 100644 --- a/lib/socket/socket.nit +++ b/lib/socket/socket.nit @@ -72,8 +72,8 @@ class TCPStream return end - var hostname = sys.gethostbyname(host.to_cstring) - if hostname.address_is_null then + var hostent = sys.gethostbyname(host.to_cstring) + if hostent.address_is_null then # Error in name lookup var err = sys.h_errno last_error = new IOError(err.to_s) @@ -84,9 +84,12 @@ class TCPStream return end - addrin = new NativeSocketAddrIn.with_hostent(hostname, port) + addrin = new NativeSocketAddrIn + addrin.fill_from_hostent hostent + addrin.port = port + address = addrin.address.to_s - init(addrin.port, hostname.h_name) + init(addrin.port, hostent.h_name.to_s) closed = not internal_connect end_reached = closed @@ -240,7 +243,12 @@ class TCPServer closed = true return end - addrin = new NativeSocketAddrIn.with_port(port, new NativeSocketAddressFamilies.af_inet) + + addrin = new NativeSocketAddrIn + addrin.family = new NativeSocketAddressFamilies.af_inet + addrin.port = port + addrin.address_any + address = addrin.address.to_s # Bind it diff --git a/lib/socket/socket_c.nit b/lib/socket/socket_c.nit index ea562b0..de1ba30 100644 --- a/lib/socket/socket_c.nit +++ b/lib/socket/socket_c.nit @@ -233,38 +233,47 @@ class SocketAcceptResult var addr_in: NativeSocketAddrIn end +# Socket address in the Internet namespace, pointer to a `struct sockaddr_in` extern class NativeSocketAddrIn `{ struct sockaddr_in* `} - new `{ - struct sockaddr_in *sai = NULL; - sai = malloc(sizeof(struct sockaddr_in)); - return sai; - `} - new with_port(port: Int, family: NativeSocketAddressFamilies) `{ + # `NULL` pointer + new nul `{ return NULL; `} + + # `malloc` a new instance + new `{ struct sockaddr_in *sai = NULL; sai = malloc(sizeof(struct sockaddr_in)); - sai->sin_family = family; - sai->sin_port = htons(port); - sai->sin_addr.s_addr = INADDR_ANY; return sai; `} - new with_hostent(hostent: NativeSocketHostent, port: Int) `{ - struct sockaddr_in *sai = NULL; - sai = malloc(sizeof(struct sockaddr_in)); - sai->sin_family = hostent->h_addrtype; - sai->sin_port = htons(port); - memcpy((char*)&sai->sin_addr.s_addr, (char*)hostent->h_addr, hostent->h_length); - return sai; + # Set `address` and `family` from `hostent` (to use with `Sys::gethostbyname`) + fun fill_from_hostent(hostent: NativeSocketHostent) `{ + self->sin_family = hostent->h_addrtype; + memcpy((char*)&self->sin_addr.s_addr, + (char*)hostent->h_addr, + hostent->h_length); `} + # Internet address as then IPV4 numbers-and-dots notation fun address: NativeString `{ return (char*)inet_ntoa(self->sin_addr); `} + # Set `address` to `INADDR_ANY` + fun address_any `{ self->sin_addr.s_addr = INADDR_ANY; `} + + # Set `address` to `INADDR_BROADCAST` + fun address_broadcast `{ self->sin_addr.s_addr = INADDR_BROADCAST; `} + + # Address family fun family: NativeSocketAddressFamilies `{ return self->sin_family; `} + # Address family + fun family=(value: NativeSocketAddressFamilies) `{ self->sin_family = value; `} + + # Port fun port: Int `{ return ntohs(self->sin_port); `} - fun destroy `{ free(self); `} + # Port + fun port=(value: Int) `{ self->sin_port = htons(value); `} end # Host entry information, a pointer to a `struct hostent`