Esempio n. 1
0
def PyNodeWirePacketSendFunction(zt_node_ptr, uptr, tptr, localSocket, addr,
                                 data, dataLen, ttl):
    node = get_node_by_zt_node_ptr(zt_node_ptr)
    if node is None:
        node = get_node_by_tptr(tptr)
    assert node is not None
    if node.stopping:
        return -1
    #if localAddr.ss_family != 0:
    #    try:
    #        print('localAddr tuple', sockaddr_storage_to_addrtuple(ffi, localAddr))
    #    except Exception, e:
    #        print(e)
    addr_in = ffi.cast("struct sockaddr_in*", addr)
    try:
        addr_tuple = sockaddr_storage_to_addrtuple(ffi, addr)
    except Exception as e:
        print(e)
    fdata = ffi.buffer(data, dataLen)
    if addr.ss_family == socket.AF_INET:
        node.udp_server.udp_sock.sendto(fdata[:], addr_tuple)
        node.sent.append((addr_tuple, binascii.hexlify(fdata)))
        if len(node.sent) > node.max_sent:
            del node.sent[0]
    else:
        #print("skipped sending to addr.ss_family:", addr.ss_family)
        pass
    return 0
Esempio n. 2
0
    def recvfrom_loop(self):
        global stopping
        while not self.node.stopping:
            try:
                data, ip_port = self.udp_sock.recvfrom(4096 * 3)
                #print('on_read', ip_port, handle.getsockname())
                if data is None:
                    return
                #print('on_read data hex', binascii.hexlify(data))
            except socket.error:
                if self.node.stopping:
                    break
                else:
                    raise
            if self.node.stopping:
                break
            self.node.received.append((ip_port, binascii.hexlify(data)))
            if len(self.node.received) > self.node.max_received:
                del self.node.received[0]
            remoteAddress_in = addrtuple_to_sockaddr(ffi, socket.AF_INET,
                                                     ip_port)
            remoteAddress = ffi.cast("struct sockaddr_storage*",
                                     remoteAddress_in)

            result = zt.ZT_Node_processWirePacket(
                self.node.zt_node_ptr, self.node.tptr, get_now(), 0,
                remoteAddress, data, len(data),
                self.node.nextBackgroundTaskDeadline)

        if stopping:
            self.node.delete()
            self.stopping = False
Esempio n. 3
0
def PyNodeEventCallback(zt_node_ptr, uptr, tptr, event, metaData):
    node = get_node_by_zt_node_ptr(zt_node_ptr)
    if node is None:
        node = get_node_by_tptr(tptr)
        assert node is not None
    if event == zt.ZT_EVENT_UP:
        print("Node %s has been initialized" % (node.name))
        node.zt_node_ptr = zt_node_ptr
        add_node_by_zt_node_ptr(node)
        node.state = 'initialized'
        node.on_initialized()
        node.address = node.status().address
        node.ztid = format(node.address, 'x')
    elif event == zt.ZT_EVENT_OFFLINE:
        print("Node %s is offline" % (node.name))
        node.state = 'offline'
    elif event == zt.ZT_EVENT_ONLINE:
        print("Node %s is online" % (node.name))
        node.state = 'online'
        node.on_online()
    elif event == zt.ZT_EVENT_DOWN:
        node.state = 'shutting_down'
        print("Node %s is shutting down" % (node.name))
    elif event == zt.ZT_EVENT_FATAL_ERROR_IDENTITY_COLLISION:
        node.state = 'identity_collision'
        print(
            "Your identity has collided with another node's ZeroTier address")
    elif event == zt.ZT_EVENT_TRACE:
        s = ffi.cast("char *", metaData)
        #print("Trace message: ", ffi.string(s))
        node.traced.append(ffi.string(s))
        if len(node.traced) > node.max_traced:
            del node.traced[0]
    elif event == zt.ZT_EVENT_USER_MESSAGE:
        msg = ffi.cast("ZT_UserMessage *", metaData)
        node.handle_message(msg)
    else:
        print("Unknown event", event)
Esempio n. 4
0
def add_node_by_zt_node_ptr(node):
    global nodes_by_uint_node_ptr
    uint_ptr = ffi.cast("uintptr_t", node.zt_node_ptr)
    assert (uint_ptr not in nodes_by_uint_tptr)
    nodes_by_uint_node_ptr[uint_ptr] = node
Esempio n. 5
0
def get_node_by_zt_node_ptr(zt_node_ptr):
    global nodes_by_uint_node_ptr
    node = nodes_by_uint_node_ptr.get(ffi.cast("uintptr_t", zt_node_ptr))
    return node