better split up of library files "types.h" & "definitions.h"
[strongswan.git] / src / libstrongswan / library.c
1 /**
2 * @file library.c
3 *
4 * @brief Helper functions and definitions.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include <string.h>
25 #include <time.h>
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <pthread.h>
29
30 #include "library.h"
31
32 #include <printf_hook.h>
33
34 ENUM(status_names, SUCCESS, DESTROY_ME,
35 "SUCCESS",
36 "FAILED",
37 "OUT_OF_RES",
38 "ALREADY_DONE",
39 "NOT_SUPPORTED",
40 "INVALID_ARG",
41 "NOT_FOUND",
42 "PARSE_ERROR",
43 "VERIFY_ERROR",
44 "INVALID_STATE",
45 "DESTROY_ME",
46 );
47
48 /**
49 * Described in header.
50 */
51 void *clalloc(void * pointer, size_t size)
52 {
53 void *data;
54 data = malloc(size);
55
56 memcpy(data, pointer,size);
57
58 return (data);
59 }
60
61 /**
62 * We use a single mutex for all refcount variables. This
63 * is not optimal for performance, but the critical section
64 * is not that long...
65 * TODO: Consider to include a mutex in each refcount_t variable.
66 */
67 static pthread_mutex_t ref_mutex = PTHREAD_MUTEX_INITIALIZER;
68
69 /**
70 * Described in header.
71 *
72 * TODO: May be implemented with atomic CPU instructions
73 * instead of a mutex.
74 */
75 void ref_get(refcount_t *ref)
76 {
77 pthread_mutex_lock(&ref_mutex);
78 (*ref)++;
79 pthread_mutex_unlock(&ref_mutex);
80 }
81
82 /**
83 * Described in header.
84 *
85 * TODO: May be implemented with atomic CPU instructions
86 * instead of a mutex.
87 */
88 bool ref_put(refcount_t *ref)
89 {
90 bool more_refs;
91
92 pthread_mutex_lock(&ref_mutex);
93 more_refs = --(*ref);
94 pthread_mutex_unlock(&ref_mutex);
95 return !more_refs;
96 }
97
98 /**
99 * output handler in printf() for time_t
100 */
101 static int print_time(FILE *stream, const struct printf_info *info,
102 const void *const *args)
103 {
104 static const char* months[] = {
105 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
106 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
107 };
108 time_t time = *((time_t*)(args[0]));
109 bool utc = TRUE;
110 struct tm t;
111
112 if (info->alt)
113 {
114 utc = *((bool*)(args[1]));
115 }
116 if (time == UNDEFINED_TIME)
117 {
118 return fprintf(stream, "--- -- --:--:--%s----",
119 info->alt ? " UTC " : " ");
120 }
121 if (utc)
122 {
123 gmtime_r(&time, &t);
124 }
125 else
126 {
127 localtime_r(&time, &t);
128 }
129 return fprintf(stream, "%s %02d %02d:%02d:%02d%s%04d",
130 months[t.tm_mon], t.tm_mday, t.tm_hour, t.tm_min,
131 t.tm_sec, utc ? " UTC " : " ", t.tm_year + 1900);
132 }
133
134 /**
135 * output handler in printf() for time deltas
136 */
137 static int print_time_delta(FILE *stream, const struct printf_info *info,
138 const void *const *args)
139 {
140 time_t start = *((time_t*)(args[0]));
141 time_t end = *((time_t*)(args[1]));
142 u_int delta = abs(end - start);
143 char* unit = "second";
144
145 if (delta > 2 * 60 * 60 * 24)
146 {
147 delta /= 60 * 60 * 24;
148 unit = "days";
149 }
150 else if (delta > 2 * 60 * 60)
151 {
152 delta /= 60 * 60;
153 unit = "hours";
154 }
155 else if (delta > 2 * 60)
156 {
157 delta /= 60;
158 unit = "minutes";
159 }
160 return fprintf(stream, "%d %s", delta, unit);
161 }
162
163 /**
164 * register printf() handlers for time_t
165 */
166 static void __attribute__ ((constructor))print_register()
167 {
168 register_printf_function(PRINTF_TIME, print_time, arginfo_int_alt_int_int);
169 register_printf_function(PRINTF_TIME_DELTA, print_time_delta, arginfo_int_int);
170 }