From b1fc4e59c7c752e4f419803da109bfa6f96cd619 Mon Sep 17 00:00:00 2001 From: Bastard Operator from Hell <5u623l20@protonmail.com> Date: Tue, 19 Mar 2019 17:55:02 +0000 Subject: [PATCH] Fix Strings and Bytes --- python/dnet.pyx | 86 ++++++++++++++++++++++++------------------------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/python/dnet.pyx b/python/dnet.pyx index 72a5defb..669ae0d8 100644 --- a/python/dnet.pyx +++ b/python/dnet.pyx @@ -15,15 +15,15 @@ and raw IP packet and Ethernet frame transmission. __author__ = 'Dug Song ' __copyright__ = 'Copyright (c) 2003 Dug Song' __license__ = 'BSD' -__url__ = 'http://github.com/5u623l20/libdnet' -__version__ = '1.13' +__url__ = 'http://libdnet.sourceforge.net/' +__version__ = '1.12' cdef extern from "dnet.h": pass cdef extern from "Python.h": - object PyString_FromStringAndSize(char *s, int len) - int PyString_Size(object o) + object PyBytes_FromStringAndSize(char *s, int len) + int PyBytes_Size(object o) int PyObject_AsReadBuffer(object o, char **pp, int *lenp) int PyInt_Check(object o) int PyLong_Check(object o) @@ -41,7 +41,7 @@ cdef extern from *: unsigned long ntohl(unsigned long n) cdef __memcpy(char *dst, object src, int n): - if PyString_Size(src) != n: + if PyBytes_Size(src) != n: raise ValueError, "not a %d-byte binary string: %r" % (n, src) memcpy(dst, src, n) @@ -96,8 +96,8 @@ ETH_TYPE_PPPOEDISC = 0x8863 # /* PPP Over Ethernet Discovery Stage */ ETH_TYPE_PPPOE = 0x8864 # /* PPP Over Ethernet Session Stage */ ETH_TYPE_LOOPBACK = 0x9000 # /* used to test interfaces */ -ETH_ADDR_UNSPEC = PyString_FromStringAndSize("\x00\x00\x00\x00\x00\x00", 6) -ETH_ADDR_BROADCAST = PyString_FromStringAndSize("\xff\xff\xff\xff\xff\xff", 6) +ETH_ADDR_UNSPEC = PyBytes_FromStringAndSize("\x00\x00\x00\x00\x00\x00", 6) +ETH_ADDR_BROADCAST = PyBytes_FromStringAndSize("\xff\xff\xff\xff\xff\xff", 6) cdef class eth: """eth(device) -> Ethernet device object @@ -117,7 +117,7 @@ cdef class eth: cdef eth_addr_t ea if eth_get(self.eth, &ea) < 0: raise OSError, __oserror() - return PyString_FromStringAndSize(ea.data, 6) + return PyBytes_FromStringAndSize(ea.data, 6) def set(self, value): """Set the MAC address for the device, returning 0 on success, @@ -138,7 +138,7 @@ cdef class eth: Arguments: frame -- binary string representing an Ethernet frame """ - return eth_send(self.eth, frame, PyString_Size(frame)) + return eth_send(self.eth, frame, PyBytes_Size(frame)) def __dealloc__(self): if self.eth: @@ -157,7 +157,7 @@ def eth_aton(buf): cdef eth_addr_t ea if __eth_aton(buf, &ea) < 0: raise ValueError, "invalid Ethernet address" - return PyString_FromStringAndSize(ea.data, 6) + return PyBytes_FromStringAndSize(ea.data, 6) def eth_pack_hdr(dst=ETH_ADDR_BROADCAST, src=ETH_ADDR_BROADCAST, type=ETH_TYPE_IP): @@ -173,7 +173,7 @@ def eth_pack_hdr(dst=ETH_ADDR_BROADCAST, src=ETH_ADDR_BROADCAST, __memcpy(s.data, src, 6) __memcpy(d.data, dst, 6) __eth_pack_hdr(hdr, d, s, type) - return PyString_FromStringAndSize(hdr, 14) + return PyBytes_FromStringAndSize(hdr, 14) # # ip.h @@ -231,11 +231,11 @@ IP_PROTO_RAW = 255 # /* Raw IP packets */ IP_PROTO_RESERVED = IP_PROTO_RAW # /* Reserved */ IP_PROTO_MAX = 255 -IP_ADDR_ANY = PyString_FromStringAndSize("\x00\x00\x00\x00", 4) -IP_ADDR_BROADCAST = PyString_FromStringAndSize("\xff\xff\xff\xff", 4) -IP_ADDR_LOOPBACK = PyString_FromStringAndSize("\x7f\x00\x00\x01", 4) -IP_ADDR_MCAST_ALL = PyString_FromStringAndSize("\xe0\x00\x00\x01", 4) -IP_ADDR_MCAST_LOCAL = PyString_FromStringAndSize("\xe0\x00\x00\xff", 4) +IP_ADDR_ANY = PyBytes_FromStringAndSize("\x00\x00\x00\x00", 4) +IP_ADDR_BROADCAST = PyBytes_FromStringAndSize("\xff\xff\xff\xff", 4) +IP_ADDR_LOOPBACK = PyBytes_FromStringAndSize("\x7f\x00\x00\x01", 4) +IP_ADDR_MCAST_ALL = PyBytes_FromStringAndSize("\xe0\x00\x00\x01", 4) +IP_ADDR_MCAST_LOCAL = PyBytes_FromStringAndSize("\xe0\x00\x00\xff", 4) cdef class ip: """ip() -> Raw IP object @@ -256,7 +256,7 @@ cdef class ip: Arguments: pkt -- binary string representing an IP packet """ - return ip_send(self.ip, pkt, PyString_Size(pkt)) + return ip_send(self.ip, pkt, PyBytes_Size(pkt)) def __dealloc__(self): if self.ip: @@ -281,7 +281,7 @@ def ip_aton(buf): cdef ip_addr_t ia if __ip_aton(buf, &ia) < 0: raise ValueError, "invalid IP address" - return PyString_FromStringAndSize(&ia, 4) + return PyBytes_FromStringAndSize(&ia, 4) def ip_checksum(pkt): """Return packed binary string representing an IP packet @@ -297,11 +297,11 @@ def ip_checksum(pkt): if n < 2048: memcpy(buf, p, n) __ip_checksum(buf, n) - return PyString_FromStringAndSize(buf, n) + return PyBytes_FromStringAndSize(buf, n) p = malloc(n) memcpy(p, pkt, n) __ip_checksum(p, n) - s = PyString_FromStringAndSize(p, n) + s = PyBytes_FromStringAndSize(p, n) free(p) return s raise TypeError @@ -337,7 +337,7 @@ def ip_pack_hdr(tos=IP_TOS_DEFAULT, len=IP_HDR_LEN, id=0, off=0, __memcpy(&s, src, 4) __memcpy(&d, dst, 4) __ip_pack_hdr(hdr, tos, len, id, off, ttl, p, s, d) - return PyString_FromStringAndSize(hdr, 20) + return PyBytes_FromStringAndSize(hdr, 20) # # ip6.h @@ -364,8 +364,8 @@ IP6_MTU_MIN = 1280 # /* minimum MTU (1024 + 256) */ IP6_HLIM_DEFAULT=64 IP6_HLIM_MAX = 255 -IP6_ADDR_UNSPEC = PyString_FromStringAndSize("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16) -IP6_ADDR_LOOPBACK = PyString_FromStringAndSize("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", 16) +IP6_ADDR_UNSPEC = PyBytes_FromStringAndSize("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16) +IP6_ADDR_LOOPBACK = PyBytes_FromStringAndSize("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", 16) def ip6_ntoa(buf): """Convert an IPv6 address from a 16-byte packed binary string to a @@ -380,7 +380,7 @@ def ip6_aton(buf): cdef ip6_addr_t ia if __ip6_aton(buf, &ia) < 0: raise ValueError, "invalid IPv6 address" - return PyString_FromStringAndSize(&ia, 16) + return PyBytes_FromStringAndSize(&ia, 16) def ip6_checksum(buf): """Return a packed binary string representing an IPv6 packet @@ -389,7 +389,7 @@ def ip6_checksum(buf): Arguments: pkt -- binary string representing an IPv6 packet """ - __ip6_checksum(buf, PyString_Size(buf)) + __ip6_checksum(buf, PyBytes_Size(buf)) return buf def ip6_pack_hdr(fc=0, fl=0, plen=0, nxt=IP_PROTO_IPV6, hlim=IP6_HLIM_DEFAULT, @@ -410,7 +410,7 @@ def ip6_pack_hdr(fc=0, fl=0, plen=0, nxt=IP_PROTO_IPV6, hlim=IP6_HLIM_DEFAULT, __memcpy(&s, src, 16) __memcpy(&d, dst, 16) __ip6_pack_hdr(hdr, fc, fl, plen, nxt, hlim, s, d) - return PyString_FromStringAndSize(hdr, 40) + return PyBytes_FromStringAndSize(hdr, 40) # # addr.h @@ -443,7 +443,7 @@ cdef class addr: def __init__(self, addrtxt=None, type=ADDR_TYPE_NONE): if addrtxt != None and addr_aton(addrtxt, &self._addr) < 0: - if PyString_Size(addrtxt) == 4: + if PyBytes_Size(addrtxt) == 4: self._addr.addr_type = ADDR_TYPE_IP self._addr.addr_bits = IP_ADDR_BITS self.ip = addrtxt @@ -483,10 +483,10 @@ cdef class addr: def __get__(self): if self._addr.addr_type != ADDR_TYPE_ETH: raise ValueError, "non-Ethernet address" - return PyString_FromStringAndSize(self._addr.addr_data8, 6) + return PyBytes_FromStringAndSize(self._addr.addr_data8, 6) def __set__(self, value): - if PyString_Size(value) != ETH_ADDR_LEN: + if PyBytes_Size(value) != ETH_ADDR_LEN: raise ValueError, "not a 6-byte string" __memcpy(self._addr.addr_data8, value, 6) self._addr.addr_type = ADDR_TYPE_ETH @@ -497,7 +497,7 @@ cdef class addr: def __get__(self): if self._addr.addr_type != ADDR_TYPE_IP: raise ValueError, "non-IP address" - return PyString_FromStringAndSize(self._addr.addr_data8, 4) + return PyBytes_FromStringAndSize(self._addr.addr_data8, 4) def __set__(self, value): # XXX - handle < 2.3, or else we'd use PyInt_AsUnsignedLongMask() @@ -505,7 +505,7 @@ cdef class addr: self._addr.addr_ip = htonl(PyInt_AsLong(value)) elif PyLong_Check(value): self._addr.addr_ip = htonl(PyLong_AsUnsignedLong(value)) - elif PyString_Size(value) != IP_ADDR_LEN: + elif PyBytes_Size(value) != IP_ADDR_LEN: raise ValueError, "not a 4-byte string" else: __memcpy(self._addr.addr_data8, value, 4) @@ -517,10 +517,10 @@ cdef class addr: def __get__(self): if self._addr.addr_type != ADDR_TYPE_IP6: raise ValueError, "non-IPv6 address" - return PyString_FromStringAndSize(self._addr.addr_data8, 16) + return PyBytes_FromStringAndSize(self._addr.addr_data8, 16) def __set__(self, value): - if PyString_Size(value) != IP6_ADDR_LEN: + if PyBytes_Size(value) != IP6_ADDR_LEN: raise ValueError, "not a 16-byte string" __memcpy(self._addr.addr_data8, value, 16) self._addr.addr_type = ADDR_TYPE_IP6 @@ -777,7 +777,7 @@ def arp_pack_hdr_ethip(op=ARP_OP_REQUEST, __memcpy(&sp, spa, 4) __memcpy(&dp, dpa, 4) __arp_pack_hdr_ethip(buf, op, sh, sp, dh, dp) - return PyString_FromStringAndSize(buf, 28) + return PyBytes_FromStringAndSize(buf, 28) # # icmp.h @@ -794,7 +794,7 @@ def icmp_pack_hdr(type, code): """ cdef char buf[4] __icmp_pack_hdr(buf, type, code) - return PyString_FromStringAndSize(buf, sizeof(buf)) + return PyBytes_FromStringAndSize(buf, sizeof(buf)) # # tcp.h @@ -860,7 +860,7 @@ def tcp_pack_hdr(sport, dport, seq=1, ack=0, flags=TH_SYN, """ cdef char buf[20] __tcp_pack_hdr(buf, sport, dport, seq, ack, flags, win, urp) - return PyString_FromStringAndSize(buf, sizeof(buf)) + return PyBytes_FromStringAndSize(buf, sizeof(buf)) # # udp.h @@ -881,7 +881,7 @@ def udp_pack_hdr(sport, dport, ulen=UDP_HDR_LEN): """ cdef char buf[8] __udp_pack_hdr(buf, sport, dport, ulen) - return PyString_FromStringAndSize(buf, sizeof(buf)) + return PyBytes_FromStringAndSize(buf, sizeof(buf)) # # intf.h @@ -1333,10 +1333,10 @@ cdef class rand: cdef char *p if len <= 1024: rand_get(self.rand, buf, len) - return PyString_FromStringAndSize(buf, len) + return PyBytes_FromStringAndSize(buf, len) p = malloc(len) rand_get(self.rand, p, len) - s = PyString_FromStringAndSize(p, len) + s = PyBytes_FromStringAndSize(p, len) free(p) return s @@ -1346,7 +1346,7 @@ cdef class rand: Arguments: string -- binary string seed value """ - rand_set(self.rand, buf, PyString_Size(buf)) + rand_set(self.rand, buf, PyBytes_Size(buf)) def add(self, buf): """Add additional entropy into the PRNG mix. @@ -1354,7 +1354,7 @@ cdef class rand: Arguments: string -- binary string """ - rand_add(self.rand, buf, PyString_Size(buf)) + rand_add(self.rand, buf, PyBytes_Size(buf)) def uint8(self): """Return a random 8-bit integer.""" @@ -1515,7 +1515,7 @@ cdef class tun: Arguments: pkt -- binary string representing an IP packet """ - return tun_send(self.tun, pkt, PyString_Size(pkt)) + return tun_send(self.tun, pkt, PyBytes_Size(pkt)) def recv(self): """Return the next packet delivered to the tunnel interface.""" @@ -1523,7 +1523,7 @@ cdef class tun: n = tun_recv(self.tun, self.buf, self.mtu) if n < 0: raise OSError, __oserror() - return PyString_FromStringAndSize(self.buf, n) + return PyBytes_FromStringAndSize(self.buf, n) def close(self): self.tun = tun_close(self.tun)