Added silc_net_tcp_create_listener2
[crypto.git] / lib / silcutil / tests / test_silcnet.c
index 246d56bf3b7a04ce9cd5d35963a1e76e9bef52e8..29748619abd1c56ae56424b2842e276f31bc2bc3 100644 (file)
@@ -66,21 +66,52 @@ SILC_FSM_STATE(test_st_connected)
 
   if (f->server_status != SILC_NET_OK) {
     SILC_LOG_DEBUG(("Creating connection failed"));
-    SILC_FSM_FINISH;
+    return SILC_FSM_FINISH;
   }
 
   silc_socket_stream_get_info(f->server_stream, NULL, &host, &ip, &port);
   SILC_LOG_DEBUG(("Connected to server %s, %s:%d", host, ip, port));
 
-  SILC_FSM_FINISH;
+  return SILC_FSM_FINISH;
 }
 
 SILC_FSM_STATE(test_st_start)
 {
   Foo f = fsm_context;
+  int ports[3];
+  SilcUInt16 *ret_ports;
+  SilcUInt32 port_count;
 
   SILC_LOG_DEBUG(("test_st_start"));
 
+  SILC_LOG_DEBUG(("Creating network listener to ports 2000, 3000 and 4000"));
+  ports[0] = 2000;
+  ports[1] = 3000;
+  ports[2] = 4000;
+  f->server = silc_net_tcp_create_listener2(NULL, ports, 3, FALSE, TRUE, TRUE,
+                                    silc_fsm_get_schedule(fsm),
+                                    test_accept_connection, f);
+  if (!f->server) {
+    /** Creating network listener failed */
+    SILC_LOG_DEBUG(("Listener creation failed"));
+    silc_fsm_next(fsm, test_st_finish);
+    return SILC_FSM_CONTINUE;
+  }
+
+  ret_ports = silc_net_listener_get_port(f->server, &port_count);
+  if (!ret_ports) {
+    SILC_LOG_DEBUG(("Listener does not work"));
+    silc_fsm_next(fsm, test_st_finish);
+    return SILC_FSM_CONTINUE;
+  }
+  SILC_LOG_DEBUG(("Bound to port %d", ret_ports[0]));
+  SILC_LOG_DEBUG(("Bound to port %d", ret_ports[1]));
+  SILC_LOG_DEBUG(("Bound to port %d", ret_ports[2]));
+  silc_free(ret_ports);
+
+  /* Close this listener and create new one */
+  silc_net_close_listener(f->server);
+
   SILC_LOG_DEBUG(("Creating network listener"));
   f->server = silc_net_tcp_create_listener(NULL, 0, 5000, TRUE, TRUE,
                                     silc_fsm_get_schedule(fsm),
@@ -89,7 +120,7 @@ SILC_FSM_STATE(test_st_start)
     /** Creating network listener failed */
     SILC_LOG_DEBUG(("Listener creation failed"));
     silc_fsm_next(fsm, test_st_finish);
-    SILC_FSM_CONTINUE;
+    return SILC_FSM_CONTINUE;
   }
 
   /* Create thread to connect to the listener */
@@ -100,7 +131,7 @@ SILC_FSM_STATE(test_st_start)
   SILC_LOG_DEBUG(("Start waiting for incoming connections"));
   silc_fsm_event_init(&f->sema, fsm);
   silc_fsm_next(fsm, test_st_second);
-  SILC_FSM_CONTINUE;
+  return SILC_FSM_CONTINUE;
 }
 
 SILC_FSM_STATE(test_st_second)
@@ -117,7 +148,7 @@ SILC_FSM_STATE(test_st_second)
     /** Accepting new connection failed */
     SILC_LOG_DEBUG(("Accepting failed %d", f->client_status));
     silc_fsm_next(fsm, test_st_finish);
-    SILC_FSM_CONTINUE;
+    return SILC_FSM_CONTINUE;
   }
 
   silc_socket_stream_get_info(f->client_stream, NULL, &host, &ip, &port);
@@ -148,7 +179,7 @@ SILC_FSM_STATE(test_st_finish)
   silc_net_close_listener(f->server);
 
   SILC_LOG_DEBUG(("Finish machine"));
-  SILC_FSM_FINISH;
+  return SILC_FSM_FINISH;
 }
 
 static void destructor(SilcFSM fsm, void *fsm_context,
@@ -172,7 +203,7 @@ int main(int argc, char **argv)
   }
 
   SILC_LOG_DEBUG(("Allocating scheduler"));
-  schedule = silc_schedule_init(0, NULL);
+  schedule = silc_schedule_init(0, NULL, NULL);
 
   f = silc_calloc(1, sizeof(*f));
   if (!f)