From: benno Date: Sun, 13 Jul 2014 00:32:08 +0000 (+0000) Subject: improve log output for relays. adjust regress tests X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=c84d60996939f5b2b19973a3a5e352278297da20;p=openbsd improve log output for relays. adjust regress tests ok reyk --- diff --git a/regress/usr.sbin/relayd/args-http-log.pl b/regress/usr.sbin/relayd/args-http-log.pl index 887c6c7651d..1e2602b1a6a 100644 --- a/regress/usr.sbin/relayd/args-http-log.pl +++ b/regress/usr.sbin/relayd/args-http-log.pl @@ -14,7 +14,7 @@ our %args = ( ], loggrep => { qr/\[Host: foo.bar\]/ => 1, - qr/\[Server: Perl\/[^\s]+\s*\]/ => 1, + qr/\{Server: Perl\/[^\s]+\s*\};/ => 1, }, }, server => { diff --git a/regress/usr.sbin/relayd/args-http-remove.pl b/regress/usr.sbin/relayd/args-http-remove.pl index 45800210b6c..762de653312 100644 --- a/regress/usr.sbin/relayd/args-http-remove.pl +++ b/regress/usr.sbin/relayd/args-http-remove.pl @@ -18,7 +18,7 @@ our %args = ( 'match response header remove X-Header-Foo', 'match response header log "*Foo"', ], - loggrep => { qr/ (?:done|last write \(done\)), \[X-Header-Foo: foo \(removed\)\s*\]/ => 1 }, + loggrep => { qr/ (?:done|last write \(done\)), \{X-Header-Foo: foo \(removed\)\s*\};/ => 1 }, }, server => { func => \&http_server, diff --git a/usr.sbin/relayd/relay.c b/usr.sbin/relayd/relay.c index 18879aef485..f4d093fdb82 100644 --- a/usr.sbin/relayd/relay.c +++ b/usr.sbin/relayd/relay.c @@ -1,4 +1,4 @@ -/* $OpenBSD: relay.c,v 1.173 2014/07/11 16:59:38 reyk Exp $ */ +/* $OpenBSD: relay.c,v 1.174 2014/07/13 00:32:08 benno Exp $ */ /* * Copyright (c) 2006 - 2014 Reyk Floeter @@ -1084,6 +1084,7 @@ relay_accept(int fd, short event, void *arg) } /* Pre-allocate log buffer */ + con->se_haslog = 0; con->se_log = evbuffer_new(); if (con->se_log == NULL) { relay_close(con, "failed to allocate log buffer"); @@ -2655,4 +2656,12 @@ relay_session_cmp(struct rsession *a, struct rsession *b) return ((int)a->se_id - b->se_id); } +void +relay_log(struct rsession *con, char *msg) +{ + if (con->se_haslog && con->se_log != NULL) { + evbuffer_add(con->se_log, msg, strlen(msg)); + } +} + SPLAY_GENERATE(session_tree, rsession, se_nodes, relay_session_cmp); diff --git a/usr.sbin/relayd/relay_http.c b/usr.sbin/relayd/relay_http.c index ede8aa4751b..4f2eb6c403f 100644 --- a/usr.sbin/relayd/relay_http.c +++ b/usr.sbin/relayd/relay_http.c @@ -1,4 +1,4 @@ -/* $OpenBSD: relay_http.c,v 1.28 2014/07/13 00:18:05 benno Exp $ */ +/* $OpenBSD: relay_http.c,v 1.29 2014/07/13 00:32:08 benno Exp $ */ /* * Copyright (c) 2006 - 2014 Reyk Floeter @@ -1389,6 +1389,8 @@ relay_apply_actions(struct ctl_relay_event *cre, struct kvlist *actions) struct kv *kv, *match, *kp, *mp, kvcopy, matchcopy, key; int addkv, ret; char buf[IBUF_READ_SIZE], *ptr; + char *msg = NULL; + const char *meth = NULL; memset(&kvcopy, 0, sizeof(kvcopy)); memset(&matchcopy, 0, sizeof(matchcopy)); @@ -1553,7 +1555,8 @@ relay_apply_actions(struct ctl_relay_event *cre, struct kvlist *actions) default: break; } - if (kv_log(con->se_log, mp, con->se_label) == -1) + if (kv_log(con, mp, con->se_label, cre->dir) + == -1) goto fail; break; default: @@ -1569,6 +1572,16 @@ relay_apply_actions(struct ctl_relay_event *cre, struct kvlist *actions) kv_free(&matchcopy); } + /* + * log tag for request and response, request method + * and end of request marker "," + */ + if ((con->se_log != NULL) && + ((meth = relay_httpmethod_byid(desc->http_method)) != NULL) && + (asprintf(&msg, " %s",meth) >= 0)) + evbuffer_add(con->se_log, msg, strlen(msg)); + free(msg); + relay_log(con, cre->dir == RELAY_DIR_REQUEST ? "" : ";"); ret = 0; fail: kv_free(&kvcopy); diff --git a/usr.sbin/relayd/relay_udp.c b/usr.sbin/relayd/relay_udp.c index 9d871c5af87..5ed418c5711 100644 --- a/usr.sbin/relayd/relay_udp.c +++ b/usr.sbin/relayd/relay_udp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: relay_udp.c,v 1.30 2014/07/09 16:42:05 reyk Exp $ */ +/* $OpenBSD: relay_udp.c,v 1.31 2014/07/13 00:32:08 benno Exp $ */ /* * Copyright (c) 2007 - 2013 Reyk Floeter @@ -280,6 +280,7 @@ relay_udp_server(int fd, short sig, void *arg) } /* Pre-allocate log buffer */ + con->se_haslog = 0; con->se_log = evbuffer_new(); if (con->se_log == NULL) { relay_close(con, "failed to allocate log buffer"); diff --git a/usr.sbin/relayd/relayd.c b/usr.sbin/relayd/relayd.c index cf434bd589f..2b0377d36a8 100644 --- a/usr.sbin/relayd/relayd.c +++ b/usr.sbin/relayd/relayd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: relayd.c,v 1.129 2014/07/11 11:48:50 reyk Exp $ */ +/* $OpenBSD: relayd.c,v 1.130 2014/07/13 00:32:08 benno Exp $ */ /* * Copyright (c) 2007 - 2014 Reyk Floeter @@ -804,25 +804,28 @@ kv_inherit(struct kv *dst, struct kv *src) } int -kv_log(struct evbuffer *log, struct kv *kv, u_int16_t labelid) +kv_log(struct rsession *con, struct kv *kv, u_int16_t labelid, + enum direction dir) { char *msg; - if (log == NULL) + if (con->se_log == NULL) return (0); - if (asprintf(&msg, " [%s%s%s%s%s]", + if (asprintf(&msg, " %s%s%s%s%s%s%s", + dir == RELAY_DIR_REQUEST ? "[" : "{", labelid == 0 ? "" : label_id2name(labelid), labelid == 0 ? "" : ", ", kv->kv_key == NULL ? "(unknown)" : kv->kv_key, kv->kv_value == NULL ? "" : ": ", - kv->kv_value == NULL ? "" : kv->kv_value) == -1) + kv->kv_value == NULL ? "" : kv->kv_value, + dir == RELAY_DIR_REQUEST ? "]" : "}") == -1) return (-1); - if (evbuffer_add(log, msg, strlen(msg)) == -1) { + if (evbuffer_add(con->se_log, msg, strlen(msg)) == -1) { free(msg); return (-1); } free(msg); - + con->se_haslog = 1; return (0); } diff --git a/usr.sbin/relayd/relayd.h b/usr.sbin/relayd/relayd.h index b103eda8d27..6c86e29cc29 100644 --- a/usr.sbin/relayd/relayd.h +++ b/usr.sbin/relayd/relayd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: relayd.h,v 1.187 2014/07/12 14:34:13 reyk Exp $ */ +/* $OpenBSD: relayd.h,v 1.188 2014/07/13 00:32:08 benno Exp $ */ /* * Copyright (c) 2006 - 2014 Reyk Floeter @@ -525,6 +525,7 @@ struct rsession { int se_retry; int se_retrycount; int se_connectcount; + int se_haslog; struct evbuffer *se_log; struct relay *se_relay; struct ctl_natlook *se_cnl; @@ -1253,7 +1254,9 @@ struct kv *kv_extend(struct kvtree *, struct kv *, char *); void kv_purge(struct kvtree *); void kv_free(struct kv *); struct kv *kv_inherit(struct kv *, struct kv *); -int kv_log(struct evbuffer *, struct kv *, u_int16_t); +void relay_log(struct rsession *, char *); +int kv_log(struct rsession *, struct kv *, u_int16_t, + enum direction); struct kv *kv_find(struct kvtree *, struct kv *); int kv_cmp(struct kv *, struct kv *); int rule_add(struct protocol *, struct relay_rule *, const char