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