~ubuntu-branches/debian/sid/kamailio/sid

« back to all changes in this revision

Viewing changes to parser/parse_ppi_pai.c

  • Committer: Package Import Robot
  • Author(s): Victor Seva
  • Date: 2014-01-06 11:47:13 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20140106114713-t8xidp4arzrnyeya
Tags: 4.1.1-1
* New upstream release
* debian/patches:
  - add upstream fixes
* Added tls outbound websocket autheph dnssec modules
  - openssl exception added to their license
* removing sparc and ia64 from supported archs
  for mono module (Closes: #728915)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 Hugh Waite
 
3
 *
 
4
 * This file is part of Kamailio, a free SIP server.
 
5
 *
 
6
 * Kamailio is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version
 
10
 *
 
11
 * Kamailio is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License 
 
17
 * along with this program; if not, write to the Free Software 
 
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 *
 
20
 * History:
 
21
 * ---------
 
22
 * 2013-03-06 Created (hpw)
 
23
 */
 
24
 
 
25
/** Parser :: Parse P-Asserted-Identity: header.
 
26
 * @file
 
27
 * @ingroup parser
 
28
 */
 
29
 
 
30
#include "parse_ppi_pai.h"
 
31
#include <stdlib.h>
 
32
#include <string.h>
 
33
#include "../dprint.h"
 
34
#include "msg_parser.h"
 
35
#include "parse_uri.h"
 
36
#include "../ut.h"
 
37
#include "../mem/mem.h"
 
38
 
 
39
/* 
 
40
 * A P-Asserted-Identity or P-Preferred-Identity header value is an addr-spec or name-addr
 
41
 * There can be only one of any URI scheme (sip(s), tel etc), which may occur on separate
 
42
 * headers or can be comma separated in a single header.
 
43
 * RFC3325 only mentions sip(s) and tel schemes, but there is no reason why other schemes
 
44
 * cannot be used in the future.
 
45
 */
 
46
 
 
47
/*!
 
48
 *
 
49
 */
 
50
#define NUM_PAI_BODIES 10
 
51
int parse_pai_ppi_body(char *buf, int len, p_id_body_t **body)
 
52
{
 
53
        static to_body_t uri_b[NUM_PAI_BODIES]; /* Temporary storage */
 
54
        int num_uri = 0;
 
55
        char *tmp;
 
56
        int i;
 
57
        memset(uri_b, 0, NUM_PAI_BODIES * sizeof(to_body_t));
 
58
 
 
59
        tmp = parse_addr_spec(buf, buf+len, &uri_b[num_uri], 1);
 
60
        if (uri_b[num_uri].error == PARSE_ERROR)
 
61
        {
 
62
                LM_ERR("Error parsing PAI/PPI body %u '%.*s'\n", num_uri, len, buf);
 
63
                return -1;
 
64
        }
 
65
        num_uri++;
 
66
        while ((*tmp == ',') && (num_uri < NUM_PAI_BODIES))
 
67
        {
 
68
                tmp++;
 
69
                tmp = parse_addr_spec(tmp, buf+len, &uri_b[num_uri], 1);
 
70
                if (uri_b[num_uri].error == PARSE_ERROR)
 
71
                {
 
72
                        LM_ERR("Error parsing PAI/PPI body %u '%.*s'\n", num_uri, len, buf);
 
73
                        return -1;
 
74
                }
 
75
                num_uri++;
 
76
        }
 
77
        if (num_uri >= NUM_PAI_BODIES)
 
78
        {
 
79
                LM_WARN("Too many bodies in PAI/PPI header '%.*s'\n", len, buf);
 
80
                LM_WARN("Ignoring bodies beyond %u\n", NUM_PAI_BODIES);
 
81
        }
 
82
        *body = pkg_malloc(sizeof(p_id_body_t) + num_uri * sizeof(to_body_t));
 
83
        if (*body == NULL)
 
84
        {
 
85
                LM_ERR("No pkg memory for pai/ppi body\n");
 
86
                return -1;
 
87
        }
 
88
        memset(*body, 0, sizeof(p_id_body_t));
 
89
        (*body)->id = (to_body_t*)((char*)(*body) + sizeof(p_id_body_t));
 
90
        (*body)->num_ids = num_uri;
 
91
        for (i=0; i< num_uri; i++)
 
92
        {
 
93
                memcpy(&(*body)->id[i], &uri_b[i], sizeof(to_body_t));
 
94
        }
 
95
        return 0;
 
96
}
 
97
 
 
98
int free_pai_ppi_body(p_id_body_t *pid_b)
 
99
{
 
100
        if (pid_b != NULL)
 
101
        {
 
102
                pkg_free(pid_b);
 
103
        }
 
104
        return 0;
 
105
}
 
106
 
 
107
/*!
 
108
 * \brief Parse all P-Asserted-Identity headers
 
109
 * \param msg The SIP message structure
 
110
 * \return 0 on success, -1 on failure
 
111
 */
 
112
int parse_pai_header(struct sip_msg* const msg)
 
113
{
 
114
        p_id_body_t *pai_b;
 
115
        p_id_body_t **prev_pid_b;
 
116
        hdr_field_t *hf;
 
117
        void **vp;
 
118
 
 
119
        if ( !msg->pai )
 
120
        {
 
121
                if (parse_headers(msg, HDR_PAI_F, 0) < 0)
 
122
                {
 
123
                        LM_ERR("Error parsing PAI header\n");
 
124
                        return -1;
 
125
                }
 
126
                if ( !msg->pai )
 
127
                        /* No PAI headers */
 
128
                        return -1;
 
129
        }
 
130
 
 
131
        if ( msg->pai->parsed )
 
132
                return 0;
 
133
 
 
134
        vp = &msg->pai->parsed;
 
135
        prev_pid_b = (p_id_body_t**)vp;
 
136
 
 
137
        for (hf = msg->pai; hf != NULL; hf = next_sibling_hdr(hf))
 
138
        {
 
139
                if (parse_pai_ppi_body(hf->body.s, hf->body.len, &pai_b) < 0)
 
140
                {
 
141
                        return -1;
 
142
                }
 
143
                hf->parsed = (void*)pai_b;
 
144
                *prev_pid_b = pai_b;
 
145
                prev_pid_b = &pai_b->next;
 
146
 
 
147
                if (parse_headers(msg, HDR_PAI_F, 1) < 0)
 
148
                {
 
149
                        LM_ERR("Error looking for subsequent PAI header");
 
150
                        return -1;
 
151
                }
 
152
        }
 
153
        return 0;
 
154
}
 
155
 
 
156
/*!
 
157
 * \brief Parse all P-Preferred-Identity headers
 
158
 * \param msg The SIP message structure
 
159
 * \return 0 on success, -1 on failure
 
160
 */
 
161
int parse_ppi_header(struct sip_msg* const msg)
 
162
{
 
163
        p_id_body_t *ppi_b, *prev_pidb;
 
164
        hdr_field_t *hf;
 
165
 
 
166
        if ( !msg->ppi )
 
167
        {
 
168
                if (parse_headers(msg, HDR_PPI_F, 0) < 0)
 
169
                {
 
170
                        LM_ERR("Error parsing PPI header\n");
 
171
                        return -1;
 
172
                }
 
173
                if ( !msg->ppi )
 
174
                        /* No PPI headers */
 
175
                        return -1;
 
176
        }
 
177
 
 
178
        if ( msg->ppi->parsed )
 
179
                return 0;
 
180
 
 
181
        if (parse_pai_ppi_body(msg->ppi->body.s, msg->ppi->body.len, &ppi_b) < 0)
 
182
        {
 
183
                return -1;
 
184
        }
 
185
        msg->ppi->parsed = (void*)ppi_b;
 
186
 
 
187
        if (parse_headers(msg, HDR_PPI_F, 1) < 0)
 
188
        {
 
189
                LM_ERR("Error looking for subsequent PPI header");
 
190
                return -1;
 
191
        }
 
192
        prev_pidb = ppi_b;
 
193
        hf = msg->ppi;
 
194
 
 
195
        if ((hf = next_sibling_hdr(hf)) != NULL)
 
196
        {
 
197
                if (parse_pai_ppi_body(hf->body.s, hf->body.len, &ppi_b) < 0)
 
198
                {
 
199
                        return -1;
 
200
                }
 
201
                hf->parsed = (void*)ppi_b;
 
202
 
 
203
                if (parse_headers(msg, HDR_PPI_F, 1) < 0)
 
204
                {
 
205
                        LM_ERR("Error looking for subsequent PPI header");
 
206
                        return -1;
 
207
                }
 
208
                prev_pidb->next = ppi_b;
 
209
                prev_pidb = ppi_b;
 
210
        }
 
211
        return 0;
 
212
}
 
213