示例#1
0
static void
read_peer_id (CcnetHandshake *handshake, ccnet_packet *packet)
{
    uint16_t len;
    char *id;

    /* get id */
    len = packet->header.length;
    id = g_malloc (len + 1);
    memcpy (id, packet->data, len);
    id[len] = '\0';
    handshake->id = id;

    if (handshake->state == INIT) {
        /* we are the slave */
        ccnet_debug ("[Conn] Incoming: Read peer id %.8s\n", id);
        send_handshake_message (handshake);
        handshake->state = ID_RECEIVED;
    } else if (handshake->state == ID_SENT) {
        /* we are the master */
        ccnet_debug ("[Conn] Outgoing: Read peer %s id %.8s\n",
                     handshake->peer->name, id);
        send_ack (handshake);
        ccnet_handshake_done (handshake, TRUE);
    } else
        g_assert (0);
}
示例#2
0
static void verify_challenge(CcnetProcessor *processor, 
                             char *code, char *code_msg,
                             char *content, int clen)
{
    CcnetKeepaliveProcPriv *priv = GET_PRIV (processor);
    ccnet_debug ("[Conn] Verify Peer Challenge\n");

    if (clen != 40 || memcmp(content, priv->random_buf, 40) != 0) {
        ccnet_debug ("[Conn] Peer Challenge failed\n");
        close_processor(processor);
        return;
    }

    CcnetUser *user = ccnet_peer_get_user(processor->peer);
    if (!user) {
        ccnet_debug ("[Conn] No user for this peer, go to auth done\n");
        processor->peer->auth_done = 1;
        g_signal_emit_by_name (processor->peer, "auth-done");
        
        send_keepalive (processor);
        reset_timeout (processor);
        return;
    }

    if (user->pubkey)
        send_challenge_user(processor, user);
    else
        get_pubkey_user(processor);
}
示例#3
0
int
ccnet_session_register_service (CcnetSession *session,
                                const char *svc_name,
                                const char *group,
                                CcnetPeer *peer)
{
    CcnetService *service = g_new0 (CcnetService, 1);

    g_assert (peer->is_local);
    g_assert (group);

    if (g_hash_table_lookup (session->service_hash, svc_name)) {
        ccnet_debug ("[Service] Service %s has already been registered\n",
                     svc_name);
        return -1;
    }
    ccnet_debug ("[Service] Service %s registered\n", svc_name);

    ccnet_perm_manager_register_service (session->perm_mgr, svc_name,
                                         group, peer);

    service->svc_name = g_strdup(svc_name);
    service->provider = peer;

    g_hash_table_insert (session->service_hash, g_strdup(svc_name), service);

    return 0;
}
示例#4
0
文件: peer.c 项目: andrey-str/ccnet
void
ccnet_peer_set_net_state (CcnetPeer *peer, int net_state)
{
    /* do not need saving */

    if (peer->net_state == net_state)
        return;

    ccnet_debug ("[Peer] Peer %s(%.8s) net state changed: %s->%s\n",
                 peer->name, peer->id,
                 ccnet_peer_get_net_state_string(peer->net_state),
                 ccnet_peer_get_net_state_string(net_state));
    peer->last_net_state = peer->net_state;

    if (net_state == PEER_DOWN) {
        if (!peer->is_local)
            --peer->manager->connected_peer;
    } else
        if (!peer->is_local)
            ++peer->manager->connected_peer;

    if (net_state == PEER_CONNECTED && !peer->io->is_incoming)
        g_object_set (peer, "can-connect", 1, NULL);

    g_object_set (peer, "net-state", net_state, NULL);
}
示例#5
0
文件: peer.c 项目: andrey-str/ccnet
static void
create_local_processor (CcnetPeer *peer, int req_id, int argc, char **argv)
{
    CcnetProcessor *processor;
    CcnetProcFactory *factory = peer->manager->session->proc_factory;

    processor = ccnet_proc_factory_create_slave_processor (
        factory, argv[0], peer, req_id);

    if (processor) {
        ccnet_processor_start (processor, argc-1, argv+1);
    } else {
        CcnetService *service;

        service = ccnet_session_get_service (peer->manager->session, argv[0]);
        if (service != NULL) {
            processor = ccnet_proc_factory_create_slave_processor (
                factory, "service-proxy", peer, req_id);
            ccnet_processor_start (processor, 0, NULL);
            ccnet_service_proxy_invoke_local (processor, service->provider,
                                              argc, argv);
        } else {
            ccnet_peer_send_response (peer, req_id, SC_UNKNOWN_SERVICE,
                                    SS_UNKNOWN_SERVICE,
                                    NULL, 0);
            ccnet_debug ("Unknown service %s invoke by %s(%.8s)\n",
                           argv[0], peer->name, peer->id);
        }
    }
}
示例#6
0
static int
load_database_config (CcnetSession *session)
{
    int ret;
    char *engine;

    engine = ccnet_key_file_get_string (session->keyf, "Database", "ENGINE");
    if (!engine || strncasecmp (engine, DB_SQLITE, sizeof(DB_SQLITE)) == 0) {
        ccnet_debug ("Use database sqlite\n");
        ret = init_sqlite_database (session);
    } else if (strncasecmp (engine, DB_MYSQL, sizeof(DB_MYSQL)) == 0) {
        ccnet_debug ("Use database Mysql\n");
        ret = init_mysql_database (session);
    }
    return ret;
}
示例#7
0
文件: peer.c 项目: andrey-str/ccnet
static void
_peer_shutdown (CcnetPeer *peer)
{
    peer->in_shutdown = 1;

    if (peer->net_state == PEER_CONNECTED) {
        peer->last_down = time(NULL);
        ccnet_packet_io_free (peer->io);
        peer->io = NULL;
        g_object_set (peer, "can-connect", 0, NULL);
    }
    peer->is_ready = 0;
    g_free (peer->dns_addr);
    peer->dns_addr = NULL;
    peer->dns_done = 0;

    /* clear session key when peer down  */
    peer->encrypt_channel = 0;
    g_free (peer->session_key);
    peer->session_key = NULL;

    ccnet_debug ("Shutdown all processors for peer %s\n", peer->name);
    shutdown_processors (peer);
    remove_write_callbacks (peer);

    ccnet_peer_set_net_state (peer, PEER_DOWN);

    g_signal_emit (peer, signals[DOWN_SIG], 0);

    peer->in_shutdown = 0;
}
示例#8
0
void ccnet_processor_handle_response (CcnetProcessor *processor, 
                                      char *code, char *code_msg,
                                      char *content, int clen)
{
    if ((code[0] == '5' || code[0] == '4') &&
        !CCNET_IS_KEEPALIVE2_PROC(processor))
    {
        ccnet_warning ("[Proc] peer %.10s, Shutdown processor %s(%d) for bad response: %s %s\n",
                       processor->peer->id, GET_PNAME(processor), PRINT_ID(processor->id),
                       code, code_msg);

        /* Stub proc should relay the message before it shuts down. */
        if (CCNET_IS_SERVICE_PROXY_PROC(processor) ||
            CCNET_IS_SERVICE_STUB_PROC(processor)) {
            CCNET_PROCESSOR_GET_CLASS (processor)->handle_response (
                processor, code, code_msg, content, clen);
        }

        if (memcmp(code, SC_UNKNOWN_SERVICE, 3) == 0)
            processor->failure = PROC_NO_SERVICE;
        else if (memcmp(code, SC_PERM_ERR, 3) == 0)
            processor->failure = PROC_PERM_ERR;
        else if (memcmp(code, SC_CON_TIMEOUT, 3) == 0)
            processor->failure = PROC_CON_TIMEOUT;
        else if (memcmp(code, SC_KEEPALIVE_TIMEOUT, 3) == 0)
            processor->failure = PROC_TIMEOUT;
        else if (memcmp(code, SC_NETDOWN, 3) == 0)
            processor->failure = PROC_NETDOWN;
        else
            processor->failure = PROC_BAD_RESP;

        ccnet_processor_done (processor, FALSE);

        return;
    }

    processor->t_packet_recv = time(NULL);

    if (memcmp (code, SC_PROC_KEEPALIVE, 3) == 0) {
        ccnet_processor_keep_alive_response (processor);
    } else if (memcmp (code, SC_PROC_ALIVE, 3) == 0) {
        /* do nothing */
    } else if (memcmp (code, SC_PROC_DEAD, 3) == 0) {
        ccnet_debug ("[Proc] Shutdown processor %s(%d) when remote processor dies\n",
                     GET_PNAME(processor), PRINT_ID(processor->id));

        if (CCNET_IS_SERVICE_PROXY_PROC(processor) ||
            CCNET_IS_SERVICE_STUB_PROC(processor)) {
            CCNET_PROCESSOR_GET_CLASS (processor)->handle_response (
                processor, code, code_msg, content, clen);
        }

        processor->failure = PROC_REMOTE_DEAD;
        ccnet_processor_done (processor, FALSE);
    } else
        CCNET_PROCESSOR_GET_CLASS (processor)->handle_response (processor,
                                                                code, code_msg, 
                                                                content, clen);
}
示例#9
0
void ccnet_processor_handle_update (CcnetProcessor *processor, 
                                    char *code, char *code_msg,
                                    char *content, int clen)
{
    g_object_ref (processor);
    processor->is_active = TRUE;

    if (code[0] == '5') {
        ccnet_debug ("[Proc] Shutdown processor %s(%d) for bad update: %s %s\n",
                     GET_PNAME(processor), PRINT_ID(processor->id),
                     code, code_msg);

        if (memcmp(code, SC_UNKNOWN_SERVICE, 3) == 0)
            processor->failure = PROC_NO_SERVICE;
        else if (memcmp(code, SC_PERM_ERR, 3) == 0)
            processor->failure = PROC_PERM_ERR;
        else if (memcmp(code, SC_NETDOWN, 3) == 0)
            processor->failure = PROC_REMOTE_DEAD;
        else
            processor->failure = PROC_BAD_RESP;

        ccnet_processor_done (processor, FALSE);
        return;
    }

    if (strncmp (code, SC_PROC_KEEPALIVE, 3) == 0) {
        ccnet_processor_send_response (processor, SC_PROC_ALIVE, 
                                       SS_PROC_ALIVE, NULL, 0);
    } else if (strncmp (code, SC_PROC_DEAD, 3) == 0) {
        ccnet_debug ("[proc] Shutdown processor %s(%d) when peer(%.8s) processor is dead\n",
                   GET_PNAME(processor), PRINT_ID(processor->id),
                   processor->peer_id);
        processor->failure = PROC_REMOTE_DEAD;
        ccnet_processor_done (processor, FALSE);
    } else if (strncmp (code, SC_PROC_DONE, 3) == 0) {
        ccnet_debug ("[proc] Shutdown processor when receive 103: service done\n");
        ccnet_processor_done (processor, TRUE);
    } else {
        CCNET_PROCESSOR_GET_CLASS (processor)->handle_update (processor, 
                                                              code, code_msg, 
                                                              content, clen);
    }
    processor->is_active = FALSE;
    g_object_unref (processor);
}
示例#10
0
static int
cmdrsp_cb (const char *code, char *content, int clen, void *data)
{
    RegisterServiceCB cb = data;

    if (clen != 0)
        ccnet_debug ("Receive cmd response {\n%s}\n", content);
    else 
        ccnet_debug ("Receive cmd response null\n");
    
    if (cb) {
        if (memcmp (SC_SERV_EXISTED, code, 3) == 0)
            cb (FALSE);
        else
            cb (TRUE);
    }
    return 0;
}
示例#11
0
static void verify_challenge_user(CcnetProcessor *processor, 
                                  char *code, char *code_msg,
                                  char *content, int clen)
{
    CcnetKeepaliveProcPriv *priv = GET_PRIV (processor);
    ccnet_debug ("[Conn] Verify User Challenge\n");

    if (clen != 40 || memcmp(content, priv->random_buf, 40) != 0) {
        ccnet_debug ("[Keepalive] Challenge failed\n");
        close_processor(processor);
        return;
    }

    processor->peer->auth_done = 1;
    g_signal_emit_by_name (processor->peer, "auth-done");

    send_keepalive (processor);
    reset_timeout (processor);
}
示例#12
0
static void close_processor_in_timeout(CcnetProcessor *processor)
{
    CcnetPeer *peer = processor->peer;

    ccnet_debug ("[Conn] keepalive timeout current state is %d\n",
                 processor->state);

    ccnet_processor_done (processor, FALSE);
    ccnet_peer_shutdown (peer);
    peer->num_fails++;
}
示例#13
0
static void recv_pubkey(CcnetProcessor *processor, 
                        char *code, char *code_msg,
                        char *content, int clen)
{
    g_assert (processor->state == WAIT_PUBKEY);

    if (clen == 0 || content[clen-1] != '\0') {
        ccnet_debug ("[Conn] Bad public key format\n");
        close_processor (processor);
        return;
    }

    ccnet_peer_set_pubkey (processor->peer, content);
    if (processor->peer->pubkey == NULL) {
        ccnet_debug ("[Conn] Bad public key format\n");
        close_processor (processor);
        return;
    }

    send_challenge (processor);
}
示例#14
0
gboolean remove_service_cmp (gpointer key,
                             gpointer value,
                             gpointer user_data)
{
    CcnetService *service = value;

    if (service->provider == user_data) {
        ccnet_debug ("[Service] Service %s un-registered\n", (char *)key);
        return TRUE;
    }
    return FALSE;
}
示例#15
0
static void handle_response (CcnetProcessor *processor,
                             char *code, char *code_msg,
                             char *content, int clen)
{
    ServiceProxyPriv *priv = GET_PRIV (processor);
    if(!priv->stub_proc || !((CcnetProcessor *)priv->stub_proc)->peer->is_local)
        ccnet_debug ("[Svc Proxy] %s:%d [%s] handle response: %s %s\n",
                     GET_PNAME(processor), PRINT_ID(processor->id),
                     priv->name, code, code_msg);

    /* relay this response */
    ccnet_processor_send_response (processor, code, code_msg, content, clen);
}
示例#16
0
static void
send_handshake_message (CcnetHandshake *handshake)
{
    const char *id = handshake->session->base.id;
    char buf[256];
    ccnet_packet *packet = (ccnet_packet *)buf;

    packet->header.version = 1;
    packet->header.type = CCNET_MSG_HANDSHAKE;
    memcpy (packet->data, id, 40);
    packet->header.length = 40;
    packet->header.id = 0;
    
    ccnet_packet_io_write_packet (handshake->io, packet);

    if (handshake->peer)
        ccnet_debug ("[Conn] Outgoing: Send my id to %s(%.10s)\n",
                     handshake->peer->name, handshake->peer->id);
    else
        ccnet_debug ("[Conn] Incoming: Send my id to %.10s\n",
                     handshake->id);
}
示例#17
0
static void
read_ok (CcnetHandshake *handshake, ccnet_packet *packet)
{
    if (packet->header.type != CCNET_MSG_OK) {
        ccnet_warning ("[Conn] Read wrong ack format\n");
        ccnet_handshake_done (handshake, FALSE);
    } else  {
        ccnet_debug ("[Conn] Incoming: Read ack (%.10s)\n", handshake->id);
        ccnet_handshake_done (handshake, TRUE);
    }
  
    return;
}
示例#18
0
static void
ccnet_handshake_done (CcnetHandshake *handshake, int isOK)
{
    /* ccnet_message ("handshakeDone: %s\n", isOK ? "connected" : "aborting"); */
    ccnet_debug ("[Conn] HandshakeDone %s\n", isOK ? "connected" : "aborting");

    ccnet_packet_io_set_iofuncs (handshake->io, NULL, NULL, NULL, NULL);
    fire_done_func (handshake, isOK);
    if (handshake->peer)
        g_object_unref (handshake->peer);
    g_free (handshake->id);
    g_free (handshake);
}
示例#19
0
文件: peer.c 项目: andrey-str/ccnet
static void
ccnet_peer_set_addr_str (CcnetPeer *peer, const char *addr_str)
{
    /* do not need saving */
    if (!addr_str)
        return;
    if (peer->addr_str && strcmp(addr_str, peer->addr_str) == 0)
        return;

    g_object_set (peer, "ip", addr_str, NULL);
    ccnet_debug ("[Peer] Updated peer %s(%.10s) address %s\n",
                 peer->name, peer->id, addr_str);
}
示例#20
0
static void
send_ack (CcnetHandshake *handshake)
{
    ccnet_packet packet;
    packet.header.version = 1;
    packet.header.type = CCNET_MSG_OK;
    packet.header.length = 0;
    packet.header.id = 0;
    
    ccnet_packet_io_write_packet (handshake->io, &packet);

    ccnet_debug ("[Conn] Outgoing: Send ack to %s(%.10s)\n", 
                 handshake->peer->name, handshake->peer->id);
}
示例#21
0
static void send_challenge_user(CcnetProcessor *processor, CcnetUser *user)
{
    CcnetKeepaliveProcPriv *priv = GET_PRIV (processor);
    unsigned char *buf;
    int len;

    ccnet_debug ("[Keepalive] Send user challenge to %.8s\n",
                 processor->peer->id);
    RAND_pseudo_bytes (priv->random_buf, 40);
    buf = public_key_encrypt (user->pubkey, priv->random_buf, 40, &len);
    ccnet_processor_send_update (processor, "321", NULL, (char *)buf, len);

    g_free(buf);
    processor->state = WAIT_CHALLENGE_USER;
    reset_timeout (processor);
}
示例#22
0
文件: peer.c 项目: andrey-str/ccnet
static void
canRead (ccnet_packet *packet, void *vpeer)
{
    CcnetPeer *peer = vpeer;
    g_object_ref (peer);

    /* if (!peer->is_local) */
    /*     ccnet_debug ("[RECV] Recieve packat from %s type is %d, id is %d\n", */
    /*                  peer->id, packet->header.type, packet->header.id); */

    if (packet->header.id == 0)
        return;

    if (packet->header.type != CCNET_MSG_ENCPACKET) {
        handle_packet (packet, peer);
    } else {
        /* ccnet_debug ("receive an encrypt packet\n"); */

        if (!peer->session_key) {
            ccnet_debug("Receive a encrypted packet from %s(%.8s) while "
                        "not having session key \n", peer->name, peer->id);
            goto out;
        }

        char *data;
        int len;
        int ret;
        ret = ccnet_decrypt_with_key (&data, &len, packet->data, packet->header.id,
                                      peer->key, peer->iv);
        if (ret < 0)
            ccnet_warning ("[SEND] decryption error for peer %s(%.8s) \n",
                           peer->name, peer->id);
        else {
            ccnet_packet *new_pac = (ccnet_packet *)data;
            /* byte order, from network to host */
            new_pac->header.length = ntohs(new_pac->header.length);
            new_pac->header.id = ntohl (new_pac->header.id);

            handle_packet (new_pac, peer);
            g_free (data);
        }
    }

out:
    g_object_unref (peer);
}
示例#23
0
static void
canRead (ccnet_packet *packet, void *arg)
{
    CcnetHandshake *handshake = (CcnetHandshake *)arg;
    ccnet_debug("current state is %d\n", handshake->state);

    switch (handshake->state) {
    case INIT:
        read_peer_id (handshake, packet);
        break;
    case ID_SENT:
        read_peer_id (handshake, packet);
        break;
    case ID_RECEIVED:
        read_ok (handshake, packet);
        break;
    default: g_assert(0) ;
    }
}
示例#24
0
void
ccnet_processor_done (CcnetProcessor *processor, gboolean success)
{
    if (processor->thread_running) {
        processor->delay_shutdown = TRUE;
        processor->was_success = success;
        return;
    }

    if (processor->state == STATE_IN_SHUTDOWN) {
        return;
    }

    processor->state = STATE_IN_SHUTDOWN;
    if (processor->failure == PROC_NOTSET)
        processor->failure = PROC_DONE;
    if (!processor->peer->is_local)
        ccnet_debug ("Processsor %s(%d) done %d\n", GET_PNAME(processor),
                     PRINT_ID(processor->id), success);

    if (!processor->detached && success)
    {
        if (!IS_SLAVE (processor)) {
            ccnet_processor_send_update (processor, SC_PROC_DONE, SS_PROC_DONE,
                                         NULL, 0);
        }
    }

    /* When we emit the done signal, the corresponding handler may
     * shutdown the peer, we should remove this processor from the
     * peers processor list, otherwise this processor will be freed
     * twice. */
    g_signal_emit (processor, signals[DONE_SIG], 0, success);

    if (!processor->detached) {
        ccnet_peer_remove_processor (processor->peer, processor);
    }

    ccnet_processor_release_resource (processor);

    ccnet_proc_factory_recycle (processor->session->proc_factory, processor);
}
示例#25
0
static void handle_update (CcnetProcessor *processor,
                           char *code, char *code_msg,
                           char *content, int clen)
{
    CcnetMessage *msg;

    if (processor->peer->is_local) {
        msg = ccnet_message_from_string_local (content, clen);
        ccnet_send_message (processor->session, msg);
        ccnet_message_unref (msg);
    } else {
        msg = ccnet_message_from_string (content, clen);
        if (!msg) {
            g_warning ("Wrong message format.\n");
            ccnet_processor_done (processor, FALSE);
            return;
        }

        msg->rtime = time(NULL);
        ccnet_debug ("[msg] Received a message : %s - %.10s\n", 
                     msg->app, msg->body);

        int ret = ccnet_recv_message (processor->session, msg);
        if (ret == -1) {
            ccnet_message ("[msg] Message from %.8s permission error\n", 
                           msg->from);
            ccnet_processor_send_response (processor, SC_PERM_ERR,
                                           SS_PERM_ERR, NULL, 0);
            ccnet_processor_done (processor, TRUE);
            ccnet_message_unref (msg);
            return;
        }

        ccnet_message_unref (msg);
    }

    ccnet_processor_send_response (processor, "200", "OK", NULL, 0);
    ccnet_processor_done (processor, TRUE);
}
示例#26
0
static void
myHandshakeDoneCB (CcnetHandshake *handshake,
                   CcnetPacketIO  *io,
                   int             is_connected,
                   const char     *peer_id,
                   void           *vmanager)
{
    CcnetConnManager *manager = vmanager;
    CcnetPeerManager *peerMgr = manager->session->peer_mgr;
    CcnetPeer *peer;

    if (!is_connected) {
        if (ccnet_packet_io_is_incoming (io)) {
            ccnet_warning ("[conn] incoming handshake fails.\n");
            ccnet_packet_io_free (io);
            return;
        }

        /* temporally use peer, so don't need to increase the reference */
        peer = handshake->peer;
        if (peer->num_fails == 0) {
            /* print error for the first time */
            ccnet_message ("[Conn] peer %s(%.10s) connection fails\n", 
                         peer->name, peer->id);
        } else {
            ccnet_debug ("[Conn] peer %s(%.10s) connection fails\n", 
                         peer->name, peer->id);
        }
        if (peer->net_state == PEER_CONNECTED) {
            ccnet_debug ("[Conn] But Peer %s(%.10s) is already connected me.\n",
                         peer->name, peer->id);
        } else if (peer->net_state == PEER_DOWN){
            ccnet_peer_shutdown(peer);
        }

        ccnet_packet_io_free (io);

        peer->num_fails++;
        peer->in_connection = 0;
        return;
    }

    if (!ccnet_packet_io_is_incoming (io)) {
        peer = handshake->peer;
        peer->in_connection = 0;
        
        if (peer->to_resolve) {
            if (!peer_id_valid(peer_id)) {
                /* TODO: Remove the peer */
                ccnet_warning ("[Conn] Resolving: Received invalid peer id\n");
                return;
            }
            ccnet_debug ("[Conn] Resolving: Peer %.8s is resolved\n", peer_id);
            memcpy (peer->id, peer_id, 40);
            peer->id[40] = '\0';
            on_resolve_peer_connected (peer, io);
            return;
        }
        /* ref for using the peer below */
        g_object_ref (peer);
    } else {
        /* incoming */
        if (peer_id == NULL) {
            ccnet_warning ("Unknown peer (no-id) connecting\n");
            ccnet_packet_io_free (io);
            return;
        }
        peer = ccnet_peer_manager_get_peer (peerMgr, peer_id);
        if (!peer) {
            ccnet_message ("Unknown peer %s connecting\n", peer_id);
            peer = ccnet_peer_new (peer_id);
            ccnet_peer_manager_add_peer (peerMgr, peer);
            set_peer_address_from_socket(peer, io);
            peer->last_up = time(NULL);
            on_unauthed_peer_connected (peer, io);
            g_object_unref (peer);
            return;
        }
        set_peer_address_from_socket(peer, io);
        peer->last_up = time(NULL);
    }
    /* hold a reference on the peer */

    if (peer->net_state == PEER_CONNECTED) {
        ccnet_message ("[Conn] Peer %s (%.10s) is already connected. "
                       "Discarding this handshake.\n", 
                       peer->name, peer->id);
        ccnet_packet_io_free (io);
        g_object_unref (peer);
        return;
    }

    ccnet_message ("[Conn] Peer %s (%.10s) connected\n",
                   peer->name, peer->id);
    peer->num_fails = 0;
    on_peer_connected (peer, io);
    g_object_unref (peer);
}
示例#27
0
/**
 * return %TRUE if an outgoing connection is started, %FALSE otherwise.
 *
 * When peer's ip address is not looked up yet, an dns request will be sent,
 * and when dns done, this function will be called again.
 */
gboolean
ccnet_conn_manager_connect_peer (CcnetConnManager *manager, CcnetPeer *peer)
{
    CcnetPacketIO *io;
    /* int interval; */
    const char *addr = NULL;
    int port = 0;

    if (peer->in_connection)
        return FALSE;

    /* time_t now = time(NULL); */
    
    if (peer->net_state == PEER_CONNECTED)
        return FALSE;

    if (peer->dns_addr)
        addr = peer->dns_addr;
    else if (!peer->redirected) {
        if (!peer->public_addr)
            goto err_connect;

        if (is_valid_ipaddr(peer->public_addr))
            addr = peer->public_addr;
        else {
            dns_lookup_peer (peer);
            return TRUE;        /* same as out going is started */
        }
    } else {
        if (!peer->redirect_addr)
            goto err_connect;

        if (is_valid_ipaddr(peer->redirect_addr))
            addr = peer->redirect_addr;
        else {
            dns_lookup_peer (peer);
            return TRUE;        /* same as out going is started */
        }
    }

    if (!addr)
        goto err_connect;

    if (!peer->redirected)
        port = peer->public_port;
    else
        port = peer->redirect_port;

    ccnet_peer_update_address (peer, addr, port);

    /* interval = get_reconnect_interval_secs (peer); */
    /* if (now - peer->last_try_time < interval) { */
        /* ccnet_debug ("[Conn] Less than interval: (%d - %d = %d, %d)\n", */
        /*              now, peer->last_try_time, now - peer->last_try_time, */
        /*              interval); */
    /*     return FALSE; */
    /* } */
    /* peer->last_try_time = now; */

    if (peer->num_fails == 0) {
        /* print log for the first time */
        ccnet_message ("[Conn] Start outgoing connect to %s(%.10s) %s:%d\n", 
                       peer->name, peer->id, addr, port);
    } else {
        ccnet_debug ("[Conn] Start outgoing connect to %s(%.10s) %s:%d\n", 
                     peer->name, peer->id, addr, port);
    }
    io = ccnet_packet_io_new_outgoing (manager->session, addr, port);
    
    if (io == NULL) {
        /* ccnet_warning ("Failed to create socket for peer %s (%.10s)\n", 
           peer->name, peer->id); */
        goto err_connect;
    } else {
        peer->in_connection = 1;
        ccnet_handshake_new (manager->session, peer, io, 
                             myHandshakeDoneCB, manager);
        return TRUE;
    }

err_connect:
    peer->num_fails++;
    return FALSE;
}
示例#28
0
文件: peer.c 项目: andrey-str/ccnet
static void
handle_update (CcnetPeer *peer, int req_id, char *data, int len)
{
    CcnetProcessor *processor;
    char *code, *code_msg = 0, *content = 0;
    int clen;
    char *ptr, *end;

    if (len < 4)
        goto error;
    
    code = data;
    
    ptr = data + 3;
    if (*ptr == '\n') {
        /* no code_msg */
        *ptr++ = '\0';
        content = ptr;
        clen = len - (ptr - data);
        goto parsed;
    }
    
    if (*ptr != ' ')
        goto error;
    
    *ptr++ = '\0';
    code_msg = ptr;

    end = data + len;
    for (ptr = data; *ptr != '\n' && ptr != end; ptr++) ;

    if (ptr == end)             /* must end with '\n' */
        goto error;

    /* if (*(ptr-1) == '\r') */
    /*     *(ptr-1) = '\0'; */
    *ptr++ = '\0';
    content = ptr;
    clen = len - (ptr - data);
    
parsed:
    processor = ccnet_peer_get_processor (peer, SLAVE_ID(req_id));
    if (processor == NULL) {
        if (memcmp(code, SC_PROC_DEAD, 3) != 0 
            && memcmp(code, SC_PROC_DONE, 3) != 0) {
            ccnet_debug ("Delayed update from %s(%.8s), id is %d, %s %s\n",
                         peer->name, peer->id, req_id, code, code_msg);
            ccnet_peer_send_response (peer, req_id,
                                      SC_PROC_DEAD, SS_PROC_DEAD,
                                      NULL, 0);
        }
        return;
    }

    /* if (!peer->is_local) */
    /*     ccnet_debug ("[RECV] handle_update %s id is %d, %s %s\n", */
    /*                  GET_PNAME(processor), PRINT_ID(processor->id), */
    /*                  code, code_msg); */
    peer->in_processor_call = 1;
    ccnet_processor_handle_update (processor, code, code_msg, content, clen);
    peer->in_processor_call = 0;
    return;

error:
    ccnet_warning ("Bad update format from %s\n", peer->id);
}