Python socket.htons() Examples
The following are 30
code examples of socket.htons().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
socket
, or try the search function
.
![](https://www.programcreek.com/common/static/images/search.png)
Example #1
Source File: supersocket.py From scapy with GNU General Public License v2.0 | 7 votes |
def __init__(self, type=ETH_P_IP, filter=None, iface=None, promisc=None, nofilter=0): # noqa: E501 self.outs = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW) # noqa: E501 self.outs.setsockopt(socket.SOL_IP, socket.IP_HDRINCL, 1) self.ins = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type)) # noqa: E501 self.iface = iface if iface is not None: self.ins.bind((self.iface, type)) if not six.PY2: try: # Receive Auxiliary Data (VLAN tags) self.ins.setsockopt(SOL_PACKET, PACKET_AUXDATA, 1) self.ins.setsockopt( socket.SOL_SOCKET, SO_TIMESTAMPNS, 1 ) self.auxdata_available = True except OSError: # Note: Auxiliary Data is only supported since # Linux 2.6.21 msg = "Your Linux Kernel does not support Auxiliary Data!" log_runtime.info(msg)
Example #2
Source File: packet.py From vulscan with MIT License | 6 votes |
def __init__(self, srcp, dstp, seqn=1): self.srcp = srcp self.dstp = dstp self.seqn = seqn self.ackn = 0 self.offset = 5 # Data offset: 5x4 = 20 bytes self.reserved = 0 self.urg = 0 self.ack = 0 self.psh = 0 self.rst = 0 self.syn = 1 self.fin = 0 self.window = socket.htons(5840) self.checksum = 0 self.urgp = 0 self.payload = ""
Example #3
Source File: ip.py From Nscan with Apache License 2.0 | 6 votes |
def pack(self): ip_header = struct.pack("!BBHHHBBH4s4s", self.ver_ihl, self.tos, self.length, self.id, self.flags_offset, self.ttl, self.protocol, 0, # checksum should be 0 self.src, self.dst) self.checksum = checksum(ip_header) ip_header = struct.pack("!BBHHHBBH4s4s", self.ver_ihl, self.tos, self.length, self.id, self.flags_offset, self.ttl, self.protocol, socket.htons(self.checksum), self.src, self.dst) return ip_header
Example #4
Source File: tcp.py From Nscan with Apache License 2.0 | 6 votes |
def __init__(self, srcp, dstp): self.srcp = srcp self.dstp = dstp self.seqn = 10 self.ackn = 0 self.offset = 5 # Data offset: 5x4 = 20 bytes self.reserved = 0 self.urg = 0 self.ack = 0 self.psh = 0 self.rst = 0 self.syn = 1 self.fin = 0 self.window = htons(5840) self.checksum = 0 self.urgp = 0 self.payload = ""
Example #5
Source File: tcp.py From Nscan with Apache License 2.0 | 6 votes |
def __init__(self, srcp, dstp): self.srcp = srcp self.dstp = dstp self.seqn = 10 self.ackn = 0 self.offset = 5 # Data offset: 5x4 = 20 bytes self.reserved = 0 self.urg = 0 self.ack = 0 self.psh = 0 self.rst = 0 self.syn = 1 self.fin = 0 self.window = htons(5840) self.checksum = 0 self.urgp = 0 self.payload = ""
Example #6
Source File: inet.py From scapy with GNU General Public License v2.0 | 6 votes |
def answers(self, other): if not isinstance(other, IP): return 0 # Check if IP addresses match test_IPsrc = not conf.checkIPsrc or self.src == other.src test_IPdst = self.dst == other.dst # Check if IP ids match test_IPid = not conf.checkIPID or self.id == other.id test_IPid |= conf.checkIPID and self.id == socket.htons(other.id) # Check if IP protocols match test_IPproto = self.proto == other.proto if not (test_IPsrc and test_IPdst and test_IPid and test_IPproto): return 0 return self.payload.answers(other.payload)
Example #7
Source File: test_bsc.py From AIT-Core with MIT License | 6 votes |
def test_mocked_eth_socket_with_rawsocket(self, socket_mock): socket_family = getattr(gevent.socket, 'AF_PACKET', gevent.socket.AF_INET) rawsocket_is_installed = True if bsc.RAW_SOCKET_FD else False if not rawsocket_is_installed: rawsocket_fd = 'fake_rawsocket_fd' bsc.RAW_SOCKET_FD = rawsocket_fd else: rawsocket_fd = bsc.RAW_SOCKET_FD handler = {'name':'name', 'log_dir':'/tmp'} sl = bsc.SocketStreamCapturer([handler], ['eho0', 0], 'ethernet') # We need to test a different load if the rawsocket package is used socket_mock.fromfd.assert_called_with(rawsocket_fd, socket_family, gevent.socket.SOCK_RAW, socket.htons(bsc.ETH_PROTOCOL)) assert sl.conn_type == 'ethernet' if not rawsocket_is_installed: bsc.RAW_SOCKET_FD = None
Example #8
Source File: test_bsc.py From AIT-Core with MIT License | 6 votes |
def test_mocked_eth_socket(self, socket_mock): socket_family = getattr(gevent.socket, 'AF_PACKET', gevent.socket.AF_INET) proto = bsc.ETH_PROTOCOL handler = {'name':'name', 'log_dir':'/tmp'} bsc.RAW_SOCKET_FD = 'foobar' sl = bsc.SocketStreamCapturer([handler], ['eho0', 0], 'ethernet') # We need to test a different load if the rawsocket package is used if not bsc.RAW_SOCKET_FD: socket_mock.socket.assert_called_with(socket_family, gevent.socket.SOCK_RAW, socket.htons(proto)) else: socket_mock.fromfd.assert_called_with(bsc.RAW_SOCKET_FD, socket_family, gevent.socket.SOCK_RAW, socket.htons(proto)) assert sl.conn_type == 'ethernet' bsc.RAW_SOCKET_FD = None
Example #9
Source File: ping.py From g3ar with BSD 2-Clause "Simplified" License | 6 votes |
def send_one_ping(my_socket, dest_addr, ID): """ Send one ping to the given >dest_addr<. """ dest_addr = socket.gethostbyname(dest_addr) # Header is type (8), code (8), checksum (16), id (16), sequence (16) my_checksum = 0 # Make a dummy heder with a 0 checksum. header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, my_checksum, ID, 1) bytesInDouble = struct.calcsize("d") data = (192 - bytesInDouble) * "Q" data = struct.pack("d", default_timer()) + data # Calculate the checksum on the data and the dummy header. my_checksum = checksum(header + data) # Now that we have the right checksum, we put that in. It's just easier # to make up a new header than to stuff it into the dummy. header = struct.pack( "bbHHh", ICMP_ECHO_REQUEST, 0, socket.htons(my_checksum), ID, 1 ) packet = header + data my_socket.sendto(packet, (dest_addr, 1)) # Don't know about the 1
Example #10
Source File: ping.py From g3ar with BSD 2-Clause "Simplified" License | 6 votes |
def send_one_ping(my_socket, dest_addr, ID): """ Send one ping to the given >dest_addr<. """ dest_addr = socket.gethostbyname(dest_addr) # Header is type (8), code (8), checksum (16), id (16), sequence (16) my_checksum = 0 # Make a dummy heder with a 0 checksum. header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, my_checksum, ID, 1) bytesInDouble = struct.calcsize("d") data = (192 - bytesInDouble) * "Q" data = struct.pack("d", default_timer()) + data # Calculate the checksum on the data and the dummy header. my_checksum = checksum(header + data) # Now that we have the right checksum, we put that in. It's just easier # to make up a new header than to stuff it into the dummy. header = struct.pack( "bbHHh", ICMP_ECHO_REQUEST, 0, socket.htons(my_checksum), ID, 1 ) packet = header + data my_socket.sendto(packet, (dest_addr, 1)) # Don't know about the 1
Example #11
Source File: monitor_linux.py From ryu with Apache License 2.0 | 6 votes |
def __init__(self, *args, **kwargs): super(VRRPInterfaceMonitorNetworkDevice, self).__init__(*args, **kwargs) self.__is_active = True config = self.config if config.is_ipv6: family = socket.AF_INET6 ether_type = ether.ETH_TYPE_IPV6 mac_address = vrrp.vrrp_ipv6_src_mac_address(config.vrid) else: family = socket.AF_INET ether_type = ether.ETH_TYPE_IP mac_address = vrrp.vrrp_ipv4_src_mac_address(config.vrid) # socket module doesn't define IPPROTO_VRRP self.ip_socket = socket.socket(family, socket.SOCK_RAW, inet.IPPROTO_VRRP) self.packet_socket = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(ether_type)) self.packet_socket.bind((self.interface.device_name, ether_type, socket.PACKET_MULTICAST, arp.ARP_HW_TYPE_ETHERNET, addrconv.mac.text_to_bin(mac_address))) self.ifindex = if_nametoindex(self.interface.device_name)
Example #12
Source File: nmap.py From PythonMiniProbe with BSD 3-Clause "New" or "Revised" License | 6 votes |
def send_one_ping(self, my_socket, dest_addr, ID): """ Send one ping to the given >dest_addr<. """ dest_addr = socket.gethostbyname(dest_addr) # Header is type (8), code (8), checksum (16), id (16), sequence (16) my_checksum = 0 # Make a dummy heder with a 0 checksum. header = struct.pack("bbHHh", self.ICMP_ECHO_REQUEST, 0, my_checksum, ID, 1) bytesInDouble = struct.calcsize("d") data = (192 - bytesInDouble) * "Q" data = struct.pack("d", time.time()) + data # Calculate the checksum on the data and the dummy header. my_checksum = self.checksum(header + data) # Now that we have the right checksum, we put that in. It's just easier # to make up a new header than to stuff it into the dummy. header = struct.pack( "bbHHh", self.ICMP_ECHO_REQUEST, 0, socket.htons(my_checksum), ID, 1 ) packet = header + data my_socket.sendto(packet, (dest_addr, 1)) # Don't know about the 1
Example #13
Source File: ping.py From stash with MIT License | 6 votes |
def send_one_ping(my_socket, dest_addr, ID): """ Send one ping to the given >dest_addr<. """ dest_addr = socket.gethostbyname(dest_addr) # Header is type (8), code (8), checksum (16), id (16), sequence (16) my_checksum = 0 # Make a dummy heder with a 0 checksum. header = struct.pack(b"bbHHh", ICMP_ECHO_REQUEST, 0, my_checksum, ID, 1) bytesInDouble = struct.calcsize("d") data = (192 - bytesInDouble) * b"Q" data = struct.pack("d", default_timer()) + data # Calculate the checksum on the data and the dummy header. my_checksum = checksum(header + data) # Now that we have the right checksum, we put that in. It's just easier # to make up a new header than to stuff it into the dummy. header = struct.pack(b"bbHHh", ICMP_ECHO_REQUEST, 0, socket.htons(my_checksum), ID, 1) packet = header + data my_socket.sendto(packet, (dest_addr, 1)) # Don't know about the 1
Example #14
Source File: sample_router.py From ryu with Apache License 2.0 | 5 votes |
def _arp_loop(self): try: with contextlib.closing( socket.socket( socket.AF_PACKET, socket.SOCK_RAW, socket.htons(ether.ETH_TYPE_ARP))) as packet_socket: packet_socket.bind((self.interface.device_name, socket.htons(ether.ETH_TYPE_ARP), socket.PACKET_BROADCAST, arp.ARP_HW_TYPE_ETHERNET, mac_lib.BROADCAST)) self._arp_loop_socket(packet_socket) except greenlet.GreenletExit: # suppress thread.kill exception pass
Example #15
Source File: test_socket.py From ironpython3 with Apache License 2.0 | 5 votes |
def testNtoH(self): # This just checks that htons etc. are their own inverse, # when looking at the lower 16 or 32 bits. sizes = {socket.htonl: 32, socket.ntohl: 32, socket.htons: 16, socket.ntohs: 16} for func, size in sizes.items(): mask = (1<<size) - 1 for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210): self.assertEqual(i & mask, func(func(i&mask)) & mask) swapped = func(mask) self.assertEqual(swapped & mask, mask) self.assertRaises(OverflowError, func, 1<<34)
Example #16
Source File: rogue_dhcp.py From tripleo-validations with Apache License 2.0 | 5 votes |
def ip_checksum(self): generated_checksum = self._checksum(self.ip_header(checksum=0)) return socket.htons(generated_checksum)
Example #17
Source File: linux.py From dash-hack with MIT License | 5 votes |
def __init__(self, iface = None, type = ETH_P_ALL, filter=None, nofilter=0): if iface is None: iface = conf.iface self.ins = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type)) self.ins.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 0) _flush_fd(self.ins) if not nofilter: if conf.except_filter: if filter: filter = "(%s) and not (%s)" % (filter, conf.except_filter) else: filter = "not (%s)" % conf.except_filter if filter is not None: attach_filter(self.ins, filter) self.ins.bind((iface, type)) self.ins.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 2**30) self.outs = self.ins self.outs.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 2**30) sa_ll = self.outs.getsockname() if sa_ll[3] in conf.l2types: self.LL = conf.l2types[sa_ll[3]] elif sa_ll[1] in conf.l3types: self.LL = conf.l3types[sa_ll[1]] else: self.LL = conf.default_l2 warning("Unable to guess type (interface=%s protocol=%#x family=%i). Using %s" % (sa_ll[0],sa_ll[1],sa_ll[3],self.LL.name))
Example #18
Source File: inet.py From dash-hack with MIT License | 5 votes |
def answers(self, other): if not isinstance(other, IP): return 0 if not ( ((conf.checkIPsrc == 0) or (self.dst == other.dst)) and (self.src == other.src) and ( ((conf.checkIPID == 0) or (self.id == other.id) or (conf.checkIPID == 1 and self.id == socket.htons(other.id)))) and (self.proto == other.proto) ): return 0 return self.payload.answers(other.payload)
Example #19
Source File: supersocket.py From dash-hack with MIT License | 5 votes |
def __init__(self, type = ETH_P_IP, filter=None, iface=None, promisc=None, nofilter=0): self.outs = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW) self.outs.setsockopt(socket.SOL_IP, socket.IP_HDRINCL, 1) self.ins = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type)) if iface is not None: self.ins.bind((iface, type))
Example #20
Source File: test_socket.py From ironpython3 with Apache License 2.0 | 5 votes |
def testNtoHErrors(self): good_values = [ 1, 2, 3, 1, 2, 3 ] bad_values = [ -1, -2, -3, -1, -2, -3 ] for k in good_values: socket.ntohl(k) socket.ntohs(k) socket.htonl(k) socket.htons(k) for k in bad_values: self.assertRaises(OverflowError, socket.ntohl, k) self.assertRaises(OverflowError, socket.ntohs, k) self.assertRaises(OverflowError, socket.htonl, k) self.assertRaises(OverflowError, socket.htons, k)
Example #21
Source File: inet.py From mptcp-abuse with GNU General Public License v2.0 | 5 votes |
def answers(self, other): if not isinstance(other, IP): return 0 if not ( ((conf.checkIPsrc == 0) or (self.dst == other.dst)) and (self.src == other.src) and ( ((conf.checkIPID == 0) or (self.id == other.id) or (conf.checkIPID == 1 and self.id == socket.htons(other.id)))) and (self.proto == other.proto) ): return 0 return self.payload.answers(other.payload)
Example #22
Source File: plain_coap_client.py From py-air-control with MIT License | 5 votes |
def _checksum_icmp(self, source_string): countTo = (int(len(source_string) / 2)) * 2 sum = 0 count = 0 loByte = 0 hiByte = 0 while count < countTo: if sys.byteorder == "little": loByte = source_string[count] hiByte = source_string[count + 1] else: loByte = source_string[count + 1] hiByte = source_string[count] sum = sum + (hiByte * 256 + loByte) count += 2 if countTo < len(source_string): # Check for odd length loByte = source_string[len(source_string) - 1] sum += loByte sum &= 0xFFFFFFFF sum = (sum >> 16) + (sum & 0xFFFF) sum += sum >> 16 answer = ~sum & 0xFFFF answer = socket.htons(answer) return answer
Example #23
Source File: test_socket.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def testNtoHErrors(self): good_values = [ 1, 2, 3, 1, 2, 3 ] bad_values = [ -1, -2, -3, -1, -2, -3 ] for k in good_values: socket.ntohl(k) socket.ntohs(k) socket.htonl(k) socket.htons(k) for k in bad_values: self.assertRaises(OverflowError, socket.ntohl, k) self.assertRaises(OverflowError, socket.ntohs, k) self.assertRaises(OverflowError, socket.htonl, k) self.assertRaises(OverflowError, socket.htons, k)
Example #24
Source File: test_socket.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def testNtoH(self): # This just checks that htons etc. are their own inverse, # when looking at the lower 16 or 32 bits. sizes = {socket.htonl: 32, socket.ntohl: 32, socket.htons: 16, socket.ntohs: 16} for func, size in sizes.items(): mask = (1<<size) - 1 for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210): self.assertEqual(i & mask, func(func(i&mask)) & mask) swapped = func(mask) self.assertEqual(swapped & mask, mask) self.assertRaises(OverflowError, func, 1<<34)
Example #25
Source File: factory.py From yabgp with Apache License 2.0 | 5 votes |
def get_tcp_md5sig(md5_str, host, port): """set tcp md5 """ os_type = platform.system() if os_type != 'Linux': LOG.error('YABGP has no MD5 support for %s', os_type) return None # address family if netaddr.IPAddress(host).version == 4: # ipv4 address afi = AFNUM_INET elif netaddr.IPAddress(host).version == 6: # ipv6 address LOG.error("Does not support ipv6 address family") sys.exit() else: afi = None LOG.error('Peer address is not a valid ipv4/ipv6 address') try: n_port = socket.htons(port) if afi == AFNUM_INET: n_addr = socket.inet_pton(socket.AF_INET, host) tcp_md5sig = 'HH4s%dx2xH4x%ds' % ( bgp_cons.SS_PADSIZE_IPV4, bgp_cons.TCP_MD5SIG_MAXKEYLEN) md5sig = struct.pack( tcp_md5sig, socket.AF_INET, n_port, n_addr, len(md5_str), md5_str.encode()) return md5sig else: return None except socket.error as e: LOG.error('This linux machine does not support TCP_MD5SIG: (%s)', str(e)) return None
Example #26
Source File: inet6.py From scapy with GNU General Public License v2.0 | 5 votes |
def __init__(self, type=ETH_P_IPV6, filter=None, iface=None, promisc=None, nofilter=0): # noqa: E501 L3RawSocket.__init__(self, type, filter, iface, promisc) # NOTE: if fragmentation is needed, it will be done by the kernel (RFC 2292) # noqa: E501 self.outs = socket.socket(socket.AF_INET6, socket.SOCK_RAW, socket.IPPROTO_RAW) # noqa: E501 self.ins = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type)) # noqa: E501
Example #27
Source File: linux.py From mptcp-abuse with GNU General Public License v2.0 | 5 votes |
def __init__(self, iface = None, type = ETH_P_ALL, filter=None, nofilter=0): if iface is None: iface = conf.iface self.ins = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type)) self.ins.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 0) _flush_fd(self.ins) if not nofilter: if conf.except_filter: if filter: filter = "(%s) and not (%s)" % (filter, conf.except_filter) else: filter = "not (%s)" % conf.except_filter if filter is not None: attach_filter(self.ins, filter) self.ins.bind((iface, type)) self.ins.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 2**30) self.outs = self.ins self.outs.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 2**30) sa_ll = self.outs.getsockname() if sa_ll[3] in conf.l2types: self.LL = conf.l2types[sa_ll[3]] elif sa_ll[1] in conf.l3types: self.LL = conf.l3types[sa_ll[1]] else: self.LL = conf.default_l2 warning("Unable to guess type (interface=%s protocol=%#x family=%i). Using %s" % (sa_ll[0],sa_ll[1],sa_ll[3],self.LL.name))
Example #28
Source File: linux.py From mptcp-abuse with GNU General Public License v2.0 | 5 votes |
def __init__(self, type = ETH_P_ALL, filter=None, promisc=None, iface=None, nofilter=0): self.type = type self.ins = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type)) self.ins.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 0) _flush_fd(self.ins) if iface: self.ins.bind((iface, type)) if not nofilter: if conf.except_filter: if filter: filter = "(%s) and not (%s)" % (filter, conf.except_filter) else: filter = "not (%s)" % conf.except_filter if filter is not None: attach_filter(self.ins, filter) self.ins.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 2**30) self.outs = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type)) self.outs.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 2**30) if promisc is None: promisc = conf.promisc self.promisc = promisc if self.promisc: if iface is None: self.iff = get_if_list() else: if iface.__class__ is list: self.iff = iface else: self.iff = [iface] for i in self.iff: set_promisc(self.ins, i)
Example #29
Source File: inet6.py From mptcp-abuse with GNU General Public License v2.0 | 5 votes |
def __init__(self, type = ETH_P_IPV6, filter=None, iface=None, promisc=None, nofilter=0): L3RawSocket.__init__(self, type, filter, iface, promisc) # NOTE: if fragmentation is needed, it will be done by the kernel (RFC 2292) self.outs = socket.socket(socket.AF_INET6, socket.SOCK_RAW, socket.IPPROTO_RAW) self.ins = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type))
Example #30
Source File: inet6.py From CVE-2016-6366 with MIT License | 5 votes |
def __init__(self, type = ETH_P_IPV6, filter=None, iface=None, promisc=None, nofilter=0): L3RawSocket.__init__(self, type, filter, iface, promisc) # NOTE: if fragmentation is needed, it will be done by the kernel (RFC 2292) self.outs = socket.socket(socket.AF_INET6, socket.SOCK_RAW, socket.IPPROTO_RAW) self.ins = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type))