netsupp.h
00001 /* 00002 * This file is part of the KDE libraries 00003 * Copyright (C) 2000-2003 Thiago Macieira <thiago.macieira@kdemail.net>> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Library General Public License 00016 * along with this library; see the file COPYING.LIB. If not, write to 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 **/ 00020 00021 #ifndef _NETSUPP_H_ 00022 #define _NETSUPP_H_ 00023 00024 #include "kdelibs_export.h" 00025 00026 #ifdef HAVE_CONFIG_H 00027 #include <config.h> 00028 #endif 00029 00030 #include <sys/socket.h> 00031 #include <netdb.h> 00032 //#include "ksockaddr.h" 00033 00034 #ifdef __CYGWIN__ 00035 typedef unsigned ksocklen_t; 00036 #endif 00037 00038 /* 00039 * Seems some systems don't know about AF_LOCAL 00040 */ 00041 #ifndef AF_LOCAL 00042 #define AF_LOCAL AF_UNIX 00043 #define PF_LOCAL PF_UNIX 00044 #endif 00045 00046 #ifdef CLOBBER_IN6 00047 #define kde_in6_addr in6_addr 00048 #define kde_sockaddr_in6 sockaddr_in6 00049 #endif 00050 00051 /*** IPv6 structures that might be missing from some implementations ***/ 00052 00057 struct kde_in6_addr 00058 { 00059 unsigned char __u6_addr[16]; 00060 }; 00061 00066 struct kde_sockaddr_in6 00067 { 00068 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN 00069 Q_UINT8 sin6_len; 00070 Q_UINT8 sin6_family; 00071 #else //HAVE_STRUCT_SOCKADDR_SA_LEN 00072 Q_UINT16 sin6_family; 00073 #endif 00074 unsigned short sin6_port; /* RFC says in_port_t */ 00075 Q_UINT32 sin6_flowinfo; 00076 struct kde_in6_addr sin6_addr; 00077 Q_UINT32 sin6_scope_id; 00078 }; 00079 00080 /* IPv6 test macros that could be missing from some implementations */ 00081 00082 #define KDE_IN6_IS_ADDR_UNSPECIFIED(a) \ 00083 (((Q_UINT32 *) (a))[0] == 0 && ((Q_UINT32 *) (a))[1] == 0 && \ 00084 ((Q_UINT32 *) (a))[2] == 0 && ((Q_UINT32 *) (a))[3] == 0) 00085 00086 #define KDE_IN6_IS_ADDR_LOOPBACK(a) \ 00087 (((Q_UINT32 *) (a))[0] == 0 && ((Q_UINT32 *) (a))[1] == 0 && \ 00088 ((Q_UINT32 *) (a))[2] == 0 && ((Q_UINT32 *) (a))[3] == htonl (1)) 00089 00090 #define KDE_IN6_IS_ADDR_MULTICAST(a) (((u_int8_t *) (a))[0] == 0xff) 00091 00092 #define KDE_IN6_IS_ADDR_LINKLOCAL(a) \ 00093 ((((Q_UINT32 *) (a))[0] & htonl (0xffc00000)) == htonl (0xfe800000)) 00094 00095 #define KDE_IN6_IS_ADDR_SITELOCAL(a) \ 00096 ((((Q_UINT32 *) (a))[0] & htonl (0xffc00000)) == htonl (0xfec00000)) 00097 00098 #define KDE_IN6_IS_ADDR_V4MAPPED(a) \ 00099 ((((Q_UINT32 *) (a))[0] == 0) && (((Q_UINT32 *) (a))[1] == 0) && \ 00100 (((Q_UINT32 *) (a))[2] == htonl (0xffff))) 00101 00102 #define KDE_IN6_IS_ADDR_V4COMPAT(a) \ 00103 ((((Q_UINT32 *) (a))[0] == 0) && (((Q_UINT32 *) (a))[1] == 0) && \ 00104 (((Q_UINT32 *) (a))[2] == 0) && (ntohl (((Q_UINT32 *) (a))[3]) > 1)) 00105 00106 #define KDE_IN6_ARE_ADDR_EQUAL(a,b) \ 00107 ((((Q_UINT32 *) (a))[0] == ((Q_UINT32 *) (b))[0]) && \ 00108 (((Q_UINT32 *) (a))[1] == ((Q_UINT32 *) (b))[1]) && \ 00109 (((Q_UINT32 *) (a))[2] == ((Q_UINT32 *) (b))[2]) && \ 00110 (((Q_UINT32 *) (a))[3] == ((Q_UINT32 *) (b))[3])) 00111 00112 #define KDE_IN6_IS_ADDR_MC_NODELOCAL(a) \ 00113 (KDE_IN6_IS_ADDR_MULTICAST(a) && ((((Q_UINT8 *) (a))[1] & 0xf) == 0x1)) 00114 00115 #define KDE_IN6_IS_ADDR_MC_LINKLOCAL(a) \ 00116 (KDE_IN6_IS_ADDR_MULTICAST(a) && ((((Q_UINT8 *) (a))[1] & 0xf) == 0x2)) 00117 00118 #define KDE_IN6_IS_ADDR_MC_SITELOCAL(a) \ 00119 (KDE_IN6_IS_ADDR_MULTICAST(a) && ((((Q_UINT8 *) (a))[1] & 0xf) == 0x5)) 00120 00121 #define KDE_IN6_IS_ADDR_MC_ORGLOCAL(a) \ 00122 (KDE_IN6_IS_ADDR_MULTICAST(a) && ((((Q_UINT8 *) (a))[1] & 0xf) == 0x8)) 00123 00124 #define KDE_IN6_IS_ADDR_MC_GLOBAL(a) \ 00125 (KDE_IN6_IS_ADDR_MULTICAST(a) && ((((Q_UINT8 *) (a))[1] & 0xf) == 0xe)) 00126 00127 #ifdef NEED_IN6_TESTS 00128 # define IN6_IS_ADDR_UNSPECIFIED KDE_IN6_IS_ADDR_UNSPECIFIED 00129 # define IN6_IS_ADDR_LOOPBACK KDE_IN6_IS_ADDR_LOOPBACK 00130 # define IN6_IS_ADDR_MULTICAST KDE_IN6_IS_ADDR_MULTICAST 00131 # define IN6_IS_ADDR_LINKLOCAL KDE_IN6_IS_ADDR_LINKLOCAL 00132 # define IN6_IS_ADDR_SITELOCAL KDE_IN6_IS_ADDR_SITELOCAL 00133 # define IN6_IS_ADDR_V4MAPPED KDE_IN6_IS_ADDR_V4MAPPED 00134 # define IN6_IS_ADDR_V4COMPAT KDE_IN6_IS_ADDR_V4COMPAT 00135 # define IN6_ARE_ADDR_EQUAL KDE_IN6_ARE_ADDR_EQUAL 00136 # define IN6_IS_ADDR_MC_NODELOCAL KDE_IN6_IS_ADDR_MC_NODELOCAL 00137 # define IN6_IS_ADDR_MC_LINKLOCAL KDE_IN6_IS_ADDR_MC_LINKLOCAL 00138 # define IN6_IS_ADDR_MC_SITELOCAL KDE_IN6_IS_ADDR_MC_SITELOCAL 00139 # define IN6_IS_ADDR_MC_ORGLOCAL KDE_IN6_IS_ADDR_MC_ORGLOCAL 00140 # define IN6_IS_ADDR_MC_GLOBAL KDE_IN6_IS_ADDR_MC_GLOBAL 00141 #endif 00142 00143 /* Special internal structure */ 00144 00145 #define KAI_SYSTEM 0 /* data is all-system */ 00146 #define KAI_LOCALUNIX 1 /* data contains a Unix addrinfo allocated by us */ 00147 #define KAI_QDNS 2 /* data contains data derived from QDns */ 00148 00149 struct addrinfo; /* forward declaration; this could be needed */ 00150 00159 struct kde_addrinfo 00160 { 00161 struct addrinfo *data; 00162 int origin; 00163 }; 00164 00165 extern KDECORE_EXPORT int kde_getaddrinfo(const char *name, const char *service, 00166 const struct addrinfo* hint, 00167 struct kde_addrinfo** result); 00168 extern KDECORE_EXPORT void kde_freeaddrinfo(struct kde_addrinfo *p); 00169 00170 #if !defined(HAVE_GETADDRINFO) || defined(HAVE_BROKEN_GETADDRINFO) 00171 00172 # ifndef HAVE_STRUCT_ADDRINFO 00173 00176 struct addrinfo 00177 { 00178 int ai_flags; 00179 int ai_family; 00180 int ai_socktype; 00181 int ai_protocol; 00182 int ai_addrlen; 00183 struct sockaddr *ai_addr; 00184 char *ai_canonname; 00185 struct addrinfo *ai_next; 00186 }; 00187 # endif 00188 00189 # ifdef AI_PASSIVE 00190 # undef AI_PASSIVE 00191 # undef AI_CANONNAME 00192 # undef AI_NUMERICHOST 00193 # endif 00194 00195 /* Possible values for `ai_flags' field in `addrinfo' structure. */ 00196 # define AI_PASSIVE 1 /* Socket address is intended for `bind'. */ 00197 # define AI_CANONNAME 2 /* Request for canonical name. */ 00198 # define AI_NUMERICHOST 4 /* Don't use name resolution. */ 00199 00200 # ifdef EAI_ADDRFAMILY 00201 # undef EAI_ADDRFAMILY 00202 # undef EAI_AGAIN 00203 # undef EAI_BADFLAGS 00204 # undef EAI_FAIL 00205 # undef EAI_FAMILY 00206 # undef EAI_MEMORY 00207 # undef EAI_NODATA 00208 # undef EAI_NONAME 00209 # undef EAI_SERVICE 00210 # undef EAI_SOCKTYPE 00211 # undef EAI_SYSTEM 00212 # endif 00213 00214 /* Error values for `getaddrinfo' function. */ 00215 # define EAI_ADDRFAMILY 1 /* Address family for NAME not supported. */ 00216 # define EAI_AGAIN 2 /* Temporary failure in name resolution. */ 00217 # define EAI_BADFLAGS 3 /* Invalid value for `ai_flags' field. */ 00218 # define EAI_FAIL 4 /* Non-recoverable failure in name res. */ 00219 # define EAI_FAMILY 5 /* `ai_family' not supported. */ 00220 # define EAI_MEMORY 6 /* Memory allocation failure. */ 00221 # define EAI_NODATA 7 /* No address associated with NAME. */ 00222 # define EAI_NONAME 8 /* NAME or SERVICE is unknown. */ 00223 # define EAI_SERVICE 9 /* SERVICE not supported for `ai_socktype'. */ 00224 # define EAI_SOCKTYPE 10 /* `ai_socktype' not supported. */ 00225 # define EAI_SYSTEM 11 /* System error returned in `errno'. */ 00226 00227 /* 00228 * These are specified in the RFC 00229 * We won't undefine them. If someone defined them to a different value 00230 * the preprocessor will generate an error 00231 */ 00232 # define NI_MAXHOST 1025 00233 # define NI_MAXSERV 32 00234 00235 # ifdef NI_NUMERICHOST 00236 # undef NI_NUMERICHOST 00237 # undef NI_NUMERICSERV 00238 # undef NI_NOFQDN 00239 # undef NI_NAMEREQD 00240 # undef NI_DGRAM 00241 # endif 00242 00243 # define NI_NUMERICHOST 1 /* Don't try to look up hostname. */ 00244 # define NI_NUMERICSERV 2 /* Don't convert port number to name. */ 00245 # define NI_NOFQDN 4 /* Only return nodename portion. */ 00246 # define NI_NAMEREQD 8 /* Don't return numeric addresses. */ 00247 # define NI_DGRAM 16 /* Look up UDP service rather than TCP. */ 00248 00249 # ifdef getaddrinfo 00250 # undef getaddrinfo 00251 # endif 00252 00253 namespace KDE 00254 { 00256 extern KDECORE_EXPORT int getaddrinfo(const char *name, const char *service, 00257 const struct addrinfo* hint, 00258 struct addrinfo** result); 00260 extern KDECORE_EXPORT void freeaddrinfo(struct addrinfo* ai); 00262 extern KDECORE_EXPORT char *gai_strerror(int errorcode); 00264 extern KDECORE_EXPORT int getnameinfo(const struct sockaddr *sa, 00265 unsigned int salen, 00266 char *host, size_t hostlen, 00267 char *serv, size_t servlen, 00268 int flags); 00269 } 00270 00271 # define getaddrinfo KDE::getaddrinfo 00272 # define freeaddrinfo KDE::freeaddrinfo 00273 # define gai_strerror KDE::gai_strerror 00274 # define getnameinfo KDE::getnameinfo 00275 00276 00277 #endif 00278 00279 #ifndef HAVE_INET_PTON 00280 00281 namespace KDE 00282 { 00284 extern KDECORE_EXPORT int inet_pton(int af, const char *cp, void* buf); 00285 } 00286 00287 # define inet_pton KDE::inet_pton 00288 #endif 00289 00290 #ifndef HAVE_INET_NTOP 00291 00292 namespace KDE 00293 { 00295 extern KDECORE_EXPORT const char* inet_ntop(int af, const void *cp, char *buf, size_t len); 00296 } 00297 00298 # define inet_ntop KDE::inet_ntop 00299 #endif 00300 00301 #endif