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