Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / s_bsd.c
index 3c02c469cbd24211c5aa63adf3b68451fe6efcf4..010d631151a8497cedc6192f9861015fefb682be 100644 (file)
@@ -196,12 +196,10 @@ static void connect_dns_callback(void* vptr, struct DNSReply* reply)
 void close_connections(int close_stderr)
 {
   int i;
-#if 0
   close(0);
   close(1);
   if (close_stderr)
     close(2);
-#endif
   for (i = 3; i < MAXCONNECTIONS; ++i)
     close(i);
 }
@@ -358,7 +356,7 @@ unsigned int deliver_it(struct Client *cptr, struct MsgQ *buf)
 
   switch (os_sendv_nonb(cli_fd(cptr), buf, &bytes_count, &bytes_written)) {
   case IO_SUCCESS:
-    cli_flags(cptr) &= ~FLAGS_BLOCKED;
+    ClrFlag(cptr, FLAG_BLOCKED);
 
     cli_sendB(cptr) += bytes_written;
     cli_sendB(&me)  += bytes_written;
@@ -375,14 +373,14 @@ unsigned int deliver_it(struct Client *cptr, struct MsgQ *buf)
      * say it was blocked
      */
     if (bytes_written < bytes_count)
-      cli_flags(cptr) |= FLAGS_BLOCKED;
+      SetFlag(cptr, FLAG_BLOCKED);
     break;
   case IO_BLOCKED:
-    cli_flags(cptr) |= FLAGS_BLOCKED;
+    SetFlag(cptr, FLAG_BLOCKED);
     break;
   case IO_FAILURE:
     cli_error(cptr) = errno;
-    cli_flags(cptr) |= FLAGS_DEADSOCKET;
+    SetFlag(cptr, FLAG_DEADSOCKET);
     break;
   }
   return bytes_written;
@@ -460,7 +458,7 @@ static int completed_connection(struct Client* cptr)
    * Make us timeout after twice the timeout for DNS look ups
    */
   cli_lasttime(cptr) = CurrentTime;
-  cli_flags(cptr) |= FLAGS_PINGSENT;
+  SetFlag(cptr, FLAG_PINGSENT);
 
   sendrawto_one(cptr, MSG_SERVER " %s 1 %Tu %Tu J%s %s%s +%s :%s",
                 cli_name(&me), cli_serv(&me)->timestamp, newts,
@@ -540,7 +538,7 @@ void close_connection(struct Client *cptr)
     socket_del(&(cli_socket(cptr))); /* queue a socket delete */
     cli_fd(cptr) = -1;
   }
-  cli_flags(cptr) |= FLAGS_DEADSOCKET;
+  SetFlag(cptr, FLAG_DEADSOCKET);
 
   MsgQClear(&(cli_sendQ(cptr)));
   client_drop_sendq(cli_connect(cptr));
@@ -704,18 +702,21 @@ static int read_packet(struct Client *cptr, int socket_ready)
        DBufLength(&(cli_recvQ(cptr))) > feature_int(FEAT_CLIENT_FLOOD))) {
     switch (os_recv_nonb(cli_fd(cptr), readbuf, sizeof(readbuf), &length)) {
     case IO_SUCCESS:
-      if (length) {
-        cli_lasttime(cptr) = CurrentTime;
+      if (length)
+      {
+        if (!IsServer(cptr))
+          cli_lasttime(cptr) = CurrentTime;
         if (cli_lasttime(cptr) > cli_since(cptr))
           cli_since(cptr) = cli_lasttime(cptr);
-        cli_flags(cptr) &= ~(FLAGS_PINGSENT | FLAGS_NONL);
+        ClrFlag(cptr, FLAG_PINGSENT);
+        ClrFlag(cptr, FLAG_NONL);
       }
       break;
     case IO_BLOCKED:
       break;
     case IO_FAILURE:
       cli_error(cptr) = errno;
-      /* cptr->flags |= FLAGS_DEADSOCKET; */
+      /* SetFlag(cpt, FLAG_DEADSOCKET); */
       return 0;
     }
   }
@@ -724,18 +725,19 @@ static int read_packet(struct Client *cptr, int socket_ready)
    * For server connections, we process as many as we can without
    * worrying about the time of day or anything :)
    */
-  if (length > 0 && IsServer(cptr)) {
+  if (length > 0 && IsServer(cptr))
     return server_dopacket(cptr, readbuf, length);
-  }
-  else {
+  else if (length > 0 && (IsHandshake(cptr) || IsConnecting(cptr)))
+    return connect_dopacket(cptr, readbuf, length);
+  else
+  {
     /*
      * Before we even think of parsing what we just read, stick
      * it on the end of the receive queue and do it when its
      * turn comes around.
      */
-    if (length > 0 && 0 == dbuf_put(&(cli_recvQ(cptr)), readbuf, length)) {
+    if (length > 0 && dbuf_put(&(cli_recvQ(cptr)), readbuf, length) == 0)
       return exit_client(cptr, cptr, &me, "dbuf_put fail");
-    }
 
     if (DBufLength(&(cli_recvQ(cptr))) > feature_int(FEAT_CLIENT_FLOOD))
       return exit_client(cptr, cptr, &me, "Excess Flood");
@@ -743,14 +745,6 @@ static int read_packet(struct Client *cptr, int socket_ready)
     while (DBufLength(&(cli_recvQ(cptr))) && !NoNewLine(cptr) && 
            (IsTrusted(cptr) || cli_since(cptr) - CurrentTime < 10))
     {
-      /*
-       * If it has become registered as a Server
-       * then skip the per-message parsing below.
-       */
-      if (IsServer(cptr)) {
-        dolen = dbuf_get(&(cli_recvQ(cptr)), readbuf, sizeof(readbuf));
-        return (dolen) ? server_dopacket(cptr, readbuf, dolen) : 1;
-      }
       dolen = dbuf_getmsg(&(cli_recvQ(cptr)), cli_buffer(cptr), BUFSIZE);
       /*
        * Devious looking...whats it do ? well..if a client
@@ -760,19 +754,46 @@ static int read_packet(struct Client *cptr, int socket_ready)
        * deletes the rest of the buffer contents.
        * -avalon
        */
-      if (0 == dolen) {
+      if (dolen == 0)
+      {
         if (DBufLength(&(cli_recvQ(cptr))) < 510)
-          cli_flags(cptr) |= FLAGS_NONL;
+          SetFlag(cptr, FLAG_NONL);
         else
           DBufClear(&(cli_recvQ(cptr)));
       }
-      else if (CPTR_KILLED == client_dopacket(cptr, dolen))
+      else if (client_dopacket(cptr, dolen) == CPTR_KILLED)
         return CPTR_KILLED;
+      /*
+       * If it has become registered as a Server
+       * then skip the per-message parsing below.
+       */
+      if (IsHandshake(cptr) || IsServer(cptr))
+      {
+        while (-1)
+        {
+          dolen = dbuf_get(&(cli_recvQ(cptr)), readbuf, sizeof(readbuf));
+          if (dolen <= 0)
+            return 1;
+          else if (dolen == 0)
+          {
+            if (DBufLength(&(cli_recvQ(cptr))) < 510)
+              SetFlag(cptr, FLAG_NONL);
+            else
+              DBufClear(&(cli_recvQ(cptr)));
+          }
+          else if ((IsServer(cptr) &&
+                    server_dopacket(cptr, readbuf, dolen) == CPTR_KILLED) ||
+                   (!IsServer(cptr) &&
+                    connect_dopacket(cptr, readbuf, dolen) == CPTR_KILLED))
+            return CPTR_KILLED;
+        }
+      }
     }
 
     /* If there's still data to process, wait 2 seconds first */
     if (DBufLength(&(cli_recvQ(cptr))) && !NoNewLine(cptr) &&
-       !t_onqueue(&(cli_proc(cptr)))) {
+       !t_onqueue(&(cli_proc(cptr))))
+    {
       Debug((DEBUG_LIST, "Adding client process timer for %C", cptr));
       cli_freeflag(cptr) |= FREEFLAG_TIMER;
       timer_add(&(cli_proc(cptr)), client_timer_callback, cli_connect(cptr),
@@ -1001,7 +1022,7 @@ static void client_sock_callback(struct Event* ev)
   case ET_EOF: /* end of file on socket */
     Debug((DEBUG_ERROR, "READ ERROR: fd = %d %d", cli_fd(cptr),
           cli_error(cptr)));
-    cli_flags(cptr) |= FLAGS_DEADSOCKET;
+    SetFlag(cptr, FLAG_DEADSOCKET);
     if ((IsServer(cptr) || IsHandshake(cptr)) && cli_error(cptr) == 0) {
       exit_client_msg(cptr, cptr, &me, "Server %s closed the connection (%s)",
                      cli_name(cptr), cli_serv(cptr)->last_error_msg);
@@ -1013,7 +1034,7 @@ static void client_sock_callback(struct Event* ev)
     break;
 
   case ET_WRITE: /* socket is writable */
-    cli_flags(cptr) &= ~FLAGS_BLOCKED;
+    ClrFlag(cptr, FLAG_BLOCKED);
     if (cli_listing(cptr) && MsgQLength(&(cli_sendQ(cptr))) < 2048)
       list_next_channels(cptr, 64);
     Debug((DEBUG_SEND, "Sending queued data to %C", cptr));