aboutsummaryrefslogtreecommitdiff
path: root/dpid
diff options
context:
space:
mode:
Diffstat (limited to 'dpid')
-rw-r--r--dpid/dpi.h3
-rw-r--r--dpid/dpid.c29
-rw-r--r--dpid/main.c49
3 files changed, 48 insertions, 33 deletions
diff --git a/dpid/dpi.h b/dpid/dpi.h
index 9c5ea4a5..4ae33375 100644
--- a/dpid/dpi.h
+++ b/dpid/dpi.h
@@ -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);
}
}