Move add_rtattr() from ifset.c to nl.c as nl_add_rtatr().
This commit is contained in:
parent
1222f4f22a
commit
54a7f54a4a
33
ndhc/ifset.c
33
ndhc/ifset.c
@ -53,6 +53,7 @@
|
|||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "ifch_proto.h"
|
#include "ifch_proto.h"
|
||||||
#include "strl.h"
|
#include "strl.h"
|
||||||
|
#include "nl.h"
|
||||||
|
|
||||||
static int set_if_flag(short flag)
|
static int set_if_flag(short flag)
|
||||||
{
|
{
|
||||||
@ -88,30 +89,6 @@ static int set_if_flag(short flag)
|
|||||||
return ret;
|
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
|
// 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.
|
// gives a unique mapping for each value. Gives correct result for v=0.
|
||||||
static int trailz(uint32_t v)
|
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_scope = RT_SCOPE_UNIVERSE;
|
||||||
ifaddrmsg->ifa_index = client_config.ifindex;
|
ifaddrmsg->ifa_index = client_config.ifindex;
|
||||||
|
|
||||||
if (add_rtattr(header, sizeof request, IFA_LOCAL,
|
if (nl_add_rtattr(header, sizeof request, IFA_LOCAL,
|
||||||
&ipaddr, sizeof ipaddr) < 0) {
|
&ipaddr, sizeof ipaddr) < 0) {
|
||||||
log_line("%s: (%s) couldn't add IFA_LOCAL to nlmsg",
|
log_line("%s: (%s) couldn't add IFA_LOCAL to nlmsg",
|
||||||
client_config.interface, __func__);
|
client_config.interface, __func__);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (add_rtattr(header, sizeof request, IFA_BROADCAST,
|
if (nl_add_rtattr(header, sizeof request, IFA_BROADCAST,
|
||||||
&bcast, sizeof bcast) < 0) {
|
&bcast, sizeof bcast) < 0) {
|
||||||
log_line("%s: (%s) couldn't add IFA_BROADCAST to nlmsg",
|
log_line("%s: (%s) couldn't add IFA_BROADCAST to nlmsg",
|
||||||
client_config.interface, __func__);
|
client_config.interface, __func__);
|
||||||
return;
|
return;
|
||||||
|
27
ndhc/nl.c
27
ndhc/nl.c
@ -1,6 +1,6 @@
|
|||||||
/* nl.c - low level netlink protocol functions
|
/* 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.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
@ -30,6 +30,7 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
#include <linux/rtnetlink.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
@ -37,6 +38,30 @@
|
|||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "nl.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,
|
void nl_attr_parse(const struct nlmsghdr *nlh, size_t offset,
|
||||||
nl_attr_parse_fn workfn, void *data)
|
nl_attr_parse_fn workfn, void *data)
|
||||||
{
|
{
|
||||||
|
16
ndhc/nl.h
16
ndhc/nl.h
@ -1,6 +1,6 @@
|
|||||||
/* nl.h - low level netlink protocol functions
|
/* 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.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* 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;
|
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);
|
typedef int (*nl_attr_parse_fn)(struct nlattr *attr, int type, void *data);
|
||||||
void nl_attr_parse(const struct nlmsghdr *nlh, size_t offset,
|
extern void nl_attr_parse(const struct nlmsghdr *nlh, size_t offset,
|
||||||
nl_attr_parse_fn workfn, void *data);
|
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 *);
|
typedef int (*nlmsg_foreach_fn)(const struct nlmsghdr *, void *);
|
||||||
int nl_foreach_nlmsg(char *buf, size_t blen, uint32_t portid,
|
extern int nl_foreach_nlmsg(char *buf, size_t blen, uint32_t portid,
|
||||||
nlmsg_foreach_fn pfn, void *fnarg);
|
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_ */
|
#endif /* NK_NL_H_ */
|
||||||
|
Loading…
Reference in New Issue
Block a user