xorp

xorp_osdep_mid.h

00001 /* -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*- */
00002 /* vim:set sts=4 ts=8: */
00003 
00004 /*
00005  * Copyright (c) 2001-2011 XORP, Inc and Others
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU Lesser General Public License, Version
00009  * 2.1, June 1999 as published by the Free Software Foundation.
00010  * Redistribution and/or modification of this program under the terms of
00011  * any other version of the GNU Lesser General Public License is not
00012  * permitted.
00013  * 
00014  * This program is distributed in the hope that it will be useful, but
00015  * WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00017  * see the GNU Lesser General Public License, Version 2.1, a copy of
00018  * which can be found in the XORP LICENSE.lgpl file.
00019  * 
00020  * XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00021  * http://xorp.net
00022  */
00023 
00024 #ifndef __LIBXORP_XORP_OSDEP_MID_H__
00025 #define __LIBXORP_XORP_OSDEP_MID_H__
00026 
00027 /*------------------------------------------------------------------------*/
00028 
00029 /*
00030  * Windows-specific preprocessor definitions.
00031  */
00032 #if defined(HOST_OS_WINDOWS)
00033 
00034 #include <windows.h>
00035 #include <winbase.h>
00036 #include <winsock2.h>
00037 #include <ws2tcpip.h>
00038 #include <iphlpapi.h>
00039 #include <process.h>
00040 
00041 #ifdef __cplusplus
00042 extern "C" {
00043 #endif
00044 
00045 #ifndef IPPROTO_PIM
00046 #define IPPROTO_PIM 103
00047 #endif
00048 
00049 #ifndef IN_EXPERIMENTAL
00050 #define IN_EXPERIMENTAL(i)  (((uint32_t)(i) & 0xf0000000) == 0xf0000000)
00051 #endif
00052 
00053 #ifndef IN_BADCLASS
00054 #define IN_BADCLASS(i)      (((uint32_t)(i) & 0xf0000000) == 0xf0000000)
00055 #endif
00056 
00057 #ifndef INADDR_MAX_LOCAL_GROUP
00058 #define INADDR_MAX_LOCAL_GROUP  (uint32_t)0xe00000ff    /* 224.0.0.255 */
00059 #endif
00060 
00061 #ifndef IN_LOOPBACKNET
00062 #define IN_LOOPBACKNET  127
00063 #endif
00064 
00065 #ifndef IFNAMSIZ
00066 #define IFNAMSIZ    255
00067 #endif
00068 
00069 #ifndef IF_NAMESIZE
00070 #define IF_NAMESIZE 255
00071 #endif
00072 
00073 #ifndef IP_MAXPACKET
00074 #define IP_MAXPACKET    65535
00075 #endif
00076 
00077 #ifndef MAXHOSTNAMELEN
00078 #define MAXHOSTNAMELEN MAX_HOSTNAME_LEN
00079 #endif
00080 
00081 #ifndef IPVERSION
00082 #define IPVERSION 4
00083 #endif
00084 
00085 /*
00086  * iovec is common enough that we ship our own for Windows,
00087  * with the members swapped around to be compatible with
00088  * Winsock2's scatter/gather send functions.
00089  */
00090 struct iovec {
00091     u_long  iov_len;    /* Length. (len in WSABUF) */
00092     char    *iov_base;  /* Base address. (buf in WSABUF) */
00093 };
00094 
00095 /* From netinet/ip.h */
00096 /*
00097  * Structure of an internet header, naked of options.
00098  */
00099 struct ip {
00100     uint8_t ip_h_v; /* header & version..which is first depends on byte order. */
00101 //#if __BYTE_ORDER == __LITTLE_ENDIAN
00102 //    unsigned int ip_hl:4;               /* header length */
00103 //    unsigned int ip_v:4;                /* version */
00104 //#endif
00105 //#if __BYTE_ORDER == __BIG_ENDIAN
00106 //    unsigned int ip_v:4;                /* version */
00107 //    unsigned int ip_hl:4;               /* header length */
00108 //#endif
00109     uint8_t ip_tos;                    /* type of service */
00110     unsigned short ip_len;                     /* total length */
00111     unsigned short ip_id;                      /* identification */
00112     unsigned short ip_off;                     /* fragment offset field */
00113 #define IP_RF 0x8000                    /* reserved fragment flag */
00114 #define IP_DF 0x4000                    /* dont fragment flag */
00115 #define IP_MF 0x2000                    /* more fragments flag */
00116 #define IP_OFFMASK 0x1fff               /* mask for fragmenting bits */
00117     uint8_t ip_ttl;                    /* time to live */
00118     uint8_t ip_p;                      /* protocol */
00119     unsigned short ip_sum;                     /* checksum */
00120     struct in_addr ip_src, ip_dst;      /* source and dest address */
00121 };
00122 
00123 struct ip6_hdr {
00124     union {
00125         struct ip6_hdrctl {
00126             uint32_t ip6_un1_flow;   /* 4 bits version, 8 bits TC,
00127                                         20 bits flow-ID */
00128             uint16_t ip6_un1_plen;   /* payload length */
00129             uint8_t  ip6_un1_nxt;    /* next header */
00130             uint8_t  ip6_un1_hlim;   /* hop limit */
00131     } ip6_un1;
00132         uint8_t ip6_un2_vfc;       /* 4 bits version, top 4 bits tclass */
00133     } ip6_ctlun;
00134     struct in6_addr ip6_src;      /* source address */
00135     struct in6_addr ip6_dst;      /* destination address */
00136 };
00137 #define ip6_flow  ip6_ctlun.ip6_un1.ip6_un1_flow
00138 
00139 
00140 /*------------------------------------------------------------------------*/
00141 
00142 #ifdef __cplusplus
00143 }
00144 #endif
00145 
00146 typedef HANDLE xfd_t;
00147 typedef SOCKET xsock_t;
00148 
00149 typedef unsigned long gid_t;        /* XXX: This is a hack. */
00150 typedef unsigned long uid_t;        /* XXX: This is a hack. */
00151 
00152 #ifdef _NO_OLDNAMES
00153 /*
00154  * When _NO_OLDNAMES is defined, we need to put certain things
00155  * back into the namespace.
00156  *
00157  * XXX: Workaround a problem whereby some of the tests which ship with
00158  * GNU autoconf will try to define pid_t in an environment which
00159  * does not have it.
00160  */
00161 //#ifdef pid_t
00162 //#undef pid_t
00163 //#endif
00164 //typedef _off_t off_t;
00165 //typedef _pid_t pid_t;
00166 typedef long ssize_t;       /* XXX: This is a hack. */
00167 typedef _sigset_t sigset_t; /* XXX: Appease libtecla. */
00168 
00169 /* XXX: This is a hack. */
00170 #define getpid()        ( (pid_t) GetCurrentProcessId() )
00171 
00172 #define isascii(c)      __isascii((c))
00173 #define snprintf        _snprintf
00174 #define sleep           _sleep
00175 #define strdup(s)       _strdup((s))
00176 #define strcasecmp(s1, s2)  _stricmp((s1),(s2))
00177 #define vsnprintf       _vsnprintf
00178 
00179 #else /* !_NO_OLDNAMES */
00180 #define sleep           Sleep
00181 #endif
00182 
00183 #define XORP_BAD_FD     INVALID_HANDLE_VALUE
00184 #define XORP_BAD_SOCKET     INVALID_SOCKET
00185 
00186 /*
00187  * Windows expects (char *) and (const char *) pointers for transmitted
00188  * data and socket option data.
00189  */
00190 #define XORP_BUF_CAST(x) ((char *)(x))
00191 #define XORP_CONST_BUF_CAST(x) ((const char *)(x))
00192 #define XORP_SOCKOPT_CAST(x) ((char *)(x))
00193 
00194 /* I/O errors pertaining to non-blocking sockets. */
00195 #ifndef EWOULDBLOCK
00196 #define EWOULDBLOCK WSAEWOULDBLOCK
00197 #endif
00198 
00199 #else /* !HOST_OS_WINDOWS */
00200 
00201 /* Use a portable file descriptor type. */
00202 typedef int xfd_t;
00203 typedef int xsock_t;
00204 
00205 /* Use Windows-a-likes for checking return values from I/O syscalls. */
00206 #define XORP_BAD_FD     ((xfd_t) -1)
00207 #define XORP_BAD_SOCKET     XORP_BAD_FD
00208 
00209 /*
00210  * The rest of the world expects void pointers to transmitted
00211  * data and socket option data.
00212  */
00213 #define XORP_BUF_CAST(x) ((void *)(x))
00214 #define XORP_CONST_BUF_CAST(x) ((const void *)(x))
00215 #define XORP_SOCKOPT_CAST(x) (x)
00216 
00217 #endif /* HOST_OS_WINDOWS */
00218 
00219 /*------------------------------------------------------------------------*/
00220 
00221 /*
00222  * Portable missing functions.
00223  */
00224 
00225 #ifdef __cplusplus
00226 extern "C" {
00227 #endif
00228 
00229 #ifndef HAVE_STRLCPY
00230 size_t strlcpy(char *dst, const char *src, size_t size);
00231 #endif
00232 
00233 /*
00234  * IP address presentation routines.
00235  */
00236 #ifndef HAVE_INET_PTON
00237 int inet_pton(int af, const char *src, void *dst);
00238 #endif
00239 #ifndef HAVE_INET_NTOP
00240 const char *inet_ntop(int af, const void *src, char *dst, size_t size);
00241 #endif
00242 
00243 /*
00244  * getopt.
00245  */
00246 #ifdef NEED_GETOPT
00247 extern char *optarg;
00248 extern int optind;
00249 extern int optopt;
00250 extern int opterr;
00251 extern int optreset;
00252 
00253 int getopt(int argc, char * const argv[], const char *optstring);
00254 #endif /* NEED_GETOPT */
00255 
00256 #ifdef __cplusplus
00257 }
00258 #endif
00259 
00260 #endif /* __LIBXORP_XORP_OSDEP_MID_H__ */
 All Classes Namespaces Functions Variables Typedefs Enumerations