コード例 #1
0
ファイル: packets.c プロジェクト: CZ-NIC/rtrlib
/*
 * @brief Appends the Prefix PDU pdu to ary.
 */
static int rtr_store_prefix_pdu(struct rtr_socket *rtr_socket, const void *pdu, const unsigned int pdu_size, void **ary,
                                unsigned int *ind, unsigned int *size)
{
    const enum pdu_type type = rtr_get_pdu_type(pdu);
    assert(type  == IPV4_PREFIX || type == IPV6_PREFIX);
    if ((*ind) >= *size) {
        *size += TEMPORARY_PDU_STORE_INCREMENT_VALUE;
        void *tmp = realloc(*ary, *size * pdu_size);
        if (tmp == NULL) {
            const char txt[] = "Realloc failed";
            RTR_DBG("%s", txt);
            rtr_send_error_pdu(rtr_socket, pdu, pdu_size, INTERNAL_ERROR, txt, sizeof(txt));
            rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
            free(*ary);
            ary = NULL;
            return RTR_ERROR;
        }
        *ary = tmp;
    }
    if (type == IPV4_PREFIX) {
        struct pdu_ipv4 *ary_ipv4 = *ary;

        memcpy(ary_ipv4 + *ind, pdu, pdu_size);
    } else if (type == IPV6_PREFIX) {
        struct pdu_ipv6 *ary_ipv6 = *ary;

        memcpy(ary_ipv6 + *ind, pdu, pdu_size);
    }
    (*ind)++;
    return RTR_SUCCESS;
}
コード例 #2
0
ファイル: packets.c プロジェクト: CZ-NIC/rtrlib
static int rtr_handle_cache_response_pdu(struct rtr_socket *rtr_socket, char *pdu)
{

    RTR_DBG1("Cache Response PDU received");
    struct pdu_cache_response *cr_pdu = (struct pdu_cache_response *) pdu;
    //set connection session_id
    if (rtr_socket->request_session_id) {
        if (rtr_socket->last_update != 0) {
            //if this isnt the first sync, but we already received records, delete old records in the pfx_table
            pfx_table_src_remove(rtr_socket->pfx_table, rtr_socket);
            spki_table_src_remove(rtr_socket->spki_table, rtr_socket);

            rtr_socket->last_update = 0;
        }
        rtr_socket->session_id = cr_pdu->session_id;
    } else {
        if (rtr_socket->session_id != cr_pdu->session_id) {
            const char txt[] = "Wrong session_id in Cache Response PDU"; //TODO: Appendrtr_socket->session_id to string
            rtr_send_error_pdu(rtr_socket, NULL, 0, CORRUPT_DATA, txt, sizeof(txt));
            rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
            return RTR_ERROR;
        }
    }
    return RTR_SUCCESS;
}
コード例 #3
0
ファイル: rtr.c プロジェクト: mceyran/rtrlib
void rtr_stop(struct rtr_socket *rtr_socket) {
    rtr_change_socket_state(rtr_socket, RTR_SHUTDOWN);
    if(rtr_socket->thread_id != 0) {
        pthread_kill(rtr_socket->thread_id, SIGUSR1);
        pthread_join(rtr_socket->thread_id, NULL);
    }
    RTR_DBG1("Socket shut down");
}
コード例 #4
0
ファイル: packets.c プロジェクト: CZ-NIC/rtrlib
static int rtr_update_spki_table(struct rtr_socket* rtr_socket, const void* pdu)
{
    const enum pdu_type type = rtr_get_pdu_type(pdu);
    assert(type == ROUTER_KEY);

    struct spki_record entry;

    size_t pdu_size = sizeof(struct pdu_router_key);
    rtr_key_pdu_2_spki_record(rtr_socket, pdu, &entry,type);

    int rtval;
    if (((struct pdu_router_key*) pdu)->flags == 1)
        rtval = spki_table_add_entry(rtr_socket->spki_table, &entry);

    else if (((struct pdu_router_key*) pdu)->flags == 0)
        rtval = spki_table_remove_entry(rtr_socket->spki_table, &entry);

    else {
        const char txt[] = "Router Key PDU with invalid flags value received";
        RTR_DBG("%s", txt);
        rtr_send_error_pdu(rtr_socket, pdu, pdu_size, CORRUPT_DATA, txt, sizeof(txt));
        return RTR_ERROR;
    }

    if (rtval == SPKI_DUPLICATE_RECORD) {
        //TODO: This debug message isn't working yet, how to display SKI/SPKI without %x?
        RTR_DBG("Duplicate Announcement for router key: ASN: %u received",entry.asn);
        rtr_send_error_pdu(rtr_socket, pdu, pdu_size, DUPLICATE_ANNOUNCEMENT , NULL, 0);
        rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
        return RTR_ERROR;
    } else if (rtval == SPKI_RECORD_NOT_FOUND) {
        RTR_DBG1("Withdrawal of unknown router key");
        rtr_send_error_pdu(rtr_socket, pdu, pdu_size, WITHDRAWAL_OF_UNKNOWN_RECORD, NULL, 0);
        rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
        return RTR_ERROR;
    } else if (rtval == SPKI_ERROR) {
        const char txt[] = "spki_table Error";
        RTR_DBG("%s", txt);
        rtr_send_error_pdu(rtr_socket, pdu, pdu_size, INTERNAL_ERROR, txt, sizeof(txt));
        rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
        return RTR_ERROR;
    }

    return RTR_SUCCESS;
}
コード例 #5
0
ファイル: packets.c プロジェクト: CZ-NIC/rtrlib
static int rtr_set_last_update(struct rtr_socket *rtr_socket)
{
    if (lrtr_get_monotonic_time(&(rtr_socket->last_update)) == -1) {
        RTR_DBG1("get_monotonic_time(..) failed ");
        rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
        return RTR_ERROR;
    }
    return RTR_SUCCESS;
}
コード例 #6
0
ファイル: packets.c プロジェクト: CZ-NIC/rtrlib
static int rtr_update_pfx_table(struct rtr_socket *rtr_socket, const void *pdu)
{
    const enum pdu_type type = rtr_get_pdu_type(pdu);
    assert(type == IPV4_PREFIX || type == IPV6_PREFIX);

    struct pfx_record pfxr;
    size_t pdu_size = (type == IPV4_PREFIX ? sizeof(struct pdu_ipv4) : sizeof(struct pdu_ipv6));
    rtr_prefix_pdu_2_pfx_record(rtr_socket, pdu, &pfxr, type);

    int rtval;
    if (((struct pdu_ipv4 *) pdu)->flags == 1)
        rtval = pfx_table_add(rtr_socket->pfx_table, &pfxr);
    else if (((struct pdu_ipv4 *) pdu)->flags == 0)
        rtval = pfx_table_remove(rtr_socket->pfx_table, &pfxr);
    else {
        const char txt[] = "Prefix PDU with invalid flags value received";
        RTR_DBG("%s", txt);
        rtr_send_error_pdu(rtr_socket, pdu, pdu_size, CORRUPT_DATA, txt, sizeof(txt));
        return RTR_ERROR;
    }

    if (rtval == PFX_DUPLICATE_RECORD) {
        char ip[INET6_ADDRSTRLEN];
        lrtr_ip_addr_to_str(&(pfxr.prefix), ip, INET6_ADDRSTRLEN);
        RTR_DBG("Duplicate Announcement for record: %s/%u-%u, ASN: %u, received", ip, pfxr.min_len, pfxr.max_len, pfxr.asn);
        rtr_send_error_pdu(rtr_socket, pdu, pdu_size, DUPLICATE_ANNOUNCEMENT , NULL, 0);
        rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
        return RTR_ERROR;
    } else if (rtval == PFX_RECORD_NOT_FOUND) {
        RTR_DBG1("Withdrawal of unknown record");
        rtr_send_error_pdu(rtr_socket, pdu, pdu_size, WITHDRAWAL_OF_UNKNOWN_RECORD, NULL, 0);
        rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
        return RTR_ERROR;
    } else if (rtval == PFX_ERROR) {
        const char txt[] = "PFX_TABLE Error";
        RTR_DBG("%s", txt);
        rtr_send_error_pdu(rtr_socket, pdu, pdu_size, INTERNAL_ERROR, txt, sizeof(txt));
        rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
        return RTR_ERROR;
    }

    return RTR_SUCCESS;
}
コード例 #7
0
ファイル: packets.c プロジェクト: CZ-NIC/rtrlib
int rtr_send_reset_query(struct rtr_socket *rtr_socket)
{
    RTR_DBG1("Sending reset query");
    struct pdu_reset_query pdu;
    pdu.ver = rtr_socket->version;
    pdu.type = 2;
    pdu.flags = 0;
    pdu.len = 8;

    if (rtr_send_pdu(rtr_socket, &pdu, sizeof(pdu)) != RTR_SUCCESS) {
        rtr_change_socket_state(rtr_socket, RTR_ERROR_TRANSPORT);
        return RTR_ERROR;
    }
    return RTR_SUCCESS;
}
コード例 #8
0
ファイル: packets.c プロジェクト: CZ-NIC/rtrlib
int rtr_send_serial_query(struct rtr_socket *rtr_socket)
{
    struct pdu_serial_query pdu;
    pdu.ver = rtr_socket->version;
    pdu.type = SERIAL_QUERY;
    pdu.session_id =rtr_socket->session_id;
    pdu.len = sizeof(pdu);
    pdu.sn =rtr_socket->serial_number;


    RTR_DBG("sending serial query, SN: %u",rtr_socket->serial_number);
    if (rtr_send_pdu(rtr_socket, &pdu, sizeof(pdu)) != RTR_SUCCESS) {
        rtr_change_socket_state(rtr_socket, RTR_ERROR_TRANSPORT);
        return RTR_ERROR;
    }
    return RTR_SUCCESS;
}
コード例 #9
0
ファイル: rtr.c プロジェクト: ColinBS/rtrlib
void rtr_stop(struct rtr_socket *rtr_socket)
{
    RTR_DBG1("rtr_stop()");
    rtr_change_socket_state(rtr_socket, RTR_SHUTDOWN);
    if(rtr_socket->thread_id != 0) {
        RTR_DBG1("pthread_cancel()");
        pthread_cancel(rtr_socket->thread_id);
        RTR_DBG1("pthread_join()");
        pthread_join(rtr_socket->thread_id, NULL);

        tr_close(rtr_socket->tr_socket);
        rtr_socket->request_session_id = true;
        rtr_socket->serial_number = 0;
        rtr_socket->last_update = 0;
        pfx_table_src_remove(rtr_socket->pfx_table, rtr_socket);
        spki_table_src_remove(rtr_socket->spki_table, rtr_socket);
        rtr_socket->thread_id = 0;
    }
    RTR_DBG1("Socket shut down");
}
コード例 #10
0
ファイル: packets.c プロジェクト: CZ-NIC/rtrlib
static int rtr_store_router_key_pdu(struct rtr_socket *rtr_socket, const void *pdu, const unsigned int pdu_size,
				    struct pdu_router_key **ary, unsigned int *ind, unsigned int *size)
{
    assert(rtr_get_pdu_type(pdu) == ROUTER_KEY);

    if ((*ind) >= *size) {
        *size += TEMPORARY_PDU_STORE_INCREMENT_VALUE;
        void *tmp = realloc(*ary, *size * pdu_size);
        if (tmp == NULL) {
            const char txt[] = "Realloc failed";
            RTR_DBG("%s", txt);
            rtr_send_error_pdu(rtr_socket, pdu, pdu_size, INTERNAL_ERROR, txt, sizeof(txt));
            rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
            free(*ary);
            ary = NULL;
            return RTR_ERROR;
        }
        *ary = tmp;
    }

    memcpy((struct pdu_router_key *) *ary + *ind, pdu, pdu_size);
    (*ind)++;
    return RTR_SUCCESS;
}
コード例 #11
0
ファイル: packets.c プロジェクト: CZ-NIC/rtrlib
int rtr_sync_receive_and_store_pdus(struct rtr_socket *rtr_socket){
    char pdu[RTR_MAX_PDU_LEN];
    enum pdu_type type;
    int retval = RTR_SUCCESS;

    struct pdu_ipv6 *ipv6_pdus = NULL;
    unsigned int ipv6_pdus_nindex = 0; //next free index in ipv6_pdus
    unsigned int ipv6_pdus_size = 0;

    struct pdu_ipv4 *ipv4_pdus = NULL;
    unsigned int ipv4_pdus_size = 0; //next free index in ipv4_pdus
    unsigned int ipv4_pdus_nindex = 0;

    struct pdu_router_key *router_key_pdus = NULL;
    unsigned int router_key_pdus_size = 0;
    unsigned int router_key_pdus_nindex = 0;

    //receive LRTR_IPV4/IPV6 PDUs till EOD
    do {
        retval = rtr_receive_pdu(rtr_socket, pdu, RTR_MAX_PDU_LEN, RTR_RECV_TIMEOUT);
        if (retval == TR_WOULDBLOCK) {
            rtr_change_socket_state(rtr_socket, RTR_ERROR_TRANSPORT);
            retval = RTR_ERROR;
            goto cleanup;
        } else if (retval < 0) {
            retval = RTR_ERROR;
            goto cleanup;
        }

        type = rtr_get_pdu_type(pdu);
        if (type == IPV4_PREFIX) {
            if (rtr_store_prefix_pdu(rtr_socket, pdu, sizeof(*ipv4_pdus), (void **) &ipv4_pdus, &ipv4_pdus_nindex, &ipv4_pdus_size) == RTR_ERROR){
                rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
                retval = RTR_ERROR;
                goto cleanup;
            }
        } else if (type == IPV6_PREFIX) {
            if (rtr_store_prefix_pdu(rtr_socket, pdu, sizeof(*ipv6_pdus), (void **) &ipv6_pdus, &ipv6_pdus_nindex, &ipv6_pdus_size) == RTR_ERROR){
                rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
                retval = RTR_ERROR;
                goto cleanup;
            }
        } else if (type == ROUTER_KEY) {
            if (rtr_store_router_key_pdu(rtr_socket, pdu, sizeof(*router_key_pdus), &router_key_pdus, &router_key_pdus_nindex, &router_key_pdus_size) == RTR_ERROR){
                rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
                retval = RTR_ERROR;
                goto cleanup;
            }
        } else if (type == EOD) {
            RTR_DBG1("EOD PDU received.");
            struct pdu_end_of_data_v0 *eod_pdu = (struct pdu_end_of_data_v0 *) pdu;

            if (eod_pdu->session_id != rtr_socket->session_id) {
                char txt[67];
                snprintf(txt, sizeof(txt),"Expected session_id: %u, received session_id. %u in EOD PDU",rtr_socket->session_id, eod_pdu->session_id);
                rtr_send_error_pdu(rtr_socket, pdu, RTR_MAX_PDU_LEN, CORRUPT_DATA, txt, strlen(txt) + 1);
                rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
                retval = RTR_ERROR;
                goto cleanup;
            }

            if (eod_pdu->ver == RTR_PROTOCOL_VERSION_1) {
                rtr_socket->expire_interval = ((struct pdu_end_of_data_v1 *) pdu)->expire_interval;
                rtr_socket->refresh_interval = ((struct pdu_end_of_data_v1 *) pdu)->refresh_interval;
                rtr_socket->retry_interval = ((struct pdu_end_of_data_v1 *) pdu)->retry_interval;
                RTR_DBG("New interval values: expire_interval:%u, refresh_interval:%u, retry_interval:%u",
                        rtr_socket->expire_interval, rtr_socket->refresh_interval, rtr_socket->retry_interval);
            }

            int retval = PFX_SUCCESS;
            //add all IPv4 prefix pdu to the pfx_table
            for (unsigned int i = 0; i < ipv4_pdus_nindex; i++) {
                if (rtr_update_pfx_table(rtr_socket, &(ipv4_pdus[i])) == PFX_ERROR) {
                    //undo all record updates, except the last which produced the error
                    RTR_DBG("Error during data synchronisation, recovering Serial Nr. %u state",rtr_socket->serial_number);
                    for (unsigned int j = 0; j < i && retval == PFX_SUCCESS; j++)
                        retval = rtr_undo_update_pfx_table(rtr_socket, &(ipv4_pdus[j]));
                    if (retval == RTR_ERROR) {
                        RTR_DBG1("Couldn't undo all update operations from failed data synchronisation: Purging all records");
                        pfx_table_src_remove(rtr_socket->pfx_table, rtr_socket);
                        rtr_socket->request_session_id = true;
                    }
                    rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
                    retval = RTR_ERROR;
                    goto cleanup;
                }
            }
            //add all IPv6 prefix pdu to the pfx_table
            for (unsigned int i = 0; i < ipv6_pdus_nindex; i++) {
                if (rtr_update_pfx_table(rtr_socket, &(ipv6_pdus[i])) == PFX_ERROR) {
                    //undo all record updates if error occured
                    RTR_DBG("Error during data synchronisation, recovering Serial Nr. %u state",rtr_socket->serial_number);
                    for (unsigned int j = 0; j < ipv4_pdus_nindex && retval == PFX_SUCCESS; j++)
                        retval = rtr_undo_update_pfx_table(rtr_socket, &(ipv4_pdus[j]));
                    for (unsigned int j = 0; j < i && retval == PFX_SUCCESS; j++)
                        retval = rtr_undo_update_pfx_table(rtr_socket, &(ipv6_pdus[j]));
                    if (retval == PFX_ERROR) {
                        RTR_DBG1("Couldn't undo all update operations from failed data synchronisation: Purging all records");
                        pfx_table_src_remove(rtr_socket->pfx_table, rtr_socket);
                        rtr_socket->request_session_id = true;
                    }
                    rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
                    retval = RTR_ERROR;
                    goto cleanup;
                }
            }

            //add all router key pdu to the spki_table
            for (unsigned int i = 0; i < router_key_pdus_nindex; i++) {
                if (rtr_update_spki_table(rtr_socket, &(router_key_pdus[i])) == SPKI_ERROR) {
                    RTR_DBG("Error during router key data synchronisation, recovering Serial Nr. %u state",rtr_socket->serial_number);
                    for (unsigned int j = 0; j < ipv4_pdus_nindex && retval == PFX_SUCCESS; j++)
                        retval = rtr_undo_update_pfx_table(rtr_socket, &(ipv4_pdus[j]));
                    for (unsigned int j = 0; j < ipv6_pdus_nindex && retval == PFX_SUCCESS; j++)
                        retval = rtr_undo_update_pfx_table(rtr_socket, &(ipv6_pdus[j]));
		     // cppcheck-suppress duplicateExpression
                    for (unsigned int j = 0; j < i && (retval == PFX_SUCCESS || retval == SPKI_SUCCESS); j++)
                        retval = rtr_undo_update_spki_table(rtr_socket, &(router_key_pdus[j]));
		     // cppcheck-suppress duplicateExpression
                    if (retval == RTR_ERROR || retval == SPKI_ERROR) {
                        RTR_DBG1("Couldn't undo all update operations from failed data synchronisation: Purging all key entries");
                        spki_table_src_remove(rtr_socket->spki_table, rtr_socket);
                        rtr_socket->request_session_id = true;
                    }
                    rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
                    retval = RTR_ERROR;
                    goto cleanup;
                }
            }
            rtr_socket->serial_number = eod_pdu->sn;
            RTR_DBG("Sync successfull, received %u Prefix PDUs, %u Router Key PDUs, session_id: %u, SN: %u", (ipv4_pdus_nindex + ipv6_pdus_nindex), router_key_pdus_nindex,rtr_socket->session_id,rtr_socket->serial_number);
            goto cleanup;
        } else if (type == ERROR) {
            rtr_handle_error_pdu(rtr_socket, pdu);
            retval = RTR_ERROR;
            goto cleanup;
        } else if (type == SERIAL_NOTIFY) {
            RTR_DBG1("Ignoring Serial Notify");
        } else {
            RTR_DBG("Received unexpected PDU (Type: %u)", ((struct pdu_header *) pdu)->type);
            const char txt[] = "Unexpected PDU received during data synchronisation";
            rtr_send_error_pdu(rtr_socket, pdu, sizeof(struct pdu_header), CORRUPT_DATA, txt, sizeof(txt));
            retval = RTR_ERROR;
            goto cleanup;
        }
    } while (type != EOD);

    cleanup:
    free(router_key_pdus);
    free(ipv6_pdus);
    free(ipv4_pdus);
    return retval;
}
コード例 #12
0
ファイル: packets.c プロジェクト: CZ-NIC/rtrlib
static int rtr_handle_error_pdu(struct rtr_socket *rtr_socket, const void *buf)
{
    RTR_DBG1("Error PDU received");  //TODO: append server ip & port
    const struct pdu_error *pdu = buf;

    switch (pdu->error_code) {
    case CORRUPT_DATA:
        RTR_DBG1("Corrupt data received");
        rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
        break;
    case INTERNAL_ERROR:
        RTR_DBG1("Internal error on server-side");
        rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
        break;
    case NO_DATA_AVAIL:
        RTR_DBG1("No data available");
        rtr_change_socket_state(rtr_socket, RTR_ERROR_NO_DATA_AVAIL);
        break;
    case INVALID_REQUEST:
        RTR_DBG1("Invalid request from client");
        rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
        break;
    case UNSUPPORTED_PROTOCOL_VER:
        RTR_DBG1("Client uses unsupported protocol version");
        if (pdu->ver <= RTR_PROTOCOL_MAX_SUPPORTED_VERSION &&
                pdu->ver >= RTR_PROTOCOL_MIN_SUPPORTED_VERSION &&
                pdu->ver < rtr_socket->version)
        {
            RTR_DBG("Downgrading from %i to version %i", rtr_socket->version, pdu->ver);
            rtr_socket->version = pdu->ver;
            rtr_change_socket_state(rtr_socket, RTR_FAST_RECONNECT);
        }
        else {
            RTR_DBG("Got UNSUPPORTED_PROTOCOL_VER error PDU with invalid values,\
                    current version:%i, PDU version:%i", rtr_socket->version, pdu->ver);
            rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
        }
        break;
    case UNSUPPORTED_PDU_TYPE:
        RTR_DBG1("Client set unsupported PDU type");
        rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
        break;
    default:
        RTR_DBG("error unknown, server sent unsupported error code %u", pdu->error_code);
        rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
        break;
    }

    const uint32_t len_err_txt = ntohl(*((uint32_t *) (pdu->rest + pdu->len_enc_pdu)));
    if (len_err_txt > 0) {
        if ((sizeof(pdu->ver) + sizeof(pdu->type) + sizeof(pdu->error_code) + sizeof(pdu->len) + sizeof(pdu->len_enc_pdu) + pdu->len_enc_pdu + 4 + len_err_txt) != pdu->len)
            RTR_DBG1("error: Length of error text contains an incorrect value");
        else {
            //assure that the error text contains an terminating \0 char
            char txt[len_err_txt + 1];
            char *pdu_txt = (char *) pdu->rest + pdu->len_enc_pdu + 4;
            snprintf(txt, len_err_txt + 1, "%s", pdu_txt);
            RTR_DBG("Error PDU included the following error msg: \'%s\'", txt);
        }
    }

    return RTR_SUCCESS;
}
コード例 #13
0
ファイル: packets.c プロジェクト: CZ-NIC/rtrlib
/*
 * if RTR_ERROR was returned a error PDU was sent, and the socket state changed
 * @param pdu_len must >= RTR_MAX_PDU_LEN Bytes
 * @return RTR_SUCCESS
 * @return RTR_ERROR, error pdu was sent and socket_state changed
 * @return TR_WOULDBLOCK
 * \post
 * If RTR_SUCCESS is returned PDU points to a well formed PDU that has
 * the appropriate size for the PDU type it pretend to be. Thus, casting it to
 * the PDU type struct and using it is save. Furthermore all PDU field are
 * in host-byte-order.
 */
static int rtr_receive_pdu(struct rtr_socket *rtr_socket, void *pdu, const size_t pdu_len, const time_t timeout)
{
    int error = RTR_SUCCESS;

    assert(pdu_len >= RTR_MAX_PDU_LEN);

    if (rtr_socket->state == RTR_SHUTDOWN)
        return RTR_ERROR;
    //receive packet header
    error = tr_recv_all(rtr_socket->tr_socket, pdu, sizeof(struct pdu_header), timeout);
    if (error < 0)
        goto error;
    else
        error = RTR_SUCCESS;

    //header in hostbyte order, retain original received pdu, in case we need to detach it to an error pdu
    struct pdu_header header;
    memcpy(&header, pdu, sizeof(header));
    rtr_pdu_header_to_host_byte_order(&header);

    //if header->len is < packet_header = corrupt data received
    if (header.len < sizeof(header)) {
        error = CORRUPT_DATA;
        goto error;
    } else if (header.len > RTR_MAX_PDU_LEN) { //PDU too big, > than MAX_PDU_LEN Bytes
        error = PDU_TOO_BIG;
        goto error;
    }

    //Handle live downgrading
    if (rtr_socket->has_received_pdus == false) {
      if (rtr_socket->version == RTR_PROTOCOL_VERSION_1
          && header.ver == RTR_PROTOCOL_VERSION_0
          && header.type != ERROR) {
        RTR_DBG("First received PDU is a version 0 PDU, downgrading to %u", RTR_PROTOCOL_VERSION_0);
        rtr_socket->version = RTR_PROTOCOL_VERSION_0;
      }
      rtr_socket->has_received_pdus = true;
    }

    //Handle wrong protocol version
    //If it is a error PDU, it will be handled by rtr_handle_error_pdu
    if (header.ver != rtr_socket->version  && header.type != ERROR) {
      error = UNEXPECTED_PROTOCOL_VERSION;
      goto error;
    }


    //receive packet payload
    const unsigned int remaining_len = header.len - sizeof(header);
    if (remaining_len > 0) {
        if (rtr_socket->state == RTR_SHUTDOWN)
            return RTR_ERROR;
        error = tr_recv_all(rtr_socket->tr_socket, (((char *) pdu) + sizeof(header)), remaining_len, RTR_RECV_TIMEOUT);
        if (error < 0)
            goto error;
        else
            error = RTR_SUCCESS;
    }
    //copy header in host_byte_order to pdu
    memcpy(pdu, &header, sizeof(header));

    //Check if the header len value is valid
    if (rtr_pdu_check_size(pdu) == false) {
      //TODO Restore byteorder for sending error PDU
      error = CORRUPT_DATA;
      goto error;
    }
    //At this point it is save to cast and use the PDU

    rtr_pdu_footer_to_host_byte_order(pdu);

    //Here we should handle error PDUs instead of doing it in
    //several other places...

    if (header.type == IPV4_PREFIX || header.type == IPV6_PREFIX) {
        if (((struct pdu_ipv4 *) pdu)->zero != 0)
            RTR_DBG1("Warning: Zero field of received Prefix PDU doesn't contain 0");

    }
    if (header.type == ROUTER_KEY && ((struct pdu_router_key *) pdu)->zero != 0)
        RTR_DBG1("Warning: ROUTER_KEY_PDU zero field is != 0");


    return RTR_SUCCESS;

error:
    //send error msg to server, including unmodified pdu header(pdu variable instead header)
    if (error == -1) {
        rtr_change_socket_state(rtr_socket, RTR_ERROR_TRANSPORT);
        return RTR_ERROR;
    } else if (error == TR_WOULDBLOCK) {
        RTR_DBG1("receive timeout expired");
        return TR_WOULDBLOCK;
    } else if (error == TR_INTR) {
        RTR_DBG1("receive call interrupted");
        return TR_INTR;
    } else if (error == CORRUPT_DATA) {
        RTR_DBG1("corrupt PDU received");
        const char txt[] = "corrupt data received, length value in PDU is too small";
        rtr_send_error_pdu(rtr_socket, pdu, sizeof(header), CORRUPT_DATA, txt, sizeof(txt));
    } else if (error == PDU_TOO_BIG) {
        RTR_DBG1("PDU too big");
        char txt[42];
        snprintf(txt, sizeof(txt),"PDU too big, max. PDU size is: %u bytes", RTR_MAX_PDU_LEN);
        RTR_DBG("%s", txt);
        rtr_send_error_pdu(rtr_socket, pdu, sizeof(header), CORRUPT_DATA, txt, sizeof(txt));
    } else if (error == UNSUPPORTED_PDU_TYPE) {
        RTR_DBG("Unsupported PDU type (%u) received", header.type);
        rtr_send_error_pdu(rtr_socket, pdu, sizeof(header), UNSUPPORTED_PDU_TYPE, NULL, 0);
    } else if (error == UNSUPPORTED_PROTOCOL_VER) {
        RTR_DBG("PDU with unsupported Protocol version (%u) received", header.ver);
        rtr_send_error_pdu(rtr_socket, pdu, sizeof(header), UNSUPPORTED_PROTOCOL_VER, NULL, 0);
        return RTR_ERROR;
    } else if (error == UNEXPECTED_PROTOCOL_VERSION) {
        RTR_DBG("PDU with unexpected Protocol version (%u) received", header.ver);
        rtr_send_error_pdu(rtr_socket, pdu, sizeof(header), UNEXPECTED_PROTOCOL_VERSION, NULL, 0);
        return RTR_ERROR;
    }

    rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
    return RTR_ERROR;
}
コード例 #14
0
ファイル: packets.c プロジェクト: CZ-NIC/rtrlib
int rtr_sync(struct rtr_socket *rtr_socket)
{
    char pdu[RTR_MAX_PDU_LEN];
    int rtval = rtr_receive_pdu(rtr_socket, pdu, RTR_MAX_PDU_LEN, RTR_RECV_TIMEOUT);
    //If the cache has closed the connection and we dont have a session_id
    //(no packages where exchanged) we should downgrade.
    if(rtval == TR_CLOSED && rtr_socket->request_session_id){
        RTR_DBG1("The cache server closed the connection and we have no session_id!");
        if (rtr_socket->version > RTR_PROTOCOL_MIN_SUPPORTED_VERSION)
        {
            RTR_DBG("Downgrading from %i to version %i", rtr_socket->version, rtr_socket->version-1);
            rtr_socket->version = rtr_socket->version - 1;
            rtr_change_socket_state(rtr_socket, RTR_FAST_RECONNECT);
            return RTR_ERROR;
        }
    }
    if (rtval == TR_WOULDBLOCK) {
        rtr_change_socket_state(rtr_socket, RTR_ERROR_TRANSPORT);
        return RTR_ERROR;
    } else if (rtval < 0)
        return RTR_ERROR;

    enum pdu_type type = rtr_get_pdu_type(pdu);
    //ignore serial_notify PDUs, we already sent a serial_query, must be old messages
    while (type == SERIAL_NOTIFY) {
        RTR_DBG1("Ignoring Serial Notify");
        rtval = rtr_receive_pdu(rtr_socket, pdu, RTR_MAX_PDU_LEN, RTR_RECV_TIMEOUT);
        if (rtval == TR_WOULDBLOCK) {
            rtr_change_socket_state(rtr_socket, RTR_ERROR_TRANSPORT);
            return RTR_ERROR;
        } else if (rtval < 0)
            return RTR_ERROR;
        type = rtr_get_pdu_type(pdu);
    }

    if (type == ERROR) {
        rtr_handle_error_pdu(rtr_socket, pdu);
        return RTR_ERROR;
    } else if (type == CACHE_RESET) {
        RTR_DBG1("Cache Reset PDU received");
        rtr_change_socket_state(rtr_socket, RTR_ERROR_NO_INCR_UPDATE_AVAIL);
        return RTR_ERROR;
    } else if (type == CACHE_RESPONSE) {
        rtr_handle_cache_response_pdu(rtr_socket,pdu);
    } else if (type == ERROR) {
        rtr_handle_error_pdu(rtr_socket, pdu);
        return RTR_ERROR;
    } else {
        RTR_DBG("Expected Cache Response PDU but received PDU Type (Type: %u)", ((struct pdu_header *) pdu)->type);
        const char txt[] = "Unexpected PDU received in data synchronisation";
        rtr_send_error_pdu(rtr_socket, pdu, sizeof(struct pdu_header), CORRUPT_DATA, txt, sizeof(txt));
        return RTR_ERROR;
    }

    //Receive all PDUs until EOD PDU
    if(rtr_sync_receive_and_store_pdus(rtr_socket) == RTR_ERROR){
        return RTR_ERROR;
    }

    rtr_socket->request_session_id = false;
    if (rtr_set_last_update(rtr_socket) == RTR_ERROR)
        return RTR_ERROR;
    return RTR_SUCCESS;
}
コード例 #15
0
ファイル: rtr.c プロジェクト: mceyran/rtrlib
void rtr_fsm_start(struct rtr_socket *rtr_socket) {
    rtr_socket->state = RTR_CONNECTING;
    install_sig_handler();
    while(1) {
        if(rtr_socket->state == RTR_CONNECTING) {
            RTR_DBG1("State: RTR_CONNECTING");
            //old pfx_record could exists in the pfx_table, check if they are too old and must be removed
            rtr_purge_outdated_records(rtr_socket);

            if(tr_open(rtr_socket->tr_socket) == TR_ERROR) {
                rtr_change_socket_state(rtr_socket, RTR_ERROR_TRANSPORT);
            }
            else if(rtr_socket->request_session_id) {
                //change to state RESET, if socket dont has a session_id
                rtr_change_socket_state(rtr_socket, RTR_RESET);
            }
            else {
                //if we already have a session_id, send a serial query and start to sync
                if(rtr_send_serial_query(rtr_socket) == RTR_SUCCESS)
                    rtr_change_socket_state(rtr_socket, RTR_SYNC);
                else
                    rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
            }

        }

        else if(rtr_socket->state == RTR_RESET) {
            RTR_DBG1("State: RTR_RESET");
            if (rtr_send_reset_query(rtr_socket) == 0) {
                RTR_DBG1("rtr_start: reset pdu sent");
                rtr_change_socket_state(rtr_socket, RTR_SYNC); //send reset query after connection established
            }
        }

        else if(rtr_socket->state == RTR_SYNC) {
            RTR_DBG1("State: RTR_SYNC");
            if(rtr_sync(rtr_socket) == 0)
                rtr_change_socket_state(rtr_socket, RTR_ESTABLISHED); //send reset query after connection established
        }

        else if(rtr_socket->state == RTR_ESTABLISHED) {
            RTR_DBG1("State: RTR_ESTABLISHED");
            if(rtr_wait_for_sync(rtr_socket) == RTR_SUCCESS) { //blocks till cache_timeout is expired or PDU was received
                //serial query senden
                if(rtr_send_serial_query(rtr_socket) == RTR_SUCCESS)
                    rtr_change_socket_state(rtr_socket, RTR_SYNC);
            }
        }

        else if(rtr_socket->state == RTR_ERROR_NO_DATA_AVAIL) {
            RTR_DBG1("State: RTR_ERROR_NO_DATA_AVAIL");
            rtr_socket->request_session_id = true;
            rtr_socket->serial_number = 0;
            rtr_change_socket_state(rtr_socket, RTR_RESET);
            sleep(ERR_TIMEOUT);
            rtr_purge_outdated_records(rtr_socket);
        }

        else if(rtr_socket->state == RTR_ERROR_NO_INCR_UPDATE_AVAIL) {
            RTR_DBG1("State: RTR_ERROR_NO_INCR_UPDATE_AVAIL");
            rtr_socket->request_session_id = true;
            rtr_socket->serial_number = 0;
            rtr_change_socket_state(rtr_socket, RTR_RESET);
            rtr_purge_outdated_records(rtr_socket);
        }

        else if(rtr_socket->state == RTR_ERROR_TRANSPORT) {
            RTR_DBG1("State: RTR_ERROR_TRANSPORT");
            tr_close(rtr_socket->tr_socket);
            rtr_change_socket_state(rtr_socket, RTR_CONNECTING);
            sleep(ERR_TIMEOUT);
        }

        else if(rtr_socket->state == RTR_ERROR_FATAL) {
            RTR_DBG1("State: RTR_ERROR_FATAL");
            tr_close(rtr_socket->tr_socket);
            rtr_change_socket_state(rtr_socket, RTR_CONNECTING);
            sleep(ERR_TIMEOUT);
        }

        else if(rtr_socket->state == RTR_SHUTDOWN) {
            RTR_DBG1("State: RTR_SHUTDOWN");
            tr_close(rtr_socket->tr_socket);
            rtr_socket->request_session_id = true;
            rtr_socket->serial_number = 0;
            rtr_socket->last_update = 0;
            pfx_table_src_remove(rtr_socket->pfx_table, rtr_socket);
            pthread_exit(NULL);
        }
    }
}
コード例 #16
0
ファイル: rtr.c プロジェクト: ColinBS/rtrlib
/* WARNING: This Function has cancelable sections*/
void *rtr_fsm_start(struct rtr_socket *rtr_socket)
{
   if (rtr_socket->state == RTR_SHUTDOWN)
	return NULL;

    // We don't care about the old state, but POSIX demands a non null value for setcancelstate
    int oldcancelstate;
    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);

    rtr_socket->state = RTR_CONNECTING;
    while(1) {
        if(rtr_socket->state == RTR_CONNECTING) {
            RTR_DBG1("State: RTR_CONNECTING");
            rtr_socket->has_received_pdus = false;

            //old pfx_record could exists in the pfx_table, check if they are too old and must be removed
            //old key_entry could exists in the spki_table, check if they are too old and must be removed
            rtr_purge_outdated_records(rtr_socket);

            if(tr_open(rtr_socket->tr_socket) == TR_ERROR) {
                rtr_change_socket_state(rtr_socket, RTR_ERROR_TRANSPORT);
            } else if(rtr_socket->request_session_id) {
                //change to state RESET, if socket dont has a session_id
                rtr_change_socket_state(rtr_socket, RTR_RESET);
            } else {
                //if we already have a session_id, send a serial query and start to sync
                if(rtr_send_serial_query(rtr_socket) == RTR_SUCCESS)
                    rtr_change_socket_state(rtr_socket, RTR_SYNC);
                else
                    rtr_change_socket_state(rtr_socket, RTR_ERROR_FATAL);
            }

        }

        else if(rtr_socket->state == RTR_RESET) {
            RTR_DBG1("State: RTR_RESET");
            if (rtr_send_reset_query(rtr_socket) == RTR_SUCCESS) {
                RTR_DBG1("rtr_start: reset pdu sent");
                rtr_change_socket_state(rtr_socket, RTR_SYNC); //start to sync after connection is established
            }
        }

        else if(rtr_socket->state == RTR_SYNC) {
            RTR_DBG1("State: RTR_SYNC");
            if(rtr_sync(rtr_socket) == RTR_SUCCESS)
                rtr_change_socket_state(rtr_socket, RTR_ESTABLISHED); //wait for next sync after first successful sync
        }

        else if(rtr_socket->state == RTR_ESTABLISHED) {
            RTR_DBG1("State: RTR_ESTABLISHED");

            // Allow thread cancellation for recv code path only.
            // This should be enough since we spend most of the time blocking on recv
            pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldcancelstate);
            int ret = rtr_wait_for_sync(rtr_socket); //blocks till expire_interval is expired or PDU was received
            pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldcancelstate);

            if(ret == RTR_SUCCESS) { //send serial query
                if(rtr_send_serial_query(rtr_socket) == RTR_SUCCESS)
                    rtr_change_socket_state(rtr_socket, RTR_SYNC);
            }
        }

        else if(rtr_socket->state == RTR_FAST_RECONNECT){
            RTR_DBG1("State: RTR_FAST_RECONNECT");
            tr_close(rtr_socket->tr_socket);
            rtr_change_socket_state(rtr_socket, RTR_CONNECTING);
        }

        else if(rtr_socket->state == RTR_ERROR_NO_DATA_AVAIL) {
            RTR_DBG1("State: RTR_ERROR_NO_DATA_AVAIL");
            rtr_socket->request_session_id = true;
            rtr_socket->serial_number = 0;
            rtr_change_socket_state(rtr_socket, RTR_RESET);
            sleep(rtr_socket->retry_interval);
            rtr_purge_outdated_records(rtr_socket);
        }

        else if(rtr_socket->state == RTR_ERROR_NO_INCR_UPDATE_AVAIL) {
            RTR_DBG1("State: RTR_ERROR_NO_INCR_UPDATE_AVAIL");
            rtr_socket->request_session_id = true;
            rtr_socket->serial_number = 0;
            rtr_change_socket_state(rtr_socket, RTR_RESET);
            rtr_purge_outdated_records(rtr_socket);
        }

        else if(rtr_socket->state == RTR_ERROR_TRANSPORT) {
            RTR_DBG1("State: RTR_ERROR_TRANSPORT");
            tr_close(rtr_socket->tr_socket);
            rtr_change_socket_state(rtr_socket, RTR_CONNECTING);
            RTR_DBG("Waiting %u", rtr_socket->retry_interval);
            sleep(rtr_socket->retry_interval);
        }

        else if(rtr_socket->state == RTR_ERROR_FATAL) {
            RTR_DBG1("State: RTR_ERROR_FATAL");
            tr_close(rtr_socket->tr_socket);
            rtr_change_socket_state(rtr_socket, RTR_CONNECTING);
            RTR_DBG("Waiting %u", rtr_socket->retry_interval);
            sleep(rtr_socket->retry_interval);
        }

        else if(rtr_socket->state == RTR_SHUTDOWN) {
            RTR_DBG1("State: RTR_SHUTDOWN");
            pthread_exit(NULL);
        }
    }
}