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

Generated on 17 Mar 2017 for globus_common by  doxygen 1.4.7