int silc_net_create_connection_async(int port, 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", 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;
+ }
+
+ /* 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();
+ if (rval == SOCKET_ERROR && err != WSAEWOULDBLOCK) {
+ SILC_LOG_ERROR(("Cannot connect to remote host"));
+ shutdown(sock, 2);
+ closesocket(sock);
+ return -1;
+ }
+
+ /* Set appropriate options */
+ silc_net_set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
+ 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. */
int silc_net_set_socket_nonblock(int sock)
{
- return 0;
+ unsigned long on = 1;
+ return ioctlsocket(sock, FIONBIO, &on);
}
#include "silcincludes.h"
-/* XXX Untested! */
+/* XXX This probably does not work at all the way we want. We mostly
+ need the scheduler to handle socket connections. The MSDN says the
+ WaitForMultipleObjects should not be used for sockets. Instead they
+ should be handled through windows messages (Like WSAAsyncSelect) but
+ that would require some different approach to this whole thing. Also,
+ I don't know whether I could use the Winsock's select()?? Maybe it would
+ be possible to use select(), WaitForMultipleObjects and handle the
+ windows messages with the PeekMessage() etc... Dunno... Someone who
+ knows these things should take a look at this. Also, If it requires
+ some more tweaking I can abandon this silc_select() thingy all together
+ and move the generic code to unix/ and program the SILC Scheduler
+ interface all together as platform specific. It is here just to
+ use as match common code as possible... -Pekka */
/* Our "select()" for WIN32. This actually is not the select() and does
not call Winsock's select() (since it cannot be used for our purposes)