Kaynağa Gözat

* Localhub now listens on the unix domain socket

Bryan Drewery 16 yıl önce
ebeveyn
işleme
4dca23e75f
3 değiştirilmiş dosya ile 79 ekleme ve 27 silme
  1. 17 0
      src/chanprog.c
  2. 59 25
      src/net.c
  3. 3 2
      src/net.h

+ 17 - 0
src/chanprog.c

@@ -577,6 +577,23 @@ void rehash_ip() {
     struct bot_addr *bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, conf.bot->u);
     listen_all(bi->telnet_port, 0);
     my_port = bi->telnet_port;
+  } else if (conf.bot->localhub) {
+    // Listen on the unix domain socket
+    port_t port;
+    int i = open_listen_addr_by_af(conf.localhub_socket, &port, AF_UNIX);
+    if (i < 0) {
+      putlog(LOG_ERRORS, "*", "Can't listen on %s - %s", conf.localhub_socket, i == -1 ? "it's taken." : "couldn't assign file.");
+    } else {
+      /* now setup dcc entry */
+      int idx = new_dcc(&DCC_TELNET, 0);
+      dcc[idx].addr = 0L;
+      strlcpy(dcc[idx].host, conf.localhub_socket, sizeof(dcc[idx].host));
+      dcc[idx].port = 0;
+      dcc[idx].sock = i;
+      dcc[idx].timeval = now;
+      strlcpy(dcc[idx].nick, "(unix_domain)", NICKLEN);
+      putlog(LOG_DEBUG, "*", "Listening on telnet %s", conf.localhub_socket);
+    }
   }
 }
 

+ 59 - 25
src/net.c

@@ -653,9 +653,9 @@ int open_telnet(const char *ip, port_t port, bool proxy, int identd)
  * 'addr' is ignored if af_def is AF_INET6 -poptix (02/03/03)
  */
 #ifdef USE_IPV6
-int open_address_listen(in_addr_t addr, int af_def, port_t *port)
+int open_address_listen(const char* ip, int af_def, port_t *port)
 #else
-int open_address_listen(in_addr_t addr, port_t *port)
+int open_address_listen(const char* ip, port_t *port)
 #endif /* USE_IPV6 */
  {
 //  if (firewall[0]) {
@@ -666,7 +666,7 @@ int open_address_listen(in_addr_t addr, port_t *port)
 
   int sock = 0;
   socklen_t addrlen;
-  struct sockaddr_in name;
+  union sockaddr_union name;
 
 #ifdef USE_IPV6
   if (af_def == AF_INET6) {
@@ -703,7 +703,7 @@ int open_address_listen(in_addr_t addr, port_t *port)
       return -1;
     }
   } else {
-    sock = getsock(SOCK_LISTEN, AF_INET);
+    sock = getsock(SOCK_LISTEN, af_def);
 #else
     sock = getsock(SOCK_LISTEN);
 #endif /* USE_IPV6 */
@@ -711,28 +711,48 @@ int open_address_listen(in_addr_t addr, port_t *port)
     if (sock < 0)
       return -1;
 
-    debug2("Opening listen socket on port %d with AF_INET, sock: %d", *port, sock);
-    bzero((char *) &name, sizeof(struct sockaddr_in));
-    name.sin_family = AF_INET;
-    name.sin_port = htons(*port); /* 0 = just assign us a port */
-    name.sin_addr.s_addr = addr;
-    if (bind(sock, (struct sockaddr *) &name, sizeof(name)) < 0) {
-      if (!(identd_hack && *port == 113))
-        putlog(LOG_DEBUG, "*", "Failed to bind to socket %d for listen on port %d: %s", sock, *port, strerror(errno));
-      killsock(sock);
-      return -1;
+    if (af_def == AF_UNIX)
+      debug2("Opening listen socket on %s, sock: %d", ip, sock);
+    else
+      debug3("Opening listen socket on %s:%d with AF_INET, sock: %d", ip, *port, sock);
+
+    bzero((char *) &name, sizeof(struct sockaddr *));
+    if (af_def == AF_UNIX) {
+      name.sun.sun_family = AF_UNIX;
+      strcpy(name.sun.sun_path, ip);
+      unlink(name.sun.sun_path);
+      addrlen = strlen(name.sun.sun_path) + sizeof(name.sun.sun_family);
+    } else {
+      name.sin.sin_family = af_def;
+      name.sin.sin_port = htons(*port); /* 0 = just assign us a port */
+      name.sin.sin_addr.s_addr = inet_addr(ip);
+      addrlen = sizeof(struct sockaddr_in*);
     }
-    /* what port are we on? */
-    addrlen = sizeof(name);
-    if (getsockname(sock, (struct sockaddr *) &name, &addrlen) < 0) {
-      if (!(identd_hack && *port == 113))
-        putlog(LOG_DEBUG, "*", "Failed to getsockname on socket %d for listen on port %d: %s", sock, *port, strerror(errno));
+
+    if (bind(sock, (struct sockaddr *) &name, addrlen) < 0) {
+      if (!(identd_hack && *port == 113)) {
+        if (af_def == AF_UNIX)
+          putlog(LOG_DEBUG, "*", "Failed to bind to socket %d for listen on %s: %s", sock, ip, strerror(errno));
+        else
+          putlog(LOG_DEBUG, "*", "Failed to bind to socket %d for listen on %s:%d: %s", sock, ip, *port, strerror(errno));
+      }
       killsock(sock);
       return -1;
     }
-    *port = ntohs(name.sin_port);
+
+    if (af_def != AF_UNIX) {
+      /* what port are we on? */
+      if (getsockname(sock, (struct sockaddr *) &name, &addrlen) < 0) {
+        if (!(identd_hack && *port == 113))
+          putlog(LOG_DEBUG, "*", "Failed to getsockname on socket %d for listen on port %d: %s", sock, *port, strerror(errno));
+        killsock(sock);
+        return -1;
+      }
+      *port = ntohs(name.sin.sin_port);
+    }
+
     if (listen(sock, 1) < 0) {
-      if (!(identd_hack && *port == 113))
+      if (!(identd_hack && *port == 113) && af_def != AF_UNIX)
         putlog(LOG_DEBUG, "*", "Failed to listen on socket %d for on port %d: %s", sock, *port, strerror(errno));
       killsock(sock);
       return -1;
@@ -741,7 +761,10 @@ int open_address_listen(in_addr_t addr, port_t *port)
   }
 #endif /* USE_IPV6 */
 
-  debug2("Opened listen socket on port %d with AF_INET, sock: %d", *port, sock);
+  if (af_def == AF_UNIX)
+    debug2("Opened listen socket on %s, sock: %d", ip, sock);
+  else
+    debug3("Opened listen socket on %s:%d with AF_INET, sock: %d", ip, *port, sock);
 
   return sock;
 }
@@ -752,9 +775,9 @@ int open_address_listen(in_addr_t addr, port_t *port)
 int open_listen(port_t *port)
 {
 #ifdef USE_IPV6
-  return open_address_listen(getmyip(), AF_INET, port);
+  return open_address_listen(iptostr(getmyip()), AF_INET, port);
 #else
-  return open_address_listen(getmyip(), port);
+  return open_address_listen(iptostr(getmyip()), port);
 #endif /* USE_IPV6 */
 }
 
@@ -765,7 +788,18 @@ int open_listen(port_t *port)
 int open_listen_by_af(port_t *port, int af_def)
 {
 #ifdef USE_IPV6
-  return open_address_listen(getmyip(), af_def, port);
+  return open_address_listen(iptostr(getmyip()), af_def, port);
+#else
+  return -1;
+#endif /* USE_IPV6 */
+}
+
+int open_listen_addr_by_af(const char *ip, port_t *port, int af_def)
+{
+  if (!ip)
+    ip = iptostr(getmyip());
+#ifdef USE_IPV6
+  return open_address_listen(ip, af_def, port);
 #else
   return -1;
 #endif /* USE_IPV6 */

+ 3 - 2
src/net.h

@@ -119,10 +119,11 @@ int answer(int, char *, in_addr_t *, port_t *, int);
 int findanysnum(register int);
 int open_listen(port_t *);
 int open_listen_by_af(port_t *, int);
+int open_listen_addr_by_af(const char*, port_t *, int);
 #ifdef USE_IPV6
-int open_address_listen(in_addr_t, int, port_t *);
+int open_address_listen(const char*, int, port_t *);
 #else
-int open_address_listen(in_addr_t, port_t *);
+int open_address_listen(const char*, port_t *);
 #endif /* USE_IPV6 */
 int open_telnet(const char *, port_t, bool proxy = 0, int identd = 0);
 int open_telnet_dcc(int, char *, char *);