The Netsukuku Project  0.0.9
An Alternative routing method
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
inet.h
Go to the documentation of this file.
1 /* This file is part of Netsukuku
2  * (c) Copyright 2005 Andrea Lo Pumo aka AlpT <alpt@freaknet.org>
3  *
4  * This source code is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  *
9  * This source code is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  * Please refer to the GNU Public License for more details.
13  *
14  * You should have received a copy of the GNU Public License along with
15  * this source code; if not, write to:
16  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 
19 #ifndef INET_H
20 #define INET_H
21 
22 #include "endianness.h"
23 
24 #define MAX_IP_INT 4
25 #define MAX_IP_SZ (MAX_IP_INT*sizeof(int))
26 
27 /*
28  * This is the "link-scope all-hosts multicast" address: ff02::1.
29  */
30 #define IPV6_ADDR_BROADCAST { 0xff020000, 0x0, 0x0, 0x1 }
31 
32 #define LOOPBACK_IP 0x7f000001
33 #define LOOPBACK_NET 0x7f000000
34 #define LOOPBACK_BCAST 0x7fffffff
35 
36 #define LOOPBACK_IPV6 { 0x0, 0x0, 0x0, 0x1 }
37 
38 /*
39  * `x' is in host byte order
40  *
41  * NTK_RESTRICTED_10_MASK(x):
42  * given an ipv4 IP it returns the equivalent in the 10.x.x.x class, i.e.
43  * 212.13.4.1 --> 10.13.4.1
44  *
45  * NTK_RESTRICTED_172_MASK(x): it's the same of NTK_RESTRICTED_10_MASK() but
46  * it converts the IP in the 172.16.0.0 - 172.31.255.255 range.
47  *
48  * NTK_RESTRICTED_IPV6_MASK(x):
49  * `x' in this case is the first integer of the four of an ipv6 IP.
50  * The conversion is: `x' --> fec0:xxxx:...
51  *
52  */
53 #define NTK_RESTRICTED_10_MASK(x) (((x) & ~0xff000000)|0x0a000000)
54 #define NTK_RESTRICTED_172_MASK(x) (((((x) & ~0xff000000) | 0xac000000) & ~0x00e00000) | 0x00100000)
55 #define NTK_RESTRICTED_IPV6_MASK(x) (((x) & ~0xffff0000)|0xfec00000)
56 
57 
58 /* `x' is in network order.
59  * Is `x' an IP in the range of 192.168.0.0 - 192.168.255.255 ? */
60 #define NTK_PRIVATE_C(x) (((x) & __constant_htonl(0xffff0000)) == __constant_htonl(0xc0a80000))
61 
62 /* `x' is in network order.
63  * Is `x' in 172.16.0.0 - 172.31.255.255 ? */
64 #define NTK_PRIVATE_B(x) (((x) & __constant_htonl(0xff000000)) == __constant_htonl(0xac000000))\
65  && ((x) & __constant_htonl(0x00100000)) && \
66  !((x) & __constant_htonl(0x00e00000))
67 
68 
69 /*
70  * The inet_prefix struct is used to store IP addresses in the internals of
71  * the Netsukuku code
72  */
73 typedef struct
74 {
75  u_char family; /* AF_INET or AF_INET6 */
76  u_short len; /* IP length: 4 or 16 (bytes) */
77  u_char bits; /* Number of used bits of the IP */
78  u_int data[MAX_IP_INT]; /* The address is kept in host long format,
79  word ORDER 1 (most significant word first) */
81 
82 /* int_info struct used for packing the inet_prefix struct.
83  * Note that `data' is ignored 'cause it will be converted with
84  * inet_htonl() / inet_ntohl() */
86  { INT_TYPE_16BIT },
87  { sizeof(u_char) },
88  { 1 }
89  };
90 #define INET_PREFIX_PACK_SZ (sizeof(u_char) + sizeof(u_short) +\
91  sizeof(u_char) + MAX_IP_SZ)
92 
93 
94 /* * * defines from linux/in.h * * */
95 #define LOOPBACK(x) (((x) & htonl(0xff000000)) == htonl(0x7f000000))
96 #define MULTICAST(x) (((x) & htonl(0xf0000000)) == htonl(0xe0000000))
97 #define BADCLASS(x) (((x) & htonl(0xf0000000)) == htonl(0xf0000000))
98 #define ZERONET(x) (((x) & htonl(0xff000000)) == htonl(0x00000000))
99 #define LOCAL_MCAST(x) (((x) & htonl(0xFFFFFF00)) == htonl(0xE0000000))
100 
101 /* * * defines from linux/include/net/ipv6.h * * */
102 #define IPV6_ADDR_ANY 0x0000U
103 
104 #define IPV6_ADDR_UNICAST 0x0001U
105 #define IPV6_ADDR_MULTICAST 0x0002U
106 
107 #define IPV6_ADDR_LOOPBACK 0x0010U
108 #define IPV6_ADDR_LINKLOCAL 0x0020U
109 #define IPV6_ADDR_SITELOCAL 0x0040U
110 
111 #define IPV6_ADDR_COMPATv4 0x0080U
112 
113 #define IPV6_ADDR_SCOPE_MASK 0x00f0U
114 
115 #define IPV6_ADDR_MAPPED 0x1000U
116 #define IPV6_ADDR_RESERVED 0x2000U /* reserved address space */
117 
118 /*
119  * Type of Service
120  */
121 #ifndef IPTOS_LOWDELAY
122 #define IPTOS_LOWDELAY 0x10
123 #define IPTOS_THROUGHPUT 0x08
124 #define IPTOS_RELIABILITY 0x04
125 #define IPTOS_LOWCOST 0x02
126 #define IPTOS_MINCOST IPTOS_LOWCOST
127 #endif /* IPTOS_LOWDELAY */
128 
129 
130 /*
131  * Globals
132  */
133 
134 #define RESTRICTED_10 1 /* We are using the 10.x.x.x class for
135  the restricted mode */
136 #define RESTRICTED_172 2 /* 172.16.0.0-172.31.255.255 class */
137 
138 #define RESTRICTED_10_STR "10.0.0.0-10.255.255.255"
139 #define RESTRICTED_172_STR "172.16.0.0-172.31.255.255"
140 
142 
143 /*
144  * * * Functions declaration * *
145  */
146 void inet_ntohl(u_int *data, int family);
147 void inet_htonl(u_int *data, int family);
148 int inet_setip_raw(inet_prefix *ip, u_int *data, int family);
149 int inet_setip(inet_prefix *ip, u_int *data, int family);
150 int inet_setip_bcast(inet_prefix *ip, int family);
153 int inet_setip_localaddr(inet_prefix *ip, int family, int class);
154 int inet_is_ip_local(inet_prefix *ip, int class);
155 void inet_copy_ipdata_raw(u_int *dst_data, inet_prefix *ip);
156 void inet_copy_ipdata(u_int *dst_data, inet_prefix *ip);
157 void inet_copy(inet_prefix *dst, inet_prefix *src);
158 void pack_inet_prefix(inet_prefix *ip, char *pack);
159 void unpack_inet_prefix(inet_prefix *ip, char *pack);
160 int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits);
161 int ipv6_addr_type(inet_prefix addr);
163 
164 const char *ipraw_to_str(u_int ip[MAX_IP_INT], int family);
165 const char *inet_to_str(inet_prefix ip);
166 int str_to_inet(const char *src, inet_prefix *ip);
167 int inet_to_sockaddr(inet_prefix *ip, u_short port, struct sockaddr *dst, socklen_t *dstlen);
168 int sockaddr_to_inet(struct sockaddr *ip, inet_prefix *dst, u_short *port);
169 
170 int new_socket(int sock_type);
171 int new_dgram_socket(int sock_type);
172 int inet_close(int *sk);
173 int inet_getpeername(int sk, inet_prefix *ip, short *port);
174 int join_ipv6_multicast(int socket, int idx);
175 
176 int set_keepalive_sk(int socket);
177 int unset_keepalive_sk(int socket);
178 int set_nonblock_sk(int fd);
179 int unset_nonblock_sk(int fd);
180 int set_reuseaddr_sk(int socket);
181 int set_bindtodevice_sk(int socket, char *dev);
182 int set_broadcast_sk(int socket, int family, inet_prefix *host, short port,
183  int dev_idx);
184 int new_broadcast_sk(int family, int dev_idx);
185 int set_tos_sk(int socket, int lowdelay);
186 
187 int new_tcp_conn(inet_prefix *host, short port, char *dev);
188 int new_udp_conn(inet_prefix *host, short port, char *dev);
189 int new_bcast_conn(inet_prefix *host, short port, int dev_idx);
190 
191 ssize_t inet_recv(int s, void *buf, size_t len, int flags);
192 ssize_t inet_recvfrom(int s, void *buf, size_t len, int flags,
193  struct sockaddr *from, socklen_t *fromlen);
194 ssize_t inet_recv_timeout(int s, void *buf, size_t len, int flags, u_int timeout);
195 ssize_t inet_recvfrom_timeout(int s, void *buf, size_t len, int flags,
196  struct sockaddr *from, socklen_t *fromlen, u_int timeout);
197 ssize_t inet_send(int s, const void *msg, size_t len, int flags);
198 ssize_t inet_sendto(int s, const void *msg, size_t len, int flags,
199  const struct sockaddr *to, socklen_t tolen);
200 ssize_t inet_send_timeout(int s, const void *msg, size_t len, int flags, u_int timeout);
201 ssize_t inet_sendto_timeout(int s, const void *msg, size_t len, int flags,
202  const struct sockaddr *to, socklen_t tolen, u_int timeout);
203 ssize_t inet_sendfile(int out_fd, int in_fd, off_t *offset, size_t count);
204 
205 #endif /*INET_H*/
ssize_t inet_sendto_timeout(int s, const void *msg, size_t len, int flags, const struct sockaddr *to, socklen_t tolen, u_int timeout)
Definition: inet.c:1079
int inet_is_ip_local(inet_prefix *ip, int class)
Definition: inet.c:176
int join_ipv6_multicast(int socket, int idx)
Definition: inet.c:601
const char * inet_to_str(inet_prefix ip)
Definition: inet.c:432
int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits)
Definition: inet.c:273
u_char family
Definition: inet.h:75
u_short len
Definition: inet.h:76
int new_broadcast_sk(int family, int dev_idx)
int inet_setip_bcast(inet_prefix *ip, int family)
Definition: inet.c:101
int set_tos_sk(int socket, int lowdelay)
Definition: inet.c:782
int ipv6_addr_type(inet_prefix addr)
Definition: inet.c:301
int inet_close(int *sk)
Definition: inet.c:575
void inet_copy_ipdata_raw(u_int *dst_data, inet_prefix *ip)
Definition: inet.c:198
#define INT_TYPE_16BIT
Definition: endianness.h:36
int new_tcp_conn(inet_prefix *host, short port, char *dev)
Definition: inet.c:801
ssize_t inet_recv(int s, void *buf, size_t len, int flags)
Definition: inet.c:905
int set_keepalive_sk(int socket)
Definition: inet.c:758
int inet_getpeername(int sk, inet_prefix *ip, short *port)
Definition: inet.c:581
const char * ipraw_to_str(u_int ip[4], int family)
Definition: inet.c:407
void inet_copy_ipdata(u_int *dst_data, inet_prefix *ip)
Definition: inet.c:207
Definition: inet.h:73
ssize_t inet_send(int s, const void *msg, size_t len, int flags)
Definition: inet.c:997
int new_dgram_socket(int sock_type)
Definition: inet.c:558
void pack_inet_prefix(inet_prefix *ip, char *pack)
Definition: inet.c:221
void inet_ntohl(u_int *data, int family)
Definition: inet.c:34
int new_udp_conn(inet_prefix *host, short port, char *dev)
Definition: inet.c:830
int inet_setip_loopback(inet_prefix *ip, int family)
Definition: inet.c:132
void unpack_inet_prefix(inet_prefix *ip, char *pack)
Definition: inet.c:247
int inet_to_sockaddr(inet_prefix *ip, u_short port, struct sockaddr *dst, socklen_t *dstlen)
Definition: inet.c:477
#define MAX_IP_INT
Definition: inet.h:24
int unset_keepalive_sk(int socket)
Definition: inet.c:770
int new_socket(int sock_type)
Definition: inet.c:547
ssize_t inet_sendfile(int out_fd, int in_fd, off_t *offset, size_t count)
Definition: inet.c:1104
u_char bits
Definition: inet.h:77
int set_nonblock_sk(int fd)
Definition: inet.c:635
int set_reuseaddr_sk(int socket)
Definition: inet.c:657
unsigned int socklen_t
Definition: libip4tc.c:26
int my_family
Definition: inet.h:141
int set_broadcast_sk(int socket, int family, inet_prefix *host, short port, int dev_idx)
Definition: inet.c:703
ssize_t inet_send_timeout(int s, const void *msg, size_t len, int flags, u_int timeout)
Definition: inet.c:1025
int str_to_inet(const char *src, inet_prefix *ip)
Definition: inet.c:442
int restricted_mode
Definition: inet.h:141
ssize_t inet_recvfrom_timeout(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen, u_int timeout)
Definition: inet.c:973
#define INT_INFO
Definition: endianness.h:90
int inet_setip_raw(inet_prefix *ip, u_int *data, int family)
Definition: inet.c:70
void inet_copy(inet_prefix *dst, inet_prefix *src)
Definition: inet.c:190
int inet_setip_anyaddr(inet_prefix *ip, int family)
Definition: inet.c:116
int inet_setip_localaddr(inet_prefix *ip, int family, int class)
Definition: inet.c:155
int sockaddr_to_inet(struct sockaddr *ip, inet_prefix *dst, u_short *port)
Definition: inet.c:515
ssize_t inet_recv_timeout(int s, void *buf, size_t len, int flags, u_int timeout)
Definition: inet.c:931
int restricted_class
Definition: inet.h:141
void inet_htonl(u_int *data, int family)
Definition: inet.c:53
static const int_info inet_prefix_iinfo
Definition: inet.h:85
ssize_t inet_sendto(int s, const void *msg, size_t len, int flags, const struct sockaddr *to, socklen_t tolen)
Definition: inet.c:1050
int inet_validate_ip(inet_prefix ip)
Definition: inet.c:372
int family
Definition: if.c:34
int flags
Definition: if.c:39
int inet_setip(inet_prefix *ip, u_int *data, int family)
Definition: inet.c:94
int new_bcast_conn(inet_prefix *host, short port, int dev_idx)
Definition: inet.c:860
int unset_nonblock_sk(int fd)
Definition: inet.c:646
ssize_t inet_recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen)
Definition: inet.c:951
int set_bindtodevice_sk(int socket, char *dev)
Definition: inet.c:670