blob: 76e518a7aeb7bb87594c14ff9e9c59e309f9c157 [file] [log] [blame]
James Kuszmaul82f6c042021-01-17 11:30:16 -08001/**
2 * @file via.c SIP Via decode
3 *
4 * Copyright (C) 2010 Creytiv.com
5 */
6#include <re_types.h>
7#include <re_fmt.h>
8#include <re_mbuf.h>
9#include <re_uri.h>
10#include <re_list.h>
11#include <re_sa.h>
12#include <re_msg.h>
13#include <re_sip.h>
14
15
16static int decode_hostport(const struct pl *hostport, struct pl *host,
17 struct pl *port)
18{
19 /* Try IPv6 first */
20 if (!re_regex(hostport->p, hostport->l, "\\[[0-9a-f:]+\\][:]*[0-9]*",
21 host, NULL, port))
22 return 0;
23
24 /* Then non-IPv6 host */
25 return re_regex(hostport->p, hostport->l, "[^:]+[:]*[0-9]*",
26 host, NULL, port);
27}
28
29
30/**
31 * Decode a pointer-length string into a SIP Via header
32 *
33 * @param via SIP Via header
34 * @param pl Pointer-length string
35 *
36 * @return 0 for success, otherwise errorcode
37 */
38int sip_via_decode(struct sip_via *via, const struct pl *pl)
39{
40 struct pl transp, host, port;
41 int err;
42
43 if (!via || !pl)
44 return EINVAL;
45
46 err = re_regex(pl->p, pl->l,
47 "SIP[ \t\r\n]*/[ \t\r\n]*2.0[ \t\r\n]*/[ \t\r\n]*"
48 "[A-Z]+[ \t\r\n]*[^; \t\r\n]+[ \t\r\n]*[^]*",
49 NULL, NULL, NULL, NULL, &transp,
50 NULL, &via->sentby, NULL, &via->params);
51 if (err)
52 return err;
53
54 if (!pl_strcmp(&transp, "TCP"))
55 via->tp = SIP_TRANSP_TCP;
56 else if (!pl_strcmp(&transp, "TLS"))
57 via->tp = SIP_TRANSP_TLS;
58 else if (!pl_strcmp(&transp, "UDP"))
59 via->tp = SIP_TRANSP_UDP;
60 else
61 via->tp = SIP_TRANSP_NONE;
62
63 err = decode_hostport(&via->sentby, &host, &port);
64 if (err)
65 return err;
66
67 sa_init(&via->addr, AF_INET);
68
69 (void)sa_set(&via->addr, &host, 0);
70
71 if (pl_isset(&port))
72 sa_set_port(&via->addr, pl_u32(&port));
73
74 via->val = *pl;
75
76 return msg_param_decode(&via->params, "branch", &via->branch);
77}