updates.
authorPekka Riikonen <priikone@silcnet.org>
Sun, 24 Jun 2001 09:38:22 +0000 (09:38 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Sun, 24 Jun 2001 09:38:22 +0000 (09:38 +0000)
lib/silcutil/silcsockconn.h
lib/silcutil/unix/silcunixsockconn.c
lib/silcutil/win32/silcwin32net.c
lib/silcutil/win32/silcwin32schedule.c

index d3a95c67e05b949511e0a4d64e7cad66d816d242..c265bfb86cb4e073e93e16fd4a7829b94775f402 100644 (file)
@@ -6,9 +6,9 @@
  *
  * COPYRIGHT
  *
- * Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
+ * Author: Pekka Riikonen <priikone@silnet.org>
  *
- * Copyright (C) 1997 - 2000 Pekka Riikonen
+ * Copyright (C) 1997 - 2001 Pekka Riikonen
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
index 8932d0cde66625aaf9af2030f23fa27614ddae55..acd8cb5eb5662bee12613756cac86e9ab9878d64 100644 (file)
@@ -2,9 +2,9 @@
 
   silcunixsockconn.c
 
-  Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
+  Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2000 Pekka Riikonen
+  Copyright (C) 1997 - 2001 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
index d3ff04748ed6ca0767e512cac044788c5a29ea35..e34d981aa59c0f776fe41fc0967db4e8ad03577a 100644 (file)
@@ -156,10 +156,54 @@ int silc_net_create_connection(int port, char *host)
 
 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. */
@@ -191,5 +235,6 @@ bool silc_net_addr2bin(const char *addr, unsigned char *bin,
 
 int silc_net_set_socket_nonblock(int sock)
 {
-  return 0;
+  unsigned long on = 1;
+  return ioctlsocket(sock, FIONBIO, &on);
 }
index e0f18a0b948fd61d2eb3a078ca9d9495f6f9731d..d87681dbee51a8d86ee08590378c34b8cb1e4ce6 100644 (file)
 
 #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)