def recv(self, block=True): """ Read data from socket. :returns: bytestring containing received data. """ flags = 0 if not block: flags = MSG_DONTWAIT buf = recv_all(self.sock, self.COOKIE_LEN + 5, flags) if not buf: return None, None cookie, addr_type, packet_len = struct.unpack("=8sBI", buf) if cookie != self.COOKIE: logging.critical("Dispatcher socket out of sync") raise SCIONIOError port_len = 0 if addr_type != AddrType.NONE: port_len = 2 addr_len = haddr_get_type(addr_type).LEN # We know there is data coming, block here to avoid sync problems. buf = recv_all(self.sock, addr_len + port_len + packet_len, 0) if addr_len > 0: addr = buf[:addr_len] port = buf[addr_len:addr_len + port_len] sender = (str(ipaddress.ip_address(addr)), port) else: addr = "" port = 0 sender = (None, None) packet = buf[addr_len + port_len:] return packet, sender
def get_lwip_reply(sock): try: raw_len = recv_all(sock, PLD_SIZE, 0) except OSError as e: raise SCIONTCPError("Cannot read from LWIP's socket: %s" % e) if not raw_len: return None pld_len, = struct.unpack("H", raw_len) try: return recv_all(sock, RESP_SIZE + pld_len, 0) except OSError as e: raise SCIONTCPError("Cannot read from LWIP's socket: %s" % e)
def client(svc, counter): def get_path_info(myaddr, dst_isd_as): sd = start_sciond(myaddr) path = sd.get_paths(dst_isd_as)[0] if_id = path.get_fwd_if() return (path, sd.ifid2br[if_id].addr, sd.ifid2br[if_id].port) print("client %d running:" % counter) s = SCIONTCPSocket() caddr = SCIONAddr.from_values(c_isd_as, c_ip) s.bind((caddr, 0)) path_info = get_path_info(caddr, s_isd_as) print(path_info) if svc: saddr = SCIONAddr.from_values(s_isd_as, SVCType.PS_A) s.connect(saddr, 0, *path_info) # SVC does not have a port specified else: saddr = SCIONAddr.from_values(s_isd_as, s_ip) s.connect(saddr, 5000, *path_info) # s.set_recv_tout(5.0) # print(s.get_recv_tout()) start = time.time() size = struct.unpack("!I", recv_all(s, 4, 0))[0] tmp = b'' print("To receive: %dB" % size) while len(tmp) != size: tmp += s.recv(1024) print('.', end="", flush=True) print("\nMSG received, len, svc", len(tmp), svc) time_elapsed = time.time()-start print("Time elapsed: %s, speed %.2fkB/s\n" % (time_elapsed, size/time_elapsed/1000)) s.close()
def recv_msg(self): size = struct.unpack("!I", recv_all(self.sock, 4, 0))[0] raw = recv_all(self.sock, size, 0) return build_msg(raw)