updates. New data types.
[silc.git] / lib / silcutil / silcnet.c
index d06731d12a9597b4feeb655cf35008946b0fab9e..bb55b3a2709ef7f867decb04834a711eb5e62b2b 100644 (file)
   GNU General Public License for more details.
 
 */
-/*
- * $Id$
- * $Log$
- * Revision 1.3  2000/12/17 13:07:35  priikone
- *     Added require_reverse_mapping for ServerParams.
- *
- * Revision 1.2  2000/10/31 19:48:32  priikone
- *     A LOT updates. Cannot separate. :)
- *
- * Revision 1.1  2000/09/13 17:45:16  priikone
- *     Splitted SILC core library. Core library includes now only
- *     SILC protocol specific stuff. New utility library includes the
- *     old stuff from core library that is more generic purpose stuff.
- *
- * Revision 1.3  2000/07/05 06:06:35  priikone
- *     Global cosmetic change.
- *
- * Revision 1.2  2000/06/30 10:49:48  priikone
- *     Added SOCKS4 and SOCKS5 support for SILC client.
- *
- * Revision 1.1.1.1  2000/06/27 11:36:55  priikone
- *     Imported from internal CVS/Added Log headers.
- *
- *
- */
+/* $Id$ */
 
 #include "silcincludes.h"
 #include "silcnet.h"
@@ -76,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)
@@ -314,6 +291,36 @@ void silc_net_check_host_by_sock(int sock, char **hostname, char **ip)
   SILC_LOG_DEBUG(("Resolved IP address `%s'", *ip));
 }
 
+/* Return remote port by socket. */
+
+uint16 silc_net_get_remote_port(int sock)
+{
+  struct sockaddr_in remote;
+  int len;
+
+  memset(&remote, 0, sizeof(remote));
+  len = sizeof(remote);
+  if (getpeername(sock, (struct sockaddr *)&remote, &len) < 0)
+    return 0;
+
+  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()