-
Notifications
You must be signed in to change notification settings - Fork 7
/
sip.c
247 lines (199 loc) · 4.46 KB
/
sip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/**
* @file sip.c SIP Core
*
* Copyright (C) 2010 Creytiv.com
*/
#include <re_types.h>
#include <re_mem.h>
#include <re_mbuf.h>
#include <re_sa.h>
#include <re_list.h>
#include <re_hash.h>
#include <re_fmt.h>
#include <re_uri.h>
#include <re_sys.h>
#include <re_tmr.h>
#include <re_udp.h>
#include <re_stun.h>
#include <re_msg.h>
#include <re_sip.h>
#include "sip.h"
static void destructor(void *arg)
{
struct sip *sip = arg;
if (sip->closing) {
sip->closing = false;
mem_ref(sip);
if (sip->exith)
sip->exith(sip->arg);
return;
}
sip_request_close(sip);
sip_request_close(sip);
hash_flush(sip->ht_ctrans);
mem_deref(sip->ht_ctrans);
hash_flush(sip->ht_strans);
hash_clear(sip->ht_strans_mrg);
mem_deref(sip->ht_strans);
mem_deref(sip->ht_strans_mrg);
hash_flush(sip->ht_conn);
mem_deref(sip->ht_conn);
hash_flush(sip->ht_udpconn);
mem_deref(sip->ht_udpconn);
list_flush(&sip->transpl);
list_flush(&sip->lsnrl);
mem_deref(sip->software);
mem_deref(sip->dnsc);
mem_deref(sip->stun);
}
static void lsnr_destructor(void *arg)
{
struct sip_lsnr *lsnr = arg;
if (lsnr->lsnrp)
*lsnr->lsnrp = NULL;
list_unlink(&lsnr->le);
}
/**
* Allocate a SIP stack instance
*
* @param sipp Pointer to allocated SIP stack
* @param dnsc DNS Client (optional)
* @param ctsz Size of client transactions hashtable (power of 2)
* @param stsz Size of server transactions hashtable (power of 2)
* @param tcsz Size of SIP transport hashtable (power of 2)
* @param software Software identifier
* @param exith SIP-stack exit handler
* @param arg Handler argument
*
* @return 0 if success, otherwise errorcode
*/
int sip_alloc(struct sip **sipp, struct dnsc *dnsc, uint32_t ctsz,
uint32_t stsz, uint32_t tcsz, const char *software,
sip_exit_h *exith, void *arg)
{
struct sip *sip;
int err;
if (!sipp)
return EINVAL;
sip = mem_zalloc(sizeof(*sip), destructor);
if (!sip)
return ENOMEM;
err = sip_transp_init(sip, tcsz);
if (err)
goto out;
err = sip_ctrans_init(sip, ctsz);
if (err)
goto out;
err = sip_strans_init(sip, stsz);
if (err)
goto out;
err = hash_alloc(&sip->ht_udpconn, tcsz);
if (err)
goto out;
err = stun_alloc(&sip->stun, NULL, NULL, NULL);
if (err)
goto out;
if (software) {
err = str_dup(&sip->software, software);
if (err)
goto out;
}
sip->dnsc = mem_ref(dnsc);
sip->exith = exith;
sip->arg = arg;
out:
if (err)
mem_deref(sip);
else
*sipp = sip;
return err;
}
/**
* Close the SIP stack instance
*
* @param sip SIP stack instance
* @param force Don't wait for transactions to complete
*/
void sip_close(struct sip *sip, bool force)
{
if (!sip)
return;
if (force) {
sip_request_close(sip);
sip_request_close(sip);
}
else if (!sip->closing) {
sip->closing = true;
mem_deref(sip);
}
}
/**
* Send a SIP message
*
* @param sip SIP stack instance
* @param sock Optional socket to send from
* @param tp SIP transport
* @param dst Destination network address
* @param mb Buffer containing SIP message
*
* @return 0 if success, otherwise errorcode
*/
int sip_send(struct sip *sip, void *sock, enum sip_transp tp,
const struct sa *dst, struct mbuf *mb)
{
return sip_transp_send(NULL, sip, sock, tp, dst, mb, NULL, NULL);
}
/**
* Listen for incoming SIP Requests and SIP Responses
*
* @param lsnrp Pointer to allocated listener
* @param sip SIP stack instance
* @param req True for Request, false for Response
* @param msgh SIP message handler
* @param arg Handler argument
*
* @return 0 if success, otherwise errorcode
*/
int sip_listen(struct sip_lsnr **lsnrp, struct sip *sip, bool req,
sip_msg_h *msgh, void *arg)
{
struct sip_lsnr *lsnr;
if (!sip || !msgh)
return EINVAL;
lsnr = mem_zalloc(sizeof(*lsnr), lsnr_destructor);
if (!lsnr)
return ENOMEM;
list_append(&sip->lsnrl, &lsnr->le, lsnr);
lsnr->msgh = msgh;
lsnr->arg = arg;
lsnr->req = req;
if (lsnrp) {
lsnr->lsnrp = lsnrp;
*lsnrp = lsnr;
}
return 0;
}
/**
* Print debug information about the SIP stack
*
* @param pf Print function for debug output
* @param sip SIP stack instance
*
* @return 0 if success, otherwise errorcode
*/
int sip_debug(struct re_printf *pf, const struct sip *sip)
{
int err;
if (!sip)
return 0;
err = sip_transp_debug(pf, sip);
err |= sip_ctrans_debug(pf, sip);
err |= sip_strans_debug(pf, sip);
return err;
}
void sip_set_trace(struct sip *sip, sip_trace_h *traceh)
{
if (!sip)
return;
sip->traceh = traceh;
}