Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

common.h

Go to the documentation of this file.
00001 /*
00002  * $Id: common.h,v 1.360 2006/02/03 21:15:02 cbiere Exp $
00003  *
00004  * Copyright (c) 2001-2003, Richard Eckart
00005  *
00006  *----------------------------------------------------------------------
00007  * This file is part of gtk-gnutella.
00008  *
00009  *  gtk-gnutella is free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  gtk-gnutella is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *  GNU General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU General Public License
00020  *  along with gtk-gnutella; if not, write to the Free Software
00021  *  Foundation, Inc.:
00022  *      59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *----------------------------------------------------------------------
00024  */
00025 
00036 #ifndef _common_h_
00037 #define _common_h_
00038 
00039 #include "config.h"
00040 
00041 /*
00042  * Constants
00043  */
00044 
00045 #define GTA_VERSION 0               
00046 #define GTA_SUBVERSION 96           
00047 #define GTA_PATCHLEVEL 1            
00048 #define GTA_REVISION "unstable"         
00049 #define GTA_REVCHAR "u"         
00050 #define GTA_RELEASE "2006-02-03"    
00051 #define GTA_WEBSITE "http://gtk-gnutella.sourceforge.net/"
00052 
00053 #if defined(USE_GTK1)
00054 #define GTA_INTERFACE "GTK1"
00055 #elif defined(USE_GTK2)
00056 #define GTA_INTERFACE "GTK2"
00057 #elif defined(USE_TOPLESS)
00058 #define GTA_INTERFACE "Topless"
00059 #else
00060 #define GTA_INTERFACE "X11"
00061 #endif
00062 
00063 #ifndef HAS_LIBXML2
00064 #error "You need libxml2 (http://www.xmlsoft.org/) to compile Gtk-Gnutella"
00065 #endif
00066 
00067 /*
00068  * Main includes
00069  */
00070 
00071 #include <errno.h>
00072 #include <stdlib.h>
00073 #include <stdio.h>
00074 #include <signal.h>
00075 #include <unistd.h>
00076 #include <sys/types.h>
00077 
00078 #ifdef I_SYS_TIME
00079 #include <sys/time.h>
00080 #endif
00081 #ifdef I_SYS_TIME_KERNEL
00082 #define KERNEL
00083 #include <sys/time.h>
00084 #undef KERNEL
00085 #endif
00086 
00087 
00088 #ifdef MINGW32
00089 #include <ws2tcpip.h>
00090 #include <winsock.h>
00091 #define ECONNRESET WSAECONNRESET
00092 #define ECONNREFUSED WSAECONNREFUSED
00093 #define ECONNABORTED WSAECONNABORTED
00094 #define ENETUNREACH WSAENETUNREACH
00095 #define EHOSTUNREACH WSAEHOSTUNREACH
00096 #define ETIMEDOUT WSAETIMEDOUT
00097 #define EINPROGRESS WSAEINPROGRESS
00098 #define ENOTCONN WSAENOTCONN
00099 #define ENOBUFS WSAENOBUFS
00100 #define EADDRNOTAVAIL WSAEADDRNOTAVAIL
00101 #define ENETRESET WSAENETRESET
00102 #define ENETDOWN WSAENETDOWN
00103 #define EHOSTDOWN WSAEHOSTDOWN
00104 #define ENOPROTOOPT WSAENOPROTOOPT
00105 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
00106 
00107 #define SHUT_RD   SD_RECEIVE
00108 #define SHUT_WR   SD_SEND
00109 #define SHUT_RDWR SD_BOTH
00110 
00111 #define S_IXGRP  _S_IEXEC
00112 #define S_IWGRP  _S_IWRITE
00113 #define S_IRGRP  _S_IREAD
00114 
00115 #define S_IRWXG _S_IREAD
00116 #define S_IRWXO _S_IREAD
00117 
00118 #else /* !MINGW32 */
00119 
00120 #include <sys/resource.h>
00121 #include <sys/socket.h>
00122 #endif
00123 
00124 #include <sys/stat.h>
00125 
00126 #ifdef MINGW32
00127 struct iovec 
00128 {
00129     char  *iov_base;
00130     int  iov_len; 
00131 };
00132 
00133 struct passwd
00134 {
00135     char *pw_name;                /* Username.  */
00136     char *pw_passwd;              /* Password.  */
00137 #if 0
00138     __uid_t pw_uid;               /* User ID.  */
00139     __gid_t pw_gid;               /* Group ID.  */
00140     char *pw_gecos;               /* Real name.  */
00141     char *pw_dir;                 /* Home directory.  */
00142     char *pw_shell;               /* Shell program.  */
00143 #endif /* 0 */
00144 };
00145 
00146 #else /* !MINGW32 */
00147 
00148 #include <sys/uio.h>        /* For writev(), readv(), struct iovec */
00149 #include <netinet/in.h>
00150 #include <netinet/tcp.h>
00151 #include <arpa/inet.h>      /* For ntohl(), htonl() */
00152 
00153 #endif /* MINGW32 */
00154 
00155 #include <ctype.h>
00156 #include <fcntl.h>
00157 #include <string.h>
00158 #include <dirent.h>
00159 #include <setjmp.h>
00160 
00161 #ifdef I_TIME
00162 #include <time.h>
00163 #endif
00164 
00165 #ifdef I_SYS_PARAM
00166 #include <sys/param.h>
00167 #endif
00168 #ifdef I_SYS_SYSCTL
00169 #include <sys/sysctl.h>
00170 #endif
00171 #ifdef I_INVENT
00172 #include <invent.h>
00173 #endif
00174 
00175 #ifdef I_INTTYPES
00176 #include <inttypes.h>
00177 #endif /* I_INTTYPES */
00178 
00179 #include <sys/mman.h>
00180 
00181 #ifndef MAP_FAILED
00182 #define MAP_FAILED ((void *) -1)
00183 #endif  /* !MMAP_FAILED */
00184 
00185 #ifdef I_SYS_SENDFILE
00186 #include <sys/sendfile.h>
00187 #else   /* !I_SYS_SENDFILE */
00188 #ifdef HAS_SENDFILE
00189 #define USE_BSD_SENDFILE    
00190 #else
00191 /*
00192  * Proper mmap() support for memory-mapped files requires ISO C functions like
00193  * sigsetjmp().
00194  */
00195 #if defined(__STDC_VERSION__)
00196 #define USE_MMAP 1
00197 #endif  /* ISO C */
00198 
00199 #endif  /* HAS_SENDFILE */
00200 #endif  /* I_SYS_SENDFILE_H */
00201 
00202 #if defined(USE_IP_TOS) && defined(I_NETINET_IP)
00203 #include <netinet/in_systm.h>
00204 #include <netinet/ip.h>
00205 #endif
00206 
00207 /* For pedantic lint checks, define USE_LINT. We override some definitions
00208  * and hide ``inline'' to prevent certain useless warnings. */
00209 #ifdef USE_LINT
00210 #undef G_GNUC_INTERNAL
00211 #define G_GNUC_INTERNAL
00212 #undef G_INLINE_FUNC
00213 #define G_INLINE_FUNC
00214 #define inline
00215 #endif
00216 
00217 /*
00218  * Determine how large an I/O vector the kernel can accept.
00219  */
00220 
00221 #if defined(IOV_MAX)
00222 #define MAX_IOV_COUNT   IOV_MAX         
00223 #elif defined(MAXIOV)
00224 #define MAX_IOV_COUNT   MAXIOV          
00225 #elif defined(UIO_MAXIOV)
00226 #define MAX_IOV_COUNT   UIO_MAXIOV      
00227 #elif defined(_XOPEN_IOV_MAX)
00228 #define MAX_IOV_COUNT   _XOPEN_IOV_MAX  
00229 #else
00230 #define MAX_IOV_COUNT   16              
00231 #endif
00232 
00233 #include <glib.h>
00234 
00235 #ifdef USE_LINT
00236 #undef G_GNUC_INTERNAL
00237 #define G_GNUC_INTERNAL
00238 #undef G_INLINE_FUNC
00239 #define G_INLINE_FUNC
00240 #define inline
00241 #endif
00242 
00243 typedef guint64 filesize_t; 
00245 #include <stdarg.h>
00246 #include <regex.h>
00247 
00248 #include <zlib.h>
00249 
00250 #ifdef USE_GLIB1
00251 typedef void (*GCallback) (void);
00252 #define G_STRLOC __FILE__ ":" STRINGIFY(__LINE__)
00253 #endif
00254 #ifdef USE_GLIB2
00255 #include <glib-object.h>
00256 #endif
00257 
00258 /*
00259  * Portability macros.
00260  */
00261 
00262 /*
00263  * Can only use the `args' obtained via va_start(args) ONCE.  If we need
00264  * to call another vararg routine, we need to copy the original args.
00265  * The __va_copy macro is a GNU extension.
00266  */
00267 #ifdef va_copy
00268 #define VA_COPY(dest, src) va_copy(dest, src)
00269 #elif defined(__va_copy)
00270 #define VA_COPY(dest, src)  __va_copy(dest, src)
00271 #else
00272 #define VA_COPY(dest, src)  (dest) = (src)
00273 #endif
00274 
00275 /*
00276  * Standard file descriptor numbers
00277  */
00278 
00279 #ifndef STDIN_FILENO
00280 #define STDIN_FILENO 0
00281 #endif /* STDIN_FILENO */
00282 
00283 #ifndef STDOUT_FILENO
00284 #define STDOUT_FILENO 1
00285 #endif /* STDOUT_FILENO */
00286 
00287 #ifndef STDERR_FILENO
00288 #define STDERR_FILENO 2
00289 #endif /* STDERR_FILENO */
00290 
00291 
00292 /*
00293  * Other common macros.
00294  */
00295 
00296 #define SRC_PREFIX  "src/"      
00298 /*
00299  * Sources should use _WHERE_ instead of __FILE__ and call short_filename()
00300  * on the resulting string before perusing it to remove the common prefix
00301  * defined by SRC_PREFIX.
00302  */
00303 #ifdef CURDIR                   /* Set by makefile */
00304 #define _WHERE_ STRINGIFY(CURDIR) "/" __FILE__
00305 #else
00306 #define _WHERE_ __FILE__
00307 #endif
00308 
00309 /*
00310  * PACKAGE_EXTRA_SOURCE_DIR is set to $srcdir/extra_files when not compiling an
00311  * official build so it's not required to install these files for testing.
00312  */
00313 #ifdef OFFICIAL_BUILD
00314 #undef PACKAGE_EXTRA_SOURCE_DIR
00315 #else
00316 #define PACKAGE_EXTRA_SOURCE_DIR \
00317     PACKAGE_SOURCE_DIR G_DIR_SEPARATOR_S "extra_files"
00318 #endif
00319 
00325 #define G_FREE_NULL(p)  \
00326 G_STMT_START {          \
00327     if (p) {            \
00328         g_free(p);      \
00329         p = NULL;       \
00330     }                   \
00331 } G_STMT_END
00332 
00340 #define RCSID(x) \
00341 static inline const char *  \
00342 get_rcsid(void)     \
00343 {   \
00344     static const char rcsid[] = "@(#) " x;  \
00345     const char *s = rcsid;  \
00346     while (*s != '\0') {    \
00347         if (*s++ == '$')    \
00348             break;  \
00349     }   \
00350     return s;   \
00351 }
00352 
00353 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
00354 #define HAVE_GCC(major, minor) \
00355     ((__GNUC__ > (major)) || \
00356      (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
00357 #else
00358 #define HAVE_GCC(major, minor) 0
00359 #endif
00360 
00361 /*
00362  * If functions have this attribute GCC warns if it one of the specified
00363  * parameters is NULL. This macro takes a list of parameter indices. The
00364  * list must be embraced with parentheses for compatibility with C89
00365  * compilers. Example:
00366  *
00367  * void my_memcpy(void *dst, const void *src, size_t n) NON_NULL_PARAM((1, 2));
00368  */
00369 #if HAVE_GCC(3, 3)
00370 #define NON_NULL_PARAM(x) __attribute__((nonnull x))
00371 #else /* GCC < 3.3 */
00372 #define NON_NULL_PARAM(x)
00373 #endif
00374 
00379 #if HAVE_GCC(3, 0)
00380 #define PRINTF_FUNC_PTR(x, y) __attribute__((format(printf, (x), (y))))
00381 #else /* GCC < 3.0 */
00382 #define PRINTF_FUNC_PTR(x, y)
00383 #endif
00384 
00385 /* Functions using this attribute cause a warning if the returned
00386  * value is not used. */
00387 #if HAVE_GCC(3, 4)
00388 #define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
00389 #else /* GCC < 3.4 */
00390 #define WARN_UNUSED_RESULT
00391 #endif
00392 
00393 /* Functions using this attribute cause a warning if the variable
00394  * argument list does not contain a NULL pointer. */
00395 #if HAVE_GCC(4, 0)
00396 #define WARN_NEED_SENTINEL __attribute__((sentinel))
00397 #else /* GCC < 4 */
00398 #define WARN_NEED_SENTINEL
00399 #endif /* GCC >= 4 */
00400 
00401 /* Define G_LIKELY() and G_UNLIKELY() so that they are available when
00402  * using GLib 1.2 as well. These allow optimization by static branch
00403  * prediction with GCC. */
00404 #ifndef G_LIKELY
00405 #if HAVE_GCC(3, 4)  /* Just a guess, a Configure check would be better */
00406 #define G_LIKELY(x)     (__builtin_expect(x, 1))
00407 #define G_UNLIKELY(x)   (__builtin_expect(x, 0))
00408 #else /* !GCC >= 3.4 */
00409 #define G_LIKELY(x)     (x)
00410 #define G_UNLIKELY(x)   (x)
00411 #endif /* GCC >= 3.4 */
00412 #endif /* !G_LIKELY */
00413 
00417 #define CMP(a, b) ((a) == (b) ? 0 : (a) > (b) ? 1 : (-1))
00418 
00432 #define STATIC_ASSERT(x) \
00433     do { switch (0) { case ((x) ? 1 : 0): case 0: break; } } while(0)
00434 
00435 #if defined(GTA_PATCHLEVEL) && (GTA_PATCHLEVEL != 0)
00436 #define GTA_VERSION_NUMBER \
00437     STRINGIFY(GTA_VERSION) "." \
00438     STRINGIFY(GTA_SUBVERSION) "." \
00439     STRINGIFY(GTA_PATCHLEVEL) GTA_REVCHAR
00440 #else
00441 #define GTA_VERSION_NUMBER \
00442     STRINGIFY(GTA_VERSION) "." STRINGIFY(GTA_SUBVERSION) GTA_REVCHAR
00443 #endif
00444 
00445 #define GTA_PORT            6346    
00446 #define MAX_HOSTLEN         256     
00448 /* The next two defines came from huge.h --- Emile */
00449 #define SHA1_BASE32_SIZE    32      
00450 #define SHA1_RAW_SIZE       20      
00452 #define BITPRINT_BASE32_SIZE    72  
00454 #define BITPRINT_RAW_SIZE       44  
00457 /*
00458  * Forbidden glib calls.
00459  */
00460 
00461 #define g_snprintf  DONT_CALL_g_snprintf 
00462 #define g_vsnprintf DONT_CALL_g_vsnprintf 
00464 /*
00465  * Typedefs
00466  */
00467 
00468 typedef gboolean (*reclaim_fd_t)(void);
00469 
00470 /*
00471  * Standard gettext macros.
00472  */
00473 
00474 #ifdef ENABLE_NLS
00475 #  include <libintl.h>
00476 #  undef _
00477 #  define _(String) dgettext(PACKAGE, String)
00478 #  define Q_(String) g_strip_context ((String), gettext (String))
00479 #  ifdef gettext_noop
00480 #    define N_(String) gettext_noop(String)
00481 #  else
00482 #    define N_(String) (String)
00483 #  endif
00484 
00485 
00486 #else
00487 #  define textdomain(String) (String)
00488 #  define gettext(String) (String)
00489 #  define dgettext(Domain,Message) (Message)
00490 #  define dcgettext(Domain,Message,Type) (Message)
00491 #  define bindtextdomain(Domain,Directory) (Domain)
00492 #  define ngettext(Single, Plural, Number) ((Number) == 1 ? (Single) : (Plural))
00493 #  define _(String) (String)
00494 #  define N_(String) (String)
00495 #  define Q_(String) g_strip_context ((String), (String))
00496 #endif /* ENABLE_NLS */
00497 
00498 static inline const gchar *
00499 ngettext_(const gchar *msg1, const gchar *msg2, gulong n)
00500 G_GNUC_FORMAT(1) G_GNUC_FORMAT(2);
00501 
00502 static inline const gchar *
00503 ngettext_(const gchar *msg1, const gchar *msg2, gulong n)
00504 {
00505     return ngettext(msg1, msg2, n);
00506 }
00507 
00511 #define NG_(Single, Plural, Number) ngettext_((Single), (Plural), (Number))
00512 
00513 #endif /* _common_h_ */
00514 
00515 /* vi: set ts=4 sw=4 cindent: */

Generated on Sun Feb 12 10:49:56 2006 for Gtk-Gnutella by doxygen 1.3.6