forked from lukablurr/n2n_v3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn2n_net.h
148 lines (101 loc) · 3.64 KB
/
n2n_net.h
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* (C) 2007-09 - Luca Deri <[email protected]>
* Richard Andrews <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>
*
* Code contributions courtesy of:
* Babak Farrokhi <[email protected]> [FreeBSD port]
* Lukasz Taczuk
*
*/
#ifndef N2N_NET_H_
#define N2N_NET_H_
#ifndef WIN32
#include <netdb.h>//TODO ???
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <pthread.h>
#ifdef __FreeBSD__
#include <netinet/in_systm.h>
#endif /* #ifdef __FreeBSD__ */
/* *********************************************** */
/* Layer 2 */
#define ETH_ADDR_LEN 6
struct ether_hdr
{
uint8_t dhost[ETH_ADDR_LEN];
uint8_t shost[ETH_ADDR_LEN];
uint16_t type; /* higher layer protocol encapsulated */
} __attribute__ ((__packed__));
typedef struct ether_hdr ether_hdr_t;
//#define N2N_MAC_SIZE 6
typedef uint8_t n2n_mac_t[ETH_ADDR_LEN];
/** Common type used to hold stringified MAC addresses. */
#define N2N_MACSTR_SIZE 32
typedef char macstr_t[N2N_MACSTR_SIZE];
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <signal.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <unistd.h>
#endif /* #ifndef WIN32 */
int is_broadcast_mac(const uint8_t *mac);
int is_multicast_mac(const uint8_t *mac);
int is_ipv6_multicast_mac(const uint8_t *mac);
extern uint8_t is_multi_broadcast_mac(const uint8_t *dest_mac);
extern char *macaddr_str(macstr_t buf, const n2n_mac_t mac);
extern int str2mac( uint8_t *outmac /* 6 bytes */, const char *s);
/* *********************************************** */
/* Layer 3 */
#define IPV4_SIZE 4
#define IPV6_SIZE 16
/** Common type used to hold stringified IP addresses. */
typedef char ipstr_t[32];
extern char *intoa(uint32_t addr, char *buf, uint16_t buf_len);
/* *********************************************** */
/* Layer 4 */
#define DEFAULT_MTU 1400
#ifndef WIN32
#define closesocket(a) close(a)
#define SOCKET int
#endif /* #ifndef WIN32 */
struct n2n_sock
{
uint8_t family; /* AF_INET or AF_INET6; or 0 if invalid */
uint16_t port; /* host order */
union
{
uint8_t v6[IPV6_SIZE]; /* byte sequence */
uint8_t v4[IPV4_SIZE]; /* byte sequence */
} addr;
};
typedef struct n2n_sock n2n_sock_t;
#define N2N_SOCKBUF_SIZE 64 /* string representation of INET or INET6 sockets */
typedef char n2n_sock_str_t[N2N_SOCKBUF_SIZE]; /* tracing string buffer */
/* functions */
extern SOCKET open_socket(int local_port, int bind_any);
extern ssize_t sendto_sock(int sock_fd,
const void *pktbuf,
size_t pktsize,
const n2n_sock_t *dest);
extern int is_empty_ip_address(const n2n_sock_t *sock);
extern int sock_equal(const n2n_sock_t *a, const n2n_sock_t *b);
extern char* sock_to_cstr(n2n_sock_str_t out, const n2n_sock_t *sock);
extern n2n_sock_t* sock_from_cstr(n2n_sock_t *out, const n2n_sock_str_t str);
#endif /* N2N_NET_H_ */