def _CreateReceiveSock(version, port=0): # Create a socket to receive packets. read_sock = socket(net_test.GetAddressFamily(version), SOCK_DGRAM, 0) read_sock.bind((net_test.GetWildcardAddress(version), port)) # The second parameter of the tuple is the port number regardless of AF. local_port = read_sock.getsockname()[1] # Guard against the eventuality of the receive failing. net_test.SetNonBlocking(read_sock.fileno()) return read_sock, local_port
def CreateTunInterface(cls, netid): iface = cls.GetInterfaceName(netid) f = open("/dev/net/tun", "r+b") ifr = struct.pack("16sH", iface, IFF_TAP | IFF_NO_PI) ifr += "\x00" * (40 - len(ifr)) fcntl.ioctl(f, TUNSETIFF, ifr) # Give ourselves a predictable MAC address. net_test.SetInterfaceHWAddr(iface, cls.MyMacAddress(netid)) # Disable DAD so we don't have to wait for it. cls.SetSysctl("/proc/sys/net/ipv6/conf/%s/accept_dad" % iface, 0) net_test.SetInterfaceUp(iface) net_test.SetNonBlocking(f) return f
def setUp(self): super(NeighbourTest, self).setUp() self.sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE) self.sock.bind((0, RTMGRP_NEIGH)) net_test.SetNonBlocking(self.sock) for netid in self.tuns: self.SendRA(netid, retranstimer=self.RETRANS_TIME_MS, reachabletime=self.REACHABLE_TIME_MS) self.netid = random.choice(self.tuns.keys()) self.ifindex = self.ifindices[self.netid]
def SocketUDPLoopBack(packet_count, version, prog_fd): family = {4: socket.AF_INET, 6: socket.AF_INET6}[version] sock = socket.socket(family, socket.SOCK_DGRAM, 0) if prog_fd is not None: BpfProgAttachSocket(sock.fileno(), prog_fd) net_test.SetNonBlocking(sock) addr = {4: "127.0.0.1", 6: "::1"}[version] sock.bind((addr, 0)) addr = sock.getsockname() sockaddr = csocket.Sockaddr(addr) for i in xrange(packet_count): sock.sendto("foo", addr) data, retaddr = csocket.Recvfrom(sock, 4096, 0) assert "foo" == data assert sockaddr == retaddr return sock
def setUp(self): super(NeighbourTest, self).setUp() for netid in self.tuns: # Clear the ND cache entries for all routers, so each test starts with # the IPv6 default router in state STALE. addr = self._RouterAddress(netid, 6) ifindex = self.ifindices[netid] self.iproute.UpdateNeighbour(6, addr, None, ifindex, NUD_FAILED) # Configure IPv6 by sending an RA. self.SendRA(netid, retranstimer=self.RETRANS_TIME_MS, reachabletime=self.REACHABLE_TIME_MS) self.sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE) self.sock.bind((0, RTMGRP_NEIGH)) net_test.SetNonBlocking(self.sock) self.netid = random.choice(self.tuns.keys()) self.ifindex = self.ifindices[self.netid]
def __init__(self): self.sock = socket(AF_KEY, SOCK_RAW, PF_KEY_V2) net_test.SetNonBlocking(self.sock) self.seq = 0