73ef4ff3
Hu Chunming
提交三方库
|
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
|
/**
* @ingroup libosip2 The GNU oSIP stack
* @defgroup howto_parser How-To parse SIP message.
* @section howto_parser1 Parser API
For the SIP parser, the API is documented in osip_message.h
* @section howto_parser2 Basic Parser operations
+ Here is the sequence needed to parse a given buffer containing
a sip request or response.
Because the SIP message can contains binary data in its body part,
the length of the buffer must be given to the API.
~~~~~~~{.c}
osip_message_t *sip;
int i;
i=osip_message_init(&sip);
if (i!=0) { fprintf(stderr, "cannot allocate\n"); return -1; }
i=osip_message_parse(sip, buffer, length_of_buffer);
if (i!=0) { fprintf(stderr, "cannot parse sip message\n"); }
osip_message_free(sip);
~~~~~~~
+ Here is the sequence needed to convert the message into a printable
string.
**Note**: dest is allocated dynamically and must be released at the end
of the call sequence to avoid memory leaks.
When converting SIP message, the final length of the allocated buffer
will be returned in the third argument. You then have the knowledge of
the length of the data received.
~~~~~~~{.c}
char *dest=NULL;
int length=0;
i = osip_message_to_str(sip, &dest, &length);
if (i!=0) { fprintf(stderr, "cannot get printable message\n"); return -1; }
fprintf(stdout, "message:\n%s\n", dest);
osip_free(dest);
~~~~~~~
+ Here is the code to directly create an event for osip2 transactions.
When using libosip2 and its transaction management features, you'll generally
only need to create a suitable events. Thus, you'll probably use this API (only
for SIP message that you receive!):
~~~~~~~{.c}
osip_event_t *evt;
int length = size_of_buffer;
evt = osip_parse(buffer, i);
~~~~~~~
**Note**: It is important to understand that the libosip2 parser will not check
completely if the message is compliant and well formed. The application
layer is still responsible for this.
The libosip2 parser will not detect all errors (like missing headers) and it will
be up to you to verify at every action that things are as you are
expecting them to be!
* @section howto_parser3 Built-in Header Operations
osip_message_t structure contains pointers to most common headers. Other
headers (Any of them, even ones not being defined by rfc) will be saved
into a list of other headers.
+ Here is the list of parameters for headers with built-in support:
~~~~~~~{.c}
struct osip_message {
...
osip_list_t accepts;
osip_list_t accept_encodings;
osip_list_t accept_languages;
osip_list_t alert_infos;
osip_list_t allows;
osip_list_t authentication_infos;
osip_list_t authorizations;
osip_call_id_t *call_id;
osip_list_t call_infos;
osip_list_t contacts;
osip_list_t content_encodings;
osip_content_length_t *content_length;
osip_content_type_t *content_type;
osip_cseq_t *cseq;
osip_list_t error_infos;
osip_from_t *from;
osip_mime_version_t *mime_version;
osip_list_t proxy_authenticates;
osip_list_t proxy_authentication_infos:
osip_list_t proxy_authorizations;
osip_list_t record_routes;
osip_list_t routes;
osip_to_t *to;
osip_list_t vias;
osip_list_t www_authenticates;
...
};
~~~~~~~
* @section howto_parser4 Other Header Operations
Any other headers will be saved into a list of other headers.
+ Here is the osip_list for headers without built-in support:
~~~~~~~{.c}
struct osip_message {
...
osip_list_t headers;
...
};
~~~~~~~
**Note**: Those headers, even if not built-in, may have the same syntax
as other headers (many contains URI for example)µ. So you can easily re-use
already existing headers API to analyse them.
+ Here is code to add header in a sip message:
~~~~~~~{.c}
osip_message_set_header (msg, "Refer-to", refer_to_header);
~~~~~~~
~~~~~~~{.c}
osip_message_set_header (msg, "Subscription-State", subscription_state_header);
~~~~~~~
~~~~~~~{.c}
osip_message_set_header (msg, "Require", "timer");
~~~~~~~
~~~~~~~{.c}
osip_message_set_header (msg, "RAck", rack_header);
~~~~~~~
~~~~~~~{.c}
osip_message_set_header (msg, "Min-SE", min_se_header);
~~~~~~~
~~~~~~~{.c}
osip_message_set_header (msg, "x-header", x_header);
~~~~~~~
+ Here is code to find the FIRST specific header in a sip message:
~~~~~~~{.c}
osip_message_header_get_byname (msg, "min-se", 0, &min_se_header);
if (min_se_header != NULL && min_se_header->hvalue != NULL) {
~~~~~~~
**Note**: if you are looking for all headers with same name,
you need to find the first one and re-use the return position as
a parameter to search for the next one.
+ Here is code to find all headers with same name in a sip message:
~~~~~~~{.c}
int pos=0;
while (1) {
pos = osip_message_header_get_byname (msg, "x-header", pos, &min_se_new);
if (min_se_new == NULL) {
break; //no more headers
}
}
~~~~~~~
+ Here is code to handle short header usage
In some case, short name are used in SIP message for some headers.
For example, "x" is equivalent to "session-expires". In that case,
you need to search for both:
~~~~~~~{.c}
osip_message_header_get_byname (msg, "session-expires", 0, &exp);
if (exp == NULL)
osip_message_header_get_byname (msg, "x", 0, &exp);
~~~~~~~
* @section howto_parser5 Known header parser
You can browse the API documentation to know more about header parser.
libosipparser2 provides specific parser for the built-in headers (like
via, from, to, call-id, route, etc... see above for full list)
~~~~~~~{.c}
#include <osipparser2/headers/osip_header.h>
#include <osipparser2/headers/osip_accept.h>
#include <osipparser2/headers/osip_accept_encoding.h>
#include <osipparser2/headers/osip_accept_language.h>
#include <osipparser2/headers/osip_alert_info.h>
#include <osipparser2/headers/osip_allow.h>
#include <osipparser2/headers/osip_authentication_info.h>
#include <osipparser2/headers/osip_authorization.h>
#include <osipparser2/headers/osip_call_id.h>
#include <osipparser2/headers/osip_call_info.h>
#include <osipparser2/headers/osip_contact.h>
#include <osipparser2/headers/osip_content_disposition.h>
#include <osipparser2/headers/osip_content_encoding.h>
#include <osipparser2/headers/osip_content_length.h>
#include <osipparser2/headers/osip_content_type.h>
#include <osipparser2/headers/osip_cseq.h>
#include <osipparser2/headers/osip_error_info.h>
#include <osipparser2/headers/osip_from.h>
#include <osipparser2/headers/osip_mime_version.h>
#include <osipparser2/headers/osip_proxy_authenticate.h>
#include <osipparser2/headers/osip_proxy_authentication_info.h>
#include <osipparser2/headers/osip_proxy_authorization.h>
#include <osipparser2/headers/osip_record_route.h>
#include <osipparser2/headers/osip_route.h>
#include <osipparser2/headers/osip_to.h>
#include <osipparser2/headers/osip_via.h>
#include <osipparser2/headers/osip_www_authenticate.h>
~~~~~~~
Here are some sample code for Via.
+ Get Via from message:
~~~~~~~{.c}
osip_via_t *dest;
osip_message_get_via (msg, 0, &dest);
~~~~~~~
+ Add Via in message
~~~~~~~{.c}
char tmp[200];
snprintf (tmp, 200, "SIP/2.0/UDP %s:%s;rport;branch=z9hG4bK%u", ip, port, osip_build_random_number ());
osip_message_set_via (msg, tmp);
~~~~~~~
+ Parse Via:
~~~~~~~{.c}
osip_via_t *header;
char tmp[200];
snprintf (tmp, 200, "SIP/2.0/UDP %s:%s;rport;branch=z9hG4bK%u", ip, port, osip_build_random_number ());
osip_via_init (&header);
osip_via_parse (header, tmp);
osip_free(header);
~~~~~~~
+ Many other getter/setter API exists, see:
~~~~~~~{.c}
#include <osipparser2/headers/osip_via.h>
~~~~~~~
*/
|