~johannes.erdfelt/openstack-guest-agents/gentoo-dns

« back to all changes in this revision

Viewing changes to unix/lib/agentlib.c

  • Committer: Johannes Erdfelt
  • Date: 2011-10-21 16:50:41 UTC
  • mfrom: (88.3.17 networking-refactor)
  • Revision ID: johannes.erdfelt@rackspace.com-20111021165041-smmnytu2xezh2751
MergeĀ lp:~johannes.erdfelt/openstack-guest-agents/networking-refactor

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *    under the License.
18
18
 */
19
19
 
 
20
#define _BSD_SOURCE
 
21
 
20
22
#ifdef HAVE_CONFIG_H
21
23
#include <config.h>
22
24
#endif
33
35
#include <pthread.h>
34
36
#include <assert.h>
35
37
#include <errno.h>
 
38
#include <sys/socket.h>
 
39
#include <net/if.h>
 
40
#include <net/ethernet.h>
 
41
#if defined(__linux__)
 
42
#include <netpacket/packet.h>
 
43
#include <net/if_arp.h>
 
44
#elif defined(__FreeBSD__)
 
45
#include <net/if_dl.h>
 
46
#include <net/if_types.h>
 
47
#else
 
48
#error Unknown operating system
 
49
#endif
 
50
#include <ifaddrs.h>
36
51
#include "libagent_int.h"
37
52
#include "agentlib.h"
38
53
 
43
58
    return PyString_FromString(AGENT_VERSION);
44
59
}
45
60
 
 
61
static PyObject *_agentlib_get_interfaces(PyObject *self, PyObject *args)
 
62
{
 
63
    struct ifaddrs *ifa;
 
64
    if (getifaddrs(&ifa) < 0)
 
65
        return PyErr_SetFromErrno(PyExc_OSError);
 
66
 
 
67
    PyObject *interfaces = PyList_New(0);
 
68
    if (!interfaces)
 
69
        return NULL;
 
70
 
 
71
    while (ifa) {
 
72
        if (ifa->ifa_flags & IFF_LOOPBACK)
 
73
            goto next;
 
74
 
 
75
#if defined(__linux__)
 
76
        if (ifa->ifa_addr->sa_family != PF_PACKET)
 
77
            goto next;
 
78
 
 
79
        struct sockaddr_ll *sll = (struct sockaddr_ll *)ifa->ifa_addr;
 
80
        if (sll->sll_hatype != ARPHRD_ETHER)
 
81
            goto next;
 
82
 
 
83
        unsigned char *lladdr = sll->sll_addr;
 
84
#elif defined(__FreeBSD__)
 
85
        if (ifa->ifa_addr->sa_family != AF_LINK)
 
86
            goto next;
 
87
 
 
88
        struct sockaddr_dl *sdl = (struct sockaddr_dl *)ifa->ifa_addr;
 
89
        if (sdl->sdl_type != IFT_ETHER)
 
90
            goto next;
 
91
 
 
92
        unsigned char *lladdr = (unsigned char *)LLADDR(sdl);
 
93
#endif
 
94
 
 
95
        char macaddr[sizeof("00:11:22:33:44:55") + 1];
 
96
        snprintf(macaddr, sizeof(macaddr), "%02x:%02x:%02x:%02x:%02x:%02x",
 
97
                lladdr[0], lladdr[1], lladdr[2],
 
98
                lladdr[3], lladdr[4], lladdr[5]);
 
99
 
 
100
        PyObject *arg = Py_BuildValue("ss", ifa->ifa_name, macaddr);
 
101
        if (!arg)
 
102
            goto err;
 
103
 
 
104
        int ret = PyList_Append(interfaces, arg);
 
105
        Py_DECREF(arg);
 
106
        if (ret < 0)
 
107
            goto err;
 
108
 
 
109
next:
 
110
        ifa = ifa->ifa_next;
 
111
    }
 
112
    freeifaddrs(ifa);
 
113
 
 
114
    return interfaces;
 
115
 
 
116
err:
 
117
    Py_DECREF(interfaces);
 
118
    freeifaddrs(ifa);
 
119
 
 
120
    return NULL;
 
121
}
 
122
 
 
123
 
46
124
static PyObject *_agentlib_register(PyObject *self, PyObject *args)
47
125
{
48
126
    PyObject *exchange_plugin;
123
201
    {
124
202
        { "get_version", (PyCFunction)_agentlib_get_version,
125
203
                METH_NOARGS, "Get the agent version string" },
 
204
        { "get_interfaces", (PyCFunction)_agentlib_get_interfaces,
 
205
                METH_NOARGS, "Get the network interface names and "
 
206
                             "MAC addresses" },
126
207
        { "sethostname", (PyCFunction)_agentlib_sethostname,
127
208
                METH_VARARGS, "Set the system hostname" },
128
209
        { "encrypt_password", (PyCFunction)_agentlib_encrypt_password,