aboutsummaryrefslogtreecommitdiff
path: root/src/pdesc.c
blob: 77f511e9de82996f6ebab793bd20a6d57daf18a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "pdesc.h"
#include "ppkt.h"
#include "psock.h"

#include <netinet/ip_icmp.h>

static void pdesc_init(struct psock * psock, struct pdesc * pdesc, uint16_t identifier)
{
    pdesc->state = PDESC_STATE_AUTH;
    pdesc->peer = psock->current.peer;
    pdesc->identifier = identifier;
    pdesc->sequence = 0;
}

struct pdesc * pdesc_find_remote(struct psock * psock)
{
    size_t i;

    if (psock->remotes.used == psock->remotes.max || ppkt_process_icmp(psock) != 0 ||
        psock->current.packet.icmphdr->type != ICMP_ECHOREPLY || ppkt_process_ppkt(psock) != 0) {
        return NULL;
    }

    for (i = 0; i < psock->remotes.used; ++i) {
        if (psock->current.packet.icmphdr->un.echo.id == psock->remotes.descriptors[i].identifier) {
            return &psock->remotes.descriptors[i];
        }
    }

    pdesc_init(psock, &psock->remotes.descriptors[i], psock->current.packet.icmphdr->un.echo.id);

    return &psock->remotes.descriptors[i];
}