Merge branch 'silc.1.1.branch' of silc repo
[runtime.git] / lib / silcutil / unix / silcunixnet.c
index de7eeaee881d333af97733a7747d21f275bbc8bf..c216ccca1f04f1f1f2ed86d812480e6f12402ca0 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2007 Pekka Riikonen
+  Copyright (C) 1997 - 2008 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
   GNU General Public License for more details.
 
 */
-/* $Id$ */
 
-#include "silc.h"
-#include "silcnet.h"
+#include "silcruntime.h"
 
 /************************** Types and definitions ***************************/
 
@@ -143,7 +141,15 @@ silc_net_tcp_create_listener(const char **local_ip_addr,
 
   SILC_LOG_DEBUG(("Creating TCP listener"));
 
-  if (port < 0 || !schedule || !callback) {
+  if (!schedule) {
+    schedule = silc_schedule_get_global();
+    if (!schedule) {
+      silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
+      goto err;
+    }
+  }
+
+  if (port < 0 || !callback) {
     silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
     goto err;
   }
@@ -253,7 +259,15 @@ silc_net_tcp_create_listener2(const char *local_ip_addr, int *ports,
 
   SILC_LOG_DEBUG(("Creating TCP listener"));
 
-  if (!schedule || !callback) {
+  if (!schedule) {
+    schedule = silc_schedule_get_global();
+    if (!schedule) {
+      silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
+      goto err;
+    }
+  }
+
+  if (!callback) {
     silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
     goto err;
   }
@@ -397,8 +411,11 @@ silc_net_udp_connect(const char *local_ip_addr, int local_port,
   SILC_LOG_DEBUG(("Creating UDP stream"));
 
   if (!schedule) {
-    silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
-    goto err;
+    schedule = silc_schedule_get_global();
+    if (!schedule) {
+      silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
+      goto err;
+    }
   }
 
   /* Bind to local addresses */
@@ -505,7 +522,8 @@ int silc_net_udp_receive(SilcStream stream, char *remote_ip_addr,
   SilcSocketStream sock = stream;
   SilcSockaddr s;
   struct sockaddr *from;
-  int len, flen;
+  int len;
+  unsigned int flen;
 
   SILC_LOG_DEBUG(("Reading data from UDP socket %d", sock->sock));
 
@@ -837,6 +855,9 @@ SILC_FSM_STATE(silc_net_connect_st_finish)
       silc_async_free(conn->op);
   }
 
+  if (conn->sock && conn->status != SILC_NET_OK)
+    silc_net_close_connection(conn->sock);
+
   return SILC_FSM_FINISH;
 }
 
@@ -872,8 +893,18 @@ SilcAsyncOperation silc_net_tcp_connect(const char *local_ip_addr,
 {
   SilcNetConnect conn;
 
-  if (!remote_ip_addr || remote_port < 1 || !schedule || !callback)
+  if (!schedule) {
+    schedule = silc_schedule_get_global();
+    if (!schedule) {
+      silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
+      return NULL;
+    }
+  }
+
+  if (!remote_ip_addr || remote_port < 1 || !callback) {
+    silc_set_errno(SILC_ERR_INVALID_ARGUMENT);
     return NULL;
+  }
 
   SILC_LOG_DEBUG(("Creating connection to host %s port %d",
                  remote_ip_addr, remote_port));