Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
MelindaShore committed Feb 12, 2016
2 parents a841c58 + 51575d4 commit b362ded
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 32 deletions.
36 changes: 8 additions & 28 deletions examples/client_subnet.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions pygetdns_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b362ded

Please sign in to comment.