X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcutil%2Fwin32%2Fsilcwin32net.c;h=fd7542dd5fc9508aa8c6dcadf7b7fadc13623fd5;hb=a818c5b5411bbc4436d1c5f011236985c96bb787;hp=13032ce7058f5a1db00e445e0cf081ead7f68a4d;hpb=1ef4aa5f51b27e63bd29076b9c53e4b5895afa0a;p=silc.git diff --git a/lib/silcutil/win32/silcwin32net.c b/lib/silcutil/win32/silcwin32net.c index 13032ce7..fd7542dd 100644 --- a/lib/silcutil/win32/silcwin32net.c +++ b/lib/silcutil/win32/silcwin32net.c @@ -19,6 +19,8 @@ */ /* $Id$ */ +/* XXX IPv6 support missing */ + #include "silcincludes.h" #include "silcnet.h" @@ -28,7 +30,7 @@ If argument `ip_addr' is NULL `any' address will be used. Returns the created socket or -1 on error. */ -int silc_net_create_server(int port, char *ip_addr) +int silc_net_create_server(int port, const char *ip_addr) { SOCKET sock; int rval; @@ -99,7 +101,8 @@ void silc_net_close_server(int sock) socket or -1 on error. This blocks the process while trying to create the connection. */ -int silc_net_create_connection(int port, char *host) +int silc_net_create_connection(const char *local_ip, int port, + const char *host) { SOCKET sock; int rval, err; @@ -111,7 +114,8 @@ int silc_net_create_connection(int port, char *host) /* Do host lookup */ dest = gethostbyname(host); if (!dest) { - SILC_LOG_ERROR(("Network (%s) unreachable", host)); + SILC_LOG_ERROR(("Network (%s) unreachable: could not resolve the " + "IP address", host)); return -1; } @@ -139,7 +143,9 @@ int silc_net_create_connection(int port, char *host) } /* Set appropriate options */ +#if defined(TCP_NODELAY) silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1); +#endif silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1); SILC_LOG_DEBUG(("Connection created")); @@ -152,14 +158,60 @@ int silc_net_create_connection(int port, char *host) connection returns directly. To get the result of the connect() one must select() the socket and read the result after it's ready. */ -/* XXX Is the socket on WIN32 always non-blocking? */ - -int silc_net_create_connection_async(int port, char *host) +int silc_net_create_connection_async(const char *local_ip, int port, + const char *host) { + SOCKET sock; + int rval, err; + struct hostent *dest; + struct sockaddr_in desthost; + SILC_LOG_DEBUG(("Creating connection (async) to host %s port %d", host, port)); - return silc_net_create_connection(port, host); + /* Do host lookup */ + dest = gethostbyname(host); + if (!dest) { + SILC_LOG_ERROR(("Network (%s) unreachable: could not resolve the " + "IP address", host)); + return -1; + } + + /* Set socket information */ + memset(&desthost, 0, sizeof(desthost)); + desthost.sin_port = htons(port); + desthost.sin_family = AF_INET; + memcpy(&desthost.sin_addr, dest->h_addr_list[0], sizeof(desthost.sin_addr)); + + /* Create the connection socket */ + sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock == INVALID_SOCKET) { + SILC_LOG_ERROR(("Cannot create socket")); + return -1; + } + + /* Connect to the host */ + rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost)); + err = WSAGetLastError(); + if (rval == SOCKET_ERROR && err != WSAEWOULDBLOCK) { + SILC_LOG_ERROR(("Cannot connect to remote host")); + shutdown(sock, 2); + closesocket(sock); + return -1; + } + + /* Set socket to nonblocking mode */ + silc_net_set_socket_nonblock(sock); + + /* Set appropriate options */ +#if defined(TCP_NODELAY) + silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1); +#endif + silc_net_set_socket_opt(sock, SOL_SOCKET, SO_KEEPALIVE, 1); + + SILC_LOG_DEBUG(("Connection created")); + + return sock; } /* Closes the connection by closing the socket connection. */ @@ -172,8 +224,7 @@ void silc_net_close_connection(int sock) /* Converts the IP number string from numbers-and-dots notation to binary form. */ -bool silc_net_addr2bin(const char *addr, unsigned char *bin, - uint32 bin_len) +bool silc_net_addr2bin(const char *addr, void *bin, SilcUInt32 bin_len) { unsigned long ret; @@ -182,7 +233,43 @@ bool silc_net_addr2bin(const char *addr, unsigned char *bin, if (bin_len < 4) return FALSE; - SILC_PUT32_LSB(ret, bin); - + memcpy(bin, (unsigned char *)&ret, 4); return ret != INADDR_NONE; } + +/* Set socket to non-blocking mode. */ + +int silc_net_set_socket_nonblock(int sock) +{ + unsigned long on = 1; + return ioctlsocket(sock, FIONBIO, &on); +} + +/* Init Winsock2. */ + +bool silc_net_win32_init(void) +{ + int ret, sopt = SO_SYNCHRONOUS_NONALERT; + WSADATA wdata; + WORD ver = MAKEWORD(1, 1); + + ret = WSAStartup(ver, &wdata); + if (ret) + return FALSE; + + /* Allow using the SOCKET's as file descriptors so that we can poll + them with SILC Scheduler. */ + ret = setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char *)&sopt, + sizeof(sopt)); + if (ret) + return FALSE; + + return TRUE; +} + +/* Uninit Winsock2 */ + +void silc_net_win32_uninit(void) +{ + WSACleanup(); +}