6 * Copyright (C) 2009-2010 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 #include <sys/types.h>
37 #include <sys/ioctl.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
42 #include "sipe-backend.h"
43 #include "sipe-core.h" /* to ensure same API for backends */
44 #include "sipe-utils.h"
48 /* Generate 32 random bits */
49 #define RANDOM32BITS (rand() & 0xFFFF)
51 gchar
*gencallid(void)
53 return g_strdup_printf("%04Xg%04Xa%04Xi%04Xm%04Xt%04Xb%04Xx%04Xx",
54 RANDOM32BITS
, RANDOM32BITS
, RANDOM32BITS
,
55 RANDOM32BITS
, RANDOM32BITS
, RANDOM32BITS
,
56 RANDOM32BITS
, RANDOM32BITS
);
61 return g_strdup_printf("%04d%04d", RANDOM32BITS
, RANDOM32BITS
);
64 gchar
*genconfid(void)
66 return g_strdup_printf("%04X%04X%04X%04X%04X%04X%04X%04X",
67 RANDOM32BITS
, RANDOM32BITS
, RANDOM32BITS
,
68 RANDOM32BITS
, RANDOM32BITS
, RANDOM32BITS
,
69 RANDOM32BITS
, RANDOM32BITS
);
72 gchar
*get_contact(const struct sipe_account_data
*sip
)
74 return g_strdup(sip
->contact
);
77 gchar
*parse_from(const gchar
*hdr
)
80 const gchar
*tmp
, *tmp2
= hdr
;
82 if (!hdr
) return NULL
;
83 SIPE_DEBUG_INFO("parsing address out of %s", hdr
);
84 tmp
= strchr(hdr
, '<');
86 /* i hate the different SIP UA behaviours... */
87 if (tmp
) { /* sip address in <...> */
89 tmp
= strchr(tmp2
, '>');
91 from
= g_strndup(tmp2
, tmp
- tmp2
);
93 SIPE_DEBUG_INFO_NOFORMAT("found < without > in From");
97 tmp
= strchr(tmp2
, ';');
99 from
= g_strndup(tmp2
, tmp
- tmp2
);
101 from
= g_strdup(tmp2
);
104 SIPE_DEBUG_INFO("got %s", from
);
108 int parse_cseq(const gchar
*hdr
)
112 items
= g_strsplit(hdr
, " ", 1);
114 res
= atoi(items
[0]);
120 gchar
*sip_uri_from_name(const gchar
*name
)
122 return(g_strdup_printf("sip:%s", name
));
125 gchar
*sip_uri(const gchar
*string
)
127 return(strstr(string
, "sip:") ? g_strdup(string
) : sip_uri_from_name(string
));
131 get_epid(struct sipe_account_data
*sip
)
134 gchar
*self_sip_uri
= sip_uri_self(sip
);
135 sip
->epid
= sipe_get_epid(self_sip_uri
,
137 sipe_backend_network_ip_address());
138 g_free(self_sip_uri
);
140 return g_strdup(sip
->epid
);
144 sipe_get_pub_instance(struct sipe_account_data
*sip
,
148 gchar
*epid
= get_epid(sip
);
150 sscanf(epid
, "%08x", &res
);
153 if (publication_key
== SIPE_PUB_DEVICE
) {
155 } else if (publication_key
== SIPE_PUB_STATE_MACHINE
) { /* First hexadecimal digit is 0x3 */
156 res
= (res
>> 4) | 0x30000000;
157 } else if (publication_key
== SIPE_PUB_STATE_USER
) {
158 res
= 0x20000000; /* fixed */
159 } else if (publication_key
== SIPE_PUB_STATE_CALENDAR
) { /* First hexadecimal digit is 0x4 */
160 res
= (res
>> 4) | 0x40000000;
161 } else if (publication_key
== SIPE_PUB_STATE_CALENDAR_OOF
) { /* First hexadecimal digit is 0x5 */
162 res
= (res
>> 4) | 0x50000000;
163 } else if (publication_key
== SIPE_PUB_CALENDAR_DATA
||
164 publication_key
== SIPE_PUB_NOTE_OOF
)
165 { /* First hexadecimal digit is 0x4 */
166 unsigned calendar_id
= 0;
167 char *mail_hash
= sipe_get_epid(sip
->email
, "", "");
169 sscanf(mail_hash
, "%08x", &calendar_id
);
171 res
= (calendar_id
>> 4) | 0x40000000;
178 sipe_get_pub_instance_(struct sipe_account_data *sip,
179 const char *publication_key)
183 gchar *epid = get_epid(sip);
184 sscanf(epid, "%08x", &part_1);
186 sscanf(publication_key, "%uh", &part_2);
187 return part_1 + part_2;
191 sipe_is_bad_alias(const char *uri
,
195 gboolean result
= FALSE
;
197 if (!uri
) return FALSE
;
198 if (!alias
) return TRUE
;
200 if (g_str_has_prefix(alias
, "sip:") || g_str_has_prefix(alias
, "sips:")) return TRUE
;
202 /* check if alias is just SIP URI but without 'sip:' prefix */
203 uri_alias
= sip_uri_from_name(alias
);
204 if (sipe_strcase_equal(uri
, uri_alias
)) {
213 is_empty(const char *st
)
215 if (!st
|| strlen(st
) == 0)
219 /* suspecious leading or trailing staces */
220 else if (isspace((unsigned char) *st
) ||
221 isspace((unsigned char) *(st
+ strlen(st
) - 1)))
223 /* to not modify original string */
224 char *dup
= g_strdup(st
);
225 if (strlen(g_strstrip(dup
)) == 0) {
234 /** Returns newly allocated string. Must be g_free()'d */
236 replace(const char *st
,
243 if (!st
) return NULL
;
245 res
= g_strjoinv(replace
, tmp
= g_strsplit(st
, search
, -1));
251 fix_newlines(const char *st
)
253 return replace(st
, "\r\n", "\n");
257 sipe_strequal(const gchar
*left
, const gchar
*right
)
259 #if GLIB_CHECK_VERSION(2,16,0)
260 return (g_strcmp0(left
, right
) == 0);
262 return ((left
== NULL
&& right
== NULL
) ||
263 (left
!= NULL
&& right
!= NULL
&& strcmp(left
, right
) == 0));
268 sipe_strcase_equal(const gchar
*left
, const gchar
*right
)
270 return ((left
== NULL
&& right
== NULL
) ||
271 (left
!= NULL
&& right
!= NULL
&& g_ascii_strcasecmp(left
, right
) == 0));
275 sipe_utils_str_to_time(const gchar
*timestamp
)
278 g_time_val_from_iso8601(timestamp
, &time
);
283 sipe_utils_time_to_str(time_t timestamp
)
285 GTimeVal time
= { timestamp
, 0 };
286 return g_time_val_to_iso8601(&time
);
290 hex_str_to_buff(const char *hex_str
, guint8
**buff
)
297 if (!hex_str
) return 0;
299 length
= strlen(hex_str
)/2;
300 *buff
= (unsigned char *)g_malloc(length
);
301 for (i
= 0; i
< length
; i
++) {
302 two_digits
[0] = hex_str
[i
* 2];
303 two_digits
[1] = hex_str
[i
* 2 + 1];
304 two_digits
[2] = '\0';
305 (*buff
)[i
] = (unsigned char)strtoul(two_digits
, NULL
, 16);
312 buff_to_hex_str(const guint8
*buff
, const size_t buff_len
)
317 if (!buff
) return NULL
;
319 res
= g_malloc(buff_len
* 2 + 1);
320 for (i
= 0, j
= 0; i
< buff_len
; i
++, j
+=2) {
321 sprintf(&res
[j
], "%02X", buff
[i
]);
328 sipe_utils_parse_lines(GSList
**list
, gchar
**lines
, gchar
*delimiter
)
336 for(i
= 0; lines
[i
] && strlen(lines
[i
]) > 2; i
++) {
337 parts
= g_strsplit(lines
[i
], delimiter
, 2);
338 if(!parts
[0] || !parts
[1]) {
344 while(*dummy
==' ' || *dummy
=='\t') dummy
++;
345 dummy2
= g_strdup(dummy
);
346 while(lines
[i
+1] && (lines
[i
+1][0]==' ' || lines
[i
+1][0]=='\t')) {
349 while(*dummy
==' ' || *dummy
=='\t') dummy
++;
350 tmp
= g_strdup_printf("%s %s",dummy2
, dummy
);
354 *list
= sipe_utils_nameval_add(*list
, parts
[0], dummy2
);
363 sipe_utils_nameval_add(GSList
* list
, const gchar
*name
, const gchar
*value
)
365 struct sipnameval
*element
= g_new0(struct sipnameval
,1);
367 /* SANITY CHECK: the calling code must be fixed if this happens! */
369 SIPE_DEBUG_ERROR("sipe_utils_nameval_add: NULL value for %s",
374 element
->name
= g_strdup(name
);
375 element
->value
= g_strdup(value
);
376 return g_slist_append(list
, element
);
380 sipe_utils_nameval_free(GSList
*list
) {
381 struct sipnameval
*elem
;
384 list
= g_slist_remove(list
,elem
);
392 sipe_utils_nameval_find(const GSList
*list
, const gchar
*name
)
394 return sipe_utils_nameval_find_instance (list
, name
, 0);
398 sipe_utils_nameval_find_instance(const GSList
*list
, const gchar
*name
, int which
)
401 struct sipnameval
*elem
;
406 // OCS2005 can send the same header in either all caps or mixed case
407 if (sipe_strcase_equal(elem
->name
, name
)) {
413 tmp
= g_slist_next(tmp
);
418 gchar
*sipe_utils_str_replace(const gchar
*string
,
419 const gchar
*delimiter
,
420 const gchar
*replacement
)
425 if (!string
|| !delimiter
|| !replacement
) return NULL
;
427 split
= g_strsplit(string
, delimiter
, 0);
428 result
= g_strjoinv(replacement
, split
);
434 void sipe_utils_shrink_buffer(struct sipe_transport_connection
*conn
,
437 conn
->buffer_used
-= unread
- conn
->buffer
;
438 /* string terminator is not included in buffer_used */
439 memmove(conn
->buffer
, unread
, conn
->buffer_used
+ 1);
443 * Calling sizeof(struct ifreq) isn't always correct on
444 * Mac OS X (and maybe others).
446 #ifdef _SIZEOF_ADDR_IFREQ
447 # define HX_SIZE_OF_IFREQ(a) _SIZEOF_ADDR_IFREQ(a)
449 # define HX_SIZE_OF_IFREQ(a) sizeof(a)
452 const char * sipe_utils_get_suitable_local_ip(int fd
)
454 int source
= (fd
>= 0) ? fd
: socket(PF_INET
,SOCK_STREAM
, 0);
461 guint32 lhost
= htonl(127 * 256 * 256 * 256 + 1);
462 guint32 llocal
= htonl((169 << 24) + (254 << 16));
464 ifc
.ifc_len
= sizeof(buffer
);
465 ifc
.ifc_req
= (struct ifreq
*)buffer
;
466 ioctl(source
, SIOCGIFCONF
, &ifc
);
472 while (tmp
< buffer
+ ifc
.ifc_len
)
474 struct ifreq
*ifr
= (struct ifreq
*)tmp
;
475 tmp
+= HX_SIZE_OF_IFREQ(*ifr
);
477 if (ifr
->ifr_addr
.sa_family
== AF_INET
)
479 struct sockaddr_in
*sinptr
= (struct sockaddr_in
*)&ifr
->ifr_addr
;
480 if (sinptr
->sin_addr
.s_addr
!= lhost
481 && (sinptr
->sin_addr
.s_addr
& htonl(0xFFFF0000)) != llocal
)
483 long unsigned int add
= ntohl(sinptr
->sin_addr
.s_addr
);
484 g_snprintf(ip
, 16, "%lu.%lu.%lu.%lu",