33
35
#include <pthread.h>
34
36
#include <assert.h>
38
#include <sys/socket.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>
48
#error Unknown operating system
36
51
#include "libagent_int.h"
37
52
#include "agentlib.h"
43
58
return PyString_FromString(AGENT_VERSION);
61
static PyObject *_agentlib_get_interfaces(PyObject *self, PyObject *args)
64
if (getifaddrs(&ifa) < 0)
65
return PyErr_SetFromErrno(PyExc_OSError);
67
PyObject *interfaces = PyList_New(0);
72
if (ifa->ifa_flags & IFF_LOOPBACK)
75
#if defined(__linux__)
76
if (ifa->ifa_addr->sa_family != PF_PACKET)
79
struct sockaddr_ll *sll = (struct sockaddr_ll *)ifa->ifa_addr;
80
if (sll->sll_hatype != ARPHRD_ETHER)
83
unsigned char *lladdr = sll->sll_addr;
84
#elif defined(__FreeBSD__)
85
if (ifa->ifa_addr->sa_family != AF_LINK)
88
struct sockaddr_dl *sdl = (struct sockaddr_dl *)ifa->ifa_addr;
89
if (sdl->sdl_type != IFT_ETHER)
92
unsigned char *lladdr = (unsigned char *)LLADDR(sdl);
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]);
100
PyObject *arg = Py_BuildValue("ss", ifa->ifa_name, macaddr);
104
int ret = PyList_Append(interfaces, arg);
117
Py_DECREF(interfaces);
46
124
static PyObject *_agentlib_register(PyObject *self, PyObject *args)
48
126
PyObject *exchange_plugin;
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 "
126
207
{ "sethostname", (PyCFunction)_agentlib_sethostname,
127
208
METH_VARARGS, "Set the system hostname" },
128
209
{ "encrypt_password", (PyCFunction)_agentlib_encrypt_password,