diff options
author | Reyk Floeter <reyk@cvs.openbsd.org> | 2008-02-13 11:02:38 +0000 |
---|---|---|
committer | Reyk Floeter <reyk@cvs.openbsd.org> | 2008-02-13 11:02:38 +0000 |
commit | 1bdb64235fb7b4e805f079cd335df9b97f6843d5 (patch) | |
tree | 9f1ac69366259ed8adc4ac064084f00179f81f03 /usr.sbin/relayd | |
parent | a00b019d1e124baa28d8917c653d81e881b94efe (diff) |
stylistic change: move code to add protonodes from the BNF into
seperate functions in relayd.c (protonode_add/protonode_header). this
code got to big to look nice in the BNF statements...
Diffstat (limited to 'usr.sbin/relayd')
-rw-r--r-- | usr.sbin/relayd/parse.y | 97 | ||||
-rw-r--r-- | usr.sbin/relayd/relayd.c | 123 | ||||
-rw-r--r-- | usr.sbin/relayd/relayd.h | 6 |
3 files changed, 133 insertions, 93 deletions
diff --git a/usr.sbin/relayd/parse.y b/usr.sbin/relayd/parse.y index 85322751e79..e3feb7381ea 100644 --- a/usr.sbin/relayd/parse.y +++ b/usr.sbin/relayd/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.105 2008/02/11 10:53:12 reyk Exp $ */ +/* $OpenBSD: parse.y,v 1.106 2008/02/13 11:02:37 reyk Exp $ */ /* * Copyright (c) 2006 Pierre-Yves Ritschard <pyr@openbsd.org> @@ -739,98 +739,13 @@ protoptsl : SSL sslflags label = 0; } | direction protonode log { - struct protonode *pn, *proot, pk; - struct proto_tree *tree; - - if ($1 == RELAY_DIR_RESPONSE) - tree = &proto->response_tree; - else - tree = &proto->request_tree; - if ((pn = calloc(1, sizeof (*pn))) == NULL) - fatal("out of memory"); - - bcopy(&node, pn, sizeof(*pn)); - pn->key = node.key; - pn->value = node.value; - pn->type = node.type; - pn->label = label; - SIMPLEQ_INIT(&pn->head); - if ($1 == RELAY_DIR_RESPONSE) - pn->id = proto->response_nodes++; - else - pn->id = proto->request_nodes++; if ($3) - pn->flags |= PNFLAG_LOG; - if (pn->id == INT_MAX) { - yyerror("too many protocol nodes defined"); - free(pn); - YYERROR; - } - if ((proot = - RB_INSERT(proto_tree, tree, pn)) != NULL) { - /* - * A protocol node with the same key already - * exists, append it to a queue behind the - * existing node. - */ - if (SIMPLEQ_EMPTY(&proot->head)) - SIMPLEQ_NEXT(proot, entry) = pn; - SIMPLEQ_INSERT_TAIL(&proot->head, pn, entry); - } - - if (node.type == NODE_TYPE_COOKIE) - pk.key = "Cookie"; - else if (node.type == NODE_TYPE_URL) - pk.key = "Host"; - else - pk.key = "GET"; - if (node.type != NODE_TYPE_HEADER) { - pk.type = NODE_TYPE_HEADER; - pn = RB_FIND(proto_tree, tree, &pk); - if (pn == NULL) { - if ((pn = (struct protonode *) - calloc(1, sizeof(*pn))) == NULL) - fatal("out of memory"); - pn->key = strdup(pk.key); - if (pn->key == NULL) - fatal("out of memory"); - pn->value = NULL; - pn->action = NODE_ACTION_NONE; - pn->type = pk.type; - SIMPLEQ_INIT(&pn->head); - if ($1 == RELAY_DIR_RESPONSE) - pn->id = - proto->response_nodes++; - else - pn->id = proto->request_nodes++; - if (pn->id == INT_MAX) { - yyerror("too many protocol " - "nodes defined"); - YYERROR; - } - RB_INSERT(proto_tree, tree, pn); - } - switch (node.type) { - case NODE_TYPE_QUERY: - pn->flags |= PNFLAG_LOOKUP_QUERY; - break; - case NODE_TYPE_COOKIE: - pn->flags |= PNFLAG_LOOKUP_COOKIE; - break; - case NODE_TYPE_URL: - if (node.flags & - PNFLAG_LOOKUP_URL_DIGEST) - pn->flags |= node.flags & - PNFLAG_LOOKUP_URL_DIGEST; - else - pn->flags |= - PNFLAG_LOOKUP_DIGEST(0); - break; - default: - break; - } + node.flags |= PNFLAG_LOG; + node.label = label; + if (protonode_add($1, proto, &node) == -1) { + yyerror("failed to add protocol node"); + YYERROR; } - bzero(&node, sizeof(node)); } | include diff --git a/usr.sbin/relayd/relayd.c b/usr.sbin/relayd/relayd.c index b903c393819..570ad2a13aa 100644 --- a/usr.sbin/relayd/relayd.c +++ b/usr.sbin/relayd/relayd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: relayd.c,v 1.70 2008/02/11 10:42:50 reyk Exp $ */ +/* $OpenBSD: relayd.c,v 1.71 2008/02/13 11:02:37 reyk Exp $ */ /* * Copyright (c) 2006 Pierre-Yves Ritschard <pyr@openbsd.org> @@ -1012,3 +1012,124 @@ canonicalize_host(const char *host, char *name, size_t len) errno = EINVAL; return (NULL); } + +struct protonode * +protonode_header(enum direction dir, struct protocol *proto, + struct protonode *pk) +{ + struct protonode *pn; + struct proto_tree *tree; + + if (dir == RELAY_DIR_RESPONSE) + tree = &proto->response_tree; + else + tree = &proto->request_tree; + + pn = RB_FIND(proto_tree, tree, pk); + if (pn != NULL) + return (pn); + if ((pn = (struct protonode *)calloc(1, sizeof(*pn))) == NULL) { + log_warn("out of memory"); + return (NULL); + } + pn->key = strdup(pk->key); + if (pn->key == NULL) { + log_warn("out of memory"); + return (NULL); + } + pn->value = NULL; + pn->action = NODE_ACTION_NONE; + pn->type = pk->type; + SIMPLEQ_INIT(&pn->head); + if (dir == RELAY_DIR_RESPONSE) + pn->id = + proto->response_nodes++; + else + pn->id = proto->request_nodes++; + if (pn->id == INT_MAX) { + log_warnx("too many protocol " + "nodes defined"); + return (NULL); + } + RB_INSERT(proto_tree, tree, pn); + return (pn); +} + +int +protonode_add(enum direction dir, struct protocol *proto, + struct protonode *node) +{ + struct protonode *pn, *proot, pk; + struct proto_tree *tree; + + if (dir == RELAY_DIR_RESPONSE) + tree = &proto->response_tree; + else + tree = &proto->request_tree; + + if ((pn = calloc(1, sizeof (*pn))) == NULL) { + log_warn("out of memory"); + return (-1); + } + bcopy(node, pn, sizeof(*pn)); + pn->key = node->key; + pn->value = node->value; + pn->type = node->type; + pn->label = node->label; + SIMPLEQ_INIT(&pn->head); + if (dir == RELAY_DIR_RESPONSE) + pn->id = proto->response_nodes++; + else + pn->id = proto->request_nodes++; + pn->flags = node->flags; + if (pn->id == INT_MAX) { + log_warnx("too many protocol nodes defined"); + free(pn); + return (-1); + } + if ((proot = + RB_INSERT(proto_tree, tree, pn)) != NULL) { + /* + * A protocol node with the same key already + * exists, append it to a queue behind the + * existing node-> + */ + if (SIMPLEQ_EMPTY(&proot->head)) + SIMPLEQ_NEXT(proot, entry) = pn; + SIMPLEQ_INSERT_TAIL(&proot->head, pn, entry); + } + + if (node->type == NODE_TYPE_COOKIE) + pk.key = "Cookie"; + else if (node->type == NODE_TYPE_URL) + pk.key = "Host"; + else + pk.key = "GET"; + if (node->type != NODE_TYPE_HEADER) { + pk.type = NODE_TYPE_HEADER; + pn = protonode_header(dir, proto, &pk); + if (pn == NULL) + return (-1); + switch (node->type) { + case NODE_TYPE_QUERY: + pn->flags |= PNFLAG_LOOKUP_QUERY; + break; + case NODE_TYPE_COOKIE: + pn->flags |= PNFLAG_LOOKUP_COOKIE; + break; + case NODE_TYPE_URL: + if (node->flags & + PNFLAG_LOOKUP_URL_DIGEST) + pn->flags |= node->flags & + PNFLAG_LOOKUP_URL_DIGEST; + else + pn->flags |= + PNFLAG_LOOKUP_DIGEST(0); + break; + default: + break; + } + } + + return (0); +} diff --git a/usr.sbin/relayd/relayd.h b/usr.sbin/relayd/relayd.h index 446347925f6..223ab26dce7 100644 --- a/usr.sbin/relayd/relayd.h +++ b/usr.sbin/relayd/relayd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: relayd.h,v 1.97 2008/02/11 10:42:50 reyk Exp $ */ +/* $OpenBSD: relayd.h,v 1.98 2008/02/13 11:02:37 reyk Exp $ */ /* * Copyright (c) 2006, 2007 Pierre-Yves Ritschard <pyr@openbsd.org> @@ -825,6 +825,10 @@ void purge_table(struct tablelist *, struct table *); void merge_config(struct relayd *, struct relayd *); char *digeststr(enum digest_type, const u_int8_t *, size_t, char *); const char *canonicalize_host(const char *, char *, size_t); +struct protonode *protonode_header(enum direction, struct protocol *, + struct protonode *); +int protonode_add(enum direction, struct protocol *, + struct protonode *); /* carp.c */ int carp_demote_init(char *, int); |