Asterisk - The Open Source Telephony Project  18.5.0
reqresp_parser.h
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2010, Digium, Inc.
5  *
6  * See http://www.asterisk.org for more information about
7  * the Asterisk project. Please do not directly contact
8  * any of the maintainers of this project for assistance;
9  * the project provides a web site, mailing lists and IRC
10  * channels for your use.
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License Version 2. See the LICENSE file
14  * at the top of the source tree.
15  */
16 
17 /*!
18  * \file
19  * \brief sip request response parser header file
20  */
21 
22 #ifndef _SIP_REQRESP_H
23 #define _SIP_REQRESP_H
24 
25 /*! \brief uri parameters */
26 struct uriparams {
27  char *transport;
28  char *user;
29  char *method;
30  char *ttl;
31  char *maddr;
32  int lr;
33 };
34 
35 struct contact {
36  AST_LIST_ENTRY(contact) list;
37  char *name;
38  char *user;
39  char *pass;
40  char *hostport;
41  struct uriparams params;
42  char *headers;
43  char *expires;
44  char *q;
45 };
46 
47 AST_LIST_HEAD_NOLOCK(contactliststruct, contact);
48 
49 /*!
50  * \brief parses a URI in its components.
51  *
52  * \note
53  * - Multiple scheme's can be specified ',' delimited. ex: "sip:,sips:"
54  * - If a component is not requested, do not split around it. This means
55  * that if we don't have domain, we cannot split name:pass.
56  * - It is safe to call with ret_name, pass, hostport pointing all to
57  * the same place.
58  * - If no secret parameter is provided, ret_name will return with both
59  * parts, user:secret.
60  * - If the URI contains a port number, hostport will return with both
61  * parts, host:port.
62  * - This function overwrites the URI string.
63  *
64  * \retval 0 on success
65  * \retval -1 on error.
66  *
67  * \verbatim
68  * general form we are expecting is sip:user:password;user-parameters@host:port;uri-parameters?headers
69  * \endverbatim
70  */
71 int parse_uri(char *uri, const char *scheme, char **ret_name, char **pass,
72  char **hostport, char **transport);
73 
74 /*!
75  * \brief parses a URI in to all of its components and any trailing residue
76  *
77  * \retval 0 on success
78  * \retval -1 on error.
79  *
80  */
81 int parse_uri_full(char *uri, const char *scheme, char **user, char **pass,
82  char **hostport, struct uriparams *params, char **headers,
83  char **residue);
84 
85 /*!
86  * \brief Get caller id name from SIP headers, copy into output buffer
87  *
88  * \retval input string pointer placed after display-name field if possible
89  */
90 const char *get_calleridname(const char *input, char *output, size_t outputsize);
91 
92 /*!
93  * \brief Get name and number from sip header
94  *
95  * \note name and number point to malloced memory on return and must be
96  * freed. If name or number is not found, they will be returned as NULL.
97  *
98  * \retval 0 success
99  * \retval -1 failure
100  */
101 int get_name_and_number(const char *hdr, char **name, char **number);
102 
103 /*! \brief Pick out text in brackets from character string
104  * \return pointer to terminated stripped string
105  * \param tmp input string that will be modified
106  *
107  * Examples:
108  * \verbatim
109  * "foo" <bar> valid input, returns bar
110  * foo returns the whole string
111  * < "foo ... > returns the string between brackets
112  * < "foo... bogus (missing closing bracket), returns the whole string
113  * \endverbatim
114  */
115 char *get_in_brackets(char *tmp);
116 
117 /*! \brief Get text in brackets on a const without copy
118  *
119  * \param src String to search
120  * \param[out] start Set to first character inside left bracket.
121  * \param[out] length Set to lenght of string inside brackets
122  * \retval 0 success
123  * \retval -1 failure
124  * \retval 1 no brackets so got all
125  */
126 int get_in_brackets_const(const char *src,const char **start,int *length);
127 
128 /*! \brief Get text in brackets and any trailing residue
129  *
130  * \retval 0 success
131  * \retval -1 failure
132  * \retval 1 no brackets so got all
133  */
134 int get_in_brackets_full(char *tmp, char **out, char **residue);
135 
136 /*! \brief Parse the ABNF structure
137  * name-andor-addr = name-addr / addr-spec
138  * into its components and return any trailing message-header parameters
139  *
140  * \retval 0 success
141  * \retval -1 failure
142  */
143 int parse_name_andor_addr(char *uri, const char *scheme, char **name,
144  char **user, char **pass, char **domain,
145  struct uriparams *params, char **headers,
146  char **remander);
147 
148 /*! \brief Parse all contact header contacts
149  * \retval 0 success
150  * \retval -1 failure
151  * \retval 1 all contacts (star)
152  */
153 
154 int get_comma(char *parse, char **out);
155 
156 int parse_contact_header(char *contactheader, struct contactliststruct *contactlist);
157 
158 /*!
159  * \brief register request parsing tests
160  */
162 
163 /*!
164  * \brief unregister request parsing tests
165  */
167 
168 /*!
169  * \brief Parse supported header in incoming packet
170  *
171  * \details This function parses through the options parameters and
172  * builds a bit field representing all the SIP options in that field. When an
173  * item is found that is not supported, it is copied to the unsupported
174  * out buffer.
175  *
176  * \param option list
177  * \param unsupported out buffer (optional)
178  * \param unsupported out buffer length (optional)
179  *
180  * \note Because this function can be called multiple times, it will append
181  * whatever options are specified in \c options to \c unsupported. Callers
182  * of this function should make sure the unsupported buffer is clear before
183  * calling this function.
184  */
185 unsigned int parse_sip_options(const char *options, char *unsupported, size_t unsupported_len);
186 
187 /*!
188  * \brief Compare two URIs as described in RFC 3261 Section 19.1.4
189  *
190  * \param input1 First URI
191  * \param input2 Second URI
192  * \retval 0 URIs match
193  * \retval nonzero URIs do not match or one or both is malformed
194  */
195 int sip_uri_cmp(const char *input1, const char *input2);
196 
197 /*!
198  * \brief initialize request and response parser data
199  *
200  * \retval 0 Success
201  * \retval -1 Failure
202  */
203 int sip_reqresp_parser_init(void);
204 
205 /*!
206  * \brief Free resources used by request and response parser
207  */
208 void sip_reqresp_parser_exit(void);
209 
210 /*!
211  * \brief Parse a Via header
212  *
213  * This function parses the Via header and processes it according to section
214  * 18.2 of RFC 3261 and RFC 3581. Since we don't have a transport layer, we
215  * only care about the maddr and ttl parms. The received and rport params are
216  * not parsed.
217  *
218  * \note This function fails to parse some odd combinations of SWS in parameter
219  * lists.
220  *
221  * \code
222  * VIA syntax. RFC 3261 section 25.1
223  * Via = ( "Via" / "v" ) HCOLON via-parm *(COMMA via-parm)
224  * via-parm = sent-protocol LWS sent-by *( SEMI via-params )
225  * via-params = via-ttl / via-maddr
226  * / via-received / via-branch
227  * / via-extension
228  * via-ttl = "ttl" EQUAL ttl
229  * via-maddr = "maddr" EQUAL host
230  * via-received = "received" EQUAL (IPv4address / IPv6address)
231  * via-branch = "branch" EQUAL token
232  * via-extension = generic-param
233  * sent-protocol = protocol-name SLASH protocol-version
234  * SLASH transport
235  * protocol-name = "SIP" / token
236  * protocol-version = token
237  * transport = "UDP" / "TCP" / "TLS" / "SCTP"
238  * / other-transport
239  * sent-by = host [ COLON port ]
240  * ttl = 1*3DIGIT ; 0 to 255
241  * \endcode
242  */
243 struct sip_via *parse_via(const char *header);
244 
245 /*
246  * \brief Free parsed Via data.
247  */
248 void free_via(struct sip_via *v);
249 
250 #endif
static char pass[512]
char * user
char * headers
char * method
char * expires
int parse_name_andor_addr(char *uri, const char *scheme, char **name, char **user, char **pass, char **domain, struct uriparams *params, char **headers, char **remander)
Parse the ABNF structure name-andor-addr = name-addr / addr-spec into its components and return any t...
char * get_in_brackets(char *tmp)
Pick out text in brackets from character string.
void free_via(struct sip_via *v)
static int tmp()
Definition: bt_open.c:389
void sip_request_parser_register_tests(void)
register request parsing tests
int sip_uri_cmp(const char *input1, const char *input2)
Compare two URIs as described in RFC 3261 Section 19.1.4.
Domain data structure.
Definition: sip.h:888
static int input(yyscan_t yyscanner)
Definition: ast_expr2f.c:1584
int parse_uri_full(char *uri, const char *scheme, char **user, char **pass, char **hostport, struct uriparams *params, char **headers, char **residue)
parses a URI in to all of its components and any trailing residue
int get_in_brackets_const(const char *src, const char **start, int *length)
Get text in brackets on a const without copy.
char * transport
Structure to store Via information.
Definition: sip.h:874
Number structure.
Definition: app_followme.c:154
char * hostport
int parse_contact_header(char *contactheader, struct contactliststruct *contactlist)
AST_LIST_HEAD_NOLOCK(contactliststruct, contact)
char * pass
char * user
int get_name_and_number(const char *hdr, char **name, char **number)
Get name and number from sip header.
char * name
char * maddr
int get_in_brackets_full(char *tmp, char **out, char **residue)
Get text in brackets and any trailing residue.
int parse_uri(char *uri, const char *scheme, char **ret_name, char **pass, char **hostport, char **transport)
parses a URI in its components.
const char * get_calleridname(const char *input, char *output, size_t outputsize)
Get caller id name from SIP headers, copy into output buffer.
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
Definition: linkedlists.h:409
uri parameters
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1872
char * ttl
static const char name[]
Definition: cdr_mysql.c:74
structure to hold users read from users.conf
void sip_reqresp_parser_exit(void)
Free resources used by request and response parser.
void sip_request_parser_unregister_tests(void)
unregister request parsing tests
FILE * out
Definition: utils/frame.c:33
int get_comma(char *parse, char **out)
Parse all contact header contacts.
struct sip_via * parse_via(const char *header)
Parse a Via header.
char * q
int sip_reqresp_parser_init(void)
initialize request and response parser data
static struct test_options options
unsigned int parse_sip_options(const char *options, char *unsupported, size_t unsupported_len)
Parse supported header in incoming packet.