You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

inet.h 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. #ifndef INET_H
  19. #define INET_H
  20. #include "endianness.h"
  21. #define MAX_IP_INT 4
  22. #define MAX_IP_SZ (MAX_IP_INT*sizeof(int))
  23. /*
  24. * This is the "link-scope all-hosts multicast" address: ff02::1.
  25. */
  26. #define IPV6_ADDR_BROADCAST { 0xff020000, 0x0, 0x0, 0x1 }
  27. #define LOOPBACK_IP 0x7f000001
  28. #define LOOPBACK_NET 0x7f000000
  29. #define LOOPBACK_BCAST 0x7fffffff
  30. #define LOOPBACK_IPV6 { 0x0, 0x0, 0x0, 0x1 }
  31. /*
  32. * `x' is in host byte order
  33. *
  34. * NTK_RESTRICTED_10_MASK(x):
  35. * given an ipv4 IP it returns the equivalent in the 10.x.x.x class, i.e.
  36. * 212.13.4.1 --> 10.13.4.1
  37. *
  38. * NTK_RESTRICTED_172_MASK(x): it's the same of NTK_RESTRICTED_10_MASK() but
  39. * it converts the IP in the 172.16.0.0 - 172.31.255.255 range.
  40. *
  41. * NTK_RESTRICTED_IPV6_MASK(x):
  42. * `x' in this case is the first integer of the four of an ipv6 IP.
  43. * The conversion is: `x' --> fec0:xxxx:...
  44. *
  45. */
  46. #define NTK_RESTRICTED_10_MASK(x) (((x) & ~0xff000000)|0x0a000000)
  47. #define NTK_RESTRICTED_172_MASK(x) (((((x) & ~0xff000000) | 0xac000000) & ~0x00e00000) | 0x00100000)
  48. #define NTK_RESTRICTED_IPV6_MASK(x) (((x) & ~0xffff0000)|0xfec00000)
  49. /* `x' is in network order.
  50. * Is `x' an IP in the range of 192.168.0.0 - 192.168.255.255 ? */
  51. #define NTK_PRIVATE_C(x) (((x) & __constant_htonl(0xffff0000)) == __constant_htonl(0xc0a80000))
  52. /* `x' is in network order.
  53. * Is `x' in 172.16.0.0 - 172.31.255.255 ? */
  54. #define NTK_PRIVATE_B(x) (((x) & __constant_htonl(0xff000000)) == __constant_htonl(0xac000000))\
  55. && ((x) & __constant_htonl(0x00100000)) && \
  56. !((x) & __constant_htonl(0x00e00000))
  57. /*
  58. * The inet_prefix struct is used to store IP addresses in the internals of
  59. * the Netsukuku code
  60. */
  61. typedef struct {
  62. u_char family; /* AF_INET or AF_INET6 */
  63. u_short len; /* IP length: 4 or 16 (bytes) */
  64. u_char bits; /* Number of used bits of the IP */
  65. /* TODO : check if data should be uintptr_t instead of u_int
  66. note: expected ‘uintptr_t * {aka long unsigned int *}’ but argument is of type ‘u_int * {aka unsigned int *}’
  67. But explicit cast has been added to suppress warnings
  68. */
  69. u_int data[MAX_IP_INT]; /* The address is kept in host long format,
  70. word ORDER 1 (most significant word first) */
  71. } inet_prefix;
  72. /* int_info struct used for packing the inet_prefix struct.
  73. * Note that `data' is ignored 'cause it will be converted with
  74. * inet_htonl() / inet_ntohl() */
  75. INT_INFO inet_prefix_iinfo = { 1,
  76. {INT_TYPE_16BIT},
  77. {sizeof(u_char)}
  78. ,
  79. {1}
  80. };
  81. #define INET_PREFIX_PACK_SZ (sizeof(u_char) + sizeof(u_short) +\
  82. sizeof(u_char) + MAX_IP_SZ)
  83. /* * * defines from linux/in.h * * */
  84. #define LOOPBACK(x) (((x) & htonl(0xff000000)) == htonl(0x7f000000))
  85. #define MULTICAST(x) (((x) & htonl(0xf0000000)) == htonl(0xe0000000))
  86. #define BADCLASS(x) (((x) & htonl(0xf0000000)) == htonl(0xf0000000))
  87. #define ZERONET(x) (((x) & htonl(0xff000000)) == htonl(0x00000000))
  88. #define LOCAL_MCAST(x) (((x) & htonl(0xFFFFFF00)) == htonl(0xE0000000))
  89. /* * * defines from linux/include/net/ipv6.h * * */
  90. #define IPV6_ADDR_ANY 0x0000U
  91. #define IPV6_ADDR_UNICAST 0x0001U
  92. #define IPV6_ADDR_MULTICAST 0x0002U
  93. #define IPV6_ADDR_LOOPBACK 0x0010U
  94. #define IPV6_ADDR_LINKLOCAL 0x0020U
  95. #define IPV6_ADDR_SITELOCAL 0x0040U
  96. #define IPV6_ADDR_COMPATv4 0x0080U
  97. #define IPV6_ADDR_SCOPE_MASK 0x00f0U
  98. #define IPV6_ADDR_MAPPED 0x1000U
  99. #define IPV6_ADDR_RESERVED 0x2000U /* reserved address space */
  100. /*
  101. * Type of Service
  102. */
  103. #ifndef IPTOS_LOWDELAY
  104. #define IPTOS_LOWDELAY 0x10
  105. #define IPTOS_THROUGHPUT 0x08
  106. #define IPTOS_RELIABILITY 0x04
  107. #define IPTOS_LOWCOST 0x02
  108. #define IPTOS_MINCOST IPTOS_LOWCOST
  109. #endif /* IPTOS_LOWDELAY */
  110. /*
  111. * Globals
  112. */
  113. #define RESTRICTED_10 1 /* We are using the 10.x.x.x class for
  114. the restricted mode */
  115. #define RESTRICTED_172 2 /* 172.16.0.0-172.31.255.255 class */
  116. #define RESTRICTED_10_STR "10.0.0.0-10.255.255.255"
  117. #define RESTRICTED_172_STR "172.16.0.0-172.31.255.255"
  118. int my_family, restricted_mode, restricted_class;
  119. /*
  120. * * * Functions declaration * *
  121. */
  122. void inet_ntohl(u_int * data, int family);
  123. void inet_htonl(u_int * data, int family);
  124. int inet_setip_raw(inet_prefix * ip, u_int * data, int family);
  125. int inet_setip(inet_prefix * ip, u_int * data, int family);
  126. int inet_setip_bcast(inet_prefix * ip, int family);
  127. int inet_setip_anyaddr(inet_prefix * ip, int family);
  128. int inet_setip_loopback(inet_prefix * ip, int family);
  129. int inet_setip_localaddr(inet_prefix * ip, int family, int class);
  130. int inet_is_ip_local(inet_prefix * ip, int class);
  131. void inet_copy_ipdata_raw(u_int * dst_data, inet_prefix * ip);
  132. void inet_copy_ipdata(u_int * dst_data, inet_prefix * ip);
  133. void inet_copy(inet_prefix * dst, inet_prefix * src);
  134. void pack_inet_prefix(inet_prefix * ip, char *pack);
  135. void unpack_inet_prefix(inet_prefix * ip, char *pack);
  136. int inet_addr_match(const inet_prefix * a, const inet_prefix * b,
  137. int bits);
  138. int ipv6_addr_type(inet_prefix addr);
  139. int inet_validate_ip(inet_prefix ip);
  140. const char *ipraw_to_str(u_int ip[MAX_IP_INT], int family);
  141. const char *inet_to_str(inet_prefix ip);
  142. int str_to_inet(const char *src, inet_prefix * ip);
  143. int inet_to_sockaddr(inet_prefix * ip, u_short port, struct sockaddr *dst,
  144. socklen_t * dstlen);
  145. int sockaddr_to_inet(struct sockaddr *ip, inet_prefix * dst,
  146. u_short * port);
  147. int new_socket(int sock_type);
  148. int new_dgram_socket(int sock_type);
  149. int inet_close(int *sk);
  150. int inet_getpeername(int sk, inet_prefix * ip, u_short *port);
  151. int join_ipv6_multicast(int socket, int idx);
  152. int set_keepalive_sk(int socket);
  153. int unset_keepalive_sk(int socket);
  154. int set_nonblock_sk(int fd);
  155. int unset_nonblock_sk(int fd);
  156. int set_reuseaddr_sk(int socket);
  157. int set_bindtodevice_sk(int socket, char *dev);
  158. int set_broadcast_sk(int socket, int family, inet_prefix * host,
  159. short port, int dev_idx);
  160. int new_broadcast_sk(int family, int dev_idx);
  161. int set_tos_sk(int socket, int lowdelay);
  162. int new_tcp_conn(inet_prefix * host, short port, char *dev);
  163. int new_udp_conn(inet_prefix * host, short port, char *dev);
  164. int new_bcast_conn(inet_prefix * host, short port, int dev_idx);
  165. ssize_t inet_recv(int s, void *buf, size_t len, int flags);
  166. ssize_t inet_recvfrom(int s, void *buf, size_t len, int flags,
  167. struct sockaddr *from, socklen_t * fromlen);
  168. ssize_t inet_recv_timeout(int s, void *buf, size_t len, int flags,
  169. u_int timeout);
  170. ssize_t inet_recvfrom_timeout(int s, void *buf, size_t len, int flags,
  171. struct sockaddr *from, socklen_t * fromlen,
  172. u_int timeout);
  173. ssize_t inet_send(int s, const void *msg, size_t len, int flags);
  174. ssize_t inet_sendto(int s, const void *msg, size_t len, int flags,
  175. const struct sockaddr *to, socklen_t tolen);
  176. ssize_t inet_send_timeout(int s, const void *msg, size_t len, int flags,
  177. u_int timeout);
  178. ssize_t inet_sendto_timeout(int s, const void *msg, size_t len, int flags,
  179. const struct sockaddr *to, socklen_t tolen,
  180. u_int timeout);
  181. ssize_t inet_sendfile(int out_fd, int in_fd, off_t * offset, size_t count);
  182. /*#if UINTPTR_MAX == 0xffffffff
  183. #ifndef _LARGEFILE64_SOURCE
  184. #define _LARGEFILE64_SOURCE
  185. #endif
  186. ssize_t
  187. ssize_t inet_sendfile64(int out_fd, int in_fd, off64_t * offset, size_t count);
  188. #endif
  189. */
  190. #endif /*INET_H */