Skip to content

Commit 1d82d25

Browse files
committed
v2: remove internal dialog/subscribe session manager
1 parent 4a98d1a commit 1d82d25

34 files changed

Lines changed: 330 additions & 659 deletions

libsip/include/sip-agent.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,27 @@ struct sip_uas_handler_t
2929
/// @param[in] expires in seconds. if not provided, default equal to 60
3030
int (*onregister)(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, const char* user, const char* location, int expires);
3131

32-
/// @param[in] dialog nil-new invite, not nil-reinvite
33-
/// @param[out] session user-defined session-id(valid only code=2xx)
32+
/// @param[in] redialog nil-new invite, not nil-reinvite
3433
/// @return 0-ok, other-error
35-
int (*oninvite)(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, struct sip_dialog_t* dialog, const void* data, int bytes, void** session);
34+
int (*oninvite)(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, struct sip_dialog_t* redialog, const struct cstring_t* id, const void* data, int bytes);
3635
/// @param[in] code 0-ok, other-sip status code
37-
int (*onack)(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, void* session, struct sip_dialog_t* dialog, int code, const void* data, int bytes);
36+
int (*onack)(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, struct sip_dialog_t* dialog, const struct cstring_t* id, int code, const void* data, int bytes);
3837
/// @param[in] session oninvite return value
39-
int (*onprack)(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, void* session, struct sip_dialog_t* dialog, const void* data, int bytes);
40-
int (*onupdate)(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, void* session, struct sip_dialog_t* dialog, const void* data, int bytes);
41-
int (*oninfo)(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, void* session, struct sip_dialog_t* dialog, const struct cstring_t* package, const void* data, int bytes);
38+
int (*onprack)(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, const struct cstring_t* id, const void* data, int bytes);
39+
int (*onupdate)(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, const struct cstring_t* id, const void* data, int bytes);
40+
int (*oninfo)(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, const struct cstring_t* id, const struct cstring_t* package, const void* data, int bytes);
4241
/// on terminating a session(dialog)
43-
int (*onbye)(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, void* session);
44-
/// cancel a transaction(should be an invite transaction)
45-
int (*oncancel)(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, void* session);
42+
int (*onbye)(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, const struct cstring_t* id);
43+
/// cancel a transaction(should be an invite/subscribe transaction)
44+
int (*oncancel)(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, const struct cstring_t* id);
4645

47-
int (*onsubscribe)(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, struct sip_subscribe_t* subscribe, void** sub);
48-
int (*onnotify)(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, void* sub, const struct sip_event_t* event);
46+
int (*onsubscribe)(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, struct sip_subscribe_t* subscribe, const struct cstring_t* id);
47+
int (*onnotify)(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, const struct sip_event_t* event);
4948
int (*onpublish)(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, const struct sip_event_t* event);
49+
int (*onrefer)(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t);
5050

51-
int (*onmessage)(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, void* session, const void* data, int bytes);
52-
int (*onrefer)(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, void* session);
51+
/// @param[in] dialog nil-normal message, not nil-dialog message
52+
int (*onmessage)(void* param, const struct sip_message_t* req, struct sip_uas_transaction_t* t, const void* data, int bytes);
5353
};
5454

5555
struct sip_agent_t* sip_agent_create(struct sip_uas_handler_t* handler);

libsip/include/sip-dialog.h

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,23 @@ struct sip_dialog_t
3737
struct sip_uris_t routers;
3838

3939
// internal use only
40-
void* session; // user-defined session
41-
void (*ondestroy)(void* param);
42-
void* ondestroyparam;
43-
struct list_head link;
4440
char* ptr;
4541
int32_t ref;
4642
};
4743

4844
struct sip_dialog_t* sip_dialog_create(void);
4945
int sip_dialog_release(struct sip_dialog_t* dialog);
5046
int sip_dialog_addref(struct sip_dialog_t* dialog);
51-
47+
5248
int sip_dialog_init_uac(struct sip_dialog_t* dialog, const struct sip_message_t* msg);
5349
int sip_dialog_init_uas(struct sip_dialog_t* dialog, const struct sip_message_t* msg);
5450

5551
int sip_dialog_setlocaltag(struct sip_dialog_t* dialog, const struct cstring_t* tag);
5652
int sip_dialog_target_refresh(struct sip_dialog_t* dialog, const struct sip_message_t* msg);
5753
int sip_dialog_set_local_target(struct sip_dialog_t* dialog, const struct sip_message_t* msg);
5854

59-
// dialog management
60-
int sip_dialog_add(struct sip_agent_t* sip, struct sip_dialog_t* dialog);
61-
int sip_dialog_remove(struct sip_agent_t* sip, struct sip_dialog_t* dialog);
62-
63-
/// call sip_dialog_release
64-
struct sip_dialog_t* sip_dialog_fetch(struct sip_agent_t* sip, const struct cstring_t* callid, const struct cstring_t* local, const struct cstring_t* remote);
65-
66-
int sip_dialog_ondestroy(struct sip_dialog_t* dialog, void (*ondestroy)(void* param), void* param);
55+
int sip_dialog_id(struct cstring_t* id, const struct sip_dialog_t* dialog, char* ptr, int len);
56+
int sip_dialog_id_with_message(struct cstring_t* id, const struct sip_message_t* msg, char* ptr, int len);
6757

6858
#if defined(__cplusplus)
6959
}

libsip/include/sip-subscribe.h

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,20 @@ struct sip_subscribe_t
3333

3434
uint64_t expires;
3535
struct sip_event_t event;
36-
char* ptr;
37-
36+
3837
// internal use only
39-
struct list_head link;
40-
void* evtsession; // user-defined event session
38+
char* ptr;
4139
int32_t ref;
42-
int newdiaolog; // flag for remote dialog from sip dialogs link
4340
};
4441

4542
struct sip_subscribe_t* sip_subscribe_create(const struct sip_event_t* event);
4643
int sip_subscribe_release(struct sip_subscribe_t* subscribe);
4744
int sip_subscribe_addref(struct sip_subscribe_t* subscribe);
4845

49-
// subscribe management
50-
int sip_subscribe_add(struct sip_agent_t* sip, struct sip_subscribe_t* subscribe);
51-
int sip_subscribe_remove(struct sip_agent_t* sip, struct sip_subscribe_t* subscribe);
52-
53-
/// call sip_subscribe_release
54-
struct sip_subscribe_t* sip_subscribe_fetch(struct sip_agent_t* sip, const struct cstring_t* callid, const struct cstring_t* local, const struct cstring_t* remote, const struct sip_event_t* event);
55-
56-
/// call sip_subscribe_release
57-
struct sip_subscribe_t* sip_subscribe_internal_fetch(struct sip_agent_t* sip, const struct sip_message_t* msg, const struct sip_event_t* event, int uac, int* added);
46+
struct sip_subscribe_t* sip_subscribe_internal_create(struct sip_agent_t* sip, const struct sip_message_t* msg, const struct sip_event_t* event, int uac);
5847

48+
int sip_subscribe_id(struct cstring_t* id, const struct sip_subscribe_t* subscribe, char* ptr, int len);
49+
int sip_subscribe_id_with_message(struct cstring_t* id, const struct sip_message_t* msg, char* ptr, int len);
5950

6051
#if defined(__cplusplus)
6152
}

libsip/include/sip-uac.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ struct sip_uac_transaction_t;
1414
/// call sip_uac_ack on 2xx only
1515
/// @param[out] session user-defined session-id(only code=2xx)
1616
/// @return 0-ok, other-error
17-
typedef int (*sip_uac_oninvite)(void* param, const struct sip_message_t* reply, struct sip_uac_transaction_t* t, struct sip_dialog_t* dialog, int code, void** session);
17+
typedef int (*sip_uac_oninvite)(void* param, const struct sip_message_t* reply, struct sip_uac_transaction_t* t, struct sip_dialog_t* dialog, const struct cstring_t* id, int code);
1818
/// @param[in] subscribe MUST call sip_subscribe_remove on close
1919
/// @param[out] session user-defined session-id(only code=2xx)
2020
/// @return 0-ok, other-error
21-
typedef int (*sip_uac_onsubscribe)(void* param, const struct sip_message_t* reply, struct sip_uac_transaction_t* t, struct sip_subscribe_t* subscribe, int code, void** session);
21+
typedef int (*sip_uac_onsubscribe)(void* param, const struct sip_message_t* reply, struct sip_uac_transaction_t* t, struct sip_subscribe_t* subscribe, const struct cstring_t* id, int code);
2222
/// @return 0-ok, other-error
2323
typedef int (*sip_uac_onreply)(void* param, const struct sip_message_t* reply, struct sip_uac_transaction_t* t, int code);
2424
/// @return <0-error, 0-udp, 1-tcp, other-reserved

libsip/src/sip-agent.c

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,13 @@ struct sip_agent_t* sip_agent_create(struct sip_uas_handler_t* handler)
1414
locker_create(&sip->locker);
1515
LIST_INIT_HEAD(&sip->uac);
1616
LIST_INIT_HEAD(&sip->uas);
17-
LIST_INIT_HEAD(&sip->dialogs);
18-
LIST_INIT_HEAD(&sip->subscribes);
1917
memcpy(&sip->handler, handler, sizeof(sip->handler));
2018
return sip;
2119
}
2220

2321
int sip_agent_destroy(struct sip_agent_t* sip)
2422
{
2523
int32_t ref;
26-
//struct list_head *pos, *next;
27-
//struct sip_dialog_t* dialog;
28-
//struct sip_subscribe_t* subscribe;
29-
//struct sip_uac_transaction_t* uac;
30-
//struct sip_uas_transaction_t* uas;
3124

3225
assert(sip->ref > 0);
3326
ref = atomic_decrement32(&sip->ref);
@@ -36,31 +29,7 @@ int sip_agent_destroy(struct sip_agent_t* sip)
3629

3730
assert(list_empty(&sip->uac));
3831
assert(list_empty(&sip->uas));
39-
assert(list_empty(&sip->dialogs));
40-
assert(list_empty(&sip->subscribes));
41-
//list_for_each_safe(pos, next, &sip->uac)
42-
//{
43-
// uac = list_entry(pos, struct sip_uac_transaction_t, link);
44-
// assert(uac->agent == sip);
45-
// sip_uac_transaction_release(uac);
46-
//}
47-
//list_for_each_safe(pos, next, &sip->uas)
48-
//{
49-
// uas = list_entry(pos, struct sip_uas_transaction_t, link);
50-
// assert(uas->agent == sip);
51-
// sip_uac_transaction_release(uas);
52-
//}
53-
//list_for_each_safe(pos, next, &sip->dialogs)
54-
//{
55-
// dialog = list_entry(pos, struct sip_dialog_t, link);
56-
// sip_dialog_release(dialog);
57-
//}
58-
//list_for_each_safe(pos, next, &sip->subscribes)
59-
//{
60-
// subscribe = list_entry(pos, struct sip_subscribe_t, link);
61-
// sip_subscribe_release(subscribe);
62-
//}
63-
32+
6433
locker_destroy(&sip->locker);
6534
free(sip);
6635
return 0;

libsip/src/sip-dialog.c

Lines changed: 16 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ struct sip_dialog_t* sip_dialog_create(void)
2222
if (dialog)
2323
{
2424
dialog->ref = 1;
25-
LIST_INIT_HEAD(&dialog->link);
2625
dialog->state = DIALOG_ERALY;
2726
dialog->ptr = (char*)(dialog + 1);
2827
atomic_increment32(&s_gc.dialog);
@@ -118,9 +117,6 @@ int sip_dialog_release(struct sip_dialog_t* dialog)
118117
if (0 != atomic_decrement32(&dialog->ref))
119118
return 0;
120119

121-
if (dialog->ondestroy)
122-
dialog->ondestroy(dialog->ondestroyparam);
123-
124120
sip_uri_free(&dialog->local.target);
125121
sip_contact_free(&dialog->local.uri);
126122
sip_uri_free(&dialog->remote.target);
@@ -139,13 +135,6 @@ int sip_dialog_addref(struct sip_dialog_t* dialog)
139135
return r;
140136
}
141137

142-
int sip_dialog_ondestroy(struct sip_dialog_t* dialog, void (*ondestroy)(void* param), void* param)
143-
{
144-
dialog->ondestroy = ondestroy;
145-
dialog->ondestroyparam = param;
146-
return 0;
147-
}
148-
149138
int sip_dialog_setlocaltag(struct sip_dialog_t* dialog, const struct cstring_t* tag)
150139
{
151140
const char* end;
@@ -194,136 +183,24 @@ static int sip_dialog_match(const struct sip_dialog_t* dialog, const struct cstr
194183
return cstreq(callid, &dialog->callid) && cstreq(local, &dialog->local.uri.tag) && cstreq(remote, &dialog->remote.uri.tag) ? 1 : 0;
195184
}
196185

197-
static struct sip_dialog_t* sip_dialog_find(struct sip_agent_t* sip, const struct cstring_t* callid, const struct cstring_t* local, const struct cstring_t* remote)
198-
{
199-
struct list_head *pos, *next;
200-
struct sip_dialog_t* dialog;
201-
202-
list_for_each_safe(pos, next, &sip->dialogs)
203-
{
204-
dialog = list_entry(pos, struct sip_dialog_t, link);
205-
if (sip_dialog_match(dialog, callid, local, remote))
206-
return dialog;
207-
}
208-
209-
return NULL;
210-
}
211-
212-
struct sip_dialog_t* sip_dialog_fetch(struct sip_agent_t* sip, const struct cstring_t* callid, const struct cstring_t* local, const struct cstring_t* remote)
213-
{
214-
struct sip_dialog_t* dialog;
215-
locker_lock(&sip->locker);
216-
dialog = sip_dialog_find(sip, callid, local, remote);
217-
if(dialog)
218-
sip_dialog_addref(dialog);
219-
locker_unlock(&sip->locker);
220-
return dialog;
221-
}
222-
223-
int sip_dialog_add(struct sip_agent_t* sip, struct sip_dialog_t* dialog)
224-
{
225-
locker_lock(&sip->locker);
226-
if (NULL != sip_dialog_find(sip, &dialog->callid, &dialog->local.uri.tag, &dialog->remote.uri.tag))
227-
{
228-
assert(0);
229-
locker_unlock(&sip->locker);
230-
return -1; // exist
231-
}
232-
233-
// link to tail
234-
assert(1 == dialog->ref);
235-
list_insert_after(&dialog->link, sip->dialogs.prev);
236-
locker_unlock(&sip->locker);
237-
sip_dialog_addref(dialog);
238-
return 0;
239-
}
240-
241-
int sip_dialog_remove(struct sip_agent_t* sip, struct sip_dialog_t* dialog)
186+
int sip_dialog_id(struct cstring_t* id, const struct sip_dialog_t* dialog, char* ptr, int len)
242187
{
243-
// unlink dialog
244-
locker_lock(&sip->locker);
245-
//assert(1 == dialog->ref);
246-
if (dialog->link.next == NULL)
247-
{
248-
// fix remove twice
249-
locker_unlock(&sip->locker);
250-
return -1;
251-
}
252-
253-
list_remove(&dialog->link);
254-
locker_unlock(&sip->locker);
255-
sip_dialog_release(dialog);
256-
return 0;
257-
}
258-
259-
int sip_dialog_remove2(struct sip_agent_t* sip, const struct cstring_t* callid, const struct cstring_t* local, const struct cstring_t* remote)
260-
{
261-
struct sip_dialog_t* dialog;
262-
locker_lock(&sip->locker);
263-
dialog = sip_dialog_find(sip, callid, local, remote);
264-
if (dialog && dialog->link.next)
265-
list_remove(&dialog->link);
266-
locker_unlock(&sip->locker);
267-
268-
if (dialog)
269-
{
270-
sip_dialog_release(dialog);
271-
return 0;
272-
}
273-
return -1; // not found
274-
}
275-
276-
int sip_dialog_remove_early(struct sip_agent_t* sip, const struct cstring_t* callid)
277-
{
278-
struct sip_dialog_t* early;
279-
struct sip_dialog_t* dialog;
280-
struct list_head *pos, *next;
281-
282-
early = NULL;
283-
locker_lock(&sip->locker);
284-
list_for_each_safe(pos, next, &sip->dialogs)
285-
{
286-
dialog = list_entry(pos, struct sip_dialog_t, link);
287-
if (cstreq(callid, &dialog->callid) && DIALOG_ERALY == dialog->state)
288-
{
289-
//assert(0 == sip_contact_compare(&t->req->from, &dialog->local.uri));
290-
list_remove(&dialog->link);
291-
early = dialog;
292-
break;
293-
}
294-
}
295-
296-
locker_unlock(&sip->locker);
297-
if (early)
298-
{
299-
sip_dialog_release(early);
300-
return 0;
301-
}
302-
return -1; // not found
188+
int r;
189+
r = dialog ? snprintf(ptr, len, "%.*s@%.*s@%.*s", (int)dialog->callid.n, dialog->callid.p, (int)dialog->local.uri.tag.n, dialog->local.uri.tag.p, (int)dialog->remote.uri.tag.n, dialog->remote.uri.tag.p) : 0;
190+
id->p = ptr;
191+
id->n = r > 0 && r < sizeof(id) ? r : 0;
192+
return r;
303193
}
304194

305-
// MUST ADD LOCK !!!!! internal use only !!!!!!!!!
306-
struct sip_dialog_t* sip_dialog_internal_fetch(struct sip_agent_t* sip, const struct sip_message_t* msg, int uac, int* added)
195+
int sip_dialog_id_with_message(struct cstring_t *id, const struct sip_message_t* msg, char* ptr, int len)
307196
{
308-
struct sip_dialog_t* dialog;
309-
310-
*added = 0;
311-
dialog = sip_dialog_find(sip, &msg->callid, uac ? &msg->from.tag : &msg->to.tag, uac ? &msg->to.tag : &msg->from.tag);
312-
if (!dialog)
313-
{
314-
dialog = sip_dialog_create();
315-
if (!dialog || 0 != (uac ? sip_dialog_init_uac(dialog, msg) : sip_dialog_init_uas(dialog, msg)))
316-
{
317-
sip_dialog_release(dialog);
318-
return NULL;
319-
}
320-
321-
// link to sip dialogs(add ref later)
322-
list_insert_after(&dialog->link, sip->dialogs.prev);
323-
assert(dialog->ref == 1);
324-
*added = 1;
325-
}
326-
327-
sip_dialog_addref(dialog); // for sip link dialog / fetch
328-
return dialog;
197+
int r;
198+
if (msg->mode == SIP_MESSAGE_REQUEST)
199+
r = snprintf(ptr, len, "%.*s@%.*s@%.*s", (int)msg->callid.n, msg->callid.p, (int)msg->to.tag.n, msg->to.tag.p, (int)msg->from.tag.n, msg->from.tag.p);
200+
else
201+
r = snprintf(ptr, len, "%.*s@%.*s@%.*s", (int)msg->callid.n, msg->callid.p, (int)msg->from.tag.n, msg->from.tag.p, (int)msg->to.tag.n, msg->to.tag.p);
202+
203+
id->p = ptr;
204+
id->n = r > 0 && r < sizeof(id) ? r : 0;
205+
return r;
329206
}

libsip/src/sip-internal.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ struct sip_agent_t
2828
//struct sip_timer_t timer;
2929
//void* timerptr;
3030

31-
struct list_head dialogs;
32-
struct list_head subscribes;
33-
3431
struct list_head uac; // uac transactions
3532
struct list_head uas; // uas transactions
3633
struct sip_uas_handler_t handler;

0 commit comments

Comments
 (0)