summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorenzo Colitti <lorenzo@google.com>2016-01-06 17:07:19 +0900
committerLorenzo Colitti <lorenzo@google.com>2016-01-06 17:20:50 +0900
commitda3aa4c1e092b6cddc2e3b64ddde24777fedd931 (patch)
treed5cc7c6483e61844bd6bdda239f8c45003d1bc1e
parent09f36abb939ebad430a8b95beda70577c8fc1fa2 (diff)
downloadextras-da3aa4c1e092b6cddc2e3b64ddde24777fedd931.tar.gz
Make getsockopt(SO_BINDTODEVICE) actually work.
Also change "16" to "IFNAMSIZ" in various places. Change-Id: Ib22c419e77dabf3bc6b1137b774daf5313b5ffeb
-rwxr-xr-xtests/net_test/net_test.py14
-rwxr-xr-xtests/net_test/sock_diag.py4
2 files changed, 11 insertions, 7 deletions
diff --git a/tests/net_test/net_test.py b/tests/net_test/net_test.py
index f108aa8f..d7ea0130 100755
--- a/tests/net_test/net_test.py
+++ b/tests/net_test/net_test.py
@@ -56,6 +56,8 @@ IPV6_FL_S_NONE = 0
IPV6_FL_S_EXCL = 1
IPV6_FL_S_ANY = 255
+IFNAMSIZ = 16
+
IPV4_PING = "\x08\x00\x00\x00\x0a\xce\x00\x03"
IPV6_PING = "\x80\x00\x00\x00\x0a\xce\x00\x03"
@@ -171,9 +173,9 @@ def CreateSocketPair(family, socktype, addr):
def GetInterfaceIndex(ifname):
s = IPv4PingSocket()
- ifr = struct.pack("16si", ifname, 0)
+ ifr = struct.pack("%dsi" % IFNAMSIZ, ifname, 0)
ifr = fcntl.ioctl(s, scapy.SIOCGIFINDEX, ifr)
- return struct.unpack("16si", ifr)[1]
+ return struct.unpack("%dsi" % IFNAMSIZ, ifr)[1]
def SetInterfaceHWAddr(ifname, hwaddr):
@@ -182,20 +184,20 @@ def SetInterfaceHWAddr(ifname, hwaddr):
hwaddr = hwaddr.decode("hex")
if len(hwaddr) != 6:
raise ValueError("Unknown hardware address length %d" % len(hwaddr))
- ifr = struct.pack("16sH6s", ifname, scapy.ARPHDR_ETHER, hwaddr)
+ ifr = struct.pack("%dsH6s" % IFNAMSIZ, ifname, scapy.ARPHDR_ETHER, hwaddr)
fcntl.ioctl(s, SIOCSIFHWADDR, ifr)
def SetInterfaceState(ifname, up):
s = IPv4PingSocket()
- ifr = struct.pack("16sH", ifname, 0)
+ ifr = struct.pack("%dsH" % IFNAMSIZ, ifname, 0)
ifr = fcntl.ioctl(s, scapy.SIOCGIFFLAGS, ifr)
- _, flags = struct.unpack("16sH", ifr)
+ _, flags = struct.unpack("%dsH" % IFNAMSIZ, ifr)
if up:
flags |= scapy.IFF_UP
else:
flags &= ~scapy.IFF_UP
- ifr = struct.pack("16sH", ifname, flags)
+ ifr = struct.pack("%dsH" % IFNAMSIZ, ifname, flags)
ifr = fcntl.ioctl(s, scapy.SIOCSIFFLAGS, ifr)
diff --git a/tests/net_test/sock_diag.py b/tests/net_test/sock_diag.py
index 5fa7a7d5..69785aa6 100755
--- a/tests/net_test/sock_diag.py
+++ b/tests/net_test/sock_diag.py
@@ -196,7 +196,9 @@ class SockDiag(netlink.NetlinkSocket):
family = s.getsockopt(net_test.SOL_SOCKET, net_test.SO_DOMAIN)
protocol = s.getsockopt(net_test.SOL_SOCKET, net_test.SO_PROTOCOL)
if net_test.LINUX_VERSION >= (3, 8):
- iface = s.getsockopt(SOL_SOCKET, net_test.SO_BINDTODEVICE)
+ iface = s.getsockopt(SOL_SOCKET, net_test.SO_BINDTODEVICE,
+ net_test.IFNAMSIZ)
+ iface = GetInterfaceIndex(iface) if iface else 0
else:
iface = 0
src, sport = s.getsockname()[:2]