*/
/* $Id$ */
+/* XXX IPv6 support missing */
+
#include "silcincludes.h"
#include "silcnet.h"
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;
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;
/* 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;
}
}
/* 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"));
connection returns directly. To get the result of the connect() one
must select() the socket and read the result after it's ready. */
-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;
/* 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;
}
return -1;
}
- /* Set socket to nonblocking mode */
- silc_net_set_socket_nonblock(sock);
-
/* Connect to the host */
rval = connect(sock, (struct sockaddr *)&desthost, sizeof(desthost));
err = WSAGetLastError();
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"));
/* 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;
if (bin_len < 4)
return FALSE;
- SILC_PUT32_LSB(ret, bin);
-
+ memcpy(bin, (unsigned char *)&ret, 4);
return ret != INADDR_NONE;
}
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();
+}