aboutsummaryrefslogtreecommitdiffstats
path: root/channels/sip/config_parser.c
blob: 0ab9ed7693f704fd29f7a7c9b6252fba56dabee1 (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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2010, Digium, Inc.
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*!
 * \file
 * \brief sip config parsing functions and unit tests
 */

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

#include "include/sip.h"
#include "include/config_parser.h"
#include "include/sip_utils.h"

/*! \brief Parse register=> line in sip.conf
 *
 * \retval 0 on success
 * \retval -1 on failure
 */
int sip_parse_register_line(struct sip_registry *reg, int default_expiry, const char *value, int lineno)
{
	int portnum = 0;
	int domainport = 0;
	enum sip_transport transport = SIP_TRANSPORT_UDP;
	char buf[256] = "";
	char *userpart = NULL, *hostpart = NULL;
	/* register => [peer?][transport://]user[@domain][:secret[:authuser]]@host[:port][/extension][~expiry] */
	AST_DECLARE_APP_ARGS(pre1,
		AST_APP_ARG(peer);
		AST_APP_ARG(userpart);
	);
	AST_DECLARE_APP_ARGS(pre2,
		AST_APP_ARG(transport);
		AST_APP_ARG(blank);
		AST_APP_ARG(userpart);
	);
	AST_DECLARE_APP_ARGS(user1,
		AST_APP_ARG(userpart);
		AST_APP_ARG(secret);
		AST_APP_ARG(authuser);
	);
	AST_DECLARE_APP_ARGS(user2,
		AST_APP_ARG(user);
		AST_APP_ARG(domain);
	);
	AST_DECLARE_APP_ARGS(user3,
		AST_APP_ARG(authuser);
		AST_APP_ARG(domainport);
	);
	AST_DECLARE_APP_ARGS(host1,
		AST_APP_ARG(hostpart);
		AST_APP_ARG(expiry);
	);
	AST_DECLARE_APP_ARGS(host2,
		AST_APP_ARG(hostpart);
		AST_APP_ARG(extension);
	);
	AST_DECLARE_APP_ARGS(host3,
		AST_APP_ARG(host);
		AST_APP_ARG(port);
	);

	if (!value) {
		return -1;
	}

	if (!reg) {
		return -1;
	}
	ast_copy_string(buf, value, sizeof(buf));

	/*! register => [peer?][transport://]user[@domain][:secret[:authuser]]@host[:port][/extension][~expiry]
	 * becomes
	 *   userpart => [peer?][transport://]user[@domain][:secret[:authuser]]
	 *   hostpart => host[:port][/extension][~expiry]
	 */
	if ((hostpart = strrchr(buf, '@'))) {
		*hostpart++ = '\0';
		userpart = buf;
	}

	if (ast_strlen_zero(userpart) || ast_strlen_zero(hostpart)) {
		ast_log(LOG_WARNING, "Format for registration is [peer?][transport://]user[@domain][:secret[:authuser]]@host[:port][/extension][~expiry] at line %d\n", lineno);
		return -1;
	}

	/*!
	 * pre1.peer => peer
	 * pre1.userpart => [transport://]user[@domain][:secret[:authuser]]
	 * hostpart => host[:port][/extension][~expiry]
	 */
	AST_NONSTANDARD_RAW_ARGS(pre1, userpart, '?');
	if (ast_strlen_zero(pre1.userpart)) {
		pre1.userpart = pre1.peer;
		pre1.peer = NULL;
	}

	/*!
	 * pre1.peer => peer
	 * pre2.transport = transport
	 * pre2.userpart => user[@domain][:secret[:authuser]]
	 * hostpart => host[:port][/extension][~expiry]
	 */
	AST_NONSTANDARD_RAW_ARGS(pre2, pre1.userpart, '/');
	if (ast_strlen_zero(pre2.userpart)) {
		pre2.userpart = pre2.transport;
		pre2.transport = NULL;
	} else {
		pre2.transport[strlen(pre2.transport) - 1] = '\0'; /* Remove trailing : */
	}

	if (!ast_strlen_zero(pre2.blank)) {
		ast_log(LOG_WARNING, "Format for registration is [peer?][transport://]user[@domain][:secret[:authuser]]@host[:port][/extension][~expiry] at line %d\n", lineno);
		return -1;
	}

	/*!
	 * pre1.peer => peer
	 * pre2.transport = transport
	 * user1.userpart => user[@domain]
	 * user1.secret => secret
	 * user1.authuser => authuser
	 * hostpart => host[:port][/extension][~expiry]
	 */
	AST_NONSTANDARD_RAW_ARGS(user1, pre2.userpart, ':');

	/*!
	 * pre1.peer => peer
	 * pre2.transport = transport
	 * user1.userpart => user[@domain]
	 * user1.secret => secret
	 * user1.authuser => authuser
	 * host1.hostpart => host[:port][/extension]
	 * host1.expiry => [expiry]
	 */
	AST_NONSTANDARD_RAW_ARGS(host1, hostpart, '~');

	/*!
	 * pre1.peer => peer
	 * pre2.transport = transport
	 * user1.userpart => user[@domain]
	 * user1.secret => secret
	 * user1.authuser => authuser
	 * host2.hostpart => host[:port]
	 * host2.extension => [extension]
	 * host1.expiry => [expiry]
	 */
	AST_NONSTANDARD_RAW_ARGS(host2, host1.hostpart, '/');

	/*!
	 * pre1.peer => peer
	 * pre2.transport = transport
	 * user1.userpart => user[@domain]
	 * user1.secret => secret
	 * user1.authuser => authuser
	 * host3.host => host
	 * host3.port => port
	 * host2.extension => extension
	 * host1.expiry => expiry
	 */
	AST_NONSTANDARD_RAW_ARGS(host3, host2.hostpart, ':');

	/*!
	  * pre1.peer => peer
	  * pre2.transport = transport
	  * user2.user => user
	  * user2.domain => domain
	  * user1.secret => secret
	  * user1.authuser => authuser
	  * host3.host => host
	  * host3.port => port
	  * host2.extension => extension
	  * host1.expiry => expiry
	 */
	AST_NONSTANDARD_RAW_ARGS(user2, user1.userpart, '@');

	/*!
	  * pre1.peer => peer
	  * pre2.transport = transport
	  * user2.user => user
	  * user2.domain => domain
	  * user1.secret => secret
	  * user3.authuser => authuser
	  * user3.domainport => domainport
	  * host3.host => host
	  * host3.port => port
	  * host2.extension => extension
	  * host1.expiry => expiry
	 */
	AST_NONSTANDARD_RAW_ARGS(user3, user1.authuser, ':');

	/* Reordering needed due to fields being [(:secret[:username])|(:regdomainport:secret:username)]
	   but parsing being [secret[:username[:regdomainport]]] */
	if (user3.argc == 2) {
		char *reorder = user3.domainport;
		user3.domainport = user1.secret;
		user1.secret = user3.authuser;
		user3.authuser = reorder;
	}

	if (host3.port) {
		if (!(portnum = port_str2int(host3.port, 0))) {
			ast_log(LOG_NOTICE, "'%s' is not a valid port number on line %d of sip.conf. using default.\n", host3.port, lineno);
		}
	}
	if (user3.domainport) {
		if (!(domainport = port_str2int(user3.domainport, 0))) {
			ast_log(LOG_NOTICE, "'%s' is not a valid domain port number on line %d of sip.conf. using default.\n", user3.domainport, lineno);
		}
	}

	/* set transport type */
	if (!pre2.transport) {
		transport = SIP_TRANSPORT_UDP;
	} else if (!strncasecmp(pre2.transport, "tcp", 3)) {
		transport = SIP_TRANSPORT_TCP;
	} else if (!strncasecmp(pre2.transport, "tls", 3)) {
		transport = SIP_TRANSPORT_TLS;
	} else if (!strncasecmp(pre2.transport, "udp", 3)) {
		transport = SIP_TRANSPORT_UDP;
	} else {
		transport = SIP_TRANSPORT_UDP;
		ast_log(LOG_NOTICE, "'%.3s' is not a valid transport type on line %d of sip.conf. defaulting to udp.\n", pre2.transport, lineno);
	}

	/* if no portnum specified, set default for transport */
	if (!portnum) {
		if (transport == SIP_TRANSPORT_TLS) {
			portnum = STANDARD_TLS_PORT;
		} else {
			portnum = STANDARD_SIP_PORT;
		}
	}

	/* copy into sip_registry object */
	ast_string_field_set(reg, callback, ast_strip_quoted(S_OR(host2.extension, "s"), "\"", "\""));
	ast_string_field_set(reg, username, ast_strip_quoted(S_OR(user2.user, ""), "\"", "\""));
	ast_string_field_set(reg, hostname, ast_strip_quoted(S_OR(host3.host, ""), "\"", "\""));
	ast_string_field_set(reg, authuser, ast_strip_quoted(S_OR(user3.authuser, ""), "\"", "\""));
	ast_string_field_set(reg, secret, ast_strip_quoted(S_OR(user1.secret, ""), "\"", "\""));
	ast_string_field_set(reg, peername, ast_strip_quoted(S_OR(pre1.peer, ""), "\"", "\""));
	ast_string_field_set(reg, regdomain, ast_strip_quoted(S_OR(user2.domain, ""), "\"", "\""));

	reg->transport = transport;
	reg->timeout = reg->expire = -1;
	reg->portno = portnum;
	reg->regdomainport = domainport;
	reg->callid_valid = FALSE;
	reg->ocseq = INITIAL_CSEQ;
	reg->refresh = reg->expiry = reg->configured_expiry = (host1.expiry ? atoi(ast_strip_quoted(host1.expiry, "\"", "\"")) : default_expiry);

	return 0;
}

AST_TEST_DEFINE(sip_parse_register_line_test)
{
	int res = AST_TEST_PASS;
	struct sip_registry *reg;
	int default_expiry = 120;
	const char *reg1 = "name@domain";
	const char *reg2 = "name:pass@domain";
	const char *reg3 = "name@namedomain:pass:authuser@domain";
	const char *reg4 = "name@namedomain:pass:authuser@domain/extension";
	const char *reg5 = "tcp://name@namedomain:pass:authuser@domain/extension";
	const char *reg6 = "tls://name@namedomain:pass:authuser@domain/extension~111";
	const char *reg7 = "peer?tcp://name@namedomain:pass:authuser@domain:1234/extension~111";
	const char *reg8 = "peer?name@namedomain:pass:authuser@domain:1234/extension~111";
	const char *reg9 = "peer?name:pass:authuser:1234/extension~111";
	const char *reg10 = "@domin:1234";
	const char *reg12 = "name@namedomain:4321:pass:authuser@domain";
	const char *reg13 = "name@namedomain:4321::@domain";

	switch (cmd) {
	case TEST_INIT:
		info->name = "sip_parse_register_line_test";
		info->category = "/channels/chan_sip/";
		info->summary = "tests sip register line parsing";
		info->description =
							"Tests parsing of various register line configurations. "
							"Verifies output matches expected behavior.";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	/* ---Test reg 1, simple config --- */
	if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
		goto alloc_fail;
	} else if (
	    sip_parse_register_line(reg, default_expiry, reg1, 1) ||
		strcmp(reg->callback, "s")           ||
		strcmp(reg->username, "name")       ||
		strcmp(reg->regdomain, "")          ||
		strcmp(reg->hostname, "domain")     ||
		strcmp(reg->authuser, "")           ||
		strcmp(reg->secret, "")             ||
		strcmp(reg->peername, "")           ||
		reg->transport != SIP_TRANSPORT_UDP ||
		reg->timeout != -1                  ||
		reg->expire != -1                   ||
		reg->refresh != default_expiry ||
		reg->expiry != default_expiry ||
		reg->configured_expiry != default_expiry ||
		reg->portno != STANDARD_SIP_PORT    ||
		(reg->regdomainport)                ||
		reg->callid_valid != FALSE          ||
		reg->ocseq != INITIAL_CSEQ) {

		ast_test_status_update(test, "Test 1: simple config failed\n");
		res = AST_TEST_FAIL;
	}
	ast_string_field_free_memory(reg);
	ast_free(reg);

	/* ---Test reg 2, add secret --- */
	if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
		goto alloc_fail;
	} else if (
	    sip_parse_register_line(reg, default_expiry, reg2, 1) ||
		strcmp(reg->callback, "s")           ||
		strcmp(reg->username, "name")       ||
		strcmp(reg->regdomain, "")          ||
		strcmp(reg->hostname, "domain")     ||
		strcmp(reg->authuser, "")           ||
		strcmp(reg->secret, "pass")         ||
		strcmp(reg->peername, "")           ||
		reg->transport != SIP_TRANSPORT_UDP ||
		reg->timeout != -1                  ||
		reg->expire != -1                   ||
		reg->refresh != default_expiry ||
		reg->expiry != default_expiry ||
		reg->configured_expiry != default_expiry ||
		reg->portno != STANDARD_SIP_PORT    ||
		(reg->regdomainport)                ||
		reg->callid_valid != FALSE          ||
		reg->ocseq != INITIAL_CSEQ) {

		ast_test_status_update(test,  "Test 2: add secret failed\n");
		res = AST_TEST_FAIL;
	}
	ast_string_field_free_memory(reg);
	ast_free(reg);

	/* ---Test reg 3, add userdomain and authuser --- */
	if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
		goto alloc_fail;
	} else if (
	    sip_parse_register_line(reg, default_expiry, reg3, 1) ||
		strcmp(reg->callback, "s")           ||
		strcmp(reg->username, "name") ||
		strcmp(reg->regdomain, "namedomain") ||
		strcmp(reg->hostname, "domain")     ||
		strcmp(reg->authuser, "authuser")           ||
		strcmp(reg->secret, "pass")         ||
		strcmp(reg->peername, "")           ||
		reg->transport != SIP_TRANSPORT_UDP ||
		reg->timeout != -1                  ||
		reg->expire != -1                   ||
		reg->refresh != default_expiry ||
		reg->expiry != default_expiry ||
		reg->configured_expiry != default_expiry ||
		reg->portno != STANDARD_SIP_PORT    ||
		(reg->regdomainport)                ||
		reg->callid_valid != FALSE          ||
		reg->ocseq != INITIAL_CSEQ) {

		ast_test_status_update(test, "Test 3: add userdomain and authuser failed\n");
		res = AST_TEST_FAIL;
	}
	ast_string_field_free_memory(reg);
	ast_free(reg);

	/* ---Test reg 4, add callback extension --- */
	if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
		goto alloc_fail;
	} else if (
	    sip_parse_register_line(reg, default_expiry, reg4, 1) ||
		strcmp(reg->callback, "extension")           ||
		strcmp(reg->username, "name") ||
		strcmp(reg->regdomain, "namedomain") ||
		strcmp(reg->hostname, "domain")     ||
		strcmp(reg->authuser, "authuser")           ||
		strcmp(reg->secret, "pass")         ||
		strcmp(reg->peername, "")           ||
		reg->transport != SIP_TRANSPORT_UDP ||
		reg->timeout != -1                  ||
		reg->expire != -1                   ||
		reg->refresh != default_expiry ||
		reg->expiry != default_expiry ||
		reg->configured_expiry != default_expiry ||
		reg->portno != STANDARD_SIP_PORT    ||
		(reg->regdomainport)                ||
		reg->callid_valid != FALSE          ||
		reg->ocseq != INITIAL_CSEQ) {

		ast_test_status_update(test, "Test 4: add callback extension failed\n");
		res = AST_TEST_FAIL;
	}
	ast_string_field_free_memory(reg);
	ast_free(reg);

	/* ---Test reg 5, add transport --- */
	if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
		goto alloc_fail;
	} else if (
	    sip_parse_register_line(reg, default_expiry, reg5, 1) ||
		strcmp(reg->callback, "extension")           ||
		strcmp(reg->username, "name") ||
		strcmp(reg->regdomain, "namedomain") ||
		strcmp(reg->hostname, "domain")     ||
		strcmp(reg->authuser, "authuser")           ||
		strcmp(reg->secret, "pass")         ||
		strcmp(reg->peername, "")           ||
		reg->transport != SIP_TRANSPORT_TCP ||
		reg->timeout != -1                  ||
		reg->expire != -1                   ||
		reg->refresh != default_expiry ||
		reg->expiry != default_expiry ||
		reg->configured_expiry != default_expiry ||
		reg->portno != STANDARD_SIP_PORT    ||
		(reg->regdomainport)                ||
		reg->callid_valid != FALSE          ||
		reg->ocseq != INITIAL_CSEQ) {

		ast_test_status_update(test, "Test 5: add transport failed\n");
		res = AST_TEST_FAIL;
	}
	ast_string_field_free_memory(reg);
	ast_free(reg);

	/* ---Test reg 6, change to tls transport, add expiry  --- */
	if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
		goto alloc_fail;
	} else if (
	    sip_parse_register_line(reg, default_expiry, reg6, 1) ||
		strcmp(reg->callback, "extension")           ||
		strcmp(reg->username, "name") ||
		strcmp(reg->regdomain, "namedomain") ||
		strcmp(reg->hostname, "domain")     ||
		strcmp(reg->authuser, "authuser")           ||
		strcmp(reg->secret, "pass")         ||
		strcmp(reg->peername, "")           ||
		reg->transport != SIP_TRANSPORT_TLS ||
		reg->timeout != -1                  ||
		reg->expire != -1                   ||
		reg->refresh != 111 ||
		reg->expiry != 111 ||
		reg->configured_expiry != 111 ||
		reg->portno != STANDARD_TLS_PORT    ||
		(reg->regdomainport)                ||
		reg->callid_valid != FALSE          ||
		reg->ocseq != INITIAL_CSEQ) {

		ast_test_status_update(test, "Test 6: change to tls transport and add expiry failed\n");
		res = AST_TEST_FAIL;
	}
	ast_string_field_free_memory(reg);
	ast_free(reg);

	/* ---Test reg 7, change transport to tcp, add custom port, and add peer --- */
	if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
		goto alloc_fail;
	} else if (
	    sip_parse_register_line(reg, default_expiry, reg7, 1) ||
		strcmp(reg->callback, "extension")           ||
		strcmp(reg->username, "name") ||
		strcmp(reg->regdomain, "namedomain") ||
		strcmp(reg->hostname, "domain")     ||
		strcmp(reg->authuser, "authuser")           ||
		strcmp(reg->secret, "pass")         ||
		strcmp(reg->peername, "peer")           ||
		reg->transport != SIP_TRANSPORT_TCP ||
		reg->timeout != -1                  ||
		reg->expire != -1                   ||
		reg->refresh != 111 ||
		reg->expiry != 111 ||
		reg->configured_expiry != 111 ||
		reg->portno != 1234    ||
		(reg->regdomainport)                ||
		reg->callid_valid != FALSE          ||
		reg->ocseq != INITIAL_CSEQ) {

		ast_test_status_update(test, "Test 7, change transport to tcp, add custom port, and add peer failed.\n");
		res = AST_TEST_FAIL;
	}
	ast_string_field_free_memory(reg);
	ast_free(reg);

	/* ---Test reg 8, remove transport --- */
	if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
		goto alloc_fail;
	} else if (
	    sip_parse_register_line(reg, default_expiry, reg8, 1) ||
		strcmp(reg->callback, "extension")           ||
		strcmp(reg->username, "name") ||
		strcmp(reg->regdomain, "namedomain") ||
		strcmp(reg->hostname, "domain")     ||
		strcmp(reg->authuser, "authuser")           ||
		strcmp(reg->secret, "pass")         ||
		strcmp(reg->peername, "peer")           ||
		reg->transport != SIP_TRANSPORT_UDP ||
		reg->timeout != -1                  ||
		reg->expire != -1                   ||
		reg->refresh != 111 ||
		reg->expiry != 111 ||
		reg->configured_expiry != 111 ||
		reg->portno != 1234    ||
		(reg->regdomainport)                ||
		reg->callid_valid != FALSE          ||
		reg->ocseq != INITIAL_CSEQ) {

		ast_test_status_update(test, "Test 8, remove transport failed.\n");
		res = AST_TEST_FAIL;
	}
	ast_string_field_free_memory(reg);
	ast_free(reg);

	/* ---Test reg12, add domain port --- */
	if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
		goto alloc_fail;
	} else if (
	   sip_parse_register_line(reg, default_expiry, reg12, 1) ||
		strcmp(reg->callback, "s")           ||
		strcmp(reg->username, "name") ||
		strcmp(reg->regdomain, "namedomain") ||
		strcmp(reg->hostname, "domain")     ||
		strcmp(reg->authuser, "authuser")           ||
		strcmp(reg->secret, "pass")         ||
		strcmp(reg->peername, "")           ||
		reg->transport != SIP_TRANSPORT_UDP ||
		reg->timeout != -1                  ||
		reg->expire != -1                   ||
		reg->refresh != default_expiry ||
		reg->expiry != default_expiry ||
		reg->configured_expiry != default_expiry ||
		reg->portno != STANDARD_SIP_PORT    ||
		reg->regdomainport != 4321          ||
		reg->callid_valid != FALSE          ||
		reg->ocseq != INITIAL_CSEQ) {

		ast_test_status_update(test, "Test 12, add domain port failed.\n");
		res = AST_TEST_FAIL;
	}

	/* ---Test reg13, domain port without secret --- */
	if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
		goto alloc_fail;
	} else if (
	   sip_parse_register_line(reg, default_expiry, reg13, 1) ||
		strcmp(reg->callback, "s")           ||
		strcmp(reg->username, "name") ||
		strcmp(reg->regdomain, "namedomain") ||
		strcmp(reg->hostname, "domain")     ||
		strcmp(reg->authuser, "")           ||
		strcmp(reg->secret, "")         ||
		strcmp(reg->peername, "")           ||
		reg->transport != SIP_TRANSPORT_UDP ||
		reg->timeout != -1                  ||
		reg->expire != -1                   ||
		reg->refresh != default_expiry ||
		reg->expiry != default_expiry ||
		reg->configured_expiry != default_expiry ||
		reg->portno != STANDARD_SIP_PORT    ||
		reg->regdomainport != 4321          ||
		reg->callid_valid != FALSE          ||
		reg->ocseq != INITIAL_CSEQ) {

		ast_test_status_update(test, "Test 13, domain port without secret failed.\n");
		res = AST_TEST_FAIL;
}

	/* ---Test reg 9, missing domain, expected to fail --- */
	if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
		goto alloc_fail;
	} else if (!sip_parse_register_line(reg, default_expiry, reg9, 1)) {
		ast_test_status_update(test,
				"Test 9, missing domain, expected to fail but did not.\n");
		res = AST_TEST_FAIL;
	}
	ast_string_field_free_memory(reg);
	ast_free(reg);

	/* ---Test reg 10,  missing user, expected to fail --- */
	if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
		goto alloc_fail;
	} else if (!sip_parse_register_line(reg, default_expiry, reg10, 1)) {
		ast_test_status_update(test,
				"Test 10, missing user expected to fail but did not\n");
		res = AST_TEST_FAIL;
	}
	ast_string_field_free_memory(reg);
	ast_free(reg);

	/* ---Test reg 11, no registry object, expected to fail--- */
	if (!sip_parse_register_line(NULL, default_expiry, reg1, 1)) {
		ast_test_status_update(test,
				"Test 11, no registry object, expected to fail but did not.\n");
		res = AST_TEST_FAIL;
	}

	/* ---Test reg 11,  no registry line, expected to fail --- */
	if (!(reg = ast_calloc_with_stringfields(1, struct sip_registry, 256))) {
		goto alloc_fail;
	} else if (!sip_parse_register_line(reg, default_expiry, NULL, 1)) {

		ast_test_status_update(test,
				"Test 11, NULL register line expected to fail but did not.\n");
		res = AST_TEST_FAIL;
	}
	ast_string_field_free_memory(reg);
	ast_free(reg);


	return res;

alloc_fail:
	ast_test_status_update(test, "Out of memory. \n");
	return res;
}

int sip_parse_host(char *line, int lineno, char **hostname, int *portnum, enum sip_transport *transport)
{
	char *port;

	if (ast_strlen_zero(line)) {
		return -1;
	}
	if ((*hostname = strstr(line, "://"))) {
		*hostname += 3;

		if (!strncasecmp(line, "tcp", 3))
			*transport = SIP_TRANSPORT_TCP;
		else if (!strncasecmp(line, "tls", 3))
			*transport = SIP_TRANSPORT_TLS;
		else if (!strncasecmp(line, "udp", 3))
			*transport = SIP_TRANSPORT_UDP;
		else
			ast_log(LOG_NOTICE, "'%.3s' is not a valid transport type on line %d of sip.conf. defaulting to udp.\n", line, lineno);
	} else {
		*hostname = line;
		*transport = SIP_TRANSPORT_UDP;
	}

	if ((line = strrchr(*hostname, '@')))
		line++;
	else
		line = *hostname;

	if (ast_sockaddr_split_hostport(line, hostname, &port, 0) == 0) {
		ast_log(LOG_WARNING, "Cannot parse host '%s' on line %d of sip.conf.\n",
			line, lineno);
		return -1;
	}

	if (port) {
		if (!sscanf(port, "%5u", portnum)) {
			ast_log(LOG_NOTICE, "'%s' is not a valid port number on line %d of sip.conf. using default.\n", port, lineno);
			port = NULL;
		}
	} else {
		if (*transport & SIP_TRANSPORT_TLS) {
			*portnum = STANDARD_TLS_PORT;
		} else {
			*portnum = STANDARD_SIP_PORT;
		}
	}

	return 0;
}

AST_TEST_DEFINE(sip_parse_host_line_test)
{
	int res = AST_TEST_PASS;
	char *host;
	int port;
	enum sip_transport transport;
	char host1[] = "www.blah.com";
	char host2[] = "tcp://www.blah.com";
	char host3[] = "tls://10.10.10.10";
	char host4[] = "tls://10.10.10.10:1234";
	char host5[] = "10.10.10.10:1234";

	switch (cmd) {
	case TEST_INIT:
		info->name = "sip_parse_host_line_test";
		info->category = "/channels/chan_sip/";
		info->summary = "tests sip.conf host line parsing";
		info->description =
							"Tests parsing of various host line configurations. "
							"Verifies output matches expected behavior.";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	/* test 1, simple host */
	sip_parse_host(host1, 1, &host, &port, &transport);
	if (port != STANDARD_SIP_PORT ||
			ast_strlen_zero(host) || strcmp(host, "www.blah.com") ||
			transport != SIP_TRANSPORT_UDP) {
		ast_test_status_update(test, "Test 1: simple host failed.\n");
		res = AST_TEST_FAIL;
	}

	/* test 2, add tcp transport */
	sip_parse_host(host2, 1, &host, &port, &transport);
	if (port != STANDARD_SIP_PORT ||
			ast_strlen_zero(host) || strcmp(host, "www.blah.com") ||
			transport != SIP_TRANSPORT_TCP) {
		ast_test_status_update(test, "Test 2: tcp host failed.\n");
		res = AST_TEST_FAIL;
	}

	/* test 3, add tls transport */
	sip_parse_host(host3, 1, &host, &port, &transport);
	if (port != STANDARD_TLS_PORT ||
			ast_strlen_zero(host) || strcmp(host, "10.10.10.10") ||
			transport != SIP_TRANSPORT_TLS) {
		ast_test_status_update(test, "Test 3: tls host failed. \n");
		res = AST_TEST_FAIL;
	}

	/* test 4, add custom port with tls */
	sip_parse_host(host4, 1, &host, &port, &transport);
	if (port != 1234 || ast_strlen_zero(host) ||
			strcmp(host, "10.10.10.10") ||
			transport != SIP_TRANSPORT_TLS) {
		ast_test_status_update(test, "Test 4: tls host with custom port failed.\n");
		res = AST_TEST_FAIL;
	}

	/* test 5, simple host with custom port */
	sip_parse_host(host5, 1, &host, &port, &transport);
	if (port != 1234 || ast_strlen_zero(host) ||
			strcmp(host, "10.10.10.10") ||
			transport != SIP_TRANSPORT_UDP) {
		ast_test_status_update(test, "Test 5: simple host with custom port failed.\n");
		res = AST_TEST_FAIL;
	}

	/* test 6, expected failure with NULL input */
	if (!sip_parse_host(NULL, 1, &host, &port, &transport)) {
		ast_test_status_update(test, "Test 6: expected error on NULL input did not occur.\n");
		res = AST_TEST_FAIL;
	}

	return res;

}

/*! \brief SIP test registration */
void sip_config_parser_register_tests(void)
{
	AST_TEST_REGISTER(sip_parse_register_line_test);
	AST_TEST_REGISTER(sip_parse_host_line_test);
}

/*! \brief SIP test registration */
void sip_config_parser_unregister_tests(void)
{
	AST_TEST_UNREGISTER(sip_parse_register_line_test);
	AST_TEST_UNREGISTER(sip_parse_host_line_test);
}