Kannel: Open Source WAP and SMS gateway  svn-r5335
protected.c File Reference
#include <locale.h>
#include <errno.h>
#include "gwlib.h"

Go to the source code of this file.

Macros

#define MEMALIGN(x)   ((x)+(8-(((unsigned long)(x))&0x7)))
 

Enumerations

enum  { RAND, GETHOSTBYNAME, GWTIME, NUM_LOCKS }
 

Functions

static void lock (int which)
 
static void unlock (int which)
 
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)
 
size_t gw_strftime (char *s, size_t max, const char *format, const struct tm *tm)
 
int gw_rand (void)
 
int gw_gethostbyname (struct hostent *ent, const char *name, char **buff)
 

Variables

static Mutex locks [NUM_LOCKS]
 

Macro Definition Documentation

◆ MEMALIGN

#define MEMALIGN (   x)    ((x)+(8-(((unsigned long)(x))&0x7)))

Referenced by gw_gethostbyname().

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
RAND 
GETHOSTBYNAME 
GWTIME 
NUM_LOCKS 

Definition at line 80 of file protected.c.

80  {
81  RAND,
83  GWTIME,
84  NUM_LOCKS
85 };
Definition: protected.c:81

Function Documentation

◆ gw_gethostbyname()

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

Definition at line 248 of file protected.c.

References GETHOSTBYNAME, gethostbyname, lock(), MEMALIGN, name, 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

◆ gw_gmtime()

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(), timestamp_to_minutes(), 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

◆ gw_localtime()

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(), msg_to_pdu(), status_cb(), and timestamp_to_minutes().

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

◆ gw_mktime()

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(), parse_http_date(), and timestamp_to_minutes().

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

◆ gw_rand()

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(), url_pattern(), 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

◆ gw_strftime()

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().

Referenced by msg_to_pdu().

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

◆ gwlib_protected_init()

void gwlib_protected_init ( void  )

Definition at line 103 of file protected.c.

References locks, 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

◆ gwlib_protected_shutdown()

void gwlib_protected_shutdown ( void  )

Definition at line 112 of file protected.c.

References locks, 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

◆ lock()

static void lock ( int  which)
static

Definition at line 91 of file protected.c.

References locks, and mutex_lock.

Referenced by gw_gethostbyname(), gw_gmtime(), gw_localtime(), gw_mktime(), gw_rand(), and gw_strftime().

92 {
93  mutex_lock(&locks[which]);
94 }
static Mutex locks[NUM_LOCKS]
Definition: protected.c:88
#define mutex_lock(m)
Definition: thread.h:130

◆ unlock()

static void unlock ( int  which)
static

Definition at line 97 of file protected.c.

References locks, and mutex_unlock.

Referenced by gw_gethostbyname(), gw_gmtime(), gw_localtime(), gw_mktime(), gw_rand(), and gw_strftime().

98 {
99  mutex_unlock(&locks[which]);
100 }
#define mutex_unlock(m)
Definition: thread.h:136
static Mutex locks[NUM_LOCKS]
Definition: protected.c:88

Variable Documentation

◆ locks

Mutex locks[NUM_LOCKS]
static

Definition at line 88 of file protected.c.

Referenced by gwlib_protected_init(), gwlib_protected_shutdown(), lock(), and unlock().

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.