def test_addr_raw_packet(): from pypy.module._socket.interp_socket import addr_as_object if not hasattr(rsocket._c, 'sockaddr_ll'): py.test.skip("posix specific test") # HACK: To get the correct interface numer of lo, which in most cases is 1, # but can be anything (i.e. 39), we need to call the libc function # if_nametoindex to get the correct index import ctypes libc = ctypes.CDLL(ctypes.util.find_library('c')) ifnum = libc.if_nametoindex('lo') c_addr_ll = lltype.malloc(rsocket._c.sockaddr_ll, flavor='raw') addrlen = rffi.sizeof(rsocket._c.sockaddr_ll) c_addr = rffi.cast(lltype.Ptr(rsocket._c.sockaddr), c_addr_ll) rffi.setintfield(c_addr_ll, 'c_sll_ifindex', ifnum) rffi.setintfield(c_addr_ll, 'c_sll_protocol', 8) rffi.setintfield(c_addr_ll, 'c_sll_pkttype', 13) rffi.setintfield(c_addr_ll, 'c_sll_hatype', 0) rffi.setintfield(c_addr_ll, 'c_sll_halen', 3) c_addr_ll.c_sll_addr[0] = 'a' c_addr_ll.c_sll_addr[1] = 'b' c_addr_ll.c_sll_addr[2] = 'c' rffi.setintfield(c_addr, 'c_sa_family', socket.AF_PACKET) # fd needs to be somehow valid s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) fd = s.fileno() w_obj = addr_as_object(rsocket.make_address(c_addr, addrlen), fd, space) lltype.free(c_addr_ll, flavor='raw') assert space.is_true(space.eq(w_obj, space.newtuple([ space.wrap('lo'), space.wrap(socket.ntohs(8)), space.wrap(13), space.wrap(False), space.wrap("abc"), ])))
def test_addr_raw_packet(): from pypy.module._socket.interp_socket import addr_as_object if not hasattr(rsocket._c, 'sockaddr_ll'): pytest.skip("posix specific test") # HACK: To get the correct interface number of lo, which in most cases is 1, # but can be anything (i.e. 39), we need to call the libc function # if_nametoindex to get the correct index import ctypes libc = ctypes.CDLL(ctypes.util.find_library('c')) ifnum = libc.if_nametoindex('lo') c_addr_ll = lltype.malloc(rsocket._c.sockaddr_ll, flavor='raw') addrlen = rffi.sizeof(rsocket._c.sockaddr_ll) c_addr = rffi.cast(lltype.Ptr(rsocket._c.sockaddr), c_addr_ll) rffi.setintfield(c_addr_ll, 'c_sll_ifindex', ifnum) rffi.setintfield(c_addr_ll, 'c_sll_protocol', 8) rffi.setintfield(c_addr_ll, 'c_sll_pkttype', 13) rffi.setintfield(c_addr_ll, 'c_sll_hatype', 0) rffi.setintfield(c_addr_ll, 'c_sll_halen', 3) c_addr_ll.c_sll_addr[0] = 'a' c_addr_ll.c_sll_addr[1] = 'b' c_addr_ll.c_sll_addr[2] = 'c' rffi.setintfield(c_addr, 'c_sa_family', socket.AF_PACKET) # fd needs to be somehow valid s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) fd = s.fileno() w_obj = addr_as_object(rsocket.make_address(c_addr, addrlen), fd, space) lltype.free(c_addr_ll, flavor='raw') assert space.is_true(space.eq(w_obj, space.newtuple([ space.wrap('lo'), space.wrap(socket.ntohs(8)), space.wrap(13), space.wrap(False), space.wrap("abc"), ])))