File indexing completed on 2023-09-24 04:04:59
0001 /* 0002 * This file is part of the KDE libraries 0003 * Copyright (C) 2000-2003 Thiago Macieira <thiago.macieira@kdemail.net>> 0004 * 0005 * This library is free software; you can redistribute it and/or 0006 * modify it under the terms of the GNU Library General Public 0007 * License as published by the Free Software Foundation; either 0008 * version 2 of the License, or (at your option) any later version. 0009 * 0010 * This library is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0013 * Library General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU Library General Public License 0016 * along with this library; see the file COPYING.LIB. If not, write to 0017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0018 * Boston, MA 02110-1301, USA. 0019 **/ 0020 0021 #ifndef _NETSUPP_H_ 0022 #define _NETSUPP_H_ 0023 0024 #include <kdelibs4support_export.h> 0025 #include <config-kdelibs4support.h> 0026 0027 #include <config-network.h> 0028 0029 #include <sys/socket.h> 0030 #include <netdb.h> 0031 //#include "ksockaddr.h" 0032 0033 /* 0034 * Seems some systems don't know about AF_LOCAL 0035 */ 0036 #ifndef AF_LOCAL 0037 #define AF_LOCAL AF_UNIX 0038 #define PF_LOCAL PF_UNIX 0039 #endif 0040 0041 #ifdef CLOBBER_IN6 0042 #define kde_in6_addr in6_addr 0043 #define kde_sockaddr_in6 sockaddr_in6 0044 #endif 0045 0046 /*** IPv6 structures that might be missing from some implementations ***/ 0047 0048 /** @internal 0049 * An IPv6 address. 0050 * This is taken from RFC 2553 0051 */ 0052 struct kde_in6_addr { 0053 unsigned char __u6_addr[16]; 0054 }; 0055 0056 /** @internal 0057 * An IPv6 socket address 0058 * This is taken from RFC 2553. 0059 */ 0060 struct kde_sockaddr_in6 { 0061 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN 0062 quint8 sin6_len; 0063 quint8 sin6_family; 0064 #else //HAVE_STRUCT_SOCKADDR_SA_LEN 0065 quint16 sin6_family; 0066 #endif 0067 unsigned short sin6_port; /* RFC says in_port_t */ 0068 quint32 sin6_flowinfo; 0069 struct kde_in6_addr sin6_addr; 0070 quint32 sin6_scope_id; 0071 }; 0072 0073 /* IPv6 test macros that could be missing from some implementations */ 0074 0075 #define KDE_IN6_IS_ADDR_UNSPECIFIED(a) \ 0076 (((quint32 *) (a))[0] == 0 && ((quint32 *) (a))[1] == 0 && \ 0077 ((quint32 *) (a))[2] == 0 && ((quint32 *) (a))[3] == 0) 0078 0079 #define KDE_IN6_IS_ADDR_LOOPBACK(a) \ 0080 (((quint32 *) (a))[0] == 0 && ((quint32 *) (a))[1] == 0 && \ 0081 ((quint32 *) (a))[2] == 0 && ((quint32 *) (a))[3] == htonl (1)) 0082 0083 #define KDE_IN6_IS_ADDR_MULTICAST(a) (((u_int8_t *) (a))[0] == 0xff) 0084 0085 #define KDE_IN6_IS_ADDR_LINKLOCAL(a) \ 0086 ((((quint32 *) (a))[0] & htonl (0xffc00000)) == htonl (0xfe800000)) 0087 0088 #define KDE_IN6_IS_ADDR_SITELOCAL(a) \ 0089 ((((quint32 *) (a))[0] & htonl (0xffc00000)) == htonl (0xfec00000)) 0090 0091 #define KDE_IN6_IS_ADDR_V4MAPPED(a) \ 0092 ((((quint32 *) (a))[0] == 0) && (((quint32 *) (a))[1] == 0) && \ 0093 (((quint32 *) (a))[2] == htonl (0xffff))) 0094 0095 #define KDE_IN6_IS_ADDR_V4COMPAT(a) \ 0096 ((((quint32 *) (a))[0] == 0) && (((quint32 *) (a))[1] == 0) && \ 0097 (((quint32 *) (a))[2] == 0) && (ntohl (((quint32 *) (a))[3]) > 1)) 0098 0099 #define KDE_IN6_ARE_ADDR_EQUAL(a,b) \ 0100 ((((quint32 *) (a))[0] == ((quint32 *) (b))[0]) && \ 0101 (((quint32 *) (a))[1] == ((quint32 *) (b))[1]) && \ 0102 (((quint32 *) (a))[2] == ((quint32 *) (b))[2]) && \ 0103 (((quint32 *) (a))[3] == ((quint32 *) (b))[3])) 0104 0105 #define KDE_IN6_IS_ADDR_MC_NODELOCAL(a) \ 0106 (KDE_IN6_IS_ADDR_MULTICAST(a) && ((((quint8 *) (a))[1] & 0xf) == 0x1)) 0107 0108 #define KDE_IN6_IS_ADDR_MC_LINKLOCAL(a) \ 0109 (KDE_IN6_IS_ADDR_MULTICAST(a) && ((((quint8 *) (a))[1] & 0xf) == 0x2)) 0110 0111 #define KDE_IN6_IS_ADDR_MC_SITELOCAL(a) \ 0112 (KDE_IN6_IS_ADDR_MULTICAST(a) && ((((quint8 *) (a))[1] & 0xf) == 0x5)) 0113 0114 #define KDE_IN6_IS_ADDR_MC_ORGLOCAL(a) \ 0115 (KDE_IN6_IS_ADDR_MULTICAST(a) && ((((quint8 *) (a))[1] & 0xf) == 0x8)) 0116 0117 #define KDE_IN6_IS_ADDR_MC_GLOBAL(a) \ 0118 (KDE_IN6_IS_ADDR_MULTICAST(a) && ((((quint8 *) (a))[1] & 0xf) == 0xe)) 0119 0120 #ifdef NEED_IN6_TESTS 0121 # define IN6_IS_ADDR_UNSPECIFIED KDE_IN6_IS_ADDR_UNSPECIFIED 0122 # define IN6_IS_ADDR_LOOPBACK KDE_IN6_IS_ADDR_LOOPBACK 0123 # define IN6_IS_ADDR_MULTICAST KDE_IN6_IS_ADDR_MULTICAST 0124 # define IN6_IS_ADDR_LINKLOCAL KDE_IN6_IS_ADDR_LINKLOCAL 0125 # define IN6_IS_ADDR_SITELOCAL KDE_IN6_IS_ADDR_SITELOCAL 0126 # define IN6_IS_ADDR_V4MAPPED KDE_IN6_IS_ADDR_V4MAPPED 0127 # define IN6_IS_ADDR_V4COMPAT KDE_IN6_IS_ADDR_V4COMPAT 0128 # define IN6_ARE_ADDR_EQUAL KDE_IN6_ARE_ADDR_EQUAL 0129 # define IN6_IS_ADDR_MC_NODELOCAL KDE_IN6_IS_ADDR_MC_NODELOCAL 0130 # define IN6_IS_ADDR_MC_LINKLOCAL KDE_IN6_IS_ADDR_MC_LINKLOCAL 0131 # define IN6_IS_ADDR_MC_SITELOCAL KDE_IN6_IS_ADDR_MC_SITELOCAL 0132 # define IN6_IS_ADDR_MC_ORGLOCAL KDE_IN6_IS_ADDR_MC_ORGLOCAL 0133 # define IN6_IS_ADDR_MC_GLOBAL KDE_IN6_IS_ADDR_MC_GLOBAL 0134 #endif 0135 0136 /* Special internal structure */ 0137 0138 #define KAI_SYSTEM 0 /* data is all-system */ 0139 #define KAI_LOCALUNIX 1 /* data contains a Unix addrinfo allocated by us */ 0140 #define KAI_QDNS 2 /* data contains data derived from QDns */ 0141 0142 struct addrinfo; /* forward declaration; this could be needed */ 0143 0144 /** 0145 * @internal 0146 * Special purpose structure, to return data from kde_getaddrinfo to the 0147 * library functions. This defines an extra field to let us know how to 0148 * process this better. 0149 * 0150 * Currently, we use it to determine how to deallocate this stuff 0151 */ 0152 struct kde_addrinfo { 0153 struct addrinfo *data; 0154 int origin; 0155 }; 0156 0157 extern KDELIBS4SUPPORT_DEPRECATED_EXPORT int kde_getaddrinfo(const char *name, const char *service, 0158 const struct addrinfo *hint, 0159 struct kde_addrinfo **result); 0160 extern KDELIBS4SUPPORT_DEPRECATED_EXPORT void kde_freeaddrinfo(struct kde_addrinfo *p); 0161 0162 #if !HAVE_GETADDRINFO || HAVE_BROKEN_GETADDRINFO 0163 0164 # if !HAVE_STRUCT_ADDRINFO 0165 /** 0166 * @internal 0167 */ 0168 struct addrinfo { 0169 int ai_flags; /* Input flags. */ 0170 int ai_family; /* Protocol family for socket. */ 0171 int ai_socktype; /* Socket type. */ 0172 int ai_protocol; /* Protocol for socket. */ 0173 int ai_addrlen; /* Length of socket address. */ 0174 struct sockaddr *ai_addr; /* Socket address for socket. */ 0175 char *ai_canonname; /* Canonical name for service location. */ 0176 struct addrinfo *ai_next; /* Pointer to next in list. */ 0177 }; 0178 # endif 0179 0180 # ifdef AI_PASSIVE 0181 # undef AI_PASSIVE 0182 # undef AI_CANONNAME 0183 # undef AI_NUMERICHOST 0184 # endif 0185 0186 /* Possible values for `ai_flags' field in `addrinfo' structure. */ 0187 # define AI_PASSIVE 1 /* Socket address is intended for `bind'. */ 0188 # define AI_CANONNAME 2 /* Request for canonical name. */ 0189 # define AI_NUMERICHOST 4 /* Don't use name resolution. */ 0190 0191 # ifdef EAI_ADDRFAMILY 0192 # undef EAI_ADDRFAMILY 0193 # undef EAI_AGAIN 0194 # undef EAI_BADFLAGS 0195 # undef EAI_FAIL 0196 # undef EAI_FAMILY 0197 # undef EAI_MEMORY 0198 # undef EAI_NODATA 0199 # undef EAI_NONAME 0200 # undef EAI_SERVICE 0201 # undef EAI_SOCKTYPE 0202 # undef EAI_SYSTEM 0203 # endif 0204 0205 /* Error values for `getaddrinfo' function. */ 0206 # ifndef EAI_ADDRFAMILY 0207 # define EAI_ADDRFAMILY 1 /* Address family for NAME not supported. */ 0208 # endif 0209 # ifndef EAI_AGAIN 0210 # define EAI_AGAIN 2 /* Temporary failure in name resolution. */ 0211 # endif 0212 # ifndef EAI_BADFLAGS 0213 # define EAI_BADFLAGS 3 /* Invalid value for `ai_flags' field. */ 0214 # endif 0215 # ifndef EAI_FAIL 0216 # define EAI_FAIL 4 /* Non-recoverable failure in name res. */ 0217 # endif 0218 # ifndef EAI_FAMILY 0219 # define EAI_FAMILY 5 /* `ai_family' not supported. */ 0220 # endif 0221 # ifndef EAI_MEMORY 0222 # define EAI_MEMORY 6 /* Memory allocation failure. */ 0223 # endif 0224 # ifndef EAI_NODATA 0225 # define EAI_NODATA 7 /* No address associated with NAME. */ 0226 # endif 0227 # ifndef EAI_NONAME 0228 # define EAI_NONAME 8 /* NAME or SERVICE is unknown. */ 0229 # endif 0230 # ifndef EAI_SERVICE 0231 # define EAI_SERVICE 9 /* SERVICE not supported for `ai_socktype'. */ 0232 # endif 0233 # ifndef EAI_SOCKTYPE 0234 # define EAI_SOCKTYPE 10 /* `ai_socktype' not supported. */ 0235 # endif 0236 # ifndef EAI_SYSTEM 0237 # define EAI_SYSTEM 11 /* System error returned in `errno'. */ 0238 # endif 0239 0240 /* 0241 * These are specified in the RFC 0242 * We won't undefine them. If someone defined them to a different value 0243 * the preprocessor will generate an error 0244 */ 0245 # define NI_MAXHOST 1025 0246 # define NI_MAXSERV 32 0247 0248 # ifdef NI_NUMERICHOST 0249 # undef NI_NUMERICHOST 0250 # undef NI_NUMERICSERV 0251 # undef NI_NOFQDN 0252 # undef NI_NAMEREQD 0253 # undef NI_DGRAM 0254 # endif 0255 0256 # define NI_NUMERICHOST 1 /* Don't try to look up hostname. */ 0257 # define NI_NUMERICSERV 2 /* Don't convert port number to name. */ 0258 # define NI_NOFQDN 4 /* Only return nodename portion. */ 0259 # define NI_NAMEREQD 8 /* Don't return numeric addresses. */ 0260 # define NI_DGRAM 16 /* Look up UDP service rather than TCP. */ 0261 0262 # ifdef gai_strerror 0263 # undef gai_strerror 0264 # endif 0265 0266 # ifdef getaddrinfo 0267 # undef getaddrinfo 0268 # endif 0269 0270 # ifdef freeaddrinfo 0271 # undef freeaddrinfo 0272 # endif 0273 0274 # ifdef getnameinfo 0275 # undef getnameinfo 0276 # endif 0277 0278 namespace KDE 0279 { 0280 /** \internal */ 0281 extern KDELIBS4SUPPORT_DEPRECATED_EXPORT int getaddrinfo(const char *name, const char *service, 0282 const struct addrinfo *hint, 0283 struct addrinfo **result); 0284 /** \internal */ 0285 extern KDELIBS4SUPPORT_DEPRECATED_EXPORT void freeaddrinfo(struct addrinfo *ai); 0286 /** \internal */ 0287 extern KDELIBS4SUPPORT_DEPRECATED_EXPORT int getnameinfo(const struct sockaddr *sa, 0288 kde_socklen_t salen, 0289 char *host, size_t hostlen, 0290 char *serv, size_t servlen, 0291 int flags); 0292 } 0293 0294 # if !HAVE_GAI_STRERROR_PROTO 0295 /** \internal */ 0296 extern KDELIBS4SUPPORT_DEPRECATED_EXPORT char *gai_strerror(int errorcode); 0297 # endif 0298 0299 # define getaddrinfo KDE::getaddrinfo 0300 # define freeaddrinfo KDE::freeaddrinfo 0301 # define getnameinfo KDE::getnameinfo 0302 0303 #endif 0304 0305 #if !HAVE_INET_PTON && !defined(inet_pton) 0306 0307 namespace KDE 0308 { 0309 /** \internal */ 0310 extern KDELIBS4SUPPORT_DEPRECATED_EXPORT int inet_pton(int af, const char *cp, void *buf); 0311 } 0312 0313 # define inet_pton KDE::inet_pton 0314 #endif 0315 0316 #if !HAVE_INET_NTOP && !defined(inet_ntop) 0317 0318 namespace KDE 0319 { 0320 /** \internal */ 0321 extern KDELIBS4SUPPORT_DEPRECATED_EXPORT const char *inet_ntop(int af, const void *cp, char *buf, size_t len); 0322 } 0323 0324 # define inet_ntop KDE::inet_ntop 0325 #endif 0326 0327 #endif