diff options
author | Jorge Arellano Cid <jcid@dillo.org> | 2009-11-01 16:31:59 -0300 |
---|---|---|
committer | Jorge Arellano Cid <jcid@dillo.org> | 2009-11-01 16:31:59 -0300 |
commit | b643ca0bd58188da3dc98193f97fb220ab008983 (patch) | |
tree | f445722a242dd9e201a14143aa930938520b638a /dpid | |
parent | 13e0d2aabf7d06c3034b878e0cea861e4db1ddce (diff) |
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
Diffstat (limited to 'dpid')
-rw-r--r-- | dpid/dpi.h | 3 | ||||
-rw-r--r-- | dpid/dpid.c | 29 | ||||
-rw-r--r-- | dpid/main.c | 49 |
3 files changed, 48 insertions, 33 deletions
@@ -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" */ diff --git a/dpid/dpid.c b/dpid/dpid.c index 93853afb..42b0413c 100644 --- a/dpid/dpid.c +++ b/dpid/dpid.c @@ -700,16 +700,30 @@ void est_dpi_sigchld(void) } } +/*! 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 @@ void stop_active_dpis(struct dp *dpi_attr_list, int numdpis) 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 diff --git a/dpid/main.c b/dpid/main.c index fcb11cef..677fa7d2 100644 --- a/dpid/main.c +++ b/dpid/main.c @@ -112,32 +112,17 @@ static void start_server_plugin(struct dp dpi_attr) * \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 @@ static char *get_request(int sock) * \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 @@ static int get_command(int sock, char *dpi_tag) 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 @@ int main(void) 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 @@ int main(void) } if (req) free(req); - a_Misc_close_fd(sock_fd); + a_Dpip_dsh_close(sh); + a_Dpip_dsh_free(sh); } } |