Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
protected.h File Reference
#include <netdb.h>
#include <time.h>

Go to the source code of this file.

Macros

#define localtime(t)   do_not_use_localtime_directly
 
#define gmtime(t)   do_not_use_gmtime_directly
 
#define mktime(t)   do_not_use_mktime_directly
 
#define strftime(a, b, c, d)   do_not_use_strftime_directly
 
#define rand()   do_not_use_rand_directly
 
#define gethostbyname(a, b, c)   do_not_use_gethostbyname_directly
 
#define inet_ntoa(in)   use_gw_netaddr_to_octstr_instead_of_inet_ntoa
 

Functions

void gwlib_protected_init (void)
 
void gwlib_protected_shutdown (void)
 
struct tm gw_localtime (time_t t)
 
struct tm gw_gmtime (time_t t)
 
time_t gw_mktime (struct tm *tm)
 
int gw_rand (void)
 
int gw_gethostbyname (struct hostent *ret, const char *name, char **buff)
 
size_t gw_strftime (char *s, size_t max, const char *format, const struct tm *tm)
 

Macro Definition Documentation

#define gethostbyname (   a,
  b,
 
)    do_not_use_gethostbyname_directly

Definition at line 103 of file protected.h.

Referenced by gw_gethostbyname().

#define gmtime (   t)    do_not_use_gmtime_directly

Definition at line 91 of file protected.h.

Referenced by gw_gmtime().

#define inet_ntoa (   in)    use_gw_netaddr_to_octstr_instead_of_inet_ntoa

Definition at line 106 of file protected.h.

#define localtime (   t)    do_not_use_localtime_directly

Definition at line 88 of file protected.h.

Referenced by gw_localtime().

#define mktime (   t)    do_not_use_mktime_directly

Definition at line 94 of file protected.h.

Referenced by gw_mktime().

#define rand ( )    do_not_use_rand_directly

Definition at line 100 of file protected.h.

Referenced by get_random_bytes(), get_random_fd(), and gw_rand().

#define strftime (   a,
  b,
  c,
 
)    do_not_use_strftime_directly

Definition at line 97 of file protected.h.

Referenced by gw_strftime().

Function Documentation

int gw_gethostbyname ( struct hostent *  ret,
const char *  name,
char **  buff 
)

Definition at line 248 of file protected.c.

References GETHOSTBYNAME, gethostbyname, lock(), MEMALIGN, and unlock().

Referenced by make_server_socket(), setup_official_name(), tcpip_connect_nb_to_server_with_port(), tcpip_connect_to_server_with_port(), udp_bind(), and udp_create_address().

249 {
250  int len, i;
251  struct hostent *p;
252  /* Allocate enough memory to hold the full name information structs and
253  * everything. OSF1 is known to require at least 8872 bytes. The buffer
254  * required for storing all possible aliases and IP numbers is according to
255  * Stevens' Unix Network Programming 2nd editor, p. 304: 8192 bytes!
256  */
257  size_t bufflen = 9000;
258  char *bufptr, *str;
259 
261 
262  p = gethostbyname(name);
263  if (p == NULL) {
265  *buff = NULL;
266  return -1;
267  }
268 
269  *ent = *p;
270  /* alloc mem */
271  bufptr = *buff = gw_malloc(bufflen);
272  ent->h_name = bufptr;
273  /* copy h_name into buff */
274  len = strlen(p->h_name) + 1;
275  strncpy(bufptr, p->h_name, len);
276  bufptr += len;
277 
278  /* we align on even 64bit boundaries for safety */
279 #define MEMALIGN(x) ((x)+(8-(((unsigned long)(x))&0x7)))
280 
281  /* This must be aligned properly to work on many CPU architectures! */
282  bufptr = MEMALIGN(bufptr);
283 
284  ent->h_aliases = (char**)bufptr;
285 
286  /* Figure out how many aliases there are */
287  for (i = 0; p->h_aliases[i] != NULL; ++i)
288  ;
289 
290  /* Reserve room for the array */
291  bufptr += (i + 1) * sizeof(char*);
292 
293  /* Clone all known aliases */
294  for(i = 0; (str = p->h_aliases[i]); i++) {
295  len = strlen(str) + 1;
296  strncpy(bufptr, str, len);
297  ent->h_aliases[i] = bufptr;
298  bufptr += len;
299  }
300  /* Terminate the alias list with a NULL */
301  ent->h_aliases[i] = NULL;
302 
303  ent->h_addrtype = p->h_addrtype;
304  ent->h_length = p->h_length;
305 
306  /* align it for (at least) 32bit accesses */
307  bufptr = MEMALIGN(bufptr);
308 
309  ent->h_addr_list = (char**)bufptr;
310 
311  /* Figure out how many addresses there are */
312  for (i = 0; p->h_addr_list[i] != NULL; ++i)
313  ;
314 
315  /* Reserve room for the array */
316  bufptr += (i + 1) * sizeof(char*);
317 
318  i = 0;
319  len = p->h_length;
320  str = p->h_addr_list[i];
321  while (str != NULL) {
322  memcpy(bufptr, str, len);
323  ent->h_addr_list[i] = bufptr;
324  bufptr += len;
325  str = p->h_addr_list[++i];
326  }
327  ent->h_addr_list[i] = NULL;
328 
329 #undef MEMALIGN
330 
332 
333  return 0;
334 }
#define MEMALIGN(x)
static void lock(int which)
Definition: protected.c:91
static void unlock(int which)
Definition: protected.c:97
char * name
Definition: smsc_cimd2.c:212
#define gethostbyname(a, b, c)
Definition: protected.h:103
struct tm gw_gmtime ( time_t  t)

Definition at line 137 of file protected.c.

References gmtime, GWTIME, lock(), and unlock().

Referenced by date_create_iso(), date_format_http(), delivery_time_constraints(), format(), get_pattern(), make_timestamp(), msg_to_pdu(), obey_request(), set_time(), status_cb(), and urltrans_fill_escape_codes().

138 {
139  struct tm tm;
140 
141 #ifndef HAVE_GMTIME_R
142  lock(GWTIME);
143  tm = *gmtime(&t);
144  unlock(GWTIME);
145 #else
146  gmtime_r(&t, &tm);
147 #endif
148 
149  return tm;
150 }
#define gmtime(t)
Definition: protected.h:91
static void lock(int which)
Definition: protected.c:91
static void unlock(int which)
Definition: protected.c:97
struct tm gw_localtime ( time_t  t)

Definition at line 121 of file protected.c.

References GWTIME, localtime, lock(), and unlock().

Referenced by format(), msg_to_emimsg(), and status_cb().

122 {
123  struct tm tm;
124 
125 #ifndef HAVE_LOCALTIME_R
126  lock(GWTIME);
127  tm = *localtime(&t);
128  unlock(GWTIME);
129 #else
130  localtime_r(&t, &tm);
131 #endif
132 
133  return tm;
134 }
#define localtime(t)
Definition: protected.h:88
static void lock(int which)
Definition: protected.c:91
static void unlock(int which)
Definition: protected.c:97
time_t gw_mktime ( struct tm *  tm)

Definition at line 153 of file protected.c.

References GWTIME, lock(), mktime, and unlock().

Referenced by clickatell_receive_sms(), and parse_http_date().

154 {
155  time_t t;
156  lock(GWTIME);
157  t = mktime(tm);
158  unlock(GWTIME);
159 
160  return t;
161 }
static void lock(int which)
Definition: protected.c:91
static void unlock(int which)
Definition: protected.c:97
#define mktime(t)
Definition: protected.h:94
int gw_rand ( void  )

Definition at line 174 of file protected.c.

References lock(), RAND, rand, and unlock().

Referenced by choose_message(), gw_generate_id(), randomize(), route_incoming_to_boxc(), route_msg(), set_zero(), smsc2_rout(), soap_rand_attribute(), and wap_msg_recv().

175 {
176  int ret;
177 
178  lock(RAND);
179  ret = rand();
180  unlock(RAND);
181  return ret;
182 }
static void lock(int which)
Definition: protected.c:91
static void unlock(int which)
Definition: protected.c:97
#define rand()
Definition: protected.h:100
Definition: protected.c:81
size_t gw_strftime ( char *  s,
size_t  max,
const char *  format,
const struct tm *  tm 
)

Definition at line 164 of file protected.c.

References GWTIME, lock(), strftime, and unlock().

165 {
166  size_t ret;
167  lock(GWTIME);
168  ret = strftime(s, max, format, tm);
169  unlock(GWTIME);
170  return ret;
171 }
static void lock(int which)
Definition: protected.c:91
static void unlock(int which)
Definition: protected.c:97
#define strftime(a, b, c, d)
Definition: protected.h:97
void gwlib_protected_init ( void  )

Definition at line 103 of file protected.c.

References mutex_init_static, and NUM_LOCKS.

Referenced by gwlib_init().

104 {
105  int i;
106 
107  for (i = 0; i < NUM_LOCKS; ++i)
109 }
static Mutex locks[NUM_LOCKS]
Definition: protected.c:88
#define mutex_init_static(mutex)
Definition: thread.h:115
void gwlib_protected_shutdown ( void  )

Definition at line 112 of file protected.c.

References mutex_destroy(), and NUM_LOCKS.

Referenced by gwlib_shutdown().

113 {
114  int i;
115 
116  for (i = 0; i < NUM_LOCKS; ++i)
117  mutex_destroy(&locks[i]);
118 }
static Mutex locks[NUM_LOCKS]
Definition: protected.c:88
void mutex_destroy(Mutex *mutex)
Definition: thread.c:97
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.