1 /*
2  * $Id: sip_parser.h 1486 2009-08-29 14:40:38Z rco $
3  *
4  * Copyright (C) 2007 Raphael Coeffic
5  *
6  * This file is part of SEMS, a free SIP media server.
7  *
8  * SEMS is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version. This program is released under
12  * the GPL with the additional exemption that compiling, linking,
13  * and/or using OpenSSL is allowed.
14  *
15  * For a license to use the sems software under conditions
16  * other than those described here, or to purchase support for this
17  * software, please contact iptel.org by e-mail at the following addresses:
18  *    info@iptel.org
19  *
20  * SEMS is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28  */
29 
30 #ifndef _SIP_PARSER_H
31 #define _SIP_PARSER_H
32 
33 #include "cstring.h"
34 #include "parse_uri.h"
35 #include "resolver.h"
36 
37 #include <list>
38 using std::list;
39 
40 #include <netinet/in.h>
41 #include <sys/socket.h>
42 
43 struct sip_request;
44 struct sip_reply;
45 struct sip_header;
46 struct sip_via_parm;
47 struct dns_handle;
48 class trsp_socket;
49 
50 //
51 // SIP message types:
52 //
53 
54 enum {
55     SIP_UNKNOWN=0,
56     SIP_REQUEST,
57     SIP_REPLY
58 };
59 
60 
61 struct sip_request
62 {
63     //
64     // Request methods
65     //
66 
67     enum {
68 	OTHER_METHOD=0,
69 	INVITE,
70 	ACK,
71         PRACK,
72 	OPTIONS,
73 	BYE,
74 	CANCEL,
75 	REGISTER
76     };
77 
78     cstring  method_str;
79     int      method;
80 
81     cstring  ruri_str;
82     sip_uri  ruri;
83 };
84 
85 
86 struct sip_reply
87 {
88     int     code;
89     cstring reason;
90 
sip_replysip_reply91     sip_reply()
92 	: code(0)
93     {}
94 
sip_replysip_reply95     sip_reply(int code, const cstring& reason)
96 	: code(code), reason(reason)
97     {}
98 };
99 
100 
101 struct sip_msg
102 {
103     char*   buf;
104     int     len;
105 
106     // Request or Reply?
107     int     type;
108 
109     union {
110 	sip_request* request;
111 	sip_reply*   reply;
112     }u;
113 
114     list<sip_header*>  hdrs;
115 
116     sip_header*        to;
117     sip_header*        from;
118 
119     sip_header*        cseq;
120     sip_header*        rack;
121 
122     list<sip_header*>  vias;
123     sip_header*        via1;
124     sip_via_parm*      via_p1;
125 
126     sip_header*        callid;
127 
128     list<sip_header*>  contacts;
129     list<sip_header*>  route;
130     list<sip_header*>  record_route;
131     sip_header*        content_type;
132     sip_header*        content_length;
133     cstring            body;
134 
135     sockaddr_storage   local_ip;
136     trsp_socket*       local_socket;
137 
138     sockaddr_storage   remote_ip;
139 
140     sip_msg();
141     sip_msg(const char* msg_buf, int msg_len);
142     ~sip_msg();
143 
144     void copy_msg_buf(const char* msg_buf, int msg_len);
145 
146     int send(unsigned flags);
147 
148     /**
149      * Releases pointers otherwise deleted by the destructor
150      * This is useful to abandon the memory pointed at if this
151      * message is a copy of another which do own the memory.
152      */
153     void release();
154 };
155 
156 int parse_method(int* method, const char* beg, int len);
157 int parse_headers(sip_msg* msg, char** c, char* end);
158 int parse_sip_msg(sip_msg* msg, char*& err_msg);
159 
160 #define get_contact(msg) (msg->contacts.empty() ? NULL : (*msg->contacts.begin()))
161 
162 #endif
163 
164 /** EMACS **
165  * Local variables:
166  * mode: c++
167  * c-basic-offset: 4
168  * End:
169  */
170