summaryrefslogtreecommitdiffstats
path: root/isys/iface.c
blob: 51f923c06c5f212368579d34e1a7fe721b0a01b6 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * iface.c - Network interface control functions
 *
 * Copyright (C) 2006, 2007, 2008  Red Hat, Inc.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author(s): David Cantrell <dcantrell@redhat.com>
 */

#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <netlink/netlink.h>
#include <netlink/socket.h>
#include <netlink/route/addr.h>
#include <netlink/route/link.h>

#include "iface.h"
#include "str.h"

/*
 * Return an NETLINK_ROUTE cache.
 */
struct nl_cache *iface_get_link_cache(struct nl_handle **handle) {
    struct nl_cache *cache = NULL;

    if ((*handle = nl_handle_alloc()) == NULL) {
        perror("nl_handle_alloc() failure in iface_get_link_cache()");
        return NULL;
    }

    if (nl_connect(*handle, NETLINK_ROUTE)) {
        perror("nl_connect() failure in iface_get_link_cache()");
        nl_handle_destroy(*handle);
        return NULL;
    }

    if ((cache = rtnl_link_alloc_cache(*handle)) == NULL) {
        perror("rtnl_link_alloc_cache() failure in iface_get_link_cache()");
        nl_close(*handle);
        nl_handle_destroy(*handle);
        return NULL;
    }

    return cache;
}

/*
 * Given an interface name (e.g., eth0), return the IP address in human
 * readable format (i.e., the output from inet_ntop()).  Return NULL for
 * no match.  NOTE:  This function will check for IPv6 and IPv4
 * addresses.  In the case where the interface has both, the IPv4 address
 * is returned.  The only way you will get an IPv6 address from this function
 * is if that's the only address configured for the interface.
 */
char *iface_ip2str(char *ifname) {
    int ifindex = -1, buflen = 0, family = 0;
    char *buf = NULL, *bufv4 = NULL, *bufv6 = NULL, *pos = NULL;
    struct nl_handle *handle = NULL;
    struct nl_cache *cache = NULL;
    struct nl_object *obj = NULL;
    struct rtnl_addr *raddr = NULL;
    struct nl_addr *addr = NULL;

    if (ifname == NULL) {
        perror("Missing ifname in iface_ip2str()");
        return NULL;
    }

    if ((cache = iface_get_link_cache(&handle)) == NULL) {
        perror("iface_get_link_cache() failure in iface_ip2str()");
        return NULL;
    }

    ifindex = rtnl_link_name2i(cache, ifname);

    if ((cache = rtnl_addr_alloc_cache(handle)) == NULL) {
        perror("rtnl_addr_alloc_cache() failure in iface_ip2str()");
        goto ip2str_error;
    }

    /* find the IPv4 and IPv6 addresses for this interface */
    if ((obj = nl_cache_get_first(cache)) == NULL) {
        perror("nl_cache_get_first() failure in iface_ip2str()");
        goto ip2str_error;
    }

    do {
        raddr = (struct rtnl_addr *) obj;

        if (rtnl_addr_get_ifindex(raddr) == ifindex) {
            family = rtnl_addr_get_family(raddr);

            if (family == AF_INET || family == AF_INET6) {
                /* skip if we have already saved an address */
                /* FIXME: we should handle multiple addresses for the same
                 * family per interface
                 */
                if (family == AF_INET && bufv4 != NULL) {
                    continue;
                }

                if (family == AF_INET6 && bufv6 != NULL) {
                    continue;
                }

                /* get the address */
                addr = rtnl_addr_get_local(raddr);

                /* convert to human readable format */
                if (family == AF_INET) {
                    buflen = INET_ADDRSTRLEN;
                } else if (family == AF_INET6) {
                    buflen = INET6_ADDRSTRLEN;
                }

                buflen += 1;

                if ((buf = malloc(buflen)) == NULL) {
                    perror("malloc() failure on buf in iface_ip2str()");
                    nl_addr_destroy(addr);
                    goto ip2str_error;
                }

                buf = nl_addr2str(addr, buf, buflen);
                nl_addr_destroy(addr);

                /* trim the prefix notation */
                if ((pos = index(buf, '/')) != NULL) {
                    *pos = '\0';
                    if ((buf = realloc(buf, strlen(buf) + 1)) == NULL) {
                        perror("realloc() failure on buf in iface_ip2str()");
                        nl_addr_destroy(addr);
                        goto ip2str_error;
                    }
                }

                /* save the IP address in the right buffer */
                if (family == AF_INET) {
                    bufv4 = strdup(buf);
                } else if (family == AF_INET6) {
                    bufv6 = strdup(buf);
                }

                /* empty the main conversion buffer */
                if (buf) {
                    free(buf);
                    buf = NULL;
                }
            }
        }
    } while ((obj = nl_cache_get_next(obj)) != NULL);

ip2str_error:
    nl_close(handle);
    nl_handle_destroy(handle);

    /* return IPv4 address if we have both families
     * return IPv6 address if we only have IPv6 family
     * return NULL otherwise
     */
    if ((bufv4 && bufv6) || (bufv4 && !bufv6)) {
        return bufv4;
    } else if (!bufv4 && bufv6) {
        return bufv6;
    } else {
        return NULL;
    }
}

/**
 * Given an interface name (e.g., eth0), return the MAC address in human
 * readable format (e.g., 00:11:52:12:D9:A0).  Return NULL for no match.
 */
char *iface_mac2str(char *ifname) {
    int buflen = 20;
    char *buf = NULL;
    struct nl_handle *handle = NULL;
    struct nl_cache *cache = NULL;
    struct rtnl_link *link = NULL;
    struct nl_addr *addr = NULL;

    if (ifname == NULL) {
        perror("Missing ifname in iface_mac2str()");
        return NULL;
    }

    if ((cache = iface_get_link_cache(&handle)) == NULL) {
        perror("iface_get_link_cache() failure in iface_mac2str()");
        return NULL;
    }

    if ((link = rtnl_link_get_by_name(cache, ifname)) == NULL) {
        perror("rtnl_link_get_by_name() failure in iface_mac2str()");
        goto mac2str_error2;
    }

    if ((addr = rtnl_link_get_addr(link)) == NULL) {
        perror("rtnl_link_get_addr() failure in iface_mac2str()");
        goto mac2str_error3;
    }

    if ((buf = malloc(buflen)) == NULL) {
        perror("malloc() failure on buf in iface_mac2str()");
        goto mac2str_error4;
    }

    if ((buf = nl_addr2str(addr, buf, buflen)) != NULL) {
        buf = str2upper(buf);
    }

mac2str_error4:
    nl_addr_destroy(addr);
mac2str_error3:
    rtnl_link_put(link);
mac2str_error2:
    nl_close(handle);
    nl_handle_destroy(handle);

    return buf;
}