From: Ben Pfaff Date: Tue, 22 Jul 2008 21:01:10 +0000 (-0700) Subject: dhcp: Make dhcp_msg_to_string() support a multiline format also. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9c6191bf820c0abb558c4441872cfd1ac929dc36;p=openvswitch dhcp: Make dhcp_msg_to_string() support a multiline format also. The upcoming ofp-discover program wants to print out the binding information, and dhcp_msg_to_string() is pretty close to what it wants. But one-field-per-line is easier for other programs to parse. --- diff --git a/include/dhcp.h b/include/dhcp.h index 82f3d3e0..d427bb0d 100644 --- a/include/dhcp.h +++ b/include/dhcp.h @@ -267,7 +267,8 @@ bool dhcp_msg_get_uint16(const struct dhcp_msg *, int code, size_t offset, uint16_t *); const char *dhcp_option_to_string(const struct dhcp_option *, int code, struct ds *); -const char *dhcp_msg_to_string(const struct dhcp_msg *, struct ds *); +const char *dhcp_msg_to_string(const struct dhcp_msg *, bool multiline, + struct ds *); int dhcp_parse(struct dhcp_msg *, const struct buffer *); void dhcp_assemble(const struct dhcp_msg *, struct buffer *); diff --git a/lib/dhcp-client.c b/lib/dhcp-client.c index f6cc67bf..0757096f 100644 --- a/lib/dhcp-client.c +++ b/lib/dhcp-client.c @@ -439,11 +439,12 @@ dhcp_receive(struct dhclient *cli, unsigned int msgs, struct dhcp_msg *msg) if (msg->type < 0 || msg->type > 31 || !((1u << msg->type) & msgs)) { VLOG_DBG("received unexpected %s in %s state: %s", dhcp_type_name(msg->type), state_name(cli->state), - dhcp_msg_to_string(msg, &cli->s)); + dhcp_msg_to_string(msg, false, &cli->s)); } else if (msg->xid != cli->xid) { VLOG_DBG("ignoring %s with xid != %08"PRIx32" in %s state: %s", dhcp_type_name(msg->type), msg->xid, - state_name(cli->state), dhcp_msg_to_string(msg, &cli->s)); + state_name(cli->state), + dhcp_msg_to_string(msg, false, &cli->s)); } else { return true; } @@ -457,18 +458,18 @@ validate_offered_options(struct dhclient *cli, const struct dhcp_msg *msg) { uint32_t lease, netmask; if (!dhcp_msg_get_secs(msg, DHCP_CODE_LEASE_TIME, 0, &lease)) { - VLOG_WARN("%s lacks lease time: %s", - dhcp_type_name(msg->type), dhcp_msg_to_string(msg, &cli->s)); + VLOG_WARN("%s lacks lease time: %s", dhcp_type_name(msg->type), + dhcp_msg_to_string(msg, false, &cli->s)); } else if (!dhcp_msg_get_ip(msg, DHCP_CODE_SUBNET_MASK, 0, &netmask)) { - VLOG_WARN("%s lacks netmask: %s", - dhcp_type_name(msg->type), dhcp_msg_to_string(msg, &cli->s)); + VLOG_WARN("%s lacks netmask: %s", dhcp_type_name(msg->type), + dhcp_msg_to_string(msg, false, &cli->s)); } else if (lease < MIN_ACCEPTABLE_LEASE) { VLOG_WARN("Ignoring %s with %"PRIu32"-second lease time: %s", dhcp_type_name(msg->type), lease, - dhcp_msg_to_string(msg, &cli->s)); + dhcp_msg_to_string(msg, false, &cli->s)); } else if (cli->validate_offer && !cli->validate_offer(msg, cli->aux)) { VLOG_DBG("client validation hook refused offer: %s", - dhcp_msg_to_string(msg, &cli->s)); + dhcp_msg_to_string(msg, false, &cli->s)); } else { return true; } @@ -492,11 +493,12 @@ dhclient_run_SELECTING(struct dhclient *cli) if (!dhcp_msg_get_ip(&msg, DHCP_CODE_SERVER_IDENTIFIER, 0, &cli->server_ip)) { VLOG_WARN("DHCPOFFER lacks server identifier: %s", - dhcp_msg_to_string(&msg, &cli->s)); + dhcp_msg_to_string(&msg, false, &cli->s)); continue; } - VLOG_DBG("accepting DHCPOFFER: %s", dhcp_msg_to_string(&msg, &cli->s)); + VLOG_DBG("accepting DHCPOFFER: %s", + dhcp_msg_to_string(&msg, false, &cli->s)); cli->ipaddr = msg.yiaddr; state_transition(cli, S_REQUESTING); break; @@ -550,7 +552,7 @@ receive_ack(struct dhclient *cli) cli->router = INADDR_ANY; } state_transition(cli, S_BOUND); - VLOG_DBG("Bound: %s", dhcp_msg_to_string(&msg, &cli->s)); + VLOG_DBG("Bound: %s", dhcp_msg_to_string(&msg, false, &cli->s)); return true; } } @@ -758,7 +760,7 @@ do_receive_msg(struct dhclient *cli, struct dhcp_msg *msg) buffer_pull(&b, b.l7 - b.data); error = dhcp_parse(msg, &b); if (!error) { - VLOG_DBG("received %s", dhcp_msg_to_string(msg, &cli->s)); + VLOG_DBG("received %s", dhcp_msg_to_string(msg, false, &cli->s)); buffer_uninit(&b); return true; } @@ -832,7 +834,7 @@ do_send_msg(struct dhclient *cli, const struct dhcp_msg *msg) * frame to have to be discarded or fragmented if it travels over a regular * Ethernet at some point. 1500 bytes should be enough for anyone. */ if (b.size <= ETH_TOTAL_MAX) { - VLOG_DBG("sending %s", dhcp_msg_to_string(msg, &cli->s)); + VLOG_DBG("sending %s", dhcp_msg_to_string(msg, false, &cli->s)); error = netdev_send(cli->netdev, &b); if (error) { VLOG_ERR("send failed on %s: %s", diff --git a/lib/dhcp.c b/lib/dhcp.c index 1e8918f7..9ab9cc6a 100644 --- a/lib/dhcp.c +++ b/lib/dhcp.c @@ -383,7 +383,6 @@ dhcp_option_to_string(const struct dhcp_option *opt, int code, struct ds *ds) const struct arg_type *type = &types[class->type]; size_t offset; - ds_put_char(ds, ' '); if (class->name) { const char *cp; for (cp = class->name; *cp; cp++) { @@ -480,20 +479,25 @@ dhcp_option_to_string(const struct dhcp_option *opt, int code, struct ds *ds) return ds_cstr(ds); } -/* Replaces 'ds' by a string representation of 'msg'. */ +/* Replaces 'ds' by a string representation of 'msg'. If 'multiline' is + * false, 'ds' receives a single-line representation of 'msg', otherwise a + * multiline representation. */ const char * -dhcp_msg_to_string(const struct dhcp_msg *msg, struct ds *ds) +dhcp_msg_to_string(const struct dhcp_msg *msg, bool multiline, struct ds *ds) { + char separator = multiline ? '\n' : ' '; int code; ds_clear(ds); - ds_put_format(ds, "%s %s xid=%08"PRIx32" secs=%"PRIu16, + ds_put_format(ds, "%s%c%s%cxid=%08"PRIx32"%csecs=%"PRIu16, (msg->op == DHCP_BOOTREQUEST ? "BOOTREQUEST" : msg->op == DHCP_BOOTREPLY ? "BOOTREPLY" : "<>"), - dhcp_type_name(msg->type), msg->xid, msg->secs); + separator, dhcp_type_name(msg->type), + separator, msg->xid, + separator, msg->secs); if (msg->flags) { - ds_put_cstr(ds, " flags="); + ds_put_format(ds, "%cflags=", separator); if (msg->flags & DHCP_FLAGS_BROADCAST) { ds_put_cstr(ds, "[BROADCAST]"); } @@ -502,25 +506,30 @@ dhcp_msg_to_string(const struct dhcp_msg *msg, struct ds *ds) } } if (msg->ciaddr) { - ds_put_format(ds, " ciaddr="IP_FMT, IP_ARGS(&msg->ciaddr)); + ds_put_format(ds, "%cciaddr="IP_FMT, separator, IP_ARGS(&msg->ciaddr)); } if (msg->yiaddr) { - ds_put_format(ds, " yiaddr="IP_FMT, IP_ARGS(&msg->yiaddr)); + ds_put_format(ds, "%cyiaddr="IP_FMT, separator, IP_ARGS(&msg->yiaddr)); } if (msg->siaddr) { - ds_put_format(ds, " siaddr="IP_FMT, IP_ARGS(&msg->siaddr)); + ds_put_format(ds, "%csiaddr="IP_FMT, separator, IP_ARGS(&msg->siaddr)); } if (msg->giaddr) { - ds_put_format(ds, " giaddr="IP_FMT, IP_ARGS(&msg->giaddr)); + ds_put_format(ds, "%cgiaddr="IP_FMT, separator, IP_ARGS(&msg->giaddr)); } - ds_put_format(ds, " chaddr="ETH_ADDR_FMT, ETH_ADDR_ARGS(msg->chaddr)); + ds_put_format(ds, "%cchaddr="ETH_ADDR_FMT, + separator, ETH_ADDR_ARGS(msg->chaddr)); for (code = 0; code < DHCP_N_OPTIONS; code++) { const struct dhcp_option *opt = &msg->options[code]; if (opt->data) { + ds_put_char(ds, separator); dhcp_option_to_string(opt, code, ds); } } + if (multiline) { + ds_put_char(ds, separator); + } return ds_cstr(ds); } @@ -693,7 +702,7 @@ error: VLOG_DBG("invalid DHCP message dump:\n%s", ds_cstr(&ds)); ds_clear(&ds); - dhcp_msg_to_string(msg, &ds); + dhcp_msg_to_string(msg, false, &ds); VLOG_DBG("partially dissected DHCP message: %s", ds_cstr(&ds)); ds_destroy(&ds);