changeset 1387:16cf380cd04c

Convert dpid, file dpi and cookies dpi to dsh API cookies: convert to dsh API (with auth) file dpi: convert to dsh API (with auth) dpid: convert to dsh API (with auth) handle EINTR in connect(), authenticated DpiBye cmd
author Jorge Arellano Cid <jcid@dillo.org>
date Sun, 01 Nov 2009 16:31:59 -0300
parents 8f6d5a94ac0e
children eb35203124e4
files dpi/cookies.c dpi/file.c dpid/dpi.h dpid/dpid.c dpid/main.c dpip/dpip.c dpip/dpip.h src/IO/dpi.c
diffstat 8 files changed, 192 insertions(+), 139 deletions(-) [+]
line wrap: on
line diff
--- a/dpi/cookies.c	Sun Nov 01 16:31:59 2009 -0300
+++ b/dpi/cookies.c	Sun Nov 01 16:31:59 2009 -0300
@@ -42,6 +42,7 @@
 #include <sys/socket.h>
 #include <sys/stat.h>
 #include <sys/un.h>
+#include <netinet/in.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <errno.h>
@@ -114,6 +115,11 @@
    Dlist *ports;
 } CookieData_t;
 
+typedef struct {
+   Dsh *sh;
+   int status;
+} ClientInfo;
+
 /*
  * Local data
  */
@@ -1343,22 +1349,25 @@
  * Note: Buf is a zero terminated string
  * Return code: { 0:OK, 1:Abort, 2:Close }
  */
-static int srv_parse_buf(SockHandler *sh, char *Buf, size_t BufSize)
+static int srv_parse_tok(Dsh *sh, ClientInfo *client, char *Buf)
 {
    char *p, *cmd, *cookie, *host, *path, *scheme;
-   int port, ret;
-
-   if (!(p = strchr(Buf, '>'))) {
-      /* Haven't got a full tag */
-      MSG("Haven't got a full tag!\n");
-      return 1;
-   }
+   int port, ret = 1;
+   size_t BufSize = strlen(Buf);
 
    cmd = a_Dpip_get_attr_l(Buf, BufSize, "cmd");
 
-   if (cmd && strcmp(cmd, "DpiBye") == 0) {
+   if (!cmd) {
+      /* abort */
+   } else if (client->status == 0) {
+      /* authenticate */
+      if (a_Dpip_check_auth(Buf) == 1) {
+         client->status = 1;
+         ret = 0;
+      }
+   } else if (strcmp(cmd, "DpiBye") == 0) {
       dFree(cmd);
-      MSG("Cookies dpi (pid %d): Got DpiBye.\n", (int)getpid());
+      MSG("(pid %d): Got DpiBye.\n", (int)getpid());
       exit(0);
 
    } else if (cmd && strcmp(cmd, "set_cookie") == 0) {
@@ -1375,7 +1384,7 @@
       dFree(path);
       dFree(host);
       dFree(cookie);
-      return 2;
+      ret = 2;
 
    } else if (cmd && strcmp(cmd, "get_cookie") == 0) {
       dFree(cmd);
@@ -1393,19 +1402,17 @@
 
       cmd = a_Dpip_build_cmd("cmd=%s cookie=%s", "get_cookie_answer", cookie);
 
-      if (sock_handler_write_str(sh, 1, cmd)) {
+      if (a_Dpip_dsh_write_str(sh, 1, cmd)) {
           ret = 1;
       } else {
-          _MSG("sock_handler_write_str: SUCCESS cmd={%s}\n", cmd);
+          _MSG("a_Dpip_dsh_write_str: SUCCESS cmd={%s}\n", cmd);
           ret = 2;
       }
       dFree(cookie);
       dFree(cmd);
-
-      return ret;
    }
 
-   return 0;
+   return ret;
 }
 
 /* --  Termination handlers ----------------------------------------------- */
@@ -1433,12 +1440,12 @@
  * -- MAIN -------------------------------------------------------------------
  */
 int main(void) {
-   struct sockaddr_un spun;
-   int temp_sock_descriptor;
+   struct sockaddr_in sin;
    socklen_t address_size;
+   ClientInfo *client;
+   int tmp_fd,code;
    char *buf;
-   int code;
-   SockHandler *sh;
+   Dsh *sh;
 
    /* Arrange the cleanup function for terminations via exit() */
    atexit(cleanup);
@@ -1458,36 +1465,45 @@
       exit(1);
 
    /* some OSes may need this... */
-   address_size = sizeof(struct sockaddr_un);
+   address_size = sizeof(struct sockaddr_in);
 
    while (1) {
-      temp_sock_descriptor =
-         accept(STDIN_FILENO, (struct sockaddr *)&spun, &address_size);
-      if (temp_sock_descriptor == -1) {
+      tmp_fd = accept(STDIN_FILENO, (struct sockaddr *)&sin, &address_size);
+      if (tmp_fd == -1) {
          perror("[accept]");
          exit(1);
       }
 
-      /* create the SockHandler structure */
-      sh = sock_handler_new(temp_sock_descriptor,temp_sock_descriptor,8*1024);
+      /* create the Dsh structure */
+      sh = a_Dpip_dsh_new(tmp_fd, tmp_fd, 8*1024);
+      client = dNew(ClientInfo,1);
+      client->sh = sh;
+      client->status = 0;
 
       while (1) {
          code = 1;
-         if ((buf = sock_handler_read(sh)) != NULL) {
+         if ((buf = a_Dpip_dsh_read_token(sh)) != NULL) {
             /* Let's see what we fished... */
             _MSG(" buf = {%s}\n", buf);
-            code = srv_parse_buf(sh, buf, strlen(buf));
+            code = srv_parse_tok(sh, client, buf);
+            dFree(buf);
+         } else if (sh->status == DPIP_EAGAIN) {
+            /* may reach here when the tag size is larger than kernel buffer */
+            continue;
          }
+
          _MSG(" code = %d %s\n", code, code == 1 ? "EXIT" : "BREAK");
-         if (code == 1)
+         if (code == 1) {
             exit(1);
-         else if (code == 2)
+         } else if (code == 2) {
             break;
+         }
       }
 
-      _MSG("Closing SockHandler\n");
-      sock_handler_close(sh);
-      sock_handler_free(sh);
+      _MSG("Closing Dsh\n");
+      a_Dpip_dsh_close(sh);
+      a_Dpip_dsh_free(sh);
+      dFree(client);
 
    }/*while*/
 
--- a/dpi/file.c	Sun Nov 01 16:31:59 2009 -0300
+++ b/dpi/file.c	Sun Nov 01 16:31:59 2009 -0300
@@ -61,7 +61,7 @@
 } DilloDir;
 
 typedef struct {
-   SockHandler *sh;
+   Dsh *sh;
    int status;
    int old_style;
    pthread_t thrID;
@@ -278,7 +278,7 @@
 
       Uparent = Escape_uri_str(parent, NULL);
       HUparent = Escape_html_str(Uparent);
-      sock_handler_printf(Client->sh, 0,
+      a_Dpip_dsh_printf(Client->sh, 0,
          "<a href='file:%s'>Parent directory</a>", HUparent);
       dFree(HUparent);
       dFree(Uparent);
@@ -295,14 +295,14 @@
 
    /* Month, day and {hour or year} */
    if (Client->old_style) {
-      sock_handler_printf(Client->sh, 0, " %.3s %.2s", ds + 4, ds + 8);
+      a_Dpip_dsh_printf(Client->sh, 0, " %.3s %.2s", ds + 4, ds + 8);
       if (time(NULL) - mtime > 15811200) {
-         sock_handler_printf(Client->sh, 0, "  %.4s", ds + 20);
+         a_Dpip_dsh_printf(Client->sh, 0, "  %.4s", ds + 20);
       } else {
-         sock_handler_printf(Client->sh, 0, " %.5s", ds + 11);
+         a_Dpip_dsh_printf(Client->sh, 0, " %.5s", ds + 11);
       }
    } else {
-      sock_handler_printf(Client->sh, 0,
+      a_Dpip_dsh_printf(Client->sh, 0,
          "<td>%.3s&nbsp;%.2s&nbsp;%.5s", ds + 4, ds + 8,
          /* (more than 6 months old) ? year : hour; */
          (time(NULL) - mtime > 15811200) ? ds + 20 : ds + 11);
@@ -358,7 +358,7 @@
    if (Client->old_style) {
       char *dots = ".. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. ..";
       int ndots = MAXNAMESIZE - strlen(name);
-      sock_handler_printf(Client->sh, 0,
+      a_Dpip_dsh_printf(Client->sh, 0,
          "%s<a href='%s'>%s</a>"
          " %s"
          " %-11s%4d %-5s",
@@ -367,7 +367,7 @@
          filecont, size, sizeunits);
 
    } else {
-      sock_handler_printf(Client->sh, 0,
+      a_Dpip_dsh_printf(Client->sh, 0,
          "<tr align=center %s><td>%s<td align=left><a href='%s'>%s</a>"
          "<td>%s<td>%d&nbsp;%s",
          (n & 1) ? "bgcolor=#dcdcdc" : "",
@@ -375,7 +375,7 @@
          filecont, size, sizeunits);
    }
    File_print_mtime(Client, finfo->mtime);
-   sock_handler_write_str(Client->sh, 0, "\n");
+   a_Dpip_dsh_write_str(Client->sh, 0, "\n");
 
    dFree(Hname);
    dFree(HUref);
@@ -393,7 +393,7 @@
 
    /* Send DPI header */
    d_cmd = a_Dpip_build_cmd("cmd=%s url=%s", "start_send_page", orig_url);
-   sock_handler_write_str(Client->sh, 1, d_cmd);
+   a_Dpip_dsh_write_str(Client->sh, 1, d_cmd);
    dFree(d_cmd);
 
    /* Send page title */
@@ -401,7 +401,7 @@
    HUdirname = Escape_html_str(Udirname);
    Hdirname = Escape_html_str(Ddir->dirname);
 
-   sock_handler_printf(Client->sh, 0,
+   a_Dpip_dsh_printf(Client->sh, 0,
       "HTTP/1.1 200 OK\r\n"
       "Content-Type: text/html\r\n"
       "\r\n"
@@ -415,21 +415,21 @@
    dFree(Udirname);
 
    if (Client->old_style) {
-      sock_handler_write_str(Client->sh, 0, "<pre>\n");
+      a_Dpip_dsh_write_str(Client->sh, 0, "<pre>\n");
    }
 
    /* Output the parent directory */
    File_print_parent_dir(Client, Ddir->dirname);
 
    /* HTML style toggle */
-   sock_handler_write_str(Client->sh, 0,
+   a_Dpip_dsh_write_str(Client->sh, 0,
       "&nbsp;&nbsp;<a href='dpi:/file/toggle'>%</a>\n");
 
    if (dList_length(Ddir->flist)) {
       if (Client->old_style) {
-         sock_handler_write_str(Client->sh, 0, "\n\n");
+         a_Dpip_dsh_write_str(Client->sh, 0, "\n\n");
       } else {
-         sock_handler_write_str(Client->sh, 0,
+         a_Dpip_dsh_write_str(Client->sh, 0,
             "<br><br>\n"
             "<table border=0 cellpadding=1 cellspacing=0"
             " bgcolor=#E0E0E0 width=100%>\n"
@@ -441,7 +441,7 @@
             "<td><b>Modified&nbsp;at</b>\n");
       }
    } else {
-      sock_handler_write_str(Client->sh, 0, "<br><br>Directory is empty...");
+      a_Dpip_dsh_write_str(Client->sh, 0, "<br><br>Directory is empty...");
    }
 
    /* Output entries */
@@ -451,13 +451,13 @@
 
    if (dList_length(Ddir->flist)) {
       if (Client->old_style) {
-         sock_handler_write_str(Client->sh, 0, "</pre>\n");
+         a_Dpip_dsh_write_str(Client->sh, 0, "</pre>\n");
       } else {
-         sock_handler_write_str(Client->sh, 0, "</table>\n");
+         a_Dpip_dsh_write_str(Client->sh, 0, "</table>\n");
       }
    }
 
-   sock_handler_write_str(Client->sh, 0, "</BODY></HTML>\n");
+   a_Dpip_dsh_write_str(Client->sh, 0, "</BODY></HTML>\n");
 }
 
 /*
@@ -545,15 +545,16 @@
 
    /* Send DPI command */
    d_cmd = a_Dpip_build_cmd("cmd=%s url=%s", "start_send_page", orig_url);
-   sock_handler_write_str(Client->sh, 1, d_cmd);
+   a_Dpip_dsh_write_str(Client->sh, 1, d_cmd);
    dFree(d_cmd);
 
-   sock_handler_printf(Client->sh, 0, "HTTP/1.1 %s\r\n"
-                                      "Content-Type: text/plain\r\n"
-                                      "Content-Length: %ld\r\n"
-                                      "\r\n"
-                                      "%s",
-                                      status, strlen(body), body);
+   a_Dpip_dsh_printf(Client->sh, 0, 
+                     "HTTP/1.1 %s\r\n"
+                     "Content-Type: text/plain\r\n"
+                     "Content-Length: %ld\r\n"
+                     "\r\n"
+                     "%s",
+                     status, strlen(body), body);
 }
 
 /*
@@ -626,7 +627,7 @@
 
    /* Send DPI command */
    d_cmd = a_Dpip_build_cmd("cmd=%s url=%s", "start_send_page", orig_url);
-   sock_handler_write_str(Client->sh, 1, d_cmd);
+   a_Dpip_dsh_write_str(Client->sh, 1, d_cmd);
    dFree(d_cmd);
 
    /* Check for gzipped file */
@@ -646,23 +647,24 @@
    dFree(name);
 
    /* Send HTTP headers */
-   sock_handler_write_str(Client->sh, 0, "HTTP/1.1 200 OK\r\n");
+   a_Dpip_dsh_write_str(Client->sh, 0, "HTTP/1.1 200 OK\r\n");
    if (gzipped) {
-      sock_handler_write_str(Client->sh, 0, "Content-Encoding: gzip\r\n");
+      a_Dpip_dsh_write_str(Client->sh, 0, "Content-Encoding: gzip\r\n");
    }
    if (!gzipped || strcmp(ct, unknown_type)) {
-      sock_handler_printf(Client->sh, 0, "Content-Type: %s\r\n", ct);
+      a_Dpip_dsh_printf(Client->sh, 0, "Content-Type: %s\r\n", ct);
    } else {
       /* If we don't know type for gzipped data, let dillo figure it out. */
    }
-   sock_handler_printf(Client->sh, 0, "Content-Length: %ld\r\n"
-                                      "\r\n",
-                                      sb->st_size);
+   a_Dpip_dsh_printf(Client->sh, 0,
+                     "Content-Length: %ld\r\n"
+                     "\r\n",
+                     sb->st_size);
 
    /* Send body -- raw file contents */
    do {
       if ((st = read(fd, buf, LBUF)) > 0) {
-         if (sock_handler_write(Client->sh, 0, buf, (size_t)st) != 0)
+         if (a_Dpip_dsh_write(Client->sh, 0, buf, (size_t)st) != 0)
             break;
       } else if (st < 0) {
          perror("[read]");
@@ -761,7 +763,7 @@
 
    OLD_STYLE = !OLD_STYLE;
    d_cmd = a_Dpip_build_cmd("cmd=%s", "reload_request");
-   sock_handler_write_str(Client->sh, 1, d_cmd);
+   a_Dpip_dsh_write_str(Client->sh, 1, d_cmd);
    dFree(d_cmd);
 }
 
@@ -784,7 +786,7 @@
    ClientInfo *NewClient;
 
    NewClient = dNew(ClientInfo, 1);
-   NewClient->sh = sock_handler_new(sock_fd, sock_fd, 8*1024);
+   NewClient->sh = a_Dpip_dsh_new(sock_fd, sock_fd, 8*1024);
    NewClient->status = 0;
    NewClient->done = 0;
    NewClient->old_style = OLD_STYLE;
@@ -821,8 +823,8 @@
   pthread_mutex_unlock(&ClMut);
 
    _MSG("Closing Socket Handler\n");
-   sock_handler_close(Client->sh);
-   sock_handler_free(Client->sh);
+   a_Dpip_dsh_close(Client->sh);
+   a_Dpip_dsh_free(Client->sh);
    dFree(Client);
 }
 
@@ -846,34 +848,32 @@
  */
 static void File_serve_client(void *data)
 {
-   char *dpip_tag = NULL, *cmd = NULL, *url = NULL, *path, *auth, *p;
+   char *dpip_tag = NULL, *cmd = NULL, *url = NULL, *path;
    ClientInfo *Client = data;
+   int st;
 
    /* Authenticate our client... */
-   auth = sock_handler_read(Client->sh);
-   if ((p = strchr(auth, '<')) != NULL) {
-      /* auth and dpip's tag are in one chunk, separate them */
-      dpip_tag = dStrdup(p);
-      *p = 0;
-   }
-   MSG("auth={%s}\n", auth);
-   if (a_Dpip_check_auth(auth) == -1) {
-      dFree(dpip_tag);
-      dFree(auth);
-      return;
-   }
-   dFree(auth);
+   do {
+      dpip_tag = a_Dpip_dsh_read_token(Client->sh);
+   } while (!dpip_tag && Client->sh->status == DPIP_EAGAIN);
+   _MSG("auth={%s}\n", dpip_tag);
+   st = a_Dpip_check_auth(dpip_tag);
+   dFree(dpip_tag);
+   _MSG("a_Dpip_check_auth returned %d\n", st);
+   dReturn_if (st == -1);
 
    /* Read the dpi command */
-   if (!dpip_tag)
-      dpip_tag = sock_handler_read(Client->sh);
-   MSG("dpip_tag={%s}\n", dpip_tag);
+   do {
+      dpip_tag = a_Dpip_dsh_read_token(Client->sh);
+   } while (!dpip_tag && Client->sh->status == DPIP_EAGAIN);
+   _MSG("dpip_tag={%s}\n", dpip_tag);
 
    if (dpip_tag) {
       cmd = a_Dpip_get_attr(dpip_tag, "cmd");
       if (cmd) {
          if (strcmp(cmd, "DpiBye") == 0) {
             DPIBYE = 1;
+            MSG("file.dpi:: (pid %d): Got DpiBye.\n", (int)getpid());
          } else {
             url = a_Dpip_get_attr(dpip_tag, "url");
             if (!url)
--- a/dpid/dpi.h	Sun Nov 01 16:31:59 2009 -0300
+++ b/dpid/dpi.h	Sun Nov 01 16:31:59 2009 -0300
@@ -28,7 +28,8 @@
  */
 enum {
    UNKNOWN_CMD,
-   BYE_CMD, /* "DpiBye" */
+   AUTH_CMD, /* authentication */
+   BYE_CMD,  /* "DpiBye" */
    CHECK_SERVER_CMD, /* "check_server" */
    REGISTER_ALL_CMD, /* "register_all" */
    REGISTER_SERVICE_CMD /* "register_service" */
--- a/dpid/dpid.c	Sun Nov 01 16:31:59 2009 -0300
+++ b/dpid/dpid.c	Sun Nov 01 16:31:59 2009 -0300
@@ -700,16 +700,30 @@
    }
 }
 
+/*! EINTR aware connect() call */
+int ckd_connect (int sock_fd, struct sockaddr *addr, socklen_t len)
+{
+   ssize_t ret;
+
+   do {
+      ret = connect(sock_fd, addr, len);
+   } while (ret == -1 && errno == EINTR);
+   if (ret == -1) {
+      ERRMSG("dpid.c", "connect", errno);
+   }
+   return ret;
+}
+
 /*! Send DpiBye command to all active non-filter dpis
  */
 void stop_active_dpis(struct dp *dpi_attr_list, int numdpis)
 {
-   static char *DpiBye_cmd = NULL;
+   char *bye_cmd, *auth_cmd;
    int i, sock_fd;
    struct sockaddr_in sin;
 
-   if (!DpiBye_cmd)
-      DpiBye_cmd = a_Dpip_build_cmd("cmd=%s", "DpiBye");
+   bye_cmd = a_Dpip_build_cmd("cmd=%s", "DpiBye");
+   auth_cmd = a_Dpip_build_cmd("cmd=%s msg=%s", "auth", SharedKey);
 
    memset(&sin, 0, sizeof(sin));
    sin.sin_family = AF_INET;
@@ -725,16 +739,19 @@
          continue;
       }
       sin.sin_port = htons(dpi_attr_list[i].port);
-      if (connect(sock_fd, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
+      if (ckd_connect(sock_fd, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
          ERRMSG("stop_active_dpis", "connect", errno);
          MSG_ERR("%s\n", dpi_attr_list[i].path);
-      } else if (write(sock_fd, SharedKey, strlen(SharedKey)) == -1) {
+      } else if (CKD_WRITE(sock_fd, auth_cmd) == -1) {
          ERRMSG("stop_active_dpis", "write", errno);
-      } else if (write(sock_fd, DpiBye_cmd, strlen(DpiBye_cmd)) == -1) {
+      } else if (CKD_WRITE(sock_fd, bye_cmd) == -1) {
          ERRMSG("stop_active_dpis", "write", errno);
       }
       a_Misc_close_fd(sock_fd);
    }
+
+   dFree(auth_cmd);
+   dFree(bye_cmd);
 }
 
 /*! Removes dpis in dpi_attr_list from the
--- a/dpid/main.c	Sun Nov 01 16:31:59 2009 -0300
+++ b/dpid/main.c	Sun Nov 01 16:31:59 2009 -0300
@@ -112,32 +112,17 @@
  * \Return
  * pointer to dynamically allocated request tag
  */
-static char *get_request(int sock)
+static char *get_request(Dsh *sh)
 {
-   char *req, buf[10];
-   size_t buflen;
-   size_t rqsz;
-   ssize_t rdln;
-
-   req = NULL;
-   buf[0] = '\0';
-   buflen = sizeof(buf) / sizeof(buf[0]);
+   char *dpip_tag;
 
    (void) sigprocmask(SIG_BLOCK, &mask_sigchld, NULL);
-   for (rqsz = 0; (rdln = read(sock, buf, buflen)) != 0; rqsz += rdln) {
-      if (rdln == -1)
-         break;
-      req = (char *) realloc(req, rqsz + rdln + 1);
-      if (rqsz == 0)
-         req[0] = '\0';
-      strncat(req, buf, (size_t) rdln);
-   }
+   do {
+      dpip_tag = a_Dpip_dsh_read_token(sh);
+   } while (!dpip_tag && sh->status == DPIP_EAGAIN);
    (void) sigprocmask(SIG_UNBLOCK, &mask_sigchld, NULL);
-   if (rdln == -1) {
-      ERRMSG("get_request", "read", errno);
-   }
 
-   return (req);
+   return dpip_tag;
 }
 
 /*!
@@ -145,7 +130,7 @@
  * \Return
  * command code on success, -1 on failure
  */
-static int get_command(int sock, char *dpi_tag)
+static int get_command(Dsh *sh, char *dpi_tag)
 {
    char *cmd, *d_cmd;
    int COMMAND;
@@ -162,9 +147,11 @@
       MSG_ERR(": dpid failed to parse cmd in %s\n", dpi_tag);
       d_cmd = a_Dpip_build_cmd("cmd=%s msg=%s",
                                "DpiError", "Failed to parse request");
-      (void) CKD_WRITE(sock, d_cmd);
+      a_Dpip_dsh_write_str(sh, 1, d_cmd);
       dFree(d_cmd);
       COMMAND = -1;
+   } else if (strcmp("auth", cmd) == 0) {
+      COMMAND = AUTH_CMD;
    } else if (strcmp("DpiBye", cmd) == 0) {
       COMMAND = BYE_CMD;
    } else if (strcmp("check_server", cmd) == 0) {
@@ -337,10 +324,19 @@
             MSG_ERR("service pending connections, and continue\n");
          } else {
             int command;
+            Dsh *sh;
 
-            req = get_request(sock_fd);
-            command = get_command(sock_fd, req);
+            sh = a_Dpip_dsh_new(sock_fd, sock_fd, 1024);
+read_next:
+            req = get_request(sh);
+            command = get_command(sh, req);
             switch (command) {
+            case AUTH_CMD:
+               if (a_Dpip_check_auth(req) != -1) {
+                  dFree(req);
+                  goto read_next;
+               }
+               break;
             case BYE_CMD:
                stop_active_dpis(dpi_attr_list, numdpis);
                //cleanup();
@@ -368,7 +364,8 @@
             }
             if (req)
                free(req);
-            a_Misc_close_fd(sock_fd);
+            a_Dpip_dsh_close(sh);
+            a_Dpip_dsh_free(sh);
          }
       }
 
--- a/dpip/dpip.c	Sun Nov 01 16:31:59 2009 -0300
+++ b/dpip/dpip.c	Sun Nov 01 16:31:59 2009 -0300
@@ -124,10 +124,11 @@
  *
  * Return value: the attribute value, or NULL if not present or malformed.
  */
-char *a_Dpip_get_attr_l(char *tag, size_t tagsize, const char *attrname)
+char *a_Dpip_get_attr_l(const char *tag, size_t tagsize, const char *attrname)
 {
    uint_t i, n = 0, found = 0;
-   char *p, *q, *start, *val = NULL;
+   const char *p, *q, *start;
+   char *r, *s, *val = NULL;
    DpipTagParsingState state = SEEK_NAME;
 
    if (!tag || !tagsize || !attrname || !*attrname)
@@ -169,9 +170,9 @@
          p = q + 2;
       if (q && q[1] == ' ') {
          val = dStrndup(start, (uint_t)(q - start));
-         for (p = q = val; (*q = *p); ++p, ++q)
-            if (*p == Quote && p[1] == p[0])
-               ++p;
+         for (r = s = val; (*r = *s); ++r, ++s)
+            if (s[0] == Quote && s[0] == s[1])
+               ++s;
       }
    }
    return val;
@@ -181,7 +182,7 @@
  * Task: given a tag and an attribute name, return its value.
  * Return value: the attribute value, or NULL if not present or malformed.
  */
-char *a_Dpip_get_attr(char *tag, const char *attrname)
+char *a_Dpip_get_attr(const char *tag, const char *attrname)
 {
    return (tag ? a_Dpip_get_attr_l(tag, strlen(tag), attrname) : NULL);
 }
@@ -190,14 +191,19 @@
  * Check whether the given 'auth' string equals what dpid saved.
  * Return value: 1 if equal, -1 otherwise
  */
-int a_Dpip_check_auth(const char *auth)
+int a_Dpip_check_auth(const char *auth_tag)
 {
    char SharedSecret[32];
    FILE *In;
-   char *fname, *rcline = NULL, *tail;
+   char *fname, *rcline = NULL, *tail, *cmd, *msg;
    int i, port, ret = -1;
 
-   dReturn_val_if (auth == NULL, -1);
+   /* sanity checks */
+   if (!auth_tag ||
+       !(cmd = a_Dpip_get_attr(auth_tag, "cmd")) || strcmp(cmd, "auth") ||
+       !(msg = a_Dpip_get_attr(auth_tag, "msg"))) {
+      return ret;
+   }
 
    fname = dStrconcat(dGethomedir(), "/.dillo/dpid_comm_keys", NULL);
    if ((In = fopen(fname, "r")) == NULL) {
@@ -209,11 +215,13 @@
       for (i = 0; *tail && isxdigit(tail[i+1]); ++i)
          SharedSecret[i] = tail[i+1];
       SharedSecret[i] = 0;
-      if (strcmp(auth, SharedSecret) == 0)
+      if (strcmp(msg, SharedSecret) == 0)
          ret = 1;
    }
    dFree(rcline);
    dFree(fname);
+   dFree(msg);
+   dFree(cmd);
 
    return ret;
 }
@@ -376,9 +384,12 @@
 
 /*
  * Close this socket for reading and writing.
+ * (flush pending data)
  */
 void a_Dpip_dsh_close(Dsh *dsh)
 {
+   /* flush internal buffer */
+   a_Dpip_dsh_write(dsh, 1, "", 0);
    fclose(dsh->out);
    close(dsh->fd_out);
 }
--- a/dpip/dpip.h	Sun Nov 01 16:31:59 2009 -0300
+++ b/dpip/dpip.h	Sun Nov 01 16:31:59 2009 -0300
@@ -57,8 +57,8 @@
  *       (dpip character escaping is removed here)
  * Return value: the attribute value, or NULL if not present or malformed.
  */
-char *a_Dpip_get_attr(char *tag, const char *attrname);
-char *a_Dpip_get_attr_l(char *tag, size_t tagsize, const char *attrname);
+char *a_Dpip_get_attr(const char *tag, const char *attrname);
+char *a_Dpip_get_attr_l(const char *tag, size_t tagsize, const char *attrname);
 
 int a_Dpip_check_auth(const char *auth);
 
@@ -72,6 +72,13 @@
 void a_Dpip_dsh_close(Dsh *dsh);
 void a_Dpip_dsh_free(Dsh *dsh);
 
+#define a_Dpip_dsh_printf(sh, flush, ...)                   \
+   D_STMT_START {                                           \
+      Dstr *dstr = dStr_sized_new(128);                     \
+      dStr_sprintf(dstr, __VA_ARGS__);                      \
+      a_Dpip_dsh_write(sh, flush, dstr->str, dstr->len);    \
+      dStr_free(dstr, 1);                                   \
+   } D_STMT_END
 
 #ifdef __cplusplus
 }
--- a/src/IO/dpi.c	Sun Nov 01 16:31:59 2009 -0300
+++ b/src/IO/dpi.c	Sun Nov 01 16:31:59 2009 -0300
@@ -461,7 +461,7 @@
    int check_st = 1, ret = 2;
 
    check_st = Dpi_check_dpid_ids();
-   MSG("Dpi_check_dpid: check_st=%d\n", check_st);
+   _MSG("Dpi_check_dpid: check_st=%d\n", check_st);
 
    if (check_st == 1) {
       /* connection test with dpi server passed */
@@ -575,8 +575,8 @@
       cmd = a_Dpip_get_attr(rply, "cmd");
       if (strcmp(cmd, "send_data") == 0) {
          port_str = a_Dpip_get_attr(rply, "msg");
-         MSG("Dpi_get_server_port: rply=%s\n", rply);
-         MSG("Dpi_get_server_port: port_str=%s\n", port_str);
+         _MSG("Dpi_get_server_port: rply=%s\n", rply);
+         _MSG("Dpi_get_server_port: port_str=%s\n", port_str);
          dpi_port = strtol(port_str, NULL, 10);
          dFree(port_str);
          ok = 1;
@@ -598,13 +598,14 @@
 {
    struct sockaddr_in sin;
    int sock_fd, err, dpi_port, ret=-1;
+   char *cmd = NULL;
 
    /* Query dpid for the port number for this server */
    if ((dpi_port = Dpi_get_server_port(server_name)) == -1) {
-      MSG("Dpi_connect_socket:: can't get port number for %s\n", server_name);
+      _MSG("Dpi_connect_socket:: can't get port number for %s\n", server_name);
       return -1;
    }
-   MSG("Dpi_connect_socket: server=%s port=%d\n", server_name, dpi_port);
+   _MSG("Dpi_connect_socket: server=%s port=%d\n", server_name, dpi_port);
 
    /* connect with this server's socket */
    memset(&sin, 0, sizeof(sin));
@@ -627,11 +628,14 @@
       }
 
    /* send authentication Key (the server closes sock_fd on error) */
-   } else if (Dpi_blocking_write(sock_fd,SharedKey,strlen(SharedKey)) == -1) {
+   } else if (!(cmd = a_Dpip_build_cmd("cmd=%s msg=%s", "auth", SharedKey))) {
+      MSG_ERR("[Dpi_connect_socket] Can't make auth message.\n");
+   } else if (Dpi_blocking_write(sock_fd, cmd, strlen(cmd)) == -1) {
       MSG_ERR("[Dpi_connect_socket] Can't send auth message.\n");
    } else {
       ret = sock_fd;
    }
+   dFree(cmd);
 
    return ret;
 }