Shrink stack use of nl_getifdata().

This commit is contained in:
Nicholas J. Kain 2012-04-03 22:00:47 -04:00
parent 93b44ed48d
commit a8a761da14
3 changed files with 14 additions and 7 deletions

View File

@ -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);
}

View File

@ -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);

View File

@ -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_ */