00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00036 #ifndef _common_h_
00037 #define _common_h_
00038
00039 #include "config.h"
00040
00041
00042
00043
00044
00045 #define GTA_VERSION 0
00046 #define GTA_SUBVERSION 96
00047 #define GTA_PATCHLEVEL 4
00048 #define GTA_REVISION "unstable"
00049 #define GTA_REVCHAR "u"
00050 #define GTA_RELEASE "2007-06-23"
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 #error "Expected Gtk+ 1.2, Gtk+ 2.x or headless as user-interface."
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
00069
00070
00071 #include <errno.h>
00072
00073 #ifdef I_STDLIB
00074 #include <stdlib.h>
00075 #endif
00076
00077 #include <stdio.h>
00078 #include <signal.h>
00079
00080 #ifdef I_UNISTD
00081 #include <unistd.h>
00082 #endif
00083
00084 #ifdef I_SYS_TYPES
00085 #include <sys/types.h>
00086 #endif
00087
00088 #ifdef I_SYS_TIME
00089 #include <sys/time.h>
00090 #endif
00091
00092 #ifdef I_SYS_TIME_KERNEL
00093 #define KERNEL
00094 #include <sys/time.h>
00095 #undef KERNEL
00096 #endif
00097
00098 #ifdef MINGW32
00099 #include <ws2tcpip.h>
00100 #include <winsock.h>
00101 #define ECONNRESET WSAECONNRESET
00102 #define ECONNREFUSED WSAECONNREFUSED
00103 #define ECONNABORTED WSAECONNABORTED
00104 #define ENETUNREACH WSAENETUNREACH
00105 #define EHOSTUNREACH WSAEHOSTUNREACH
00106 #define ETIMEDOUT WSAETIMEDOUT
00107 #define EINPROGRESS WSAEINPROGRESS
00108 #define ENOTCONN WSAENOTCONN
00109 #define ENOBUFS WSAENOBUFS
00110 #define EADDRNOTAVAIL WSAEADDRNOTAVAIL
00111 #define ENETRESET WSAENETRESET
00112 #define ENETDOWN WSAENETDOWN
00113 #define EHOSTDOWN WSAEHOSTDOWN
00114 #define ENOPROTOOPT WSAENOPROTOOPT
00115 #define EPROTONOSUPPORT WSAEPROTONOSUPPORT
00116
00117 #define SHUT_RD SD_RECEIVE
00118 #define SHUT_WR SD_SEND
00119 #define SHUT_RDWR SD_BOTH
00120
00121 #define S_IXGRP _S_IEXEC
00122 #define S_IWGRP _S_IWRITE
00123 #define S_IRGRP _S_IREAD
00124
00125 #define S_IRWXG _S_IREAD
00126 #define S_IRWXO _S_IREAD
00127
00128 #endif
00129
00130 #ifdef I_SYS_SOCKET
00131 #include <sys/socket.h>
00132 #endif
00133
00134 #ifdef I_SYS_RESOURCE
00135 #include <sys/resource.h>
00136 #endif
00137
00138 #ifdef I_SYS_UN
00139 #include <sys/un.h>
00140 #endif
00141
00142 #ifdef I_SYS_STAT
00143 #include <sys/stat.h>
00144 #endif
00145
00146 #ifdef MINGW32
00147 struct iovec
00148 {
00149 char *iov_base;
00150 int iov_len;
00151 };
00152
00153 struct passwd
00154 {
00155 char *pw_name;
00156 char *pw_passwd;
00157 #if 0
00158 __uid_t pw_uid;
00159 __gid_t pw_gid;
00160 char *pw_gecos;
00161 char *pw_dir;
00162 char *pw_shell;
00163 #endif
00164 };
00165
00166 #else
00167
00168 #include <sys/uio.h>
00169 #include <sys/wait.h>
00170
00171 #ifdef I_NETINET_IN
00172 #include <netinet/in.h>
00173 #endif
00174
00175 #include <netinet/tcp.h>
00176
00177 #endif
00178
00179 #ifdef I_ARPA_INET
00180 #include <arpa/inet.h>
00181 #endif
00182
00183 #include <ctype.h>
00184
00185 #ifdef I_SYS_FILE
00186 #include <sys/file.h>
00187 #endif
00188
00189 #include <fcntl.h>
00190
00191 #if !defined(I_FCNTL) && !defined(I_SYS_FILE)
00192 #include <sys/fcntl.h>
00193 #endif
00194
00195 #ifdef I_STRING
00196 #include <string.h>
00197 #else
00198 #include <strings.h>
00199 #endif
00200
00201 #ifdef I_DIRENT
00202 #include <dirent.h>
00203 #endif
00204
00205 #include <setjmp.h>
00206
00207 #ifdef I_TIME
00208 #include <time.h>
00209 #endif
00210
00211 #ifdef I_SYS_PARAM
00212 #include <sys/param.h>
00213 #endif
00214
00215 #ifdef I_INTTYPES
00216 #include <inttypes.h>
00217 #endif
00218
00219 #ifdef I_SYS_UTSNAME
00220 #include <sys/utsname.h>
00221 #endif
00222
00223 #ifdef I_SYS_MMAN
00224 #include <sys/mman.h>
00225 #endif
00226
00227 #ifndef MAP_FAILED
00228 #define MAP_FAILED ((void *) -1)
00229 #endif
00230
00231 #ifdef I_SYS_SENDFILE
00232 #include <sys/sendfile.h>
00233 #else
00234 #ifdef HAS_SENDFILE
00235 #define USE_BSD_SENDFILE
00236 #else
00237
00238
00239
00240
00241 #if defined(HAS_MMAP) && defined(HAS_SIGSETJMP)
00242 #define USE_MMAP 1
00243 #endif
00244
00245 #endif
00246 #endif
00247
00248 #if defined(USE_IP_TOS) && defined(I_NETINET_IP)
00249 #include <netinet/in_systm.h>
00250 #include <netinet/ip.h>
00251 #endif
00252
00253 #if defined(HAS_POLL)
00254 #include <poll.h>
00255 #endif
00256
00257 #if !defined(AF_LOCAL) && defined(AF_UNIX)
00258 #define AF_LOCAL AF_UNIX
00259 #endif
00260
00261 #if !defined(PF_LOCAL) && defined(PF_UNIX)
00262 #define PF_LOCAL PF_UNIX
00263 #endif
00264
00265
00266
00267 #ifdef USE_LINT
00268 #undef G_GNUC_INTERNAL
00269 #define G_GNUC_INTERNAL
00270 #undef G_INLINE_FUNC
00271 #define G_INLINE_FUNC
00272 #define inline
00273 #endif
00274
00275
00276
00277
00278
00279 #if defined(IOV_MAX)
00280 #define MAX_IOV_COUNT IOV_MAX
00281 #elif defined(MAXIOV)
00282 #define MAX_IOV_COUNT MAXIOV
00283 #elif defined(UIO_MAXIOV)
00284 #define MAX_IOV_COUNT UIO_MAXIOV
00285 #elif defined(_XOPEN_IOV_MAX)
00286 #define MAX_IOV_COUNT _XOPEN_IOV_MAX
00287 #else
00288 #define MAX_IOV_COUNT 16
00289 #endif
00290
00291 #include <glib.h>
00292
00293 #if defined(USE_GLIB1) && !defined(GLIB_MAJOR_VERSION)
00294 #error "Install GLib 1.2 to compile gtk-gnutella against GLib 1.2."
00295 #endif
00296
00297 #if defined(USE_GLIB2) && !defined(GLIB_MAJOR_VERSION)
00298 #error "Install GLib 2.x to compile gtk-gnutella against GLib 2.x."
00299 #endif
00300
00301 #ifdef USE_LINT
00302 #undef G_GNUC_INTERNAL
00303 #define G_GNUC_INTERNAL
00304 #undef G_INLINE_FUNC
00305 #define G_INLINE_FUNC
00306 #define inline
00307 #endif
00308
00309 typedef guint64 filesize_t;
00311 #ifdef I_STDARG
00312 #include <stdarg.h>
00313 #endif
00314
00315 #ifdef I_REGEX
00316 #include <regex.h>
00317 #endif
00318
00319 #ifdef USE_GLIB1
00320 typedef void (*GCallback) (void);
00321 #define G_STRLOC __FILE__ ":" STRINGIFY(__LINE__)
00322 #endif
00323 #ifdef USE_GLIB2
00324 #include <glib-object.h>
00325 #endif
00326
00327
00328
00329
00330 #ifndef G_N_ELEMENTS
00331 #define G_N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0]))
00332 #endif
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343 #ifdef va_copy
00344 #define VA_COPY(dest, src) va_copy(dest, src)
00345 #elif defined(__va_copy)
00346 #define VA_COPY(dest, src) __va_copy(dest, src)
00347 #else
00348 #define VA_COPY(dest, src) (dest) = (src)
00349 #endif
00350
00351
00352
00353
00354
00355 #ifndef STDIN_FILENO
00356 #define STDIN_FILENO 0
00357 #endif
00358
00359 #ifndef STDOUT_FILENO
00360 #define STDOUT_FILENO 1
00361 #endif
00362
00363 #ifndef STDERR_FILENO
00364 #define STDERR_FILENO 2
00365 #endif
00366
00367
00368
00369
00370 #ifndef S_ISLNK
00371 #define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
00372 #endif
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382 #define MAX_INT_VAL_STEP(t) \
00383 ((t) 1 << (CHAR_BIT * sizeof(t) - 1 - ((t) -1 < 1)))
00384
00385 #define MAX_INT_VAL(t) \
00386 ((MAX_INT_VAL_STEP(t) - 1) + MAX_INT_VAL_STEP(t))
00387
00388 #define MIN_INT_VAL(t) \
00389 ((t) -MAX_INT_VAL(t) - 1)
00390
00391 #ifndef TIME_T_MAX
00392
00393 #define TIME_T_MAX MAX_INT_VAL(time_t)
00394 #endif
00395
00396 #ifndef OFF_T_MAX
00397 #define OFF_T_MAX MAX_INT_VAL(off_t)
00398 #endif
00399
00400
00401
00402
00403
00404 #define SRC_PREFIX "src/"
00406
00407
00408
00409
00410
00411 #ifdef CURDIR
00412 #define _WHERE_ STRINGIFY(CURDIR) "/" __FILE__
00413 #else
00414 #define _WHERE_ __FILE__
00415 #endif
00416
00417
00418
00419
00420
00421 #ifdef OFFICIAL_BUILD
00422 #undef PACKAGE_EXTRA_SOURCE_DIR
00423 #else
00424 #define PACKAGE_EXTRA_SOURCE_DIR \
00425 PACKAGE_SOURCE_DIR G_DIR_SEPARATOR_S "extra_files"
00426 #endif
00427
00433 #define G_FREE_NULL(p) \
00434 G_STMT_START { \
00435 if (p) { \
00436 g_free(p); \
00437 p = NULL; \
00438 } \
00439 } G_STMT_END
00440
00441 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
00442
00451 #define HAVE_GCC(major, minor) \
00452 ((__GNUC__ > (major)) || \
00453 (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
00454 #else
00455 #define HAVE_GCC(major, minor) 0
00456 #endif
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466 #if HAVE_GCC(3, 3)
00467 #define NON_NULL_PARAM(x) __attribute__((__nonnull__ x))
00468 #else
00469 #define NON_NULL_PARAM(x)
00470 #endif
00471
00476 #if HAVE_GCC(3, 0)
00477 #define PRINTF_FUNC_PTR(x, y) __attribute__((format(__printf__, (x), (y))))
00478 #else
00479 #define PRINTF_FUNC_PTR(x, y)
00480 #endif
00481
00482
00483
00484 #if HAVE_GCC(3, 4)
00485 #define WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
00486 #else
00487 #define WARN_UNUSED_RESULT
00488 #endif
00489
00490
00491
00492 #if HAVE_GCC(3, 1)
00493 #define KEEP_FUNCTION __attribute__((__used__))
00494 #else
00495 #define KEEP_FUNCTION
00496 #endif
00497
00498
00499
00500
00501
00502
00503 #define IGNORE_RESULT(x) \
00504 G_STMT_START { switch (0 != (x)) { default: ; } } G_STMT_END
00505
00506
00507
00508 #ifndef G_GNUC_NULL_TERMINATED
00509 #if HAVE_GCC(4, 0)
00510 #define G_GNUC_NULL_TERMINATED __attribute__((__sentinel__))
00511 #else
00512 #define G_GNUC_NULL_TERMINATED
00513 #endif
00514 #endif
00515
00516
00517
00518
00519 #ifndef G_LIKELY
00520 #if HAVE_GCC(3, 4)
00521 #define G_LIKELY(x) (__builtin_expect(x, 1))
00522 #define G_UNLIKELY(x) (__builtin_expect(x, 0))
00523 #else
00524 #define G_LIKELY(x) (x)
00525 #define G_UNLIKELY(x) (x)
00526 #endif
00527 #endif
00528
00529 #ifndef G_GNUC_MALLOC
00530 #if HAVE_GCC(3, 0)
00531 #define G_GNUC_MALLOC __attribute__((__malloc__))
00532 #else
00533 #define G_GNUC_MALLOC
00534 #endif
00535 #endif
00536
00537 #if HAVE_GCC(3, 1)
00538 #define ALWAYS_INLINE __attribute__((__always_inline__))
00539 #else
00540 #define ALWAYS_INLINE
00541 #endif
00542
00543 #ifdef HAS_REGPARM
00544 #define REGPARM(n) __attribute__((__regparm__((n))))
00545 #else
00546 #define REGPARM(n)
00547 #endif
00548
00552 #define CMP(a, b) ((a) == (b) ? 0 : (a) > (b) ? 1 : (-1))
00553
00557 #define SIGN(x) ((x) == 0 ? 0 : (x) > 0 ? 1 : (-1))
00558
00559
00573 #define STATIC_ASSERT(x) \
00574 do { switch (0) { case ((x) ? 1 : 0): case 0: break; } } while(0)
00575
00576 #if defined(GTA_PATCHLEVEL) && (GTA_PATCHLEVEL != 0)
00577 #define GTA_VERSION_NUMBER \
00578 STRINGIFY(GTA_VERSION) "." \
00579 STRINGIFY(GTA_SUBVERSION) "." \
00580 STRINGIFY(GTA_PATCHLEVEL) GTA_REVCHAR
00581 #else
00582 #define GTA_VERSION_NUMBER \
00583 STRINGIFY(GTA_VERSION) "." STRINGIFY(GTA_SUBVERSION) GTA_REVCHAR
00584 #endif
00585
00586 #define GTA_PORT 6346
00587 #define MAX_HOSTLEN 256
00588 #define GTA_BUILD "$Revision: 13938 $"
00589
00590
00591 #define SHA1_BASE16_SIZE 40
00592 #define SHA1_BASE32_SIZE 32
00593 #define SHA1_RAW_SIZE 20
00595 #define TTH_BASE32_SIZE 39
00596 #define TTH_RAW_SIZE 24
00598 #define BITPRINT_BASE32_SIZE 72
00600 #define BITPRINT_RAW_SIZE 44
00603 #define FILENAME_MAXBYTES 256
00604
00605
00606
00607
00608
00609 #define g_snprintf DONT_CALL_g_snprintf
00610 #define g_vsnprintf DONT_CALL_g_vsnprintf
00612
00613
00614
00615
00616 typedef gboolean (*reclaim_fd_t)(void);
00617
00618
00619
00620
00621
00622 #ifdef ENABLE_NLS
00623 # include <libintl.h>
00624 # undef _
00625 # define _(String) dgettext(PACKAGE, String)
00626 # define Q_(String) g_strip_context ((String), gettext (String))
00627 # ifdef gettext_noop
00628 # define N_(String) gettext_noop(String)
00629 # else
00630 # define N_(String) (String)
00631 # endif
00632
00633
00634 #else
00635 # define textdomain(String) (String)
00636 # define gettext(String) (String)
00637 # define dgettext(Domain,Message) (Message)
00638 # define dcgettext(Domain,Message,Type) (Message)
00639 # define bindtextdomain(Domain,Directory) (Domain)
00640 # define ngettext(Single, Plural, Number) ((Number) == 1 ? (Single) : (Plural))
00641 # define _(String) (String)
00642 # define N_(String) (String)
00643 # define Q_(String) g_strip_context ((String), (String))
00644 #endif
00645
00646 static inline const gchar *
00647 ngettext_(const gchar *msg1, const gchar *msg2, gulong n)
00648 G_GNUC_FORMAT(1) G_GNUC_FORMAT(2);
00649
00650 static inline const gchar *
00651 ngettext_(const gchar *msg1, const gchar *msg2, gulong n)
00652 {
00653 return ngettext(msg1, msg2, n);
00654 }
00655
00659 #define NG_(Single, Plural, Number) ngettext_((Single), (Plural), (Number))
00660
00668 #define RCSID(x) \
00669 static KEEP_FUNCTION inline const char * \
00670 get_rcsid(void) \
00671 { \
00672 static const char rcsid[] = "@(#) " x; \
00673 const char *s = rcsid; \
00674 while (*s != '\0') { \
00675 if (*s++ == '$') \
00676 break; \
00677 } \
00678 return s; \
00679 }
00680
00681 #include "casts.h"
00682
00683 #endif
00684
00685