From a8a761da146d7319f57ea8efe447713325c6ab6a Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Tue, 3 Apr 2012 22:00:47 -0400 Subject: [PATCH] Shrink stack use of nl_getifdata(). --- ndhc/ndhc.c | 2 +- ndhc/netlink.c | 17 ++++++++++++----- ndhc/netlink.h | 2 +- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/ndhc/ndhc.c b/ndhc/ndhc.c index b67a507..fcf5744 100644 --- a/ndhc/ndhc.c +++ b/ndhc/ndhc.c @@ -353,7 +353,7 @@ int main(int argc, char **argv) log_line("FATAL - failed to open netlink socket"); exit(EXIT_FAILURE); } - if (nl_getifdata(client_config.interface, &cs) < 0) { + if (nl_getifdata(&cs) < 0) { log_line("FATAL - failed to get interface MAC and index"); exit(EXIT_FAILURE); } diff --git a/ndhc/netlink.c b/ndhc/netlink.c index 2e4fa1c..985b783 100644 --- a/ndhc/netlink.c +++ b/ndhc/netlink.c @@ -124,6 +124,7 @@ 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) { ssize_t ret; @@ -137,12 +138,11 @@ void handle_nl_message(struct client_state_t *cs) } while (ret > 0); } -int nl_getifdata(const char *ifname, struct client_state_t *cs) +static int nl_sendgetlink(struct client_state_t *cs) { - char buf[8192]; - struct nlmsghdr *nlh = (struct nlmsghdr *)buf; + struct nlmsghdr *nlh = (struct nlmsghdr *)nlbuf; - memset(buf, 0, sizeof buf); + memset(nlbuf, 0, sizeof nlbuf); nlh->nlmsg_len = NLMSG_LENGTH(sizeof (struct ifinfomsg)); nlh->nlmsg_type = RTM_GETLINK; nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT; @@ -151,9 +151,16 @@ int nl_getifdata(const char *ifname, struct client_state_t *cs) struct sockaddr_nl addr = { .nl_family = AF_NETLINK, }; - if (sendto(cs->nlFd, buf, nlh->nlmsg_len, 0, (struct sockaddr *)&addr, + if (sendto(cs->nlFd, nlbuf, nlh->nlmsg_len, 0, (struct sockaddr *)&addr, sizeof addr) == -1) return -1; + return 0; +} + +int nl_getifdata(struct client_state_t *cs) +{ + if (nl_sendgetlink(cs)) + return -1; for (int pr = 0; !pr;) { pr = poll(&((struct pollfd){.fd=cs->nlFd,.events=POLLIN}), 1, -1); diff --git a/ndhc/netlink.h b/ndhc/netlink.h index c39e5d3..d19c407 100644 --- a/ndhc/netlink.h +++ b/ndhc/netlink.h @@ -43,6 +43,6 @@ enum { extern int nlportid; void handle_nl_message(struct client_state_t *cs); -int nl_getifdata(const char *ifname, struct client_state_t *cs); +int nl_getifdata(struct client_state_t *cs); #endif /* NK_NETLINK_H_ */