updates. New data types.
[silc.git] / lib / silcutil / silcnet.c
index 78d7ce6b233d490864ff9b1cb24632f3701e5358..bb55b3a2709ef7f867decb04834a711eb5e62b2b 100644 (file)
@@ -52,7 +52,8 @@ int silc_net_create_server(int port, char *ip_addr)
   /* Set the socket information for bind() */
   memset(&server, 0, sizeof(server));
   server.sin_family = PF_INET;
-  server.sin_port = htons(port);
+  if (port)
+    server.sin_port = htons(port);
 
   /* Convert IP address to network byte order */
   if (ip_addr)
@@ -292,7 +293,7 @@ void silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
 
 /* Return remote port by socket. */
 
-unsigned short silc_net_get_remote_port(int sock)
+uint16 silc_net_get_remote_port(int sock)
 {
   struct sockaddr_in remote;
   int len;
@@ -305,6 +306,21 @@ unsigned short silc_net_get_remote_port(int sock)
   return ntohs(remote.sin_port);
 }
 
+/* Return local port by socket. */
+
+uint16 silc_net_get_local_port(int sock)
+{
+  struct sockaddr_in local;
+  int len;
+
+  memset(&local, 0, sizeof(local));
+  len = sizeof(local);
+  if (getsockname(sock, (struct sockaddr *)&local, &len) < 0)
+    return 0;
+
+  return ntohs(local.sin_port);
+}
+
 /* Return name of localhost. */
 
 char *silc_net_localhost()