aboutsummaryrefslogtreecommitdiff
path: root/dpid/main.c
diff options
context:
space:
mode:
authorJohannes Hofmann <Johannes.Hofmann@gmx.de>2010-08-20 23:24:19 +0200
committerJohannes Hofmann <Johannes.Hofmann@gmx.de>2010-08-20 23:24:19 +0200
commitf5c598b518d1f906148534d015f50075d3e8242d (patch)
tree21dd70add5b366c3dd80641b77f6b18e0baa009e /dpid/main.c
parente98d02a01ffeb18ede86af025e51ae1ec011c75a (diff)
parent5f0fc0e48b8cbee7e1795935da0abff6627fd498 (diff)
merge
Diffstat (limited to 'dpid/main.c')
-rw-r--r--dpid/main.c118
1 files changed, 58 insertions, 60 deletions
diff --git a/dpid/main.c b/dpid/main.c
index b47ca627..5f512245 100644
--- a/dpid/main.c
+++ b/dpid/main.c
@@ -12,16 +12,15 @@
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <errno.h>
-#include <unistd.h>
-#include <limits.h>
-#include <sys/stat.h>
-#include <sys/time.h>
-#include <assert.h>
+#include <errno.h> /* for ckd_write */
+#include <unistd.h> /* for ckd_write */
+#include <stdlib.h> /* for exit */
+#include <assert.h> /* for assert */
+#include <sys/stat.h> /* for umask */
+
#include "dpid_common.h"
#include "dpid.h"
#include "dpi.h"
@@ -46,7 +45,7 @@ static int start_filter_plugin(struct dp dpi_attr)
csz = (socklen_t) sizeof(clnt_addr);
- newsock = accept(dpi_attr.socket, (struct sockaddr *) &clnt_addr, &csz);
+ newsock = accept(dpi_attr.sock_fd, (struct sockaddr *) &clnt_addr, &csz);
if (newsock == -1)
ERRMSG("start_plugin", "accept", errno);
@@ -69,7 +68,7 @@ static int start_filter_plugin(struct dp dpi_attr)
}
if (pid == 0) {
/* Child, start plugin */
- if (execl(dpi_attr.path, dpi_attr.path, NULL) == -1) {
+ if (execl(dpi_attr.path, dpi_attr.path, (char*)NULL) == -1) {
ERRMSG("start_plugin", "execl", errno);
MSG_ERR("ERROR in child proc for %s\n", dpi_attr.path);
exit(1);
@@ -91,17 +90,17 @@ static int start_filter_plugin(struct dp dpi_attr)
static void start_server_plugin(struct dp dpi_attr)
{
- if (dup2(dpi_attr.socket, STDIN_FILENO) == -1) {
+ if (dup2(dpi_attr.sock_fd, STDIN_FILENO) == -1) {
ERRMSG("start_plugin", "dup2", errno);
MSG_ERR("ERROR in child proc for %s\n", dpi_attr.path);
exit(1);
}
- if (a_Misc_close_fd(dpi_attr.socket) == -1) {
+ if (a_Misc_close_fd(dpi_attr.sock_fd) == -1) {
ERRMSG("start_plugin", "close", errno);
MSG_ERR("ERROR in child proc for %s\n", dpi_attr.path);
exit(1);
}
- if (execl(dpi_attr.path, dpi_attr.path, NULL) == -1) {
+ if (execl(dpi_attr.path, dpi_attr.path, (char*)NULL) == -1) {
ERRMSG("start_plugin", "execl", errno);
MSG_ERR("ERROR in child proc for %s\n", dpi_attr.path);
exit(1);
@@ -113,32 +112,15 @@ 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);
- }
+ dpip_tag = a_Dpip_dsh_read_token(sh, 1);
(void) sigprocmask(SIG_UNBLOCK, &mask_sigchld, NULL);
- if (rdln == -1) {
- ERRMSG("get_request", "read", errno);
- }
- return (req);
+ return dpip_tag;
}
/*!
@@ -146,7 +128,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;
@@ -156,16 +138,18 @@ static int get_command(int sock, char *dpi_tag)
return (-1);
}
- cmd = a_Dpip_get_attr(dpi_tag, strlen(dpi_tag), "cmd");
+ cmd = a_Dpip_get_attr(dpi_tag, "cmd");
if (cmd == NULL) {
ERRMSG("get_command", "a_Dpip_get_attr", 0);
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) {
@@ -221,7 +205,6 @@ static int get_open_max(void)
int main(void)
{
int i, n = 0, open_max;
- char *dirname = NULL, *sockdir = NULL;
int dpid_idle_timeout = 60 * 60; /* default, in seconds */
struct timeval select_timeout;
sigset_t mask_none;
@@ -251,6 +234,7 @@ int main(void)
/* Get list of available dpis */
numdpis = register_all(&dpi_attr_list);
+#if 0
/* Get name of socket directory */
dirname = a_Dpi_sockdir_file();
if ((sockdir = init_sockdir(dirname)) == NULL) {
@@ -258,14 +242,16 @@ int main(void)
MSG_ERR("Failed to create socket directory\n");
exit(1);
}
+#endif
/* Init and get services list */
fill_services_list(dpi_attr_list, numdpis, &services_list);
/* Remove any sockets that may have been leftover from a crash */
- cleanup(sockdir);
+ //cleanup();
+
/* Initialise sockets */
- if ((numsocks = init_srs_socket(sockdir)) == -1) {
+ if ((numsocks = init_ids_srs_socket()) == -1) {
switch (dpi_errno) {
case dpid_srs_addrinuse:
MSG_ERR("dpid refuses to start, possibly because:\n");
@@ -273,12 +259,13 @@ int main(void)
MSG_ERR("\t2) A previous dpid didn't clean up on exit.\n");
exit(1);
default:
- ERRMSG("main", "init_srs_sockets failed", 0);
+ //ERRMSG("main", "init_srs_socket failed", 0);
+ ERRMSG("main", "init_ids_srs_socket failed", 0);
exit(1);
}
}
- numsocks = init_all_dpi_sockets(dpi_attr_list, sockdir);
- //est_terminator(); /* Do we still want to clean up on an abnormal exit? */
+ numsocks = init_all_dpi_sockets(dpi_attr_list);
+ est_dpi_terminator();
est_dpi_sigchld();
(void) sigemptyset(&mask_sigchld);
@@ -309,7 +296,7 @@ int main(void)
continue;
stop_active_dpis(dpi_attr_list, numdpis);
- cleanup(sockdir);
+ //cleanup();
exit(0);
}
} while (n == -1 && errno == EINTR);
@@ -319,42 +306,51 @@ int main(void)
exit(1);
}
/* If the service req socket is selected then service the req. */
- if (FD_ISSET(srs, &selected_set)) {
- int sock;
- socklen_t csz;
- struct sockaddr_un clnt_addr;
+ if (FD_ISSET(srs_fd, &selected_set)) {
+ int sock_fd;
+ socklen_t sin_sz;
+ struct sockaddr_in sin;
char *req = NULL;
--n;
assert(n >= 0);
- csz = (socklen_t) sizeof(clnt_addr);
- sock = accept(srs, (struct sockaddr *) &clnt_addr, &csz);
- if (sock == -1) {
+ sin_sz = (socklen_t) sizeof(sin);
+ sock_fd = accept(srs_fd, (struct sockaddr *)&sin, &sin_sz);
+ if (sock_fd == -1) {
ERRMSG("main", "accept", errno);
MSG_ERR("accept on srs socket failed\n");
MSG_ERR("service pending connections, and continue\n");
} else {
int command;
+ Dsh *sh;
- req = get_request(sock);
- command = get_command(sock, 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(sockdir);
+ //cleanup();
exit(0);
break;
case CHECK_SERVER_CMD:
- send_sockpath(sock, req, dpi_attr_list);
+ send_sockport(sock_fd, req, dpi_attr_list);
break;
case REGISTER_ALL_CMD:
- register_all_cmd(sockdir);
+ register_all_cmd();
break;
case UNKNOWN_CMD:
{
char *d_cmd = a_Dpip_build_cmd("cmd=%s msg=%s",
"DpiError", "Unknown command");
- (void) CKD_WRITE(sock, d_cmd);
+ (void) CKD_WRITE(sock_fd, d_cmd);
dFree(d_cmd);
ERRMSG("main", "Unknown command", 0);
MSG_ERR(" for request: %s\n", req);
@@ -366,14 +362,15 @@ int main(void)
}
if (req)
free(req);
- a_Misc_close_fd(sock);
+ a_Dpip_dsh_close(sh);
+ a_Dpip_dsh_free(sh);
}
}
/* While there's a request on one of the plugin sockets
* find the matching plugin and start it. */
for (i = 0; n > 0 && i < numdpis; i++) {
- if (FD_ISSET(dpi_attr_list[i].socket, &selected_set)) {
+ if (FD_ISSET(dpi_attr_list[i].sock_fd, &selected_set)) {
--n;
assert(n >= 0);
@@ -387,11 +384,12 @@ int main(void)
* on its socket */
numsocks--;
assert(numsocks >= 0);
- FD_CLR(dpi_attr_list[i].socket, &sock_set);
+ FD_CLR(dpi_attr_list[i].sock_fd, &sock_set);
if ((dpi_attr_list[i].pid = fork()) == -1) {
ERRMSG("main", "fork", errno);
/* exit(1); */
} else if (dpi_attr_list[i].pid == 0) {
+ /* child */
(void) sigprocmask(SIG_SETMASK, &mask_none, NULL);
start_server_plugin(dpi_attr_list[i]);
}