aboutsummaryrefslogtreecommitdiff
path: root/sip.c
blob: d237dd8358d52bdf60527d1cea3fe297a78d6e2e (plain)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <mp.h>
#include <libsec.h>
#include <draw.h>
#include "dat.h"
#include "fns.h"

static char sipversion[] = "SIP/2.0";
static char useragent[] = "catphone (plan9front)";
static char *methodstrtab[] = {
 [REGISTER]	"REGISTER",
 [INVITE]	"INVITE",
 [ACK]		"ACK",
 [BYE]		"BYE",
 [CANCEL]	"CANCEL",
 [OPTIONS]	"OPTIONS",
 [NOTIFY]	"NOTIFY",
 [SUBSCRIBE]	"SUBSCRIBE",
 [INFO]		"INFO",
 [MESSAGE]	"MESSAGE",
 [UPDATE]	"UPDATE",
 [REFER]	"REFER",
};


static char *
getmethodstr(SipMethod m)
{
	return methodstrtab[m];
}

static char *
md5authfn(char *user, char *pass, char *uri, Sipmsg* m)
{
	uchar h1d[MD5dlen], h2d[MD5dlen], rd[MD5dlen];
	char h1ds[2*MD5dlen+1], h2ds[2*MD5dlen+1];
	static char rds[2*MD5dlen+1];
	char buf[4096];

	snprint(buf, sizeof buf, "%s:%s:%s", user, m->auth.realm, pass);
	md5((uchar*)buf, strlen(buf), h1d, nil);
	snprint(buf, sizeof buf, "%s:%s", getmethodstr(m->method), uri);
	md5((uchar*)buf, strlen(buf), h2d, nil);

	snprint(h1ds, sizeof h1ds, "%.*lH", sizeof h1d, h1d);
	snprint(h2ds, sizeof h2ds, "%.*lH", sizeof h2d, h2d);

	snprint(buf, sizeof buf, "%s:%s:%s", h1ds, m->auth.nonce, h2ds);
	md5((uchar*)buf, strlen(buf), rd, nil);
	snprint(rds, sizeof rds, "%.*lH", sizeof rd, rd);

	return rds;
}

static struct {
	char *name;
	char *(*fn)(char*, char*, char*, Sipmsg*);
} algos[] = {
 [AMD5]	{ .name = "MD5", .fn = md5authfn },
};

static uint
hash(char *s)
{
	uint h;

	h = 0x811c9dc5;
	while(*s != 0)
		h = (h^(uchar)*s++) * 0x1000193;
	return h % 13;
}

/* rfc3261 ยง 10 - Registrations */
static int
sip_register(Sip *s, char *user, char *pass)
{
	Sipmsg *req, *res;
	Hdr *h;
	Biobuf *bin, *bout;
	char *line, *p, *kv[8], *kv2[2], buf[1024];
	int n;

	if((bin = Bfdopen(s->fd, OREAD)) == nil)
		sysfatal("Bfdopen: %r");
	if((bout = Bfdopen(s->fd, OWRITE)) == nil)
		sysfatal("Bfdopen: %r");

	/* present yourself */
	req = newsipmsg();
	req->method = REGISTER;
	req->uri = smprint("sip:%s", s->nci->rsys);
	req->version = sipversion;
	snprint(buf, sizeof buf, "%s/UDP %s:%s;branch=z9hG4bK703d971c0c737b8e;rport",
		sipversion, s->nci->lsys, s->nci->lserv);
	addheader(req, "Via", buf);
	snprint(buf, sizeof buf, "<sip:%s-0x82a66a010@%s:%s>;expires=3849",
		user, s->nci->lsys, s->nci->lserv);
	addheader(req, "Contact", buf);
	addheader(req, "Max-Forwards", "70");
	snprint(buf, sizeof buf, "<sip:%s@%s>",
		user, s->nci->rsys);
	addheader(req, "To", buf);
	snprint(buf, sizeof buf, "<sip:%s@%s>;tag=4a5a693256d38cbc",
		user, s->nci->rsys);
	addheader(req, "From", buf);
	addheader(req, "Call-ID", "2cee372fc4be4e45");
	addheader(req, "CSeq", "16021 REGISTER");
	addheader(req, "User-Agent", useragent);
	addheader(req, "Allow", "INVITE,ACK,BYE,CANCEL,OPTIONS,NOTIFY,SUBSCRIBE,INFO,MESSAGE,UPDATE,REFER");
	snprint(buf, sizeof buf, "%lud", req->len);
	addheader(req, "Content-Length", buf);

	Bprint(bout, "%S", req);
	Bflush(bout);

	if(debug)
		fprint(2, "sent:\n%S\n", req);

	delsipmsg(req);

	/* wait for the challenge */
	res = newsipmsg();
	while((line = Brdline(bin, '\n')) != nil){
		if(strncmp(line, "\r\n", 2) == 0)
			break;

		p = strchr(line, '\r');
		*p++ = 0, *p = 0;

		if(strstr(line, ":") == nil && req->code == 0){
			if(gettokens(line, kv, 3, " ") == 3){
				res->version = strdup(kv[0]);
				res->code = strtoul(kv[1], nil, 10);
				res->reason = strdup(kv[2]);
			}
			continue;
		}

		if(gettokens(line, kv, 2, ": ") == 2)
			addheader(res, kv[0], kv[1]);
	}

	if(debug)
		fprint(2, "rcvd:\n%S\n", res);

	/* respond to the challenge */
	if((h = getheader(res, "WWW-Authenticate")) == nil)
		return -1;

	if((n = gettokens(h->value, kv, nelem(kv), ", ")) == 0)
		return -1;

	while(n-- > 0){
		if(gettokens(kv[n], kv2, 2, "=\"") != 2)
			continue;

		/* XXX: this hack should be replaced by a better method */
		p = strchr(kv2[1], '"');
		if(p != nil)
			*p = 0;

		if(strcmp(kv2[0], "algorithm") == 0)
			req->auth.algo = strdup(kv2[1]);
		else if(strcmp(kv2[0], "realm") == 0)
			req->auth.realm = strdup(kv2[1]);
		else if(strcmp(kv2[0], "nonce") == 0)
			req->auth.nonce = strdup(kv2[1]);
	}
	if(strcmp(res->auth.algo, "MD5") == 0){
		snprint(buf, sizeof buf, "sip:%s", s->nci->rsys);
		res->auth.response = algos[AMD5].fn(user, pass, buf, res);
		if(res->auth.response == nil)
			return -1;
	}else
		return -1;

	req = newsipmsg();
	req->method = REGISTER;
	req->uri = strdup(buf);
	req->version = sipversion;
	snprint(buf, sizeof buf, "%s/UDP %s:%s;branch=z9hG4bK703d971c0c737b8e;rport",
		sipversion, s->nci->lsys, s->nci->lserv);
	addheader(req, "Via", buf);
	snprint(buf, sizeof buf, "<sip:%s-0x82a66a010@%s:%s>;expires=3849",
		user, s->nci->lsys, s->nci->lserv);
	addheader(req, "Contact", buf);
	snprint(buf, sizeof buf, "Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\", algorithm=\"%s\"",
		user, res->auth.realm, res->auth.nonce, req->uri, res->auth.response, res->auth.algo);
	addheader(req, "Authorization", buf);
	addheader(req, "Max-Forwards", "70");
	snprint(buf, sizeof buf, "<sip:%s@%s>",
		user, s->nci->rsys);
	addheader(req, "To", buf);
	snprint(buf, sizeof buf, "<sip:%s@%s>;tag=4a5a693256d38cbc",
		user, s->nci->rsys);
	addheader(req, "From", buf);
	addheader(req, "Call-ID", "2cee372fc4be4e45");
	addheader(req, "CSeq", "16022 REGISTER");
	addheader(req, "User-Agent", useragent);
	addheader(req, "Allow", "INVITE,ACK,BYE,CANCEL,OPTIONS,NOTIFY,SUBSCRIBE,INFO,MESSAGE,UPDATE,REFER");
	snprint(buf, sizeof buf, "%lud", req->len);
	addheader(req, "Content-Length", buf);

	Bprint(bout, "%S", req);
	Bflush(bout);

	if(debug)
		fprint(2, "sent:\n%S\n", req);

	delsipmsg(res);
	delsipmsg(req);

	/* get the OK */
	res = newsipmsg();
	while((line = Brdline(bin, '\n')) != nil){
		if(strncmp(line, "\r\n", 2) == 0)
			break;

		p = strchr(line, '\r');
		*p++ = 0, *p = 0;

		if(strstr(line, ":") == nil && req->code == 0){
			if(gettokens(line, kv, 3, " ") == 3){
				res->version = strdup(kv[0]);
				res->code = strtoul(kv[1], nil, 10);
				res->reason = strdup(kv[2]);
			}
			continue;
		}

		if(gettokens(line, kv, 2, ": ") == 2)
			addheader(res, kv[0], kv[1]);
	}

	if(debug)
		fprint(2, "rcvd:\n%S\n", res);

	Bterm(bin);
	Bterm(bout);

	return 0;
}

int
Sfmt(Fmt *f)
{
	Sipmsg *m;
	Hdr *h;
	int i, n;

	m = va_arg(f->args, Sipmsg*);
	n = 0;

	if(m->code == 0){ /* request */
		n += fmtprint(f, "%s %s %s\r\n",
			getmethodstr(m->method), m->uri, m->version);
	}else{ /* response */
		n += fmtprint(f, "%s %d %s\r\n",
			m->version, m->code, m->reason);
	}

	for(i = 0; i < nelem(m->headers); i++)
		for(h = m->headers[i]; h != nil; h = h->next)
			n += fmtprint(f, "%s: %s\r\n", h->name, h->value);
	n += fmtprint(f, "\r\n");

	if(m->len > 0){
		fmtprint(f, "%.*s", (int)m->len, m->body);
		n += m->len;
	}

	return n;
}

void
SIPfmtinstall(void)
{
	fmtinstall('S', Sfmt);
}

void
addheader(Hdrtab *ht, char *name, char *value)
{
	Hdr *h, *newh;
	uint key;

	key = hash(name);
	newh = emalloc(sizeof(Hdr));
	newh->name = strdup(name);
	newh->value = strdup(value);
	newh->next = nil;

	h = ht->headers[key];
	if(h == nil){
		ht->headers[key] = newh;
		return;
	}
	while(h->next != nil)
		h = h->next;
	h->next = newh;
}

Hdr *
getheader(Hdrtab *ht, char *name)
{
	Hdr *h;
	uint key;

	key = hash(name);
	for(h = ht->headers[key]; h != nil; h = h->next)
		if(cistrcmp(h->name, name) == 0)
			return h;
	return nil;
}

void
delheader(Hdrtab *ht, char *name)
{
	Hdr *h, *nh;
	uint key;

	key = hash(name);
	h = ht->headers[key];
	while(h != nil){
		nh = h->next;
		if(cistrcmp(h->name, name) == 0){
			free(h->name);
			free(h->value);
			free(h);
		}
		h = nh;
	}
}

void
delheaders(Hdrtab *ht)
{
	Hdr *h, *nh;
	int i;

	for(i = 0; i < nelem(ht->headers); i++)
		for(h = ht->headers[i]; h != nil; h = nh){
			nh = h->next;
			free(h->name);
			free(h->value);
			free(h);
		}
}

Sipmsg *
newsipmsg(void)
{
	Sipmsg *m;

	m = emalloc(sizeof(Sipmsg));
	memset(m, 0, sizeof *m);

	return m;
}

void
delsipmsg(Sipmsg *m)
{
	if(m->uri != nil)
		free(m->uri);
	if(m->reason != nil)
		free(m->reason);
	delheaders(m);
	free(m);
}

Sip *
mksip(int fd)
{
	Sip *s;

	s = emalloc(sizeof(Sip));
	memset(s, 0, sizeof *s);
	s->version = 2;
	s->nci = getnetconninfo(nil, fd);
	if(s->nci == nil){
		werrstr("couldn't getnetconninfo");
		free(s);
		return nil;
	}
	s->fd = fd;
	s->reg = sip_register;

	return s;
}

void
rmsip(Sip *s)
{
	close(s->fd);
	freenetconninfo(s->nci);
	free(s);
}