From e99024620796047034c2d6abbce3535827fb52f6 Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Thu, 12 Apr 2012 20:06:05 -0400 Subject: [PATCH] Move nlbuf onto stack and don't share a single buffer for sending and receiving. Move nlportid into client state structure. --- ndhc/config.h | 1 + ndhc/ndhc.c | 3 ++- ndhc/netlink.c | 9 ++++----- ndhc/netlink.h | 2 -- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/ndhc/config.h b/ndhc/config.h index e4b43a2..0a3cc9e 100644 --- a/ndhc/config.h +++ b/ndhc/config.h @@ -38,6 +38,7 @@ struct client_state_t { int ifsPrevState; int listenMode; int epollFd, signalFd, listenFd, arpFd, nlFd; + int nlPortId; uint32_t clientAddr, serverAddr, routerAddr; uint32_t lease, renewTime, rebindTime, xid; uint8_t routerArp[6], serverArp[6]; diff --git a/ndhc/ndhc.c b/ndhc/ndhc.c index fcf5744..5fbfd91 100644 --- a/ndhc/ndhc.c +++ b/ndhc/ndhc.c @@ -75,6 +75,7 @@ struct client_state_t cs = { .listenFd = -1, .arpFd = -1, .nlFd = -1, + .nlPortId = -1, .routerArp = "\0\0\0\0\0\0", .serverArp = "\0\0\0\0\0\0", }; @@ -349,7 +350,7 @@ int main(int argc, char **argv) write_pid(pidfile); } - if ((cs.nlFd = nl_open(NETLINK_ROUTE, RTMGRP_LINK, &nlportid)) < 0) { + if ((cs.nlFd = nl_open(NETLINK_ROUTE, RTMGRP_LINK, &cs.nlPortId)) < 0) { log_line("FATAL - failed to open netlink socket"); exit(EXIT_FAILURE); } diff --git a/ndhc/netlink.c b/ndhc/netlink.c index b5520f7..d63ce4c 100644 --- a/ndhc/netlink.c +++ b/ndhc/netlink.c @@ -42,9 +42,6 @@ #include "nl.h" #include "state.h" -static char nlbuf[8192]; -int nlportid; - static int nlrtattr_assign(struct nlattr *attr, int type, void *data) { struct nlattr **tb = data; @@ -124,22 +121,24 @@ static int nl_process_msgs(const struct nlmsghdr *nlh, void *data) return 1; } -/* Destroys contents of nlbuf */ void handle_nl_message(struct client_state_t *cs) { + char nlbuf[8192]; ssize_t ret; assert(cs->nlFd != -1); do { ret = nl_recv_buf(cs->nlFd, nlbuf, sizeof nlbuf); if (ret == -1) break; - if (nl_foreach_nlmsg(nlbuf, ret, nlportid, nl_process_msgs, cs) == -1) + if (nl_foreach_nlmsg(nlbuf, ret, cs->nlPortId, nl_process_msgs, cs) + == -1) break; } while (ret > 0); } static int nl_sendgetlink(struct client_state_t *cs) { + char nlbuf[8192]; struct nlmsghdr *nlh = (struct nlmsghdr *)nlbuf; memset(nlbuf, 0, sizeof nlbuf); diff --git a/ndhc/netlink.h b/ndhc/netlink.h index d19c407..06b04db 100644 --- a/ndhc/netlink.h +++ b/ndhc/netlink.h @@ -40,8 +40,6 @@ enum { IFS_REMOVED }; -extern int nlportid; - void handle_nl_message(struct client_state_t *cs); int nl_getifdata(struct client_state_t *cs);