diff --git a/examples/client_subnet.py b/examples/client_subnet.py index e3b781b..66ac035 100755 --- a/examples/client_subnet.py +++ b/examples/client_subnet.py @@ -1,47 +1,27 @@ import getdns -import socket, fcntl, sys from struct import pack, unpack - - -# -# returns a tuple containing the network part of the ip -# address for the interface and the netmask, both encoded -# in strings. Definitely not portable to Windows, probably -# not portable to some Unixes. Unfortunately you have -# to pass in the name of the interface; interface name -# will be discovered in a future version -# - -def get_network_info(ifname): - SIOCGIFADDR = 0x8915 - SIOCGIFNETMASK = 0x891b - - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - netmask = fcntl.ioctl(s.fileno(), SIOCGIFNETMASK, pack('256s',ifname))[20:24] - addr = fcntl.ioctl(s.fileno(), SIOCGIFADDR, pack('256s', ifname))[20:24] - return (pack('!I', (unpack('!I', addr)[0] & unpack('!I', netmask)[0])), netmask) def main(): CLIENT_SUBNET_OPCODE = 8 - LOCAL_INTERFACE = 'eth0' + + address = '192.168.1.0' host = 'getdnsapi.net' + source_len = 12 - if len(sys.argv) == 2: - host = sys.argv[1] family = pack("!H", 1) # start building the edns option fields - source_netmask, address = get_network_info(LOCAL_INTERFACE) - scope_netmask = pack("B", 0) + source_len = pack('!B', source_len) + scope_len = pack('!B', 0) # # encoding the binary data in strings makes it really easy # to build packets by concatenating those strings # - payload = family + source_netmask + scope_netmask + address - length = pack("!H", len(payload)) + address = pack('!BBBB', 192, 168, 1, 0) + payload = family + source_len + scope_len + address ext = { 'add_opt_parameters': {'options': [ {'option_code': CLIENT_SUBNET_OPCODE, - 'option_data': length+payload} ] }} + 'option_data': payload} ] }} c = getdns.Context() c.resolution_type = getdns.RESOLUTION_STUB diff --git a/pygetdns_util.c b/pygetdns_util.c index 36a0d5a..17b7124 100644 --- a/pygetdns_util.c +++ b/pygetdns_util.c @@ -329,13 +329,13 @@ extensions_to_getdnsdict(PyDictObject *pydict) } else if (!strncmp(tmpoptionlistkey, "option_data", strlen("option_data"))) { option_data = (struct getdns_bindata *)malloc(sizeof(struct getdns_bindata)); option_data->size = PyObject_Length(optiondictvalue); + printf("XXX size is %d\n", (int)option_data->size); #if PY_MAJOR_VERSION >= 3 -/* This is almost certainly wrong */ - option_data->data = (uint8_t *)PyBytes_AsString(PyObject_Bytes(optiondictvalue)); + option_data->data = (uint8_t *)PyBytes_AS_STRING(optiondictvalue); #else -/* ditto */ - option_data->data = (uint8_t *)PyString_AsString(PyObject_Bytes(optiondictvalue)); + option_data->data = (uint8_t *)PyString_AS_STRING(optiondictvalue); #endif + getdns_dict_set_bindata(tmpoptions_list_dict, "option_data", option_data); } else { PyErr_SetString(getdns_error, GETDNS_RETURN_EXTENSION_MISFORMAT_TEXT);