globus_common  17.10
globus_libc.h
Go to the documentation of this file.
1 
7 #ifndef GLOBUS_LIBC_H
8 #define GLOBUS_LIBC_H 1
9 
10 #include "globus_common_include.h"
11 #include "globus_thread.h"
12 
13 #if defined(WIN32) && !defined(__CYGWIN__)
14 /* For addrinfo struct */
15 #include <winsock2.h>
16 #include <ws2tcpip.h>
17 #define EAI_SYSTEM 11
18 /* snprintf is already defined to mingw version */
19 #if !defined(__USE_MINGW_ANSI_STDIO)
20 #define snprintf _snprintf
21 #endif
22 #endif
23 
24 #if __GNUC__
25 # define GLOBUS_DEPRECATED(func) func __attribute__((deprecated))
26 #elif defined(_MSC_VER)
27 # define GLOBUS_DEPRECATED(func) __declspec(deprecated) func
28 #else
29 # define GLOBUS_DEPRECATED(func) func
30 #endif
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 extern globus_mutex_t globus_libc_mutex;
37 
38 #define globus_macro_libc_lock() \
39  globus_mutex_lock(&globus_libc_mutex)
40 #define globus_macro_libc_unlock() \
41  globus_mutex_unlock(&globus_libc_mutex)
42 
43 #ifdef USE_MACROS
44 #define globus_libc_lock() globus_macro_libc_lock()
45 #define globus_libc_unlock() globus_macro_libc_unlock()
46 #else /* USE_MACROS */
47 extern int globus_libc_lock(void);
48 extern int globus_libc_unlock(void);
49 #endif /* USE_MACROS */
50 
51 #if defined(va_copy)
52 # define globus_libc_va_copy(dest,src) \
53  va_copy(dest,src)
54 #elif defined(__va_copy)
55 # define globus_libc_va_copy(dest,src) \
56  __va_copy(dest,src)
57 #else
58 # define globus_libc_va_copy(dest,src) \
59  memcpy(&dest, &src, sizeof(va_list))
60 #endif
61 
62 
63 #define globus_stdio_lock globus_libc_lock
64 #define globus_stdio_unlock globus_libc_unlock
65 #define globus_libc_printf printf
66 #define globus_libc_fprintf fprintf
67 #define globus_libc_sprintf sprintf
68 #define globus_libc_vprintf vprintf
69 #define globus_libc_vfprintf vfprintf
70 #define globus_libc_vsprintf vsprintf
71 
72 #if __STDC_VERSION__ >= 199901L
73 #define globus_libc_snprintf snprintf
74 #define globus_libc_vsnprintf vsnprintf
75 #else
76 extern int globus_libc_snprintf(char *s, size_t n, const char *format, ...);
77 extern int globus_libc_vsnprintf(char *s, size_t n, const char *format,
78  va_list ap);
79 #endif
80 
81 /*
82  * File I/O routines
83  */
84 #if !defined(_WIN32)
85 #define globus_libc_open open
86 #define globus_libc_close close
87 #define globus_libc_read read
88 #define globus_libc_write write
89 #define globus_libc_umask umask
90 #define globus_libc_writev writev
91 #define globus_libc_fstat fstat
92 
93 #define globus_libc_opendir opendir
94 #define globus_libc_telldir telldir
95 #define globus_libc_seekdir seekdir
96 #define globus_libc_rewinddir rewinddir
97 #define globus_libc_closedir closedir
98 #define globus_libc_getpwuid_r getpwuid_r
99 
100 #else /* _WIN32 */
101 
102 extern
103 mode_t
104 globus_libc_umask_win32(
105  mode_t mask);
106 
107 # define globus_libc_open _open
108 # define globus_libc_close _close
109 # define globus_libc_read _read
110 # define globus_libc_write _write
111 # define globus_libc_umask globus_libc_umask_win32
112 # define globus_libc_writev(fd,iov,iovcnt) \
113  write(fd,iov[0].iov_base,iov[0].iov_len)
114 # define uid_t int
115 #if defined(TARGET_ARCH_CYGWIN) || defined(TARGET_ARCH_MINGW32)
116 #define globus_libc_opendir opendir
117 #define globus_libc_telldir telldir
118 #define globus_libc_seekdir seekdir
119 #define globus_libc_rewinddir rewinddir
120 #define globus_libc_closedir closedir
121 #endif
122 #endif /* _WIN32 */
123 extern
124 int
125 globus_libc_readdir_r(
126  DIR * dirp,
127  struct dirent ** result);
128 
129 /*
130  * Memory allocation routines
131  */
132 #define globus_malloc(bytes) globus_libc_malloc(bytes)
133 #define globus_realloc(ptr,bytes) globus_libc_realloc(ptr,bytes)
134 #define globus_calloc(nobjs,bytes) globus_libc_calloc(nobjs,bytes)
135 #define globus_free(ptr) globus_libc_free(ptr)
136 
137 #define globus_libc_malloc malloc
138 #define globus_libc_realloc realloc
139 #define globus_libc_calloc calloc
140 #define globus_libc_free free
141 #define globus_libc_alloca alloca
142 #define globus_libc_lseek lseek
143 
144 /* Miscellaneous libc functions (formerly md_unix.c) */
145 int globus_libc_gethostname(char *name, int len);
146 int globus_libc_getpid(void);
147 int globus_libc_fork(void);
148 int globus_libc_usleep(long usec);
149 double globus_libc_wallclock(void);
150 
151 /* returns # of characters printed to s */
152 extern int globus_libc_sprint_off_t(char * s, globus_off_t off);
153 /* returns 1 if scanned succeeded */
154 extern int globus_libc_scan_off_t(char *s, globus_off_t *off, int *consumed);
155 
156 /* Use getaddrinfo instead */
157 GLOBUS_DEPRECATED(struct hostent *globus_libc_gethostbyname_r(char *name,
158  struct hostent *result,
159  char *buffer,
160  int buflen,
161  int *h_errnop));
162 /* Use getnameinfo instead */
163 GLOBUS_DEPRECATED(struct hostent *globus_libc_gethostbyaddr_r(char *addr,
164  int length,
165  int type,
166  struct hostent *result,
167  char *buffer,
168  int buflen,
169  int *h_errnop));
170 
171 #ifdef _POSIX_THREAD_SAFE_FUCTIONS
172 #define globus_libc_ctime_r(clock, buf, buflen) ctime_r(clock, buf)
173 #define globus_libc_localtime_r(timer, result) localtime_r(timer, result)
174 #define globus_libc_gmtime_r(timer, result) gmtime_r(timer, result)
175 #else
176 char *globus_libc_ctime_r(/*should be const */time_t *clock, char *buf, int buflen);
177 struct tm * globus_libc_localtime_r(const time_t *timep, struct tm *result);
178 struct tm * globus_libc_gmtime_r(const time_t *timep, struct tm *result);
179 #endif
180 
181 #if !defined(_WIN32)
182 #define globus_libc_getpwnam_r getpwnam_r
183 #endif
184 
185 int
186 globus_libc_strncasecmp(
187  const char * s1,
188  const char * s2,
189  globus_size_t n);
190 
191 int globus_libc_setenv(const char *name,
192  const char *value,
193  int rewrite);
194 void globus_libc_unsetenv(const char *name);
195 
196 /* Use getenv instead */
197 GLOBUS_DEPRECATED(char *globus_libc_getenv(const char *name));
198 
199 /* Use strerror or strerror_r as needed instead */
200 char *globus_libc_system_error_string(int the_error);
201 
202 char *
203 globus_libc_strdup(const char * source);
204 
205 char *
206 globus_libc_strndup(const char * string, globus_size_t length);
207 
208 char *
209 globus_libc_strtok(
210  char * s,
211  const char * delim);
212 
213 #define globus_libc_strcmp strcmp
214 #define globus_libc_strlen strlen
215 
216 char *
218  const char ** array,
219  int count);
220 
221 int
222 globus_libc_vprintf_length(const char * fmt, va_list ap);
223 
224 int
225 globus_libc_printf_length(const char * fmt, ...);
226 
227 /* not really 'libc'... but a convenient place to put it in */
228 int globus_libc_gethomedir(char *result, int bufsize);
229 
230 char *
231 globus_common_create_string(
232  const char * format,
233  ...);
234 
235 char *
236 globus_common_create_nstring(
237  int length,
238  const char * format,
239  ...);
240 
241 char *
242 globus_common_v_create_string(
243  const char * format,
244  va_list ap);
245 
246 char *
247 globus_common_v_create_nstring(
248  int length,
249  const char * format,
250  va_list ap);
251 
252 /* for backwards compatibility */
253 #define globus_libc_memmove(d, s, n) memmove((d), (s), (n))
254 
255 #ifdef __hpux
256 # define globus_libc_setegid(a) setresgid(-1,a,-1)
257 # define globus_libc_seteuid(a) setresuid(-1,a,-1)
258 #else
259 # define globus_libc_setegid(a) setegid(a)
260 # define globus_libc_seteuid(a) seteuid(a)
261 #endif
262 
263 
264 /* IPv6 compatible utils */
265 typedef struct sockaddr_storage globus_sockaddr_t;
266 typedef struct addrinfo globus_addrinfo_t;
267 
268 #ifdef AF_INET6
269 #define GlobusLibcProtocolFamilyIsIP(family) \
270  ((family == AF_INET ? 1 : (family == AF_INET6 ? 1 : 0)))
271 #else
272 #define GlobusLibcProtocolFamilyIsIP(family) \
273  (family == AF_INET ? 1 : 0)
274 #endif
275 
276 #ifndef PF_INET
277 #define PF_INET AF_INET
278 #endif
279 
280 #ifndef PF_UNSPEC
281 #define PF_UNSPEC AF_UNSPEC
282 #endif
283 
284 #define GlobusLibcSockaddrSetFamily(_addr, fam) ((struct sockaddr *) &(_addr))->sa_family = fam
285 #define GlobusLibcSockaddrGetFamily(_addr) ((struct sockaddr *) &(_addr))->sa_family
286 
287 #ifdef AF_INET6
288 #define GlobusLibcSockaddrGetPort(addr, port) \
289  do \
290  { \
291  const struct sockaddr * _addr = (struct sockaddr *) &(addr);\
292  \
293  switch(_addr->sa_family) \
294  { \
295  case AF_INET: \
296  (port) = ntohs(((struct sockaddr_in *) _addr)->sin_port); \
297  break; \
298  \
299  case AF_INET6: \
300  (port) = ntohs(((struct sockaddr_in6 *) _addr)->sin6_port); \
301  break; \
302  \
303  default: \
304  globus_assert(0 && \
305  "Unknown family in GlobusLibcSockaddrGetPort"); \
306  (port) = -1; \
307  break; \
308  } \
309  } while(0)
310 #else
311 #define GlobusLibcSockaddrGetPort(addr, port) \
312  do \
313  { \
314  const struct sockaddr * _addr = (struct sockaddr *) &(addr);\
315  \
316  switch(_addr->sa_family) \
317  { \
318  case AF_INET: \
319  (port) = ntohs(((struct sockaddr_in *) _addr)->sin_port); \
320  break; \
321  \
322  default: \
323  globus_assert(0 && \
324  "Unknown family in GlobusLibcSockaddrGetPort"); \
325  (port) = -1; \
326  break; \
327  } \
328  } while(0)
329 #endif
330 
331 #ifdef AF_INET6
332 #define GlobusLibcSockaddrSetPort(addr, port) \
333  do \
334  { \
335  struct sockaddr * _addr = (struct sockaddr *) &(addr);\
336  \
337  switch(_addr->sa_family) \
338  { \
339  case AF_INET: \
340  ((struct sockaddr_in *) _addr)->sin_port = htons((port)); \
341  break; \
342  \
343  case AF_INET6: \
344  ((struct sockaddr_in6 *) _addr)->sin6_port = htons((port)); \
345  break; \
346  \
347  default: \
348  globus_assert(0 && \
349  "Unknown family in GlobusLibcSockaddrSetPort"); \
350  break; \
351  } \
352  } while(0)
353 #else
354 #define GlobusLibcSockaddrSetPort(addr, port) \
355  do \
356  { \
357  struct sockaddr * _addr = (struct sockaddr *) &(addr);\
358  \
359  switch(_addr->sa_family) \
360  { \
361  case AF_INET: \
362  ((struct sockaddr_in *) _addr)->sin_port = htons((port)); \
363  break; \
364  \
365  default: \
366  globus_assert(0 && \
367  "Unknown family in GlobusLibcSockaddrSetPort"); \
368  break; \
369  } \
370  } while(0)
371 #endif
372 
373 /* only use this on systems with the sin_len field (AIX) */
374 #define GlobusLibcSockaddrSetLen(addr, len) \
375  do \
376  { \
377  struct sockaddr * _addr = (struct sockaddr *) &(addr);\
378  \
379  switch(_addr->sa_family) \
380  { \
381  case AF_INET: \
382  ((struct sockaddr_in *) _addr)->sin_len = (len); \
383  break; \
384  \
385  case AF_INET6: \
386  ((struct sockaddr_in6 *) _addr)->sin6_len = (len); \
387  break; \
388  \
389  default: \
390  globus_assert(0 && \
391  "Unknown family in GlobusLibcSockaddrSetLen"); \
392  break; \
393  } \
394  } while(0)
395 
396 #define GlobusLibcSockaddrCopy(dest_addr, source_addr, source_len) \
397  (memcpy(&(dest_addr), &(source_addr), (source_len)))
398 
399 #define GlobusLibcSockaddrLen(addr) \
400  (((struct sockaddr *) (addr))->sa_family == AF_INET \
401  ? sizeof(struct sockaddr_in) : \
402  (((struct sockaddr *) (addr))->sa_family == AF_INET6 \
403  ? sizeof(struct sockaddr_in6) : -1))
404 
405 #define GLOBUS_AI_PASSIVE AI_PASSIVE
406 #define GLOBUS_AI_NUMERICHOST AI_NUMERICHOST
407 #define GLOBUS_AI_CANONNAME AI_CANONNAME
408 
409 #define GLOBUS_NI_MAXHOST NI_MAXHOST
410 #define GLOBUS_NI_NOFQDN NI_NOFQDN
411 #define GLOBUS_NI_NAMEREQD NI_NAMEREQD
412 #define GLOBUS_NI_DGRAM NI_DGRAM
413 #define GLOBUS_NI_NUMERICSERV NI_NUMERICSERV
414 #define GLOBUS_NI_NUMERICHOST NI_NUMERICHOST
415 
416 #define GLOBUS_EAI_ERROR_OFFSET 2048
417 
418 #define GLOBUS_EAI_FAMILY (EAI_FAMILY + GLOBUS_EAI_ERROR_OFFSET)
419 #define GLOBUS_EAI_SOCKTYPE (EAI_SOCKTYPE + GLOBUS_EAI_ERROR_OFFSET)
420 #define GLOBUS_EAI_BADFLAGS (EAI_BADFLAGS + GLOBUS_EAI_ERROR_OFFSET)
421 #define GLOBUS_EAI_NONAME (EAI_NONAME + GLOBUS_EAI_ERROR_OFFSET)
422 #define GLOBUS_EAI_SERVICE (EAI_SERVICE + GLOBUS_EAI_ERROR_OFFSET)
423 #define GLOBUS_EAI_ADDRFAMILY (EAI_ADDRFAMILY + GLOBUS_EAI_ERROR_OFFSET)
424 #define GLOBUS_EAI_NODATA (EAI_NODATA + GLOBUS_EAI_ERROR_OFFSET)
425 #define GLOBUS_EAI_MEMORY (EAI_MEMORY + GLOBUS_EAI_ERROR_OFFSET)
426 #define GLOBUS_EAI_FAIL (EAI_FAIL + GLOBUS_EAI_ERROR_OFFSET)
427 #define GLOBUS_EAI_AGAIN (EAI_AGAIN + GLOBUS_EAI_ERROR_OFFSET)
428 #define GLOBUS_EAI_SYSTEM (EAI_SYSTEM + GLOBUS_EAI_ERROR_OFFSET)
429 
431 globus_libc_getaddrinfo(
432  const char * node,
433  const char * service,
434  const globus_addrinfo_t * hints,
435  globus_addrinfo_t ** res);
436 
437 void
438 globus_libc_freeaddrinfo(
439  globus_addrinfo_t * res);
440 
442 globus_libc_getnameinfo(
443  const globus_sockaddr_t * addr,
444  char * hostbuf,
445  globus_size_t hostbuf_len,
446  char * servbuf,
447  globus_size_t servbuf_len,
448  int flags);
449 
451 globus_libc_addr_is_loopback(
452  const globus_sockaddr_t * addr);
453 
455 globus_libc_addr_is_wildcard(
456  const globus_sockaddr_t * addr);
457 
458 /* use this to get a numeric contact string (ip addr.. default is hostname) */
459 #define GLOBUS_LIBC_ADDR_NUMERIC 1
460 /* use this if this is a local addr; will use GLOBUS_HOSTNAME if avail */
461 #define GLOBUS_LIBC_ADDR_LOCAL 2
462 /* force IPV6 host addresses */
463 #define GLOBUS_LIBC_ADDR_IPV6 4
464 /* force IPV4 host addresses */
465 #define GLOBUS_LIBC_ADDR_IPV4 8
466 
467 /* creates a contact string of the form <host>:<port>
468  * user needs to free contact string
469  */
471 globus_libc_addr_to_contact_string(
472  const globus_sockaddr_t * addr,
473  int opts_mask,
474  char ** contact_string);
475 
476 /* copy and convert an addr between ipv4 and v4mapped */
478 globus_libc_addr_convert_family(
479  const globus_sockaddr_t * src,
480  globus_sockaddr_t * dest,
481  int dest_family);
482 
485  const char * contact_string,
486  int * host,
487  int * count,
488  unsigned short * port);
489 
490 /* create a contact string... if port == 0, it will be omitted
491  * returned string must be freed
492  *
493  * count should be 4 for ipv4 or 16 for ipv6
494  */
495 char *
496 globus_libc_ints_to_contact_string(
497  int * host,
498  int count,
499  unsigned short port);
500 
501 int
502 globus_libc_gethostaddr(
503  globus_sockaddr_t * addr);
504 
505 int
506 globus_libc_gethostaddr_by_family(
507  globus_sockaddr_t * addr,
508  int family);
509 
510 #ifdef __cplusplus
511 }
512 #endif
513 
514 #endif /* GLOBUS_LIBC_H */
Include System Headers.
globus_result_t globus_libc_contact_string_to_ints(const char *contact_string, int *host, int *count, unsigned short *port)
Definition: globus_libc.c:2856
char * globus_libc_join(const char **array, int count)
Definition: globus_libc.c:3118
Globus Threading Abstraction.
uint32_t globus_result_t
Definition: globus_types.h:84
int globus_bool_t
Boolean type.
Definition: globus_types.h:78
size_t globus_size_t
Standard size of memory objectThe globus_size_t is the size of a memory object. It is identical to si...
Definition: globus_types.h:33
Mutex.
Definition: globus_thread.h:92