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
33 #include "win32/libc_interface.h"
36 #if defined(__APPLE__) || defined(__FreeBSD__)
37 #include <sys/socket.h>
41 #include <sys/types.h>
42 #include <sys/ioctl.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
47 #include "sipe-backend.h"
48 #include "sipe-core.h" /* to ensure same API for backends */
49 #include "sipe-core-private.h"
50 #include "sipe-utils.h"
54 /* Generate 32 random bits */
55 #define RANDOM32BITS (rand() & 0xFFFF)
57 gchar
*gencallid(void)
59 return g_strdup_printf("%04Xg%04Xa%04Xi%04Xm%04Xt%04Xb%04Xx%04Xx",
60 RANDOM32BITS
, RANDOM32BITS
, RANDOM32BITS
,
61 RANDOM32BITS
, RANDOM32BITS
, RANDOM32BITS
,
62 RANDOM32BITS
, RANDOM32BITS
);
67 return g_strdup_printf("%04d%04d", RANDOM32BITS
, RANDOM32BITS
);
70 gchar
*genconfid(void)
72 return g_strdup_printf("%04X%04X%04X%04X%04X%04X%04X%04X",
73 RANDOM32BITS
, RANDOM32BITS
, RANDOM32BITS
,
74 RANDOM32BITS
, RANDOM32BITS
, RANDOM32BITS
,
75 RANDOM32BITS
, RANDOM32BITS
);
78 gchar
*get_contact(const struct sipe_core_private
*sipe_private
)
80 return g_strdup(sipe_private
->contact
);
83 gchar
*parse_from(const gchar
*hdr
)
86 const gchar
*tmp
, *tmp2
= hdr
;
88 if (!hdr
) return NULL
;
89 SIPE_DEBUG_INFO("parsing address out of %s", hdr
);
90 tmp
= strchr(hdr
, '<');
92 /* i hate the different SIP UA behaviours... */
93 if (tmp
) { /* sip address in <...> */
95 tmp
= strchr(tmp2
, '>');
97 from
= g_strndup(tmp2
, tmp
- tmp2
);
99 SIPE_DEBUG_INFO_NOFORMAT("found < without > in From");
103 tmp
= strchr(tmp2
, ';');
105 from
= g_strndup(tmp2
, tmp
- tmp2
);
107 from
= g_strdup(tmp2
);
110 SIPE_DEBUG_INFO("got %s", from
);
114 int parse_cseq(const gchar
*hdr
)
118 items
= g_strsplit(hdr
, " ", 1);
120 res
= atoi(items
[0]);
126 gchar
*sip_uri_from_name(const gchar
*name
)
128 return(g_strdup_printf("sip:%s", name
));
131 gchar
*sip_uri(const gchar
*string
)
133 return(strstr(string
, "sip:") ? g_strdup(string
) : sip_uri_from_name(string
));
137 get_epid(struct sipe_core_private
*sipe_private
)
139 if (!sipe_private
->epid
) {
140 gchar
*self_sip_uri
= sip_uri_self(sipe_private
);
141 sipe_private
->epid
= sipe_get_epid(self_sip_uri
,
143 sipe_backend_network_ip_address());
144 g_free(self_sip_uri
);
146 return g_strdup(sipe_private
->epid
);
150 sipe_get_pub_instance(struct sipe_core_private
*sipe_private
,
154 gchar
*epid
= get_epid(sipe_private
);
156 sscanf(epid
, "%08x", &res
);
159 if (publication_key
== SIPE_PUB_DEVICE
) {
161 } else if (publication_key
== SIPE_PUB_STATE_MACHINE
) { /* First hexadecimal digit is 0x3 */
162 res
= (res
>> 4) | 0x30000000;
163 } else if (publication_key
== SIPE_PUB_STATE_USER
) {
164 res
= 0x20000000; /* fixed */
165 } else if (publication_key
== SIPE_PUB_STATE_CALENDAR
) { /* First hexadecimal digit is 0x4 */
166 res
= (res
>> 4) | 0x40000000;
167 } else if (publication_key
== SIPE_PUB_STATE_CALENDAR_OOF
) { /* First hexadecimal digit is 0x5 */
168 res
= (res
>> 4) | 0x50000000;
169 } else if (publication_key
== SIPE_PUB_CALENDAR_DATA
||
170 publication_key
== SIPE_PUB_NOTE_OOF
)
171 { /* First hexadecimal digit is 0x4 */
172 unsigned calendar_id
= 0;
173 char *mail_hash
= sipe_get_epid(SIPE_ACCOUNT_DATA_PRIVATE
->email
, "", "");
175 sscanf(mail_hash
, "%08x", &calendar_id
);
177 res
= (calendar_id
>> 4) | 0x40000000;
184 sipe_is_bad_alias(const char *uri
,
188 gboolean result
= FALSE
;
190 if (!uri
) return FALSE
;
191 if (!alias
) return TRUE
;
193 if (g_str_has_prefix(alias
, "sip:") || g_str_has_prefix(alias
, "sips:")) return TRUE
;
195 /* check if alias is just SIP URI but without 'sip:' prefix */
196 uri_alias
= sip_uri_from_name(alias
);
197 if (sipe_strcase_equal(uri
, uri_alias
)) {
206 is_empty(const char *st
)
208 if (!st
|| strlen(st
) == 0)
212 /* suspecious leading or trailing staces */
213 else if (isspace((unsigned char) *st
) ||
214 isspace((unsigned char) *(st
+ strlen(st
) - 1)))
216 /* to not modify original string */
217 char *dup
= g_strdup(st
);
218 if (strlen(g_strstrip(dup
)) == 0) {
227 /** Returns newly allocated string. Must be g_free()'d */
229 replace(const char *st
,
236 if (!st
) return NULL
;
238 res
= g_strjoinv(replace
, tmp
= g_strsplit(st
, search
, -1));
243 void sipe_utils_message_debug(const gchar
*type
,
248 if (sipe_backend_debug_enabled()) {
249 GString
*str
= g_string_new("");
252 const char *marker
= sending
?
257 g_get_current_time(&currtime
);
258 time_str
= g_time_val_to_iso8601(&currtime
);
259 g_string_append_printf(str
, "\nMESSAGE START %s %s - %s\n", marker
, type
, time_str
);
260 g_string_append(str
, tmp
= replace(header
, "\r\n", "\n"));
262 g_string_append(str
, "\n");
264 g_string_append(str
, tmp
= replace(body
, "\r\n", "\n"));
266 g_string_append(str
, "\n");
268 g_string_append_printf(str
, "MESSAGE END %s %s - %s", marker
, type
, time_str
);
270 SIPE_DEBUG_INFO_NOFORMAT(str
->str
);
271 g_string_free(str
, TRUE
);
276 sipe_strequal(const gchar
*left
, const gchar
*right
)
278 #if GLIB_CHECK_VERSION(2,16,0)
279 return (g_strcmp0(left
, right
) == 0);
281 return ((left
== NULL
&& right
== NULL
) ||
282 (left
!= NULL
&& right
!= NULL
&& strcmp(left
, right
) == 0));
287 sipe_strcase_equal(const gchar
*left
, const gchar
*right
)
289 return ((left
== NULL
&& right
== NULL
) ||
290 (left
!= NULL
&& right
!= NULL
&& g_ascii_strcasecmp(left
, right
) == 0));
294 sipe_utils_str_to_time(const gchar
*timestamp
)
299 /* We have to make sure that the ISO8601 contains a time zone offset,
300 otherwise the time is interpreted as local time, not UTC!
301 @TODO: is there a better way to check this? */
302 if (timestamp
&& (len
= strlen(timestamp
) > 0) &&
303 isdigit(timestamp
[len
- 1])) {
304 gchar
*tmp
= g_strdup_printf("%sZ", timestamp
);
305 g_time_val_from_iso8601(tmp
, &time
);
308 g_time_val_from_iso8601(timestamp
, &time
);
314 sipe_utils_time_to_str(time_t timestamp
)
316 GTimeVal time
= { timestamp
, 0 };
317 return g_time_val_to_iso8601(&time
);
321 hex_str_to_buff(const char *hex_str
, guint8
**buff
)
328 if (!hex_str
) return 0;
330 length
= strlen(hex_str
)/2;
331 *buff
= (unsigned char *)g_malloc(length
);
332 for (i
= 0; i
< length
; i
++) {
333 two_digits
[0] = hex_str
[i
* 2];
334 two_digits
[1] = hex_str
[i
* 2 + 1];
335 two_digits
[2] = '\0';
336 (*buff
)[i
] = (unsigned char)strtoul(two_digits
, NULL
, 16);
343 buff_to_hex_str(const guint8
*buff
, const size_t buff_len
)
348 if (!buff
) return NULL
;
350 res
= g_malloc(buff_len
* 2 + 1);
351 for (i
= 0, j
= 0; i
< buff_len
; i
++, j
+=2) {
352 sprintf(&res
[j
], "%02X", buff
[i
]);
359 sipe_utils_parse_lines(GSList
**list
, gchar
**lines
, gchar
*delimiter
)
367 for(i
= 0; lines
[i
] && strlen(lines
[i
]) > 2; i
++) {
368 parts
= g_strsplit(lines
[i
], delimiter
, 2);
369 if(!parts
[0] || !parts
[1]) {
375 while(*dummy
==' ' || *dummy
=='\t') dummy
++;
376 dummy2
= g_strdup(dummy
);
377 while(lines
[i
+1] && (lines
[i
+1][0]==' ' || lines
[i
+1][0]=='\t')) {
380 while(*dummy
==' ' || *dummy
=='\t') dummy
++;
381 tmp
= g_strdup_printf("%s %s",dummy2
, dummy
);
385 *list
= sipe_utils_nameval_add(*list
, parts
[0], dummy2
);
394 sipe_utils_nameval_add(GSList
* list
, const gchar
*name
, const gchar
*value
)
396 struct sipnameval
*element
= g_new0(struct sipnameval
,1);
398 /* SANITY CHECK: the calling code must be fixed if this happens! */
400 SIPE_DEBUG_ERROR("sipe_utils_nameval_add: NULL value for %s",
405 element
->name
= g_strdup(name
);
406 element
->value
= g_strdup(value
);
407 return g_slist_append(list
, element
);
411 sipe_utils_nameval_free(GSList
*list
) {
412 struct sipnameval
*elem
;
415 list
= g_slist_remove(list
,elem
);
423 sipe_utils_nameval_find(const GSList
*list
, const gchar
*name
)
425 return sipe_utils_nameval_find_instance (list
, name
, 0);
429 sipe_utils_nameval_find_instance(const GSList
*list
, const gchar
*name
, int which
)
432 struct sipnameval
*elem
;
437 // OCS2005 can send the same header in either all caps or mixed case
438 if (sipe_strcase_equal(elem
->name
, name
)) {
444 tmp
= g_slist_next(tmp
);
449 gchar
*sipe_utils_str_replace(const gchar
*string
,
450 const gchar
*delimiter
,
451 const gchar
*replacement
)
456 if (!string
|| !delimiter
|| !replacement
) return NULL
;
458 split
= g_strsplit(string
, delimiter
, 0);
459 result
= g_strjoinv(replacement
, split
);
465 void sipe_utils_shrink_buffer(struct sipe_transport_connection
*conn
,
468 conn
->buffer_used
-= unread
- conn
->buffer
;
469 /* string terminator is not included in buffer_used */
470 memmove(conn
->buffer
, unread
, conn
->buffer_used
+ 1);
474 * Calling sizeof(struct ifreq) isn't always correct on
475 * Mac OS X (and maybe others).
477 #ifdef _SIZEOF_ADDR_IFREQ
478 # define HX_SIZE_OF_IFREQ(a) _SIZEOF_ADDR_IFREQ(a)
480 # define HX_SIZE_OF_IFREQ(a) sizeof(a)
483 const char * sipe_utils_get_suitable_local_ip(int fd
)
485 int source
= (fd
>= 0) ? fd
: socket(PF_INET
,SOCK_STREAM
, 0);
492 guint32 lhost
= htonl(127 * 256 * 256 * 256 + 1);
493 guint32 llocal
= htonl((169 << 24) + (254 << 16));
495 ifc
.ifc_len
= sizeof(buffer
);
496 ifc
.ifc_req
= (struct ifreq
*)buffer
;
497 ioctl(source
, SIOCGIFCONF
, &ifc
);
503 while (tmp
< buffer
+ ifc
.ifc_len
)
505 struct ifreq
*ifr
= (struct ifreq
*)tmp
;
506 tmp
+= HX_SIZE_OF_IFREQ(*ifr
);
508 if (ifr
->ifr_addr
.sa_family
== AF_INET
)
510 struct sockaddr_in
*sinptr
= (struct sockaddr_in
*)&ifr
->ifr_addr
;
511 if (sinptr
->sin_addr
.s_addr
!= lhost
512 && (sinptr
->sin_addr
.s_addr
& htonl(0xFFFF0000)) != llocal
)
514 long unsigned int add
= ntohl(sinptr
->sin_addr
.s_addr
);
515 g_snprintf(ip
, 16, "%lu.%lu.%lu.%lu",
530 gchar
*sipe_utils_presence_key(const gchar
*uri
)
532 return g_strdup_printf("<presence><%s>", uri
);
535 gchar
*sipe_utils_subscription_key(const gchar
*event
,
540 if (!is_empty(event
)) {
541 if (!g_ascii_strcasecmp(event
, "presence")) {
542 /* Subscription is identified by <presence><uri> key */
543 key
= sipe_utils_presence_key(uri
);
545 /* Subscription is identified by <event> key */
546 key
= g_strdup_printf("<%s>", event
);