Merge branch 'master' of ssh://gapil.gnulinux.it/srv/git/gapil
[gapil.git] / sources / SockUtil.c
index 28b22defae96326ad406f723a85bea0484316bb2..af3a3f7c59990b0ff43853efd16ece5dc3549ac0 100644 (file)
@@ -1,4 +1,4 @@
-/* Sockutils.c
+/* SockUtil.c
  * 
  * Copyright (C) 2004 Simone Piccardi
  * 
  */
 /***************************************************************
  *
- * File SockUtils.c 
+ * File SockUtil.c 
  * Routines for socket operations. 
  *
  * Define routines for socket handling 
  *
  * Author: S. Piccardi
  *
- * $Id$
- *
  ***************************************************************/
-#include <sys/types.h>
-#include <stdio.h>                                 /* standard I/O functions */
-#include <unistd.h>
-#include <string.h>
-#include <errno.h>
-#include <arpa/inet.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <netdb.h>
+#include <sys/types.h>   /* primitive system data types */
+#include <stdio.h>      /* standard I/O library */
+#include <unistd.h>      /* unix standard library */
+#include <string.h>      /* C strings library */
+#include <errno.h>       /* error definitions and routines */
+#include <sys/socket.h>  /* socket constants, types and functions */
+#include <arpa/inet.h>   /* IP addresses conversion utilities */
+#include <netdb.h>       /* C resolver library */
 
 #include "macros.h"
 /**************************************************************************
@@ -50,8 +46,6 @@
  * Author: Simone Piccardi
  * Dec. 2004
  *
- * $Id$ 
- *
  **************************************************************************/
 char *ip_ntop(struct addrinfo *addr, char *dst, socklen_t cnt)
 {
@@ -82,8 +76,6 @@ char *ip_ntop(struct addrinfo *addr, char *dst, socklen_t cnt)
  * Author: Simone Piccardi
  * Dec. 2004
  *
- * $Id$ 
- *
  ****************************************************************/
 int sockconn(char *host, char *serv, int prot, int type) 
 {
@@ -140,8 +132,6 @@ int sockconn(char *host, char *serv, int prot, int type)
  * Author: Simone Piccardi
  * Dec. 2004
  *
- * $Id$ 
- *
  ****************************************************************/
 int sockbind(char *host, char *serv, int prot, int type) 
 {
@@ -195,22 +185,19 @@ int sockbind(char *host, char *serv, int prot, int type)
 }
 /****************************************************************
  *
- * Routine sockbind2
+ * Routine sockbindopt
  * Return a binded socket given hostname, service, and socket type
- * Issue a SO_REUSEADDR on the socket before binding.
+ * Issue a SO_REUSEADDR on the socket before binding on reuse value.
  *
  * Author: Simone Piccardi
  * Mar. 2005
  *
- * $Id$ 
- *
  ****************************************************************/
-int sockbind2(char *host, char *serv, int prot, int type) 
+int sockbindopt(char *host, char *serv, int prot, int type, int reuse) 
 {
     struct addrinfo hint, *addr, *save;
     int res;
     int sock;
-    int opt=1;
     char buf[INET6_ADDRSTRLEN];
     /* initialize hint structure */
     memset(&hint, 0, sizeof(struct addrinfo)); 
@@ -240,7 +227,8 @@ int sockbind2(char *host, char *serv, int prot, int type)
            }
        }
        /* connect the socket */
-       if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
+       if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, 
+                      &reuse, sizeof(reuse))) {
            printf("error on socket options\n");
            return -1;
        }