summaryrefslogtreecommitdiffhomepage
path: root/contrib/fwd/src/fwd_addr.c
blob: df5d8e7c2aefa44ab5479327f63d326a80294d6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * fwd - OpenWrt firewall daemon - rtnetlink communication
 *
 *   Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
 *
 * The fwd program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * The fwd program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with the fwd program. If not, see http://www.gnu.org/licenses/.
 */


#include "fwd.h"
#include "fwd_addr.h"

struct fwd_addr_list * fwd_get_addrs(int fd, int family)
{
	struct {
		  struct nlmsghdr n;
		  struct ifaddrmsg r;
	} req;

	int offlen;
	int rtattrlen;
	int dump_done;
	char buf[16384];

	struct rtattr *rta;
	struct rtattr *rtatp;
	struct nlmsghdr *nlmp;
	struct ifaddrmsg *rtmp;

	struct fwd_addr_list *head, *entry;

	/* Build request */
	memset(&req, 0, sizeof(req));
	req.n.nlmsg_len   = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
	req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
	req.n.nlmsg_type  = RTM_GETADDR;
	req.r.ifa_family  = family;

	rta = (struct rtattr *)(((char *)&req) + NLMSG_ALIGN(req.n.nlmsg_len));
	rta->rta_len = RTA_LENGTH(family == AF_INET ? 4 : 16);

	head = entry = NULL;

	/* Send request */
	if( send(fd, &req, sizeof(req), 0) <= 0 )
		goto error;

	/* Receive responses */
	for( dump_done = 0; !dump_done; )
	{
		if( (offlen = recv(fd, buf, sizeof(buf), 0)) <= 0 )
			goto error;

		/* Parse message */
		for(nlmp = (struct nlmsghdr *)buf; offlen > sizeof(*nlmp);)
		{
			/* Dump finished? */
			if( nlmp->nlmsg_type == NLMSG_DONE )
			{
				dump_done = 1;
				break;
			}

			int len = nlmp->nlmsg_len;
			int req_len = len - sizeof(*nlmp);

			if( req_len<0 || len>offlen )
				goto error;

			if( !NLMSG_OK(nlmp, offlen) )
				goto error;

			rtmp  = (struct ifaddrmsg *) NLMSG_DATA(nlmp);
			rtatp = (struct rtattr *) IFA_RTA(rtmp);

			if( !(entry = fwd_alloc_ptr(struct fwd_addr_list)) )
				goto error;

			entry->index = rtmp->ifa_index;
			if_indextoname(rtmp->ifa_index, (char *)&entry->ifname);

			rtattrlen = IFA_PAYLOAD(nlmp);

			for( ; RTA_OK(rtatp, rtattrlen); rtatp = RTA_NEXT(rtatp, rtattrlen) )
			{
				if( rtatp->rta_type == IFA_ADDRESS )
				{
					memcpy(&entry->ipaddr, (char *) RTA_DATA(rtatp), rtatp->rta_len);
					entry->prefix = rtmp->ifa_prefixlen;
					entry->family = family;
				}
				else if( rtatp->rta_type == IFA_LABEL)
				{
					memcpy(&entry->label, (char *) RTA_DATA(rtatp), rtatp->rta_len);
				}
			}

			entry->next = head;
			head = entry;

			offlen -= NLMSG_ALIGN(len);
			nlmp = (struct nlmsghdr*)((char*)nlmp + NLMSG_ALIGN(len));
		}
	}

	return head;


	error:

	fwd_free_addrs(head);
	head = entry = NULL;

	return NULL;
}

void fwd_free_addrs(struct fwd_addr_list *head)
{
	struct fwd_addr_list *entry = head;

	while( entry != NULL )
	{
		head = entry->next;
		free(entry);
		entry = head;
	}

	head = entry = NULL;
}

struct fwd_addr_list * fwd_append_addrs(struct fwd_addr_list *head, struct fwd_addr_list *add)
{
	struct fwd_addr_list *entry = head;

	while( entry->next != NULL )
		entry = entry->next;

	return (entry->next = add);
}