瀏覽代碼

* Convert socksfile reading to new EncryptedStream/String format

Bryan Drewery 17 年之前
父節點
當前提交
c3f8e9ebbf
共有 5 個文件被更改,包括 65 次插入108 次删除
  1. 20 31
      src/dccutil.c
  2. 1 1
      src/dccutil.h
  3. 29 50
      src/misc.c
  4. 14 25
      src/net.c
  5. 1 1
      src/net.h

+ 20 - 31
src/dccutil.c

@@ -453,38 +453,30 @@ dcc_chatter(int idx)
 }
 
 int
-dcc_read(FILE *f, bool enc)
+dcc_read(bd::Stream& stream)
 {
-  char inbuf[1024] = "", *type = NULL, *buf = NULL, *buf_ptr = NULL;
   int idx = -1;
   bool isserv = 0;
-  const char salt1[] = SALT1;
+  bd::String buf, type;
 
-  while (fgets(inbuf, sizeof(inbuf), f) != NULL) {
-    remove_crlf(inbuf);
-    if (enc)
-      buf = buf_ptr = decrypt_string(salt1, inbuf);
-    else
-      buf = inbuf;
+  while (stream.tell() < stream.length()) {
+    buf = stream.getline().chomp();
 
-    if (!strcmp(buf, "+dcc")) {
-      if (enc)
-        free(buf_ptr);
+    if (buf == "+dcc")
       return idx;
-    }
     
-    type = newsplit(&buf);
-    if (!strcmp(type, "type")) {
+    type = newsplit(buf);
+    if (type == "type") {
       struct dcc_table *dcc_type = NULL;
       size_t dcc_size = 0;
 
-//      if (!strcmp(buf, "CHAT"))
+//      if (buf == "CHAT")
 //        dcc_type = &DCC_CHAT;
-      if (!strcmp(buf, "SERVER")) {
+      if (buf == "SERVER") {
         dcc_type = &SERVER_SOCKET;
         isserv = 1;
       }
-//      if (!strcmp(buf, "BOT"))
+//      if (buf == "BOT")
 //        dcc_type = &DCC_BOT;
     
       if (dcc_type) {
@@ -495,23 +487,20 @@ dcc_read(FILE *f, bool enc)
     }
 
     if (idx >= 0) {
-      if (!strcmp(type, "addr"))
-        dcc[idx].addr = my_atoul(buf);
-      if (!strcmp(type, "sock")) {
-        dcc[idx].sock = atoi(buf);
+      if (type == "addr")
+        dcc[idx].addr = my_atoul(buf.c_str());
+      if (type == "sock") {
+        dcc[idx].sock = atoi(buf.c_str());
         if (isserv)
           serv = dcc[idx].sock;
       }
-      if (!strcmp(type, "port"))
-        dcc[idx].port = atoi(buf);
-      if (!strcmp(type, "nick"))
-        strlcpy(dcc[idx].nick, buf, NICKLEN);
-      if (!strcmp(type, "host")) {
-        strlcpy(dcc[idx].host, buf, UHOSTLEN);
-      }
+      if (type == "port")
+        dcc[idx].port = atoi(buf.c_str());
+      if (type == "nick")
+        strlcpy(dcc[idx].nick, buf.c_str(), NICKLEN);
+      if (type == "host")
+        strlcpy(dcc[idx].host, buf.c_str(), UHOSTLEN);
     }
-    if (enc)
-      free(buf_ptr);
   }
   return -1;
 }

+ 1 - 1
src/dccutil.h

@@ -59,7 +59,7 @@ void identd_open(const char * = NULL, const char * = NULL);
 void identd_close();
 int listen_all(port_t, bool);
 bool valid_idx(int);
-int dcc_read(FILE *, bool);
+int dcc_read(bd::Stream&);
 void dcc_write(bd::Stream&, int);
 int check_cmd_pass(const char *, char *);
 int has_cmd_pass(const char *);

+ 29 - 50
src/misc.c

@@ -595,58 +595,41 @@ readsocks(const char *fname)
   if (!conf.bot->hub)
     restarting = 1;
 
-  FILE *f = NULL;
-
-  if (!(f = fopen(fname, "r"))) {
-    fatal(STR("CANT READ SOCKSFILE"), 0);
-  }
-
-  char buf[1024] = "", *nick = NULL, *jnick = NULL, *bufp = NULL, *type = NULL, *buf_ptr = NULL, *ip4 = NULL, *ip6 = NULL;
+  char *nick = NULL, *jnick = NULL, *ip4 = NULL, *ip6 = NULL;
   time_t old_buildts = 0;
 
-  bool enc = 0, first = 1, cached_005 = 0;
+  bool cached_005 = 0;
   const char salt1[] = SALT1;
-
-  while (fgets(buf, sizeof(buf), f) != NULL) {
-    remove_crlf(buf);
-
-    if (first) {
-      if (!strncmp(buf, STR("+enc"), 4))
-        enc = 1;
-      first = 0;
-    }
-
-    if (enc)
-      bufp = buf_ptr = decrypt_string(salt1, buf);
-    else
-      bufp = buf;
-
-//    dprintf(DP_STDOUT, "read line: %s\n", buf);
-    type = newsplit(&bufp);
-    if (!strcmp(type, STR("-dcc")))
-      dprintf(DP_STDOUT, STR("Added dcc: %d\n"), dcc_read(f, enc));
-    else if (!strcmp(type, STR("-sock")))
-      dprintf(DP_STDOUT, STR("Added fd: %d\n"), sock_read(f, enc));
-    else if (!strcmp(type, STR("+online_since")))
-      online_since = strtol(bufp, NULL, 10);
-    else if (!strcmp(type, STR("+server_floodless")))
+  EncryptedStream stream(salt1);
+  stream.loadFile(fname);
+  bd::String str, type;
+
+  while (stream.tell() < stream.length()) {
+    str = stream.getline().chomp();
+    type = newsplit(str);
+
+//    dprintf(DP_STDOUT, "read line: %s\n", buf.c_str());
+    if (type == STR("-dcc"))
+      dprintf(DP_STDOUT, STR("Added dcc: %d\n"), dcc_read(stream));
+    else if (type == "-sock")
+      dprintf(DP_STDOUT, STR("Added fd: %d\n"), sock_read(stream));
+    else if (type == "+online_since")
+      online_since = strtol(str.c_str(), NULL, 10);
+    else if (type == "+server_floodless")
       floodless = 1;
-    else if (!strcmp(type, STR("+buildts")))
-      old_buildts = strtol(bufp, NULL, 10);
-    else if (!strcmp(type, STR("+botname")))
-      nick = strdup(bufp);
-    else if (!strcmp(type, STR("+ip4")))
-      ip4 = strdup(bufp);
-    else if (!strcmp(type, STR("+ip6")))
-      ip6 = strdup(bufp);
-    else if (!strcmp(type, STR("+serv_cache"))) {
-      if (!cached_005 && strstr(bufp, "005"))
+    else if (type == "+buildts")
+      old_buildts = strtol(str.c_str(), NULL, 10);
+    else if (type == "+botname")
+      nick = strdup(str.c_str());
+    else if (type == "+ip4")
+      ip4 = strdup(str.c_str());
+    else if (type == "+ip6")
+      ip6 = strdup(str.c_str());
+    else if (type == "+serv_cache") {
+      if (!cached_005 && str.find("005"))
         cached_005 = 1;
-      dprintf(DP_CACHE, "%s", bufp);
+      dprintf(DP_CACHE, "%s", str.c_str());
     }
-
-    if (enc)
-      free(buf_ptr);
   }
 
   restart_time = now;
@@ -657,7 +640,6 @@ readsocks(const char *fname)
   tell_netdebug(DP_STDOUT);
 
   unlink(fname);
-  fclose(f);
 
   if (servidx >= 0) {
     char nserv[50] = "";
@@ -719,9 +701,6 @@ restart(int idx)
     }
   }
 
-  fprintf(socks->f, STR("+enc\n"));
-  fflush(socks->f);
-
   const char salt1[] = SALT1;
   EncryptedStream stream(salt1);
 

+ 14 - 25
src/net.c

@@ -360,45 +360,34 @@ int sockoptions(int sock, int operation, int sock_options)
 }
 
 int
-sock_read(FILE *f, bool enc)
+sock_read(bd::Stream& stream)
 {
-  char inbuf[1024] = "", *type = NULL, *buf = NULL, *buf_ptr = NULL;
   int fd = -1;
-  const char salt1[] = SALT1;
+  bd::String buf, type;
 
-  while (fgets(inbuf, sizeof(inbuf), f) != NULL) {
-    remove_crlf(inbuf);
+  while (stream.tell() < stream.length()) {
+    buf = stream.getline().chomp();
 
-    if (enc)
-      buf = buf_ptr = decrypt_string(salt1, inbuf);
-    else
-      buf = inbuf;
-
-    if (!strcmp(buf, "+sock")) {
-      if (enc)
-        free(buf_ptr);
+    if (buf == "+sock")
       return fd;
-    }
 
-    type = newsplit(&buf);
-    if (!strcmp(type, "sock")) {
-      int sock = atoi(newsplit(&buf)), options = atoi(newsplit(&buf));
+    type = newsplit(buf);
+    if (type == "sock") {
+      int sock = atoi(newsplit(buf).c_str()), options = atoi(newsplit(buf).c_str());
 
       fd = allocsock(sock, options);
     }
 
     if (fd >= 0) {
 #ifdef USE_IPV6
-      if (!strcmp(type, "af"))
-        socklist[fd].af = atoi(buf);
+      if (type == "af")
+        socklist[fd].af = atoi(buf.c_str());
 #endif
-      if (!strcmp(type, "host"))
-        socklist[fd].host = strdup(buf);
-      if (!strcmp(type, "port"))
-        socklist[fd].port = atoi(buf);
+      if (type == "host")
+        socklist[fd].host = strdup(buf.c_str());
+      if (type == "port")
+        socklist[fd].port = atoi(buf.c_str());
     }
-    if (enc)
-      free(buf_ptr);
   }
   return -1;
 }

+ 1 - 1
src/net.h

@@ -153,7 +153,7 @@ char *iptostr(in_addr_t);
 bool sock_has_data(int, int);
 int sockoptions(int sock, int operation, int sock_options);
 void init_net(void);
-int sock_read(FILE *, bool);
+int sock_read(bd::Stream&);
 void sock_write(bd::Stream&, int);
 
 extern union sockaddr_union 		cached_myip4_so;