Example #1
0
void accept_friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *message, size_t length, void *userdata)
{
    unsigned char tox_printable_id[TOX_ADDRESS_SIZE * 2 + 1];
    int32_t friendnumber;
    TOX_ERR_FRIEND_ADD friend_add_error;

    log_printf(L_DEBUG, "Got friend request\n");

    if(use_shared_secret)
    {
        if(!message)
        {
            log_printf(L_WARNING, "Friend sent NULL message - not accepting request");
            return;
        }

        if(message[length - 1] != '\0')
        {
            log_printf(L_WARNING, "Message of size %u is not NULL terminated - not accepting request", length);
            return;
        }

        if(strncmp(message, shared_secret, TOX_MAX_FRIEND_REQUEST_LENGTH-1))
        {
            log_printf(L_WARNING, "Received shared secret \"%s\" differs from our shared secret - not accepting request", message);
            return;
        }
    }
    
    memset(tox_printable_id, '\0', sizeof(tox_printable_id));
    id_to_string(tox_printable_id, public_key);

    if(server_whitelist_mode)
    {
        allowed_toxid etmp, *found = NULL;
        memcpy(etmp.toxid, public_key, TOX_PUBLIC_KEY_SIZE);
        LL_SEARCH(allowed_toxids, found, &etmp, allowed_toxid_cmp);
        if(!found)
        {
            log_printf(L_WARNING, "Rejected friend request from non-whitelisted friend %s", tox_printable_id);
            return;
        }
        log_printf(L_DEBUG, "Friend %s passed whitelist check", tox_printable_id);
    }

    friendnumber = tox_friend_add_norequest(tox, public_key, &friend_add_error);
    if(friend_add_error != TOX_ERR_FRIEND_ADD_OK)
    {
        log_printf(L_WARNING, "Could not add friend: err %u", friend_add_error);
        return;
    }

    log_printf(L_INFO, "Accepted friend request from %s as %d\n", tox_printable_id, friendnumber);
}
Example #2
0
int main() {
    int i;
    el els[10], *e, *tmp, *tmp2;
    for(i=0;i<10;i++) els[i].id='a'+i;

    /* test LL macros */
    printf("LL macros\n");
    LL_APPEND(head,&els[0]);
    LL_APPEND(head,&els[1]);
    LL_APPEND(head,&els[2]);
    LL_FOREACH(head,e)
        printf("%c ", e->id);
    printf("\n");
    LL_SEARCH_SCALAR(head, e, id, 'b');
    if (e) printf("search scalar found b\n");
    LL_SEARCH(head, e, &els[0], eltcmp);
    if (e) printf("search found %c\n",e->id);
    LL_FOREACH_SAFE(head,e,tmp) LL_DELETE(head,e);

    printf("\n");

    /* test DL macros */
    printf("DL macros\n");
    DL_APPEND(head,&els[0]);
    DL_APPEND(head,&els[1]);
    DL_APPEND(head,&els[2]);
    DL_FOREACH(head,e)
        printf("%c ", e->id);
    printf("\n");
    DL_SEARCH_SCALAR(head, e, id, 'b');
    if (e) printf("search scalar found b\n");
    DL_SEARCH(head, e, &els[0], eltcmp);
    if (e) printf("search found %c\n",e->id);
    DL_FOREACH_SAFE(head,e,tmp) DL_DELETE(head,e);
    printf("\n");

    /* test CDL macros */
    printf("CDL macros\n");
    CDL_PREPEND(head,&els[0]);
    CDL_PREPEND(head,&els[1]);
    CDL_PREPEND(head,&els[2]);
    CDL_FOREACH(head,e)
        printf("%c ", e->id);
    printf("\n");
    CDL_SEARCH_SCALAR(head, e, id, 'b');
    if (e) printf("search scalar found b\n");
    CDL_SEARCH(head, e, &els[0], eltcmp);
    if (e) printf("search found %c\n",e->id);
    CDL_FOREACH_SAFE(head,e,tmp,tmp2) CDL_DELETE(head,e);


    return 0;
}
Example #3
0
int handle_request_tunnel_frame(protocol_frame *rcvd_frame)
{
    char *hostname = NULL;
    tunnel *tun;
    int port = -1;
    int sockfd = 0;
    uint16_t tunnel_id;

    if(client_mode)
    {
        log_printf(L_WARNING, "Got tunnel request frame from friend #%d when in client mode\n", rcvd_frame->friendnumber);
        return -1;
    }
    
    port = rcvd_frame->connid;
    hostname = calloc(1, rcvd_frame->data_length + 1);
    if(!hostname)
    {
        log_printf(L_ERROR, "Could not allocate memory for tunnel request hostname\n");
        return -1;
    }

    strncpy(hostname, (char *)rcvd_frame->data, rcvd_frame->data_length);
    hostname[rcvd_frame->data_length] = '\0';

    log_printf(L_INFO, "Got a request to forward data from %s:%d\n", hostname, port);
    
    // check rules
    if (rules_policy == VALIDATE && nrules > 0 ) {
        
        rule temp_rule, *found = NULL;
        temp_rule.host = hostname;
        temp_rule.port = port;
        
        LL_SEARCH(rules, found, &temp_rule, rule_cmp);
        if(!found)
        {
            log_printf(L_WARNING, "Rejected, request not in rules\n");
            if(hostname)
            {
                free(hostname);
            }
            return -1;
        }
    } else if (rules_policy != NONE) {
        log_printf(L_WARNING, "Filter option active but no allowed host/port. All requests will be dropped.\n");
        if(hostname)
        {
            free(hostname);
        }
        return -1;
    }



    tunnel_id = get_random_tunnel_id();
    log_printf(L_DEBUG, "Tunnel ID: %d\n", tunnel_id);

    sockfd = get_client_socket(hostname, port);
    if(sockfd >= 0)
    {
        tun = tunnel_create(sockfd, tunnel_id, rcvd_frame->friendnumber);
        if(tun)
        {
            FD_SET(sockfd, &master_server_fds);
            update_select_nfds(sockfd);
            log_printf(L_DEBUG, "Created tunnel, yay!\n");
            send_tunnel_ack_frame(tun);
        }
        else
        {
            log_printf(L_ERROR, "Couldn't allocate memory for tunnel\n");
            close(sockfd);
        }
    }
    else
    {
        log_printf(L_WARNING, "Could not connect to %s:%d\n", hostname, port);
        /* TODO send reject */
    }

    free(hostname);

    return 0;
}
Example #4
0
/**
 * @brief Transition from current FSM state into another state.
 *
 * @param[in,out] tcb     TCB holding the FSM state.
 * @param[in]     state   State to transition in.
 *
 * @return   Zero on success.
 */
static int _transition_to(gnrc_tcp_tcb_t *tcb, fsm_state_t state)
{
    DEBUG("_transition_to: %d\n", state);

    gnrc_tcp_tcb_t *iter = NULL;

    switch (state) {
        case FSM_STATE_CLOSED:
            /* Clear retransmit queue */
            _clear_retransmit(tcb);

            /* Remove connection from active connections */
            mutex_lock(&_list_tcb_lock);
            LL_DELETE(_list_tcb_head, tcb);
            mutex_unlock(&_list_tcb_lock);

            /* Free potencially allocated receive buffer */
            _rcvbuf_release_buffer(tcb);
            tcb->status |= STATUS_NOTIFY_USER;
            break;

        case FSM_STATE_LISTEN:
            /* Clear address info */
#ifdef MODULE_GNRC_IPV6
            if (tcb->address_family == AF_INET6) {
                if (tcb->status & STATUS_ALLOW_ANY_ADDR) {
                    ipv6_addr_set_unspecified((ipv6_addr_t *) tcb->local_addr);
                }
                ipv6_addr_set_unspecified((ipv6_addr_t *) tcb->peer_addr);
            }
#endif
            tcb->peer_port = PORT_UNSPEC;

            /* Allocate receive buffer */
            if (_rcvbuf_get_buffer(tcb) == -ENOMEM) {
                return -ENOMEM;
            }

            /* Add connection to active connections (if not already active) */
            mutex_lock(&_list_tcb_lock);
            LL_SEARCH(_list_tcb_head, iter, tcb, TCB_EQUAL);
            if (iter == NULL) {
                LL_PREPEND(_list_tcb_head, tcb);
            }
            mutex_unlock(&_list_tcb_lock);
            break;

        case FSM_STATE_SYN_SENT:
            /* Allocate rceveive buffer */
            if (_rcvbuf_get_buffer(tcb) == -ENOMEM) {
                return -ENOMEM;
            }

            /* Add connection to active connections (if not already active) */
            mutex_lock(&_list_tcb_lock);
            LL_SEARCH(_list_tcb_head, iter, tcb, TCB_EQUAL);
            /* If connection is not already active: Check port number, append TCB */
            if (iter == NULL) {
                /* Check if port number was specified */
                if (tcb->local_port != PORT_UNSPEC) {
                    /* Check if given port number is use: return error and release buffer */
                    if (_is_local_port_in_use(tcb->local_port)) {
                        mutex_unlock(&_list_tcb_lock);
                        _rcvbuf_release_buffer(tcb);
                        return -EADDRINUSE;
                    }
                }
                /* Pick random port */
                else {
                    tcb->local_port = _get_random_local_port();
                }
                LL_PREPEND(_list_tcb_head, tcb);
            }
            mutex_unlock(&_list_tcb_lock);
            break;

        case FSM_STATE_SYN_RCVD:
        case FSM_STATE_ESTABLISHED:
        case FSM_STATE_CLOSE_WAIT:
            tcb->status |= STATUS_NOTIFY_USER;
            break;

        case FSM_STATE_TIME_WAIT:
            _restart_timewait_timer(tcb);
            break;

        default:
            break;
    }
    tcb->state = state;
    return 0;
}