فهرست منبع

* Use findanysnum() in more places

Bryan Drewery 16 سال پیش
والد
کامیت
32122c6e17
2فایلهای تغییر یافته به همراه88 افزوده شده و 100 حذف شده
  1. 82 93
      src/net.c
  2. 6 7
      src/socket.c

+ 82 - 93
src/net.c

@@ -260,16 +260,15 @@ void cache_my_ip()
  */
 int sockoptions(int sock, int operation, int sock_options)
 {
-  for (int i = 0; i < MAXSOCKS; i++) {
-    if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED)) {
-      if (operation == EGG_OPTION_SET)
-	      socklist[i].flags |= sock_options;
-      else if (operation == EGG_OPTION_UNSET)
-	      socklist[i].flags &= ~sock_options;
-      else
-	      return -2;
-      return 0;
-    }
+  int i = -1;
+  if ((i = findanysnum(sock)) != -1) {
+    if (operation == EGG_OPTION_SET)
+      socklist[i].flags |= sock_options;
+    else if (operation == EGG_OPTION_UNSET)
+      socklist[i].flags &= ~sock_options;
+    else
+      return -2;
+    return 0;
   }
   return -1;
 }
@@ -406,25 +405,24 @@ void real_killsock(register int sock, const char *file, int line)
     return;
   }
 
-  for (register int i = 0; i < MAXSOCKS; i++) {
-    if ((socklist[i].sock == sock) && !(socklist[i].flags & SOCK_UNUSED)) {
-      close(socklist[i].sock);
-      if (socklist[i].inbuf != NULL) {
-	delete socklist[i].inbuf;
-	socklist[i].inbuf = NULL;
-      }
-      if (socklist[i].outbuf != NULL) {
-	delete socklist[i].outbuf;
-	socklist[i].outbuf = NULL;
-      }
-      if (socklist[i].host)
-        free(socklist[i].host);
-      bzero(&socklist[i], sizeof(socklist[i]));
-      socklist[i].flags = SOCK_UNUSED;
-      socks_total--;
-      sdprintf("killsock(%d, %s, %d) (socklist: %d)", sock, file, line, i);
-      return;
+  int i = -1;
+  if ((i = findanysnum(sock)) != -1) {
+    close(socklist[i].sock);
+    if (socklist[i].inbuf != NULL) {
+      delete socklist[i].inbuf;
+      socklist[i].inbuf = NULL;
+    }
+    if (socklist[i].outbuf != NULL) {
+      delete socklist[i].outbuf;
+      socklist[i].outbuf = NULL;
     }
+    if (socklist[i].host)
+      free(socklist[i].host);
+    bzero(&socklist[i], sizeof(socklist[i]));
+    socklist[i].flags = SOCK_UNUSED;
+    socks_total--;
+    sdprintf("killsock(%d, %s, %d) (socklist: %d)", sock, file, line, i);
+    return;
   }
   putlog(LOG_MISC, "*", "Attempt to kill un-allocated socket %d %s:%d !!", sock, file, line);
 }
@@ -451,9 +449,9 @@ static int proxy_connect(int sock, const char *ip, port_t port, int proxy_type)
     } else {	/* if not resolved, resolve it with blocking calls.. (shouldn't happen ever) */
       return -1;
     }
-    for (int i = 0; i < MAXSOCKS; i++)
-      if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
-        socklist[i].flags |= SOCK_PROXYWAIT;
+    int i = -1;
+    if ((i = findanysnum(sock)) != -1)
+      socklist[i].flags |= SOCK_PROXYWAIT;
 #ifdef USE_IPV6
     if (af_ty == AF_INET6)
       simple_snprintf(s, sizeof s,
@@ -574,13 +572,11 @@ int open_telnet_raw(int sock, const char *ipIn, port_t sport, bool proxy_on, int
     socklen = SUN_LEN(&so.sock_un);
   }
 
-  for (int i = 0; i < MAXSOCKS; i++) {
-    if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == sock)) {
-      socklist[i].flags = (socklist[i].flags & ~SOCK_VIRTUAL) | SOCK_CONNECT;
-      socklist[i].host = strdup(ipIn);
-      socklist[i].port = port;
-      break;
-    }
+  int i = -1;
+  if ((i = findanysnum(sock)) != -1) {
+    socklist[i].flags = (socklist[i].flags & ~SOCK_VIRTUAL) | SOCK_CONNECT;
+    socklist[i].host = strdup(ipIn);
+    socklist[i].port = port;
   }
 
   if (identd && sport) //Only open identd if not a unix domain socket
@@ -1245,59 +1241,55 @@ void tputs(register int z, const char *s, size_t len)
 
   register int x, idx;
 
-  for (register int i = 0; i < MAXSOCKS; i++) {
-    if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == z)) {
-      for (idx = 0; idx < dcc_total; idx++) {
-        if (dcc[idx].type && (dcc[idx].sock == z) && dcc[idx].type->name) {
-          if (!strncmp(dcc[idx].type->name, "BOT", 3))
-                traffic.out_today.bn += len;
-          else if (!strcmp(dcc[idx].type->name, "SERVER"))
-                traffic.out_today.irc += len;
-          else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
-                traffic.out_today.dcc += len;
-          else if (!strncmp(dcc[idx].type->name, "FILES", 5))
-                traffic.out_today.filesys += len;
-          else if (!strcmp(dcc[idx].type->name, "SEND"))
-                traffic.out_today.trans += len;
-          else if (!strncmp(dcc[idx].type->name, "GET", 3))
-                traffic.out_today.trans += len;
-          else
-                traffic.out_today.unknown += len;
-          break;
-        }
-      }
+  int i = -1;
+  if ((i = findanysnum(z)) != -1) {
+    if ((idx = findanyidx(z)) != -1 && dcc[idx].type->name) {
+      if (!strncmp(dcc[idx].type->name, "BOT", 3))
+        traffic.out_today.bn += len;
+      else if (!strcmp(dcc[idx].type->name, "SERVER"))
+        traffic.out_today.irc += len;
+      else if (!strncmp(dcc[idx].type->name, "CHAT", 4))
+        traffic.out_today.dcc += len;
+      else if (!strncmp(dcc[idx].type->name, "FILES", 5))
+        traffic.out_today.filesys += len;
+      else if (!strcmp(dcc[idx].type->name, "SEND"))
+        traffic.out_today.trans += len;
+      else if (!strncmp(dcc[idx].type->name, "GET", 3))
+        traffic.out_today.trans += len;
+      else
+        traffic.out_today.unknown += len;
+    }
 
-      if (len && socklist[i].encstatus)
-        s = link_write(i, s, &len);
+    if (len && socklist[i].encstatus)
+      s = link_write(i, s, &len);
 
 #ifdef HAVE_ZLIB_H
-/*
-      if (socklist[i].gz) { 		
-        FILE *fp;
-        fp = gzdopen(z, "wb0");
-        x = gzwrite(fp, s, len);
-        
-      } else
-*/
+    /*
+       if (socklist[i].gz) {
+       FILE *fp;
+       fp = gzdopen(z, "wb0");
+       x = gzwrite(fp, s, len);
+
+       } else
+       */
 #endif /* HAVE_ZLIB_H */
 
-      if (socklist[i].outbuf != NULL) {
-	/* Already queueing: just add it */
-        *(socklist[i].outbuf) += bd::String(s, len);
-	return;
-      }
-      /* Try. */
-        x = write(z, s, len);
-      if (x == -1)
-	x = 0;
-      if ((size_t) x < len) {
-	/* Socket is full, queue it */
-	socklist[i].outbuf = new bd::String(&s[x], len - x);
-      }
-//      if (socklist[i].encstatus && s)
-//        free(s);
+    if (socklist[i].outbuf != NULL) {
+      /* Already queueing: just add it */
+      *(socklist[i].outbuf) += bd::String(s, len);
       return;
     }
+    /* Try. */
+    x = write(z, s, len);
+    if (x == -1)
+      x = 0;
+    if ((size_t) x < len) {
+      /* Socket is full, queue it */
+      socklist[i].outbuf = new bd::String(&s[x], len - x);
+    }
+    //      if (socklist[i].encstatus && s)
+    //        free(s);
+    return;
   }
 
   /* Make sure we don't cause a crash by looping here */
@@ -1457,19 +1449,16 @@ void tell_netdebug(int idx)
 bool sock_has_data(int type, int sock)
 {
   bool ret = 0;
-  int i;
+  int i = -1;
 
-  for (i = 0; i < MAXSOCKS; i++)
-    if (!(socklist[i].flags & SOCK_UNUSED) && socklist[i].sock == sock)
-      break;
-  if (i < MAXSOCKS) {
+  if ((i = findanysnum(sock)) != -1) {
     switch (type) {
       case SOCK_DATA_OUTGOING:
-	ret = (socklist[i].outbuf != NULL);
-	break;
+        ret = (socklist[i].outbuf != NULL);
+        break;
       case SOCK_DATA_INCOMING:
-	ret = (socklist[i].inbuf != NULL);
-	break;
+        ret = (socklist[i].inbuf != NULL);
+        break;
     }
   } else
     debug1("sock_has_data: could not find socket #%d, returning false.", sock);

+ 6 - 7
src/socket.c

@@ -215,13 +215,12 @@ int socket_create(const char *dest_ip, int dest_port, const char *src_ip, int sr
 
 
         if (flags & SOCKET_CLIENT) {
-          for (int i = 0; i < MAXSOCKS; i++) {
-            if (!(socklist[i].flags & SOCK_UNUSED) && (socklist[i].sock == sock)) {
-              socklist[i].flags = (socklist[i].flags & ~SOCK_VIRTUAL) | SOCK_CONNECT | SOCK_PASS;
-              socklist[i].host = strdup(dest_ip);
-              socklist[i].port = dest_port;
-            }
-          }
+			int i = -1;
+			if ((i = findanysnum(sock)) != -1) {
+				socklist[i].flags = (socklist[i].flags & ~SOCK_VIRTUAL) | SOCK_CONNECT | SOCK_PASS;
+				socklist[i].host = strdup(dest_ip);
+				socklist[i].port = dest_port;
+			}
 
           if (connect(sock, &dest_name.u.addr, dest_name.len) != 0) {
 		if (errno != EINPROGRESS) {