def command(broker, sock, env, cmd, rsp): url = cmd.get_attr('IPR_ATTR_HOST') key = cmd.get_attr('IPR_ATTR_SSL_KEY') cert = cmd.get_attr('IPR_ATTR_SSL_CERT') ca = cmd.get_attr('IPR_ATTR_SSL_CA') (new_sock, addr) = get_socket(url, server=True, key=key, cert=cert, ca=ca) new_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) new_sock.bind(addr) if new_sock.type == socket.SOCK_STREAM: new_sock.listen(16) broker.ioloop.register(new_sock, broker.handle_connect) else: broker.ioloop.register(new_sock, broker.route, defer=True) broker.servers.add(new_sock) broker._rlist.add(new_sock) broker.sockets[url] = new_sock
def command(broker, sock, env, cmd, rsp): url = cmd.get_attr('IPR_ATTR_HOST') key = cmd.get_attr('IPR_ATTR_SSL_KEY') cert = cmd.get_attr('IPR_ATTR_SSL_CERT') ca = cmd.get_attr('IPR_ATTR_SSL_CA') target = urlparse.urlparse(url) peer = broker.addr remote = False established = False uid = str(uuid.uuid4()) route = broker.route if url in broker.providers: new_sock = broker.providers[url] established = True gate = lambda d, s:\ new_sock.send(broker.gate_forward(d, s)) elif target.scheme == 'netlink': res = target.path.split("/") new_sock = NetlinkSocket(int(res[1])) new_sock.bind(int(res[2])) gate = lambda d, s:\ new_sock.sendto(broker.gate_untag(d, s), (0, 0)) route = broker.route_netlink elif target.scheme == 'udp': (new_sock, addr) = get_socket(url, server=False) gate = lambda d, s:\ new_sock.sendto(broker.gate_forward(d, s), addr) remote = True else: (new_sock, addr) = get_socket(url, server=False, key=key, cert=cert, ca=ca) try: new_sock.connect(addr) except Exception: new_sock.close() raise remote = True # stream sockets provide the peer announce buf = io.BytesIO() buf.length = buf.write(new_sock.recv(16384)) buf.seek(0) msg = envmsg(buf) msg.decode() buf = io.BytesIO() buf.length = buf.write(msg.get_attr('IPR_ATTR_CDATA')) buf.seek(0) msg = mgmtmsg(buf) msg.decode() peer = msg.get_attr('IPR_ATTR_ADDR') gate = lambda d, s:\ new_sock.send(broker.gate_forward(d, s)) port = broker.alloc_addr() link = broker.register_link(uid=uid, port=port, sock=new_sock, established=established, remote=remote) link.gate = gate broker.discover[target.path] = port rsp['attrs'].append(['IPR_ATTR_UUID', uid]) rsp['attrs'].append(['IPR_ATTR_ADDR', peer]) try: broker.ioloop.register(new_sock, route, defer=True) except Exception as e: if e.errno != errno.EEXIST: raise e