1 /*
2  * presence module - presence server implementation
3  *
4  * Copyright (C) 2006 Voice Sistem S.R.L.
5  *
6  * This file is part of Kamailio, a free SIP server.
7  *
8  * Kamailio 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
12  *
13  * Kamailio 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.
17  *
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23 
24 /*! \file
25  * \brief Kamailio presence module :: Utility functions
26  * \ref utils_func.c
27  * \ingroup presence
28  */
29 
30 
31 #ifndef UTILS_FUNC_H
32 #define UTILS_FUNC_H
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include "../../core/mem/mem.h"
37 #include "../../core/dprint.h"
38 #include "../../core/str.h"
39 #include "../../core/parser/msg_parser.h"
40 #include "../../core/parser/parse_event.h"
41 
42 #define LCONTACT_BUF_SIZE 1024
43 #define BAD_EVENT_CODE 489
44 #define INTERVAL_TOO_BRIEF 423
45 
46 
47 #define EVENT_DIALOG_SLA(ev)                        \
48 	((ev)->type == EVENT_DIALOG                     \
49 			&& ((ev)->params.hooks.event_dialog.sla \
50 					   || (ev)->params.hooks.event_dialog.ma))
51 
52 
uandd_to_uri(str user,str domain,str * out)53 static inline int uandd_to_uri(str user, str domain, str *out)
54 {
55 	int size;
56 
57 	if(out == 0)
58 		return -1;
59 
60 	size = user.len + domain.len + 7;
61 	out->s = (char *)pkg_malloc(size);
62 
63 	if(out->s == NULL) {
64 		LM_ERR("no more memory\n");
65 		return -1;
66 	}
67 	strcpy(out->s, "sip:");
68 	out->len = 4;
69 	if(user.s != NULL && user.len > 0) {
70 		memcpy(out->s + out->len, user.s, user.len);
71 		out->len += user.len;
72 		out->s[out->len++] = '@';
73 	}
74 	memcpy(out->s + out->len, domain.s, domain.len);
75 	out->len += domain.len;
76 	out->s[out->len] = '\0';
77 
78 	return 0;
79 }
80 
ps_fill_local_contact(struct sip_msg * msg,str * contact)81 static inline int ps_fill_local_contact(struct sip_msg *msg, str *contact)
82 {
83 	str ip;
84 	char *proto;
85 	int port;
86 	int len;
87 	int plen;
88 	char *p;
89 
90 	contact->s = (char *)pkg_malloc(LCONTACT_BUF_SIZE);
91 	if(contact->s == NULL) {
92 		LM_ERR("No more memory\n");
93 		goto error;
94 	}
95 
96 	memset(contact->s, 0, LCONTACT_BUF_SIZE);
97 	contact->len = 0;
98 
99 	plen = 3;
100 	if(msg->rcv.proto == PROTO_NONE || msg->rcv.proto == PROTO_UDP)
101 		proto = "udp";
102 	else if(msg->rcv.proto == PROTO_TLS)
103 		proto = "tls";
104 	else if(msg->rcv.proto == PROTO_TCP)
105 		proto = "tcp";
106 	else if(msg->rcv.proto == PROTO_SCTP) {
107 		proto = "sctp";
108 		plen = 4;
109 	} else if(msg->rcv.proto == PROTO_WS || msg->rcv.proto == PROTO_WSS) {
110 		proto = "ws";
111 		plen = 2;
112 	} else {
113 		LM_ERR("unsupported proto\n");
114 		goto error;
115 	}
116 
117 	if(msg->rcv.bind_address->useinfo.name.len > 0) {
118 		ip = msg->rcv.bind_address->useinfo.name;
119 	} else {
120 		ip = msg->rcv.bind_address->address_str;
121 	}
122 
123 	if(msg->rcv.bind_address->useinfo.port_no > 0) {
124 		port = msg->rcv.bind_address->useinfo.port_no;
125 	} else {
126 		port = msg->rcv.bind_address->port_no;
127 	}
128 
129 	p = contact->s;
130 	if(strncmp(ip.s, "sip:", 4) != 0) {
131 		memcpy(p, "sip:", 4);
132 		contact->len += 4;
133 		p += 4;
134 	}
135 	if(msg->rcv.bind_address->address.af == AF_INET6) {
136 		*p = '[';
137 		contact->len += 1;
138 		p += 1;
139 	}
140 	strncpy(p, ip.s, ip.len);
141 	contact->len += ip.len;
142 	p += ip.len;
143 	if(msg->rcv.bind_address->address.af == AF_INET6) {
144 		*p = ']';
145 		contact->len += 1;
146 		p += 1;
147 	}
148 	if(contact->len > LCONTACT_BUF_SIZE - 21) {
149 		LM_ERR("buffer overflow\n");
150 		goto error;
151 	}
152 	len = sprintf(p, ":%d;transport=", port);
153 	if(len < 0) {
154 		LM_ERR("unsuccessful sprintf\n");
155 		goto error;
156 	}
157 	contact->len += len;
158 	p += len;
159 	strncpy(p, proto, plen);
160 	contact->len += plen;
161 
162 	return 0;
163 error:
164 	if(contact->s != NULL)
165 		pkg_free(contact->s);
166 	contact->s = 0;
167 	contact->len = 0;
168 	return -1;
169 }
170 
171 //str* int_to_str(long int n);
172 
173 int a_to_i(char *s, int len);
174 
175 void to64frombits(unsigned char *out, const unsigned char *in, int inlen);
176 
177 int send_error_reply(struct sip_msg *msg, int reply_code, str reply_str);
178 
179 #endif
180