Move add_rtattr() from ifset.c to nl.c as nl_add_rtatr().

This commit is contained in:
Nicholas J. Kain 2014-03-12 15:25:07 -04:00
parent 1222f4f22a
commit 54a7f54a4a
3 changed files with 40 additions and 36 deletions

View File

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

View File

@ -1,6 +1,6 @@
/* nl.c - low level netlink protocol functions
*
* Copyright (c) 2011 Nicholas J. Kain <njkain at gmail dot com>
* Copyright (c) 2011-2014 Nicholas J. Kain <njkain at gmail dot com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -30,6 +30,7 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <linux/rtnetlink.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
@ -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)
{

View File

@ -1,6 +1,6 @@
/* nl.h - low level netlink protocol functions
*
* Copyright (c) 2011 Nicholas J. Kain <njkain at gmail dot com>
* Copyright (c) 2011-2014 Nicholas J. Kain <njkain at gmail dot com>
* 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_ */