Skip to content

Commit 9ff9a24

Browse files
committed
sip: add sip contact, needed for GRUU
- in sipsess and sipevent, the cuser parameter can now be a contact-username or a uri (e.g. GRUU) - Tested by Juha Heinanen (with baresip+gruu)
1 parent 502a4e6 commit 9ff9a24

File tree

10 files changed

+118
-26
lines changed

10 files changed

+118
-26
lines changed

include/re_sip.h

+14
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,13 @@ struct sip_loopstate {
214214
uint16_t last_scode;
215215
};
216216

217+
/** SIP Contact */
218+
struct sip_contact {
219+
const char *uri;
220+
const struct sa *addr;
221+
enum sip_transp tp;
222+
};
223+
217224
struct sip;
218225
struct sip_lsnr;
219226
struct sip_request;
@@ -303,6 +310,13 @@ int sip_auth_alloc(struct sip_auth **authp, sip_auth_h *authh,
303310
void sip_auth_reset(struct sip_auth *auth);
304311

305312

313+
/* contact */
314+
void sip_contact_set(struct sip_contact *contact, const char *uri,
315+
const struct sa *addr, enum sip_transp tp);
316+
int sip_contact_print(struct re_printf *pf,
317+
const struct sip_contact *contact);
318+
319+
306320
/* dialog */
307321
int sip_dialog_alloc(struct sip_dialog **dlgp,
308322
const char *uri, const char *to_uri,

src/sip/contact.c

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @file sip/contact.c SIP contact functions
3+
*
4+
* Copyright (C) 2010 Creytiv.com
5+
*/
6+
#include <string.h>
7+
#include <re_types.h>
8+
#include <re_fmt.h>
9+
#include <re_mbuf.h>
10+
#include <re_uri.h>
11+
#include <re_list.h>
12+
#include <re_sa.h>
13+
#include <re_msg.h>
14+
#include <re_sip.h>
15+
16+
17+
/**
18+
* Set contact parameters
19+
*
20+
* @param contact SIP Contact object
21+
* @param uri Username or URI
22+
* @param addr IP-address and port
23+
* @param tp SIP Transport
24+
*/
25+
void sip_contact_set(struct sip_contact *contact, const char *uri,
26+
const struct sa *addr, enum sip_transp tp)
27+
{
28+
if (!contact)
29+
return;
30+
31+
contact->uri = uri;
32+
contact->addr = addr;
33+
contact->tp = tp;
34+
}
35+
36+
37+
/**
38+
* Print contact header
39+
*
40+
* @param pf Print function
41+
* @param contact SIP Contact object
42+
*
43+
* @return 0 for success, otherwise errorcode
44+
*/
45+
int sip_contact_print(struct re_printf *pf, const struct sip_contact *contact)
46+
{
47+
if (!contact)
48+
return 0;
49+
50+
if (contact->uri && strchr(contact->uri, ':'))
51+
return re_hprintf(pf, "Contact: <%s>\r\n", contact->uri);
52+
else
53+
return re_hprintf(pf, "Contact: <sip:%s@%J%s>\r\n",
54+
contact->uri,
55+
contact->addr,
56+
sip_transp_param(contact->tp));
57+
}

src/sip/mod.mk

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
SRCS += sip/addr.c
88
SRCS += sip/auth.c
9+
SRCS += sip/contact.c
910
SRCS += sip/cseq.c
1011
SRCS += sip/ctrans.c
1112
SRCS += sip/dialog.c

src/sipevent/notify.c

+9-4
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,13 @@ static void response_handler(int err, const struct sip_msg *msg, void *arg)
177177
static int send_handler(enum sip_transp tp, const struct sa *src,
178178
const struct sa *dst, struct mbuf *mb, void *arg)
179179
{
180+
struct sip_contact contact;
180181
struct sipnot *not = arg;
181182
(void)dst;
182183

183-
return mbuf_printf(mb, "Contact: <sip:%s@%J%s>\r\n",
184-
not->cuser, src, sip_transp_param(tp));
184+
sip_contact_set(&contact, not->cuser, src, tp);
185+
186+
return mbuf_printf(mb, "%H", sip_contact_print, &contact);
185187
}
186188

187189

@@ -282,16 +284,19 @@ int sipnot_notify(struct sipnot *not)
282284
int sipnot_reply(struct sipnot *not, const struct sip_msg *msg,
283285
uint16_t scode, const char *reason)
284286
{
287+
struct sip_contact contact;
285288
uint32_t expires;
286289

287290
expires = (uint32_t)(tmr_get_expire(&not->tmr) / 1000);
288291

292+
sip_contact_set(&contact, not->cuser, &msg->dst, msg->tp);
293+
289294
return sip_treplyf(NULL, NULL, not->sip, msg, true, scode, reason,
290-
"Contact: <sip:%s@%J%s>\r\n"
295+
"%H"
291296
"Expires: %u\r\n"
292297
"Content-Length: 0\r\n"
293298
"\r\n",
294-
not->cuser, &msg->dst, sip_transp_param(msg->tp),
299+
sip_contact_print, &contact,
295300
expires);
296301
}
297302

src/sipevent/subscribe.c

+8-6
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,13 @@ static void response_handler(int err, const struct sip_msg *msg, void *arg)
298298
static int send_handler(enum sip_transp tp, const struct sa *src,
299299
const struct sa *dst, struct mbuf *mb, void *arg)
300300
{
301+
struct sip_contact contact;
301302
struct sipsub *sub = arg;
302303
(void)dst;
303304

304-
return mbuf_printf(mb, "Contact: <sip:%s@%J%s>\r\n",
305-
sub->cuser, src, sip_transp_param(tp));
305+
sip_contact_set(&contact, sub->cuser, src, tp);
306+
307+
return mbuf_printf(mb, "%H", sip_contact_print, &contact);
306308
}
307309

308310

@@ -449,7 +451,7 @@ static int sipsub_alloc(struct sipsub **subp, struct sipevent_sock *sock,
449451
* @param event SIP Event to subscribe to
450452
* @param id SIP Event ID (optional)
451453
* @param expires Subscription expires value
452-
* @param cuser Contact username
454+
* @param cuser Contact username or URI
453455
* @param routev Optional route vector
454456
* @param routec Number of routes
455457
* @param authh Authentication handler
@@ -496,7 +498,7 @@ int sipevent_subscribe(struct sipsub **subp, struct sipevent_sock *sock,
496498
* @param event SIP Event to subscribe to
497499
* @param id SIP Event ID (optional)
498500
* @param expires Subscription expires value
499-
* @param cuser Contact username
501+
* @param cuser Contact username or URI
500502
* @param authh Authentication handler
501503
* @param aarg Authentication handler argument
502504
* @param aref True to ref argument
@@ -536,7 +538,7 @@ int sipevent_dsubscribe(struct sipsub **subp, struct sipevent_sock *sock,
536538
* @param uri SIP Request URI
537539
* @param from_name SIP From-header Name (optional)
538540
* @param from_uri SIP From-header URI
539-
* @param cuser Contact username
541+
* @param cuser Contact username or URI
540542
* @param routev Optional route vector
541543
* @param routec Number of routes
542544
* @param authh Authentication handler
@@ -579,7 +581,7 @@ int sipevent_refer(struct sipsub **subp, struct sipevent_sock *sock,
579581
* @param subp Pointer to allocated SIP subscriber client
580582
* @param sock SIP Event socket
581583
* @param dlg Established SIP Dialog
582-
* @param cuser Contact username
584+
* @param cuser Contact username or URI
583585
* @param authh Authentication handler
584586
* @param aarg Authentication handler argument
585587
* @param aref True to ref argument

src/sipreg/reg.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ static int request(struct sipreg *reg, bool reset_ls)
293293
"Content-Length: 0\r\n"
294294
"\r\n",
295295
reg->regid > 0
296-
? "Supported: outbound, path\r\n" : "",
296+
? "Supported: gruu, outbound, path\r\n" : "",
297297
reg->hdrs ? mbuf_buf(reg->hdrs) : NULL,
298298
reg->hdrs ? mbuf_get_left(reg->hdrs) : (size_t)0);
299299
}

src/sipsess/accept.c

+14-8
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static void cancel_handler(void *arg)
4242
* @param msg Incoming SIP message
4343
* @param scode Response status code
4444
* @param reason Response reason phrase
45-
* @param cuser Contact username
45+
* @param cuser Contact username or URI
4646
* @param ctype Session content-type
4747
* @param desc Content description (e.g. SDP)
4848
* @param authh SIP Authentication handler
@@ -103,24 +103,28 @@ int sipsess_accept(struct sipsess **sessp, struct sipsess_sock *sock,
103103
if (scode >= 200)
104104
err = sipsess_reply_2xx(sess, msg, scode, reason, desc,
105105
fmt, &ap);
106-
else
106+
else {
107+
struct sip_contact contact;
108+
109+
sip_contact_set(&contact, sess->cuser, &msg->dst, msg->tp);
110+
107111
err = sip_treplyf(&sess->st, NULL, sess->sip,
108112
msg, true, scode, reason,
109-
"Contact: <sip:%s@%J%s>\r\n"
113+
"%H"
110114
"%v"
111115
"%s%s%s"
112116
"Content-Length: %zu\r\n"
113117
"\r\n"
114118
"%b",
115-
sess->cuser, &msg->dst,
116-
sip_transp_param(msg->tp),
119+
sip_contact_print, &contact,
117120
fmt, &ap,
118121
desc ? "Content-Type: " : "",
119122
desc ? sess->ctype : "",
120123
desc ? "\r\n" : "",
121124
desc ? mbuf_get_left(desc) : (size_t)0,
122125
desc ? mbuf_buf(desc) : NULL,
123126
desc ? mbuf_get_left(desc) : (size_t)0);
127+
}
124128

125129
va_end(ap);
126130

@@ -151,6 +155,7 @@ int sipsess_accept(struct sipsess **sessp, struct sipsess_sock *sock,
151155
int sipsess_progress(struct sipsess *sess, uint16_t scode, const char *reason,
152156
struct mbuf *desc, const char *fmt, ...)
153157
{
158+
struct sip_contact contact;
154159
va_list ap;
155160
int err;
156161

@@ -159,16 +164,17 @@ int sipsess_progress(struct sipsess *sess, uint16_t scode, const char *reason,
159164

160165
va_start(ap, fmt);
161166

167+
sip_contact_set(&contact, sess->cuser, &sess->msg->dst, sess->msg->tp);
168+
162169
err = sip_treplyf(&sess->st, NULL, sess->sip, sess->msg, true,
163170
scode, reason,
164-
"Contact: <sip:%s@%J%s>\r\n"
171+
"%H"
165172
"%v"
166173
"%s%s%s"
167174
"Content-Length: %zu\r\n"
168175
"\r\n"
169176
"%b",
170-
sess->cuser, &sess->msg->dst,
171-
sip_transp_param(sess->msg->tp),
177+
sip_contact_print, &contact,
172178
fmt, &ap,
173179
desc ? "Content-Type: " : "",
174180
desc ? sess->ctype : "",

src/sipsess/connect.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ static int invite(struct sipsess *sess);
2525
static int send_handler(enum sip_transp tp, const struct sa *src,
2626
const struct sa *dst, struct mbuf *mb, void *arg)
2727
{
28+
struct sip_contact contact;
2829
struct sipsess *sess = arg;
2930
(void)dst;
3031

31-
return mbuf_printf(mb, "Contact: <sip:%s@%J%s>\r\n",
32-
sess->cuser, src, sip_transp_param(tp));
32+
sip_contact_set(&contact, sess->cuser, src, tp);
33+
34+
return mbuf_printf(mb, "%H", sip_contact_print, &contact);
3335
}
3436

3537

@@ -156,7 +158,7 @@ static int invite(struct sipsess *sess)
156158
* @param to_uri To SIP uri
157159
* @param from_name From display name
158160
* @param from_uri From SIP uri
159-
* @param cuser Contact username
161+
* @param cuser Contact username or URI
160162
* @param routev Outbound route vector
161163
* @param routec Outbound route vector count
162164
* @param ctype Session content-type

src/sipsess/modify.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,13 @@ static void reinvite_resp_handler(int err, const struct sip_msg *msg,
109109
static int send_handler(enum sip_transp tp, const struct sa *src,
110110
const struct sa *dst, struct mbuf *mb, void *arg)
111111
{
112+
struct sip_contact contact;
112113
struct sipsess *sess = arg;
113114
(void)dst;
114115

115-
return mbuf_printf(mb, "Contact: <sip:%s@%J%s>\r\n",
116-
sess->cuser, src, sip_transp_param(tp));
116+
sip_contact_set(&contact, sess->cuser, src, tp);
117+
118+
return mbuf_printf(mb, "%H", sip_contact_print, &contact);
117119
}
118120

119121

src/sipsess/reply.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ int sipsess_reply_2xx(struct sipsess *sess, const struct sip_msg *msg,
8282
const char *fmt, va_list *ap)
8383
{
8484
struct sipsess_reply *reply;
85+
struct sip_contact contact;
8586
int err = ENOMEM;
8687

8788
reply = mem_zalloc(sizeof(*reply), destructor);
@@ -93,15 +94,17 @@ int sipsess_reply_2xx(struct sipsess *sess, const struct sip_msg *msg,
9394
reply->msg = mem_ref((void *)msg);
9495
reply->sess = sess;
9596

97+
sip_contact_set(&contact, sess->cuser, &msg->dst, msg->tp);
98+
9699
err = sip_treplyf(&sess->st, &reply->mb, sess->sip,
97100
msg, true, scode, reason,
98-
"Contact: <sip:%s@%J%s>\r\n"
101+
"%H"
99102
"%v"
100103
"%s%s%s"
101104
"Content-Length: %zu\r\n"
102105
"\r\n"
103106
"%b",
104-
sess->cuser, &msg->dst, sip_transp_param(msg->tp),
107+
sip_contact_print, &contact,
105108
fmt, ap,
106109
desc ? "Content-Type: " : "",
107110
desc ? sess->ctype : "",

0 commit comments

Comments
 (0)