libc: make stdio_impl.h an internal libc header
[unleashed/tickless.git] / usr / src / lib / libsip / common / sip_gids.c
blob97f4b10f38ad4955c0bd7b42c488eb42df0145d9
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <pthread.h>
34 #include <unistd.h>
35 #include <sip.h>
36 #ifdef __linux__
37 #include <sasl/sasl.h>
38 #include <sasl/saslplug.h>
39 #else
40 #include <sys/md5.h>
41 #endif
43 #include "sip_miscdefs.h"
44 #include "sip_msg.h"
46 void sip_md5_hash(char *, int, char *, int, char *, int, char *, int,
47 char *, int, char *, int, uchar_t *);
49 #define SIP_RANDOM_LEN 20
52 * Wrapper around /dev/urandom
54 static int
55 sip_get_random(char *buf, int buflen)
57 static int devrandom = -1;
59 if (devrandom == -1 &&
60 (devrandom = open("/dev/urandom", O_RDONLY)) == -1) {
61 return (-1);
64 if (read(devrandom, buf, buflen) == -1)
65 return (-1);
66 return (0);
70 * Get MD5 hash of call_id, from_tag, to_tag using key
72 void
73 sip_md5_hash(char *str1, int lstr1, char *str2, int lstr2, char *str3,
74 int lstr3, char *str4, int lstr4, char *str5, int lstr5,
75 char *str6, int lstr6, uchar_t *digest)
77 MD5_CTX ctx;
79 #ifdef __linux__
80 _sasl_MD5Init(&ctx);
82 _sasl_MD5Update(&ctx, (uchar_t *)&sip_hash_salt, sizeof (uint64_t));
84 if (str1 != NULL)
85 _sasl_MD5Update(&ctx, (uchar_t *)str1, lstr1);
87 if (str2 != NULL)
88 _sasl_MD5Update(&ctx, (uchar_t *)str2, lstr2);
90 if (str3 != NULL)
91 _sasl_MD5Update(&ctx, (uchar_t *)str3, lstr3);
93 if (str4 != NULL)
94 _sasl_MD5Update(&ctx, (uchar_t *)str4, lstr4);
96 if (str5 != NULL)
97 _sasl_MD5Update(&ctx, (uchar_t *)str5, lstr5);
99 if (str6 != NULL)
100 _sasl_MD5Update(&ctx, (uchar_t *)str6, lstr6);
102 _sasl_MD5Final(digest, &ctx);
103 #else /* solaris */
104 MD5Init(&ctx);
106 MD5Update(&ctx, (uchar_t *)&sip_hash_salt, sizeof (uint64_t));
108 if (str1 != NULL)
109 MD5Update(&ctx, (uchar_t *)str1, lstr1);
111 if (str2 != NULL)
112 MD5Update(&ctx, (uchar_t *)str2, lstr2);
114 if (str3 != NULL)
115 MD5Update(&ctx, (uchar_t *)str3, lstr3);
117 if (str4 != NULL)
118 MD5Update(&ctx, (uchar_t *)str4, lstr4);
120 if (str5 != NULL)
121 MD5Update(&ctx, (uchar_t *)str5, lstr5);
123 if (str6 != NULL)
124 MD5Update(&ctx, (uchar_t *)str6, lstr6);
126 MD5Final(digest, &ctx);
127 #endif
131 * generate a guid (globally unique id)
133 char *
134 sip_guid()
136 int i;
137 uint8_t *r;
138 uint32_t random;
139 uint32_t time;
140 char *guid;
141 int guidlen;
142 #ifdef __linux__
143 struct timespec tspec;
144 #endif
146 guid = (char *)malloc(SIP_RANDOM_LEN + 1);
147 if (guid == NULL)
148 return (NULL);
150 * Get a 32-bit random #
152 if (sip_get_random((char *)&random, sizeof (random)) != 0)
153 return (NULL);
154 #ifdef __linux__
155 if (clock_gettime(CLOCK_REALTIME, &tspec) != 0)
156 return (NULL);
157 time = (uint32_t)tspec.tv_nsec;
158 #else
160 * Get 32-bits from gethrtime()
162 time = (uint32_t)gethrtime();
163 #endif
164 (void) snprintf(guid, SIP_RANDOM_LEN + 1, "%u%u", random, time);
165 guidlen = strlen(guid);
168 * just throw in some alphabets too
170 r = (uint8_t *)malloc(guidlen);
171 if (sip_get_random((char *)r, guidlen) != 0) {
172 free(guid);
173 return (NULL);
175 for (i = 0; i < guidlen; i++) {
176 if ((r[i] >= 65 && r[i] <= 90) ||
177 (r[i] >= 97 && r[i] <= 122)) {
178 guid[i] = r[i];
181 free(r);
182 return (guid);
186 * Generate branchid for a transaction
188 char *
189 sip_branchid(sip_msg_t sip_msg)
191 char *guid;
192 char *branchid;
193 _sip_header_t *via;
194 unsigned char md5_hash[16];
195 _sip_header_t *to;
196 _sip_header_t *from;
197 _sip_header_t *callid;
198 _sip_msg_t *_sip_msg;
199 int cseq;
200 MD5_CTX ctx;
201 size_t len;
202 int hdrlen;
203 int i;
205 if (sip_msg == NULL) {
206 generate_bid:
207 if ((branchid = (char *)malloc(SIP_BRANCHID_LEN + 1)) == NULL)
208 return (NULL);
209 guid = sip_guid();
210 if (guid == NULL) {
211 free(branchid);
212 return (NULL);
214 (void) snprintf(branchid, SIP_BRANCHID_LEN + 1, "z9hG4bK%s",
215 guid);
216 free(guid);
217 return (branchid);
219 _sip_msg = (_sip_msg_t *)sip_msg;
220 (void) pthread_mutex_lock(&_sip_msg->sip_msg_mutex);
221 via = sip_search_for_header(_sip_msg, SIP_VIA, NULL);
222 if (via == NULL) {
223 (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex);
224 goto generate_bid;
226 to = sip_search_for_header(_sip_msg, SIP_TO, NULL);
227 from = sip_search_for_header(_sip_msg, SIP_FROM, NULL);
228 callid = sip_search_for_header(_sip_msg, SIP_CALL_ID, NULL);
229 (void) pthread_mutex_unlock(&_sip_msg->sip_msg_mutex);
230 cseq = sip_get_callseq_num(_sip_msg, NULL);
231 if (to == NULL || from == NULL || callid == NULL || cseq == -1)
232 return (NULL);
233 if (_sip_msg->sip_msg_req_res == NULL ||
234 _sip_msg->sip_msg_req_res->U.sip_request.sip_request_uri.
235 sip_str_ptr == NULL) {
236 return (NULL);
238 len = 2 * sizeof (md5_hash) + 1;
239 if ((branchid = malloc(len)) == NULL)
240 return (NULL);
241 #ifdef __linux__
242 _sasl_MD5Init(&ctx);
243 hdrlen = via->sip_hdr_end - via->sip_hdr_start;
244 _sasl_MD5Update(&ctx, (uchar_t *)via->sip_hdr_start, hdrlen);
245 hdrlen = to->sip_hdr_end - to->sip_hdr_start;
246 _sasl_MD5Update(&ctx, (uchar_t *)to->sip_hdr_start, hdrlen);
247 hdrlen = from->sip_hdr_end - from->sip_hdr_start;
248 _sasl_MD5Update(&ctx, (uchar_t *)from->sip_hdr_start, hdrlen);
249 hdrlen = callid->sip_hdr_end - callid->sip_hdr_start;
250 _sasl_MD5Update(&ctx, (uchar_t *)callid->sip_hdr_start, hdrlen);
251 _sasl_MD5Update(&ctx, (uchar_t *)_sip_msg->sip_msg_req_res->
252 U.sip_request.sip_request_uri.sip_str_ptr,
253 _sip_msg->sip_msg_req_res->U.sip_request.
254 sip_request_uri.sip_str_len);
255 _sasl_MD5Update(&ctx, (uchar_t *)&cseq, sizeof (int));
256 _sasl_MD5Final(md5_hash, &ctx);
257 #else /* solaris */
258 MD5Init(&ctx);
259 hdrlen = via->sip_hdr_end - via->sip_hdr_start;
260 MD5Update(&ctx, (uchar_t *)via->sip_hdr_start, hdrlen);
261 hdrlen = to->sip_hdr_end - to->sip_hdr_start;
262 MD5Update(&ctx, (uchar_t *)to->sip_hdr_start, hdrlen);
263 hdrlen = from->sip_hdr_end - from->sip_hdr_start;
264 MD5Update(&ctx, (uchar_t *)from->sip_hdr_start, hdrlen);
265 hdrlen = callid->sip_hdr_end - callid->sip_hdr_start;
266 MD5Update(&ctx, (uchar_t *)callid->sip_hdr_start, hdrlen);
267 MD5Update(&ctx, (uchar_t *)_sip_msg->sip_msg_req_res->
268 U.sip_request.sip_request_uri.sip_str_ptr,
269 _sip_msg->sip_msg_req_res->U.sip_request.
270 sip_request_uri.sip_str_len);
271 MD5Update(&ctx, (uchar_t *)&cseq, sizeof (int));
272 MD5Final(md5_hash, &ctx);
273 #endif
274 for (i = 0; i < sizeof (md5_hash); i++) {
275 (void) snprintf(&branchid[2 * i], len - (2 * i), "%02x",
276 md5_hash[i]);
278 return (branchid);
281 uint32_t
282 sip_get_cseq()
284 time_t tval;
286 tval = time(NULL);
288 return ((uint32_t)tval);
291 uint32_t
292 sip_get_rseq()
294 time_t tval;
296 tval = time(NULL);
298 return ((uint32_t)tval);