globus_libc.h

Go to the documentation of this file.
00001 /*
00002  * Copyright 1999-2006 University of Chicago
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00022 #ifndef GLOBUS_LIBC_H
00023 #define GLOBUS_LIBC_H 1
00024 
00025 #include "globus_common_include.h"
00026 #include "globus_thread.h"
00027 
00028 #if defined(WIN32) && !defined(__CYGWIN__)
00029 /* For addrinfo struct */
00030 #include <winsock2.h>
00031 #include <ws2tcpip.h>
00032 #define EAI_SYSTEM 11
00033 #define snprintf _snprintf
00034 #endif
00035 
00036 #if __GNUC__
00037 #   define GLOBUS_DEPRECATED(func) func __attribute__((deprecated))
00038 #elif defined(_MSC_VER)
00039 #   define GLOBUS_DEPRECATED(func)  __declspec(deprecated) func
00040 #else
00041 #   define GLOBUS_DEPRECATED(func) func
00042 #endif
00043 
00044 #ifdef __cplusplus
00045 extern "C" {
00046 #endif
00047 
00048 extern globus_mutex_t globus_libc_mutex;
00049 
00050 #define globus_macro_libc_lock() \
00051     globus_mutex_lock(&globus_libc_mutex)
00052 #define globus_macro_libc_unlock() \
00053     globus_mutex_unlock(&globus_libc_mutex)
00054 
00055 #ifdef USE_MACROS
00056 #define globus_libc_lock()   globus_macro_libc_lock()
00057 #define globus_libc_unlock() globus_macro_libc_unlock()
00058 #else  /* USE_MACROS */
00059 extern int globus_libc_lock(void);
00060 extern int globus_libc_unlock(void);
00061 #endif /* USE_MACROS */
00062 
00063 #if defined(va_copy)
00064 #   define globus_libc_va_copy(dest,src) \
00065         va_copy(dest,src)
00066 #elif defined(__va_copy)
00067 #   define globus_libc_va_copy(dest,src) \
00068         __va_copy(dest,src)
00069 #else
00070 #   define globus_libc_va_copy(dest,src) \
00071         memcpy(&dest, &src, sizeof(va_list))
00072 #endif
00073 
00074 
00075 #define globus_stdio_lock globus_libc_lock
00076 #define globus_stdio_unlock globus_libc_unlock
00077 #define globus_libc_printf   printf
00078 #define globus_libc_fprintf  fprintf
00079 #define globus_libc_sprintf  sprintf
00080 #define globus_libc_vprintf  vprintf
00081 #define globus_libc_vfprintf vfprintf
00082 #define globus_libc_vsprintf vsprintf
00083 
00084 #if __STDC_VERSION__ >= 199901L
00085 #define globus_libc_snprintf snprintf
00086 #define globus_libc_vsnprintf vsnprintf
00087 #else
00088 extern int globus_libc_snprintf(char *s, size_t n, const char *format, ...);
00089 extern int globus_libc_vsnprintf(char *s, size_t n, const char *format,
00090     va_list ap);
00091 #endif
00092 
00093 /*
00094  * File I/O routines
00095  */
00096 #if !defined(_WIN32)
00097 #define globus_libc_open open
00098 #define globus_libc_close close
00099 #define globus_libc_read read
00100 #define globus_libc_write write
00101 #define globus_libc_umask umask
00102 #define globus_libc_writev writev
00103 #define globus_libc_fstat fstat
00104 
00105 #define globus_libc_opendir opendir
00106 #define globus_libc_telldir telldir
00107 #define globus_libc_seekdir seekdir
00108 #define globus_libc_rewinddir rewinddir
00109 #define globus_libc_closedir closedir
00110 #define globus_libc_getpwuid_r getpwuid_r
00111 
00112 #else /* _WIN32 */
00113 
00114 extern
00115 mode_t
00116 globus_libc_umask_win32(
00117     mode_t                          mask); 
00118 
00119 #    define globus_libc_open            _open
00120 #    define globus_libc_close           _close
00121 #    define globus_libc_read            _read
00122 #    define globus_libc_write           _write
00123 #    define globus_libc_umask           globus_libc_umask_win32
00124 #           define globus_libc_writev(fd,iov,iovcnt) \
00125                     write(fd,iov[0].iov_base,iov[0].iov_len)
00126 #   define uid_t int
00127 #if defined(TARGET_ARCH_CYGWIN) || defined(TARGET_ARCH_MINGW32)
00128 #define globus_libc_opendir opendir
00129 #define globus_libc_telldir telldir
00130 #define globus_libc_seekdir seekdir
00131 #define globus_libc_rewinddir rewinddir
00132 #define globus_libc_closedir closedir
00133 #endif
00134 #endif /* _WIN32 */
00135 extern
00136 int
00137 globus_libc_readdir_r(
00138     DIR *                           dirp,
00139     struct dirent **                result);
00140 
00141 /*
00142  * Memory allocation routines
00143  */
00144 #define globus_malloc(bytes) globus_libc_malloc(bytes)
00145 #define globus_realloc(ptr,bytes) globus_libc_realloc(ptr,bytes)
00146 #define globus_calloc(nobjs,bytes) globus_libc_calloc(nobjs,bytes)
00147 #define globus_free(ptr) globus_libc_free(ptr)
00148 
00149 #define globus_libc_malloc      malloc
00150 #define globus_libc_realloc     realloc
00151 #define globus_libc_calloc      calloc
00152 #define globus_libc_free        free
00153 #define globus_libc_alloca      alloca
00154 #define globus_libc_lseek lseek
00155 
00156 /* Miscellaneous libc functions (formerly md_unix.c) */
00157 int globus_libc_gethostname(char *name, int len);
00158 int globus_libc_getpid(void);
00159 int globus_libc_fork(void);
00160 int globus_libc_usleep(long usec);
00161 double globus_libc_wallclock(void);
00162 
00163 /* returns # of characters printed to s */
00164 extern int globus_libc_sprint_off_t(char * s, globus_off_t off);
00165 /* returns 1 if scanned succeeded */
00166 extern int globus_libc_scan_off_t(char *s, globus_off_t *off, int *consumed);
00167 
00168 /* Use getaddrinfo instead */
00169 GLOBUS_DEPRECATED(struct hostent *globus_libc_gethostbyname_r(char *name,
00170                                             struct hostent *result,
00171                                             char *buffer,
00172                                             int buflen,
00173                                             int *h_errnop));
00174 /* Use getnameinfo instead */
00175 GLOBUS_DEPRECATED(struct hostent *globus_libc_gethostbyaddr_r(char *addr,
00176                                             int length,
00177                                             int type,
00178                                             struct hostent *result,
00179                                             char *buffer,
00180                                             int buflen,
00181                                             int *h_errnop));
00182 
00183 #ifdef _POSIX_THREAD_SAFE_FUCTIONS
00184 #define globus_libc_ctime_r(clock, buf, buflen) ctime_r(clock, buf)
00185 #define globus_libc_localtime_r(timer, result) localtime_r(timer, result)
00186 #define globus_libc_gmtime_r(timer, result) gmtime_r(timer, result)
00187 #else
00188 char *globus_libc_ctime_r(/*should be const */time_t *clock, char *buf, int buflen);
00189 struct tm * globus_libc_localtime_r(const time_t *timep, struct tm *result);
00190 struct tm * globus_libc_gmtime_r(const time_t *timep, struct tm *result);
00191 #endif
00192 
00193 #if !defined(_WIN32)
00194 #define globus_libc_getpwnam_r getpwnam_r
00195 #endif
00196 
00197 int
00198 globus_libc_strncasecmp(
00199     const char *                            s1,
00200     const char *                            s2,
00201     globus_size_t                           n);
00202 
00203 int globus_libc_setenv(register const char *name,
00204                        register const char *value,
00205                        int rewrite);
00206 void globus_libc_unsetenv(register const char *name);
00207 
00208 /* Use getenv instead */
00209 GLOBUS_DEPRECATED(char *globus_libc_getenv(register const char *name));
00210 
00211 /* Use strerror or strerror_r as needed instead */
00212 char *globus_libc_system_error_string(int the_error);
00213 
00214 char *
00215 globus_libc_strdup(const char * source);
00216 
00217 char *
00218 globus_libc_strndup(const char * string, globus_size_t length);
00219 
00220 char *
00221 globus_libc_strtok(
00222     char *                                  s, 
00223     const char *                            delim);
00224 
00225 #define globus_libc_strcmp strcmp
00226 #define globus_libc_strlen strlen
00227 
00228 char *
00229 globus_libc_join(
00230     const char **                       array,
00231     int                                 count);
00232     
00233 int
00234 globus_libc_vprintf_length(const char * fmt, va_list ap);
00235 
00236 int
00237 globus_libc_printf_length(const char * fmt, ...);
00238 
00239 /* not really 'libc'... but a convenient place to put it in */
00240 int globus_libc_gethomedir(char *result, int bufsize);
00241 
00242 char *
00243 globus_common_create_string(
00244     const char *                        format,
00245     ...);
00246 
00247 char *
00248 globus_common_create_nstring(
00249     int                                 length,
00250     const char *                        format,
00251     ...);
00252 
00253 char *
00254 globus_common_v_create_string(
00255     const char *                        format,
00256     va_list                             ap);
00257 
00258 char *
00259 globus_common_v_create_nstring(
00260     int                                 length,
00261     const char *                        format,
00262     va_list                             ap);
00263 
00264 /* for backwards compatibility */
00265 #define globus_libc_memmove(d, s, n) memmove((d), (s), (n)) 
00266 
00267 #ifdef __hpux
00268 #   define   globus_libc_setegid(a)  setresgid(-1,a,-1)
00269 #   define   globus_libc_seteuid(a)  setresuid(-1,a,-1)
00270 #else
00271 #   define   globus_libc_setegid(a)  setegid(a)
00272 #   define   globus_libc_seteuid(a)  seteuid(a)
00273 #endif
00274 
00275 
00276 /* IPv6 compatible utils */
00277 typedef struct sockaddr_storage         globus_sockaddr_t;
00278 typedef struct addrinfo                 globus_addrinfo_t;
00279 
00280 #ifdef AF_INET6
00281 #define GlobusLibcProtocolFamilyIsIP(family)                                \
00282     ((family == AF_INET ? 1 : (family == AF_INET6 ? 1 : 0)))
00283 #else
00284 #define GlobusLibcProtocolFamilyIsIP(family)                                \
00285     (family == AF_INET ? 1 : 0)
00286 #endif
00287 
00288 #ifndef PF_INET
00289 #define PF_INET AF_INET
00290 #endif
00291 
00292 #ifndef PF_UNSPEC
00293 #define PF_UNSPEC AF_UNSPEC
00294 #endif
00295 
00296 #define GlobusLibcSockaddrSetFamily(_addr, fam)  ((struct sockaddr *) &(_addr))->sa_family = fam
00297 #define GlobusLibcSockaddrGetFamily(_addr)  ((struct sockaddr *) &(_addr))->sa_family
00298 
00299 #ifdef AF_INET6
00300 #define GlobusLibcSockaddrGetPort(addr, port)                               \
00301     do                                                                      \
00302     {                                                                       \
00303         const struct sockaddr *         _addr = (struct sockaddr *) &(addr);\
00304                                                                             \
00305         switch(_addr->sa_family)                                            \
00306         {                                                                   \
00307           case AF_INET:                                                     \
00308             (port) = ntohs(((struct sockaddr_in *) _addr)->sin_port);       \
00309             break;                                                          \
00310                                                                             \
00311           case AF_INET6:                                                    \
00312             (port) = ntohs(((struct sockaddr_in6 *) _addr)->sin6_port);     \
00313             break;                                                          \
00314                                                                             \
00315           default:                                                          \
00316             globus_assert(0 &&                                              \
00317                 "Unknown family in GlobusLibcSockaddrGetPort");             \
00318             (port) = -1;                                                    \
00319             break;                                                          \
00320         }                                                                   \
00321     } while(0)
00322 #else
00323 #define GlobusLibcSockaddrGetPort(addr, port)                               \
00324     do                                                                      \
00325     {                                                                       \
00326         const struct sockaddr *         _addr = (struct sockaddr *) &(addr);\
00327                                                                             \
00328         switch(_addr->sa_family)                                            \
00329         {                                                                   \
00330           case AF_INET:                                                     \
00331             (port) = ntohs(((struct sockaddr_in *) _addr)->sin_port);       \
00332             break;                                                          \
00333                                                                             \
00334           default:                                                          \
00335             globus_assert(0 &&                                              \
00336                 "Unknown family in GlobusLibcSockaddrGetPort");             \
00337             (port) = -1;                                                    \
00338             break;                                                          \
00339         }                                                                   \
00340     } while(0)
00341 #endif
00342 
00343 #ifdef AF_INET6
00344 #define GlobusLibcSockaddrSetPort(addr, port)                               \
00345     do                                                                      \
00346     {                                                                       \
00347         struct sockaddr *               _addr = (struct sockaddr *) &(addr);\
00348                                                                             \
00349         switch(_addr->sa_family)                                            \
00350         {                                                                   \
00351           case AF_INET:                                                     \
00352             ((struct sockaddr_in *) _addr)->sin_port = htons((port));       \
00353             break;                                                          \
00354                                                                             \
00355           case AF_INET6:                                                    \
00356             ((struct sockaddr_in6 *) _addr)->sin6_port = htons((port));     \
00357             break;                                                          \
00358                                                                             \
00359           default:                                                          \
00360             globus_assert(0 &&                                              \
00361                 "Unknown family in GlobusLibcSockaddrSetPort");             \
00362             break;                                                          \
00363         }                                                                   \
00364     } while(0)
00365 #else
00366 #define GlobusLibcSockaddrSetPort(addr, port)                               \
00367     do                                                                      \
00368     {                                                                       \
00369         struct sockaddr *               _addr = (struct sockaddr *) &(addr);\
00370                                                                             \
00371         switch(_addr->sa_family)                                            \
00372         {                                                                   \
00373           case AF_INET:                                                     \
00374             ((struct sockaddr_in *) _addr)->sin_port = htons((port));       \
00375             break;                                                          \
00376                                                                             \
00377           default:                                                          \
00378             globus_assert(0 &&                                              \
00379                 "Unknown family in GlobusLibcSockaddrSetPort");             \
00380             break;                                                          \
00381         }                                                                   \
00382     } while(0)
00383 #endif
00384 
00385 /* only use this on systems with the sin_len field (AIX) */
00386 #define GlobusLibcSockaddrSetLen(addr, len)                                 \
00387     do                                                                      \
00388     {                                                                       \
00389         struct sockaddr *               _addr = (struct sockaddr *) &(addr);\
00390                                                                             \
00391         switch(_addr->sa_family)                                            \
00392         {                                                                   \
00393           case AF_INET:                                                     \
00394             ((struct sockaddr_in *) _addr)->sin_len = (len);                \
00395             break;                                                          \
00396                                                                             \
00397           case AF_INET6:                                                    \
00398             ((struct sockaddr_in6 *) _addr)->sin6_len = (len);              \
00399             break;                                                          \
00400                                                                             \
00401           default:                                                          \
00402             globus_assert(0 &&                                              \
00403                 "Unknown family in GlobusLibcSockaddrSetLen");              \
00404             break;                                                          \
00405         }                                                                   \
00406     } while(0)
00407 
00408 #define GlobusLibcSockaddrCopy(dest_addr, source_addr, source_len)          \
00409     (memcpy(&(dest_addr), &(source_addr), (source_len)))
00410 
00411 #define GlobusLibcSockaddrLen(addr)                                         \
00412     (((struct sockaddr *) (addr))->sa_family == AF_INET                     \
00413         ? sizeof(struct sockaddr_in) :                                      \
00414             (((struct sockaddr *) (addr))->sa_family == AF_INET6            \
00415         ? sizeof(struct sockaddr_in6) : -1))
00416 
00417 #define GLOBUS_AI_PASSIVE               AI_PASSIVE
00418 #define GLOBUS_AI_NUMERICHOST           AI_NUMERICHOST
00419 #define GLOBUS_AI_CANONNAME             AI_CANONNAME
00420 
00421 #define GLOBUS_NI_MAXHOST               NI_MAXHOST
00422 #define GLOBUS_NI_NOFQDN                NI_NOFQDN
00423 #define GLOBUS_NI_NAMEREQD              NI_NAMEREQD
00424 #define GLOBUS_NI_DGRAM                 NI_DGRAM
00425 #define GLOBUS_NI_NUMERICSERV           NI_NUMERICSERV
00426 #define GLOBUS_NI_NUMERICHOST           NI_NUMERICHOST
00427 
00428 #define GLOBUS_EAI_ERROR_OFFSET         2048
00429 
00430 #define GLOBUS_EAI_FAMILY            (EAI_FAMILY     + GLOBUS_EAI_ERROR_OFFSET)
00431 #define GLOBUS_EAI_SOCKTYPE          (EAI_SOCKTYPE   + GLOBUS_EAI_ERROR_OFFSET)
00432 #define GLOBUS_EAI_BADFLAGS          (EAI_BADFLAGS   + GLOBUS_EAI_ERROR_OFFSET)
00433 #define GLOBUS_EAI_NONAME            (EAI_NONAME     + GLOBUS_EAI_ERROR_OFFSET)
00434 #define GLOBUS_EAI_SERVICE           (EAI_SERVICE    + GLOBUS_EAI_ERROR_OFFSET)
00435 #define GLOBUS_EAI_ADDRFAMILY        (EAI_ADDRFAMILY + GLOBUS_EAI_ERROR_OFFSET)
00436 #define GLOBUS_EAI_NODATA            (EAI_NODATA     + GLOBUS_EAI_ERROR_OFFSET)
00437 #define GLOBUS_EAI_MEMORY            (EAI_MEMORY     + GLOBUS_EAI_ERROR_OFFSET)
00438 #define GLOBUS_EAI_FAIL              (EAI_FAIL       + GLOBUS_EAI_ERROR_OFFSET)
00439 #define GLOBUS_EAI_AGAIN             (EAI_AGAIN      + GLOBUS_EAI_ERROR_OFFSET)
00440 #define GLOBUS_EAI_SYSTEM            (EAI_SYSTEM     + GLOBUS_EAI_ERROR_OFFSET)
00441        
00442 globus_result_t
00443 globus_libc_getaddrinfo(
00444     const char *                        node,
00445     const char *                        service,
00446     const globus_addrinfo_t *           hints,
00447     globus_addrinfo_t **                res);
00448 
00449 void
00450 globus_libc_freeaddrinfo(
00451     globus_addrinfo_t *                 res);
00452 
00453 globus_result_t
00454 globus_libc_getnameinfo(
00455     const globus_sockaddr_t *           addr,
00456     char *                              hostbuf,
00457     globus_size_t                       hostbuf_len,
00458     char *                              servbuf,
00459     globus_size_t                       servbuf_len,
00460     int                                 flags);
00461 
00462 globus_bool_t
00463 globus_libc_addr_is_loopback(
00464     const globus_sockaddr_t *           addr);
00465 
00466 globus_bool_t
00467 globus_libc_addr_is_wildcard(
00468     const globus_sockaddr_t *           addr);
00469     
00470 /* use this to get a numeric contact string (ip addr.. default is hostname) */
00471 #define GLOBUS_LIBC_ADDR_NUMERIC        1
00472 /* use this if this is a local addr; will use GLOBUS_HOSTNAME if avail */
00473 #define GLOBUS_LIBC_ADDR_LOCAL          2
00474 /* force IPV6 host addresses */
00475 #define GLOBUS_LIBC_ADDR_IPV6           4
00476 /* force IPV4 host addresses */
00477 #define GLOBUS_LIBC_ADDR_IPV4           8
00478 
00479 /* creates a contact string of the form <host>:<port>
00480  * user needs to free contact string
00481  */
00482 globus_result_t
00483 globus_libc_addr_to_contact_string(
00484     const globus_sockaddr_t *           addr,
00485     int                                 opts_mask,
00486     char **                             contact_string);
00487 
00488 /* copy and convert an addr between ipv4 and v4mapped */
00489 globus_result_t
00490 globus_libc_addr_convert_family(
00491     const globus_sockaddr_t *           src,
00492     globus_sockaddr_t *                 dest,
00493     int                                 dest_family);
00494 
00495 globus_result_t
00496 globus_libc_contact_string_to_ints(
00497     const char *                        contact_string,
00498     int *                               host,
00499     int *                               count,
00500     unsigned short *                    port);
00501 
00502 /* create a contact string... if port == 0, it will be omitted
00503  * returned string must be freed
00504  * 
00505  * count should be 4 for ipv4 or 16 for ipv6
00506  */
00507 char *
00508 globus_libc_ints_to_contact_string(
00509     int *                               host,
00510     int                                 count,
00511     unsigned short                      port);
00512 
00513 int
00514 globus_libc_gethostaddr(
00515     globus_sockaddr_t *                 addr);
00516 
00517 int
00518 globus_libc_gethostaddr_by_family(
00519     globus_sockaddr_t *                 addr,
00520     int                                 family);
00521 
00522 #ifdef __cplusplus
00523 }
00524 #endif
00525 
00526 #endif /* GLOBUS_LIBC_H */

Generated on 20 Jun 2015 for globus_common by  doxygen 1.4.7