nat_traversal.h 985 Bytes
Newer Older
Jason Fong's avatar
Jason Fong committed
1
2
3
4
5
6
7
8
9
#include <stdint.h>

#include "nat_type.h"

typedef struct client client;
struct client {
    int sfd;
    uint32_t id;
    char buf[128];
Fong's avatar
Fong committed
10
    //use a stack-based buffer to prevent memory allocation every time
Jason Fong's avatar
Jason Fong committed
11
12
13
14
    char* msg_buf;
    nat_type type;
    char ext_ip[16];
    uint16_t ext_port;
Fong's avatar
Fong committed
15
16
17
18
19
    // ttl of hole punching packets, 
    // it should be greater than the number of hops between host to NAT of own side
    // and less than the number of hops between host to NAT of remote side,
    // so that the hole punching packets just die in the way
    int ttl; 
Jason Fong's avatar
Jason Fong committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
};

struct peer_info {
    char ip[16];
    uint16_t port;
    uint16_t type;
};

enum msg_type {     
     Enroll = 0x01,      
     GetPeerInfo = 0x02,     
     NotifyPeer = 0x03,      
 };

// public functions
Fong's avatar
Fong committed
35
int enroll(struct peer_info self, struct sockaddr_in punch_server, client* c);
Fong's avatar
Fong committed
36
pthread_t wait_for_command(int* server_sock);
Jason Fong's avatar
Jason Fong committed
37
38
int connect_to_peer(client* cli, uint32_t peer_id);
void on_connected(int sock);