diff --git a/ndhc/ifset.c b/ndhc/ifset.c index 0c35924..61570e8 100644 --- a/ndhc/ifset.c +++ b/ndhc/ifset.c @@ -53,6 +53,7 @@ #include "log.h" #include "ifch_proto.h" #include "strl.h" +#include "nl.h" static int set_if_flag(short flag) { @@ -88,30 +89,6 @@ static int set_if_flag(short flag) return ret; } -#define NLMSG_TAIL(nmsg) \ - ((struct rtattr *) (((uint8_t*) (nmsg)) + \ - NLMSG_ALIGN((nmsg)->nlmsg_len))) - -static int add_rtattr(struct nlmsghdr *n, size_t max_length, int type, - const void *data, size_t data_length) -{ - size_t length; - struct rtattr *rta; - - length = RTA_LENGTH(data_length); - - if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(length) > max_length) - return -E2BIG; - - rta = NLMSG_TAIL(n); - rta->rta_type = type; - rta->rta_len = length; - memcpy(RTA_DATA(rta), data, data_length); - n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(length); - - return 0; -} - // 32-bit position values are relatively prime to 37, so the residue mod37 // gives a unique mapping for each value. Gives correct result for v=0. static int trailz(uint32_t v) @@ -192,14 +169,14 @@ void perform_ip_subnet_bcast(const char *str_ipaddr, ifaddrmsg->ifa_scope = RT_SCOPE_UNIVERSE; ifaddrmsg->ifa_index = client_config.ifindex; - if (add_rtattr(header, sizeof request, IFA_LOCAL, - &ipaddr, sizeof ipaddr) < 0) { + if (nl_add_rtattr(header, sizeof request, IFA_LOCAL, + &ipaddr, sizeof ipaddr) < 0) { log_line("%s: (%s) couldn't add IFA_LOCAL to nlmsg", client_config.interface, __func__); return; } - if (add_rtattr(header, sizeof request, IFA_BROADCAST, - &bcast, sizeof bcast) < 0) { + if (nl_add_rtattr(header, sizeof request, IFA_BROADCAST, + &bcast, sizeof bcast) < 0) { log_line("%s: (%s) couldn't add IFA_BROADCAST to nlmsg", client_config.interface, __func__); return; diff --git a/ndhc/nl.c b/ndhc/nl.c index 0c41c3b..4c9473e 100644 --- a/ndhc/nl.c +++ b/ndhc/nl.c @@ -1,6 +1,6 @@ /* nl.c - low level netlink protocol functions * - * Copyright (c) 2011 Nicholas J. Kain + * Copyright (c) 2011-2014 Nicholas J. Kain * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -37,6 +38,30 @@ #include "log.h" #include "nl.h" +#define NLMSG_TAIL(nmsg) \ + ((struct rtattr *) (((uint8_t*) (nmsg)) + \ + NLMSG_ALIGN((nmsg)->nlmsg_len))) + +int nl_add_rtattr(struct nlmsghdr *n, size_t max_length, int type, + const void *data, size_t data_length) +{ + size_t length; + struct rtattr *rta; + + length = RTA_LENGTH(data_length); + + if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(length) > max_length) + return -E2BIG; + + rta = NLMSG_TAIL(n); + rta->rta_type = type; + rta->rta_len = length; + memcpy(RTA_DATA(rta), data, data_length); + n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(length); + + return 0; +} + void nl_attr_parse(const struct nlmsghdr *nlh, size_t offset, nl_attr_parse_fn workfn, void *data) { diff --git a/ndhc/nl.h b/ndhc/nl.h index 5bb74e6..2c5de75 100644 --- a/ndhc/nl.h +++ b/ndhc/nl.h @@ -1,6 +1,6 @@ /* nl.h - low level netlink protocol functions * - * Copyright (c) 2011 Nicholas J. Kain + * Copyright (c) 2011-2014 Nicholas J. Kain * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -66,16 +66,18 @@ static inline int nlmsg_get_error(const struct nlmsghdr *nlh) return err->error & 0x7fffffff; } +extern int nl_add_rtattr(struct nlmsghdr *n, size_t max_length, int type, + const void *data, size_t data_length); typedef int (*nl_attr_parse_fn)(struct nlattr *attr, int type, void *data); -void nl_attr_parse(const struct nlmsghdr *nlh, size_t offset, - nl_attr_parse_fn workfn, void *data); +extern void nl_attr_parse(const struct nlmsghdr *nlh, size_t offset, + nl_attr_parse_fn workfn, void *data); -ssize_t nl_recv_buf(int fd, char *buf, size_t blen); +extern ssize_t nl_recv_buf(int fd, char *buf, size_t blen); typedef int (*nlmsg_foreach_fn)(const struct nlmsghdr *, void *); -int nl_foreach_nlmsg(char *buf, size_t blen, uint32_t portid, - nlmsg_foreach_fn pfn, void *fnarg); +extern int nl_foreach_nlmsg(char *buf, size_t blen, uint32_t portid, + nlmsg_foreach_fn pfn, void *fnarg); -int nl_open(int nltype, int nlgroup, int *nlportid); +extern int nl_open(int nltype, int nlgroup, int *nlportid); #endif /* NK_NL_H_ */