File: etherinfo.c

package info (click to toggle)
python-ethtool 0.14-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 324 kB
  • sloc: ansic: 1,770; python: 1,509; makefile: 24; sh: 16
file content (296 lines) | stat: -rw-r--r-- 8,151 bytes parent folder | download | duplicates (2)
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* etherinfo.c - Retrieve ethernet interface info via NETLINK
 *
 * Copyright (C) 2009-2013 Red Hat Inc.
 *
 * David Sommerseth <davids@redhat.com>
 * Parts of this code is based on ideas and solutions in iproute2
 *
 * This application 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; version 2.
 *
 * This application 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.
 */

#include <Python.h>
#include "include/py3c/compat.h"
#include <bytesobject.h>
#include <bits/sockaddr.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <stdlib.h>
#include <asm/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netlink/cache.h>
#include <netlink/addr.h>
#include <netlink/errno.h>
#include <netlink/route/addr.h>
#include <netlink/route/link.h>
#include <netlink/route/rtnl.h>
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include "etherinfo_struct.h"
#include "etherinfo.h"

/*
 *
 *   Internal functions for working with struct etherinfo
 *
 */


/**
 *  libnl callback function.
 *  Does the real parsing of a record returned by NETLINK.
 *  This function parses LINK related packets
 *
 * @param obj   Pointer to a struct nl_object response
 * @param arg   Pointer to a struct etherinfo element where the parse result
 *              will be saved
 */
static void callback_nl_link(struct nl_object *obj, void *arg)
{
    PyEtherInfo *ethi = (PyEtherInfo *) arg;
    struct rtnl_link *link = (struct rtnl_link *) obj;
    char hwaddr[130];

    if ((ethi == NULL) || (ethi->hwaddress != NULL)) {
        return;
    }

    memset(&hwaddr, 0, 130);
    nl_addr2str(rtnl_link_get_addr(link), hwaddr, sizeof(hwaddr));
    if (ethi->hwaddress) {
        Py_XDECREF(ethi->hwaddress);
    }
    ethi->hwaddress = PyStr_FromFormat("%s", hwaddr);
}


/**
 *  libnl callback function.  Does the real parsing of a record returned by
 *  NETLINK.  This function parses ADDRESS related packets
 *
 * @param obj   Pointer to a struct nl_object response
 * @param arg   Pointer to a struct etherinfo element where the parse result
 *              will be saved
 */
static void callback_nl_address(struct nl_object *obj, void *arg)
{
    PyObject *py_addrlist = (PyObject *) arg;
    struct rtnl_addr *rtaddr = (struct rtnl_addr *) obj;
    PyObject *addr_obj = NULL;
    int af_family = -1;

    if (py_addrlist == NULL) {
        return;
    }

    /* Ensure that we're processing only known address types.
     * Currently only IPv4 and IPv6 is handled
     */
    af_family = rtnl_addr_get_family(rtaddr);
    if (af_family != AF_INET && af_family != AF_INET6) {
        return;
    }

    /* Prepare a new Python object with the IP address */
    addr_obj = make_python_address_from_rtnl_addr(rtaddr);
    if (!addr_obj) {
        return;
    }
    /* Append the IP address object to the address list */
    PyList_Append(py_addrlist, addr_obj);
    Py_DECREF(addr_obj);
}


/**
 * Sets the etherinfo.index member to the corresponding device set in
 * etherinfo.device
 *
 * @param self A pointer the current PyEtherInfo Python object which contains
 *             the device name and the place where to save the corresponding
 *             index value.
 *
 * @return Returns 1 on success, otherwise 0.  On error, a Python error
 *         exception is set.
 */
static int _set_device_index(PyEtherInfo *self)
{
    struct nl_cache *link_cache;
    struct rtnl_link *link;

    /* Find the interface index we're looking up.
     * As we don't expect it to change, we're reusing a "cached"
     * interface index if we have that
     */
    if (self->index < 0) {
        if ((errno = rtnl_link_alloc_cache(get_nlc(),
                                           AF_UNSPEC, &link_cache)) < 0) {
            PyErr_SetString(PyExc_OSError, nl_geterror(errno));
            return 0;
        }
        link = rtnl_link_get_by_name(link_cache, PyStr_AsString(self->device));
        if (!link) {
            errno = ENODEV;
            PyErr_SetFromErrno(PyExc_IOError);
            nl_cache_free(link_cache);
            return 0;
        }
        self->index = rtnl_link_get_ifindex(link);
        if (self->index <= 0) {
            errno = ENODEV;
            PyErr_SetFromErrno(PyExc_IOError);
            rtnl_link_put(link);
            nl_cache_free(link_cache);
            return 0;
        }

        rtnl_link_put(link);
        nl_cache_free(link_cache);
    }
    return 1;
}


/*
 *
 *   Exported functions - API frontend
 *
 */

/**
 * Populate the PyEtherInfo Python object with link information for the current
 * device
 *
 * @param self  Pointer to the device object, a PyEtherInfo Python object
 *
 * @return Returns 1 on success, otherwise 0
 */
int get_etherinfo_link(PyEtherInfo *self)
{
    struct nl_cache *link_cache;
    struct rtnl_link *link;
    int err = 0;

    if (!self) {
        return 0;
    }

    /* Open a NETLINK connection on-the-fly */
    if (!open_netlink(self)) {
        PyErr_Format(PyExc_RuntimeError,
                     "Could not open a NETLINK connection for %s",
                     PyStr_AsString(self->device));
        return 0;
    }

    if (_set_device_index(self) != 1) {
        return 0;
    }

    /* Extract MAC/hardware address of the interface */
    if ((err = rtnl_link_alloc_cache(get_nlc(), AF_UNSPEC, &link_cache)) < 0) {
        PyErr_SetString(PyExc_OSError, nl_geterror(err));
        return 0;
    }
    link = rtnl_link_alloc();
    if (!link) {
        errno = ENOMEM;
        PyErr_SetFromErrno(PyExc_OSError);
        return 0;
    }
    rtnl_link_set_ifindex(link, self->index);
    nl_cache_foreach_filter(link_cache, OBJ_CAST(link),
                            callback_nl_link, self);
    rtnl_link_put(link);
    nl_cache_free(link_cache);

    return 1;
}



/**
 * Query NETLINK for device IP address configuration
 *
 * @param self   A PyEtherInfo Python object for the current device to retrieve
 *               IP address configuration data from
 * @param query  What to query for.  Must be NLQRY_ADDR4 for IPv4 addresses or
 *               NLQRY_ADDR6 for IPv6 addresses.
 *
 * @return Returns a Python list containing PyNetlinkIPaddress objects on
 *         success, otherwise NULL
 */
PyObject * get_etherinfo_address(PyEtherInfo *self, nlQuery query)
{
    struct nl_cache *addr_cache;
    struct rtnl_addr *addr;
    PyObject *addrlist = NULL;
    int err = 0;

    if (!self) {
        return NULL;
    }

    /* Open a NETLINK connection on-the-fly */
    if (!open_netlink(self)) {
        PyErr_Format(PyExc_RuntimeError,
                     "Could not open a NETLINK connection for %s",
                     PyStr_AsString(self->device));
        return NULL;
    }

    if(!_set_device_index(self)) {
        return NULL;
    }

    /* Query the for requested info via NETLINK */
    /* Extract IP address information */
    if ((err = rtnl_addr_alloc_cache(get_nlc(), &addr_cache)) < 0) {
        PyErr_SetString(PyExc_OSError, nl_geterror(err));
        nl_cache_free(addr_cache);
        return NULL;
    }

    addr = rtnl_addr_alloc();

    if (!addr) {
        errno = ENOMEM;
        PyErr_SetFromErrno(PyExc_OSError);
        return NULL;
    }
    rtnl_addr_set_ifindex(addr, self->index);

    switch( query) {
    case NLQRY_ADDR4:
        rtnl_addr_set_family(addr, AF_INET);
        break;

    case NLQRY_ADDR6:
        rtnl_addr_set_family(addr, AF_INET6);
        break;

    default:
        return NULL;
    }

    /* Retrieve all address information */
    addrlist = PyList_New(0);  /* The list where to put the address object */
    assert(addrlist);

    /* Loop through all configured addresses */
    nl_cache_foreach_filter(addr_cache, OBJ_CAST(addr),
                            callback_nl_address, addrlist);
    rtnl_addr_put(addr);
    nl_cache_free(addr_cache);

    return addrlist;
}