2 The oSIP library implements the Session Initiation Protocol (SIP -rfc2543-)
3 Copyright (C) 2001 Aymeric MOIZARD jack@atosc.org
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <osipparser2/osip_port.h>
29 #include <osipparser2/sdp_message.h>
30 #include <osip2/osip_negotiation.h>
32 int test_sdp_message (char *msg, int verbose);
33 int test_accessor_get_api (sdp_message_t * sdp);
34 int test_accessor_set_api (sdp_message_t * sdp);
37 typedef struct _ua_context_t
40 /* only one audio port is allowed at this time.. In the case, we
41 receive more than one m audio line, this may fail... */
42 char *m_audio_port; /* audio port to be used for this session */
43 char *m_video_port; /* audio port to be used for this session */
48 ua_context_t *ua_context = NULL;
50 osip_negotiation_t *osip_negotiation = NULL;
53 ua_sdp_accept_audio_codec (osip_negotiation_ctx_t * context,
54 char *port, char *number_of_port,
55 int audio_qty, char *payload)
57 /* this may come from buggy implementation who */
58 /* propose several sdp lines while they only want 1 connection */
62 if (0 == strcmp (payload, "0") || 0 == strcmp (payload, "3") ||
63 0 == strcmp (payload, "7") || 0 == strcmp (payload, "8"))
69 ua_sdp_accept_video_codec (osip_negotiation_ctx_t * context,
70 char *port, char *number_of_port,
71 int video_qty, char *payload)
73 /* this may come from buggy implementation who */
74 /* propose several sdp lines while they only want 1 connection */
78 if (0 == strcmp (payload, "31"))
84 ua_sdp_accept_other_codec (osip_negotiation_ctx_t * context,
85 char *type, char *port,
86 char *number_of_port, char *payload)
93 ua_sdp_get_video_port (osip_negotiation_ctx_t * context, int pos_media)
97 ua_con = (ua_context_t *) context->mycontext;
98 return osip_strdup (ua_con->m_video_port); /* this port should not be static ... */
99 /* also, this method should be called more than once... */
100 /* If there is more than one audio line, this may fail :( */
104 ua_sdp_get_audio_port (osip_negotiation_ctx_t * context, int pos_media)
106 ua_context_t *ua_con;
108 ua_con = (ua_context_t *) context->mycontext;
109 return osip_strdup (ua_con->m_audio_port); /* this port should not be static ... */
110 /* also, this method should be called more than once... */
111 /* If there is more than one audio line, this may fail :( */
115 main (int argc, char **argv)
118 int verbose = 0; /* 0: verbose, 1 (or nothing: not verbose) */
124 static int num_test = 0;
128 if (0 == strncmp (argv[3], "-v", 2))
132 torture_file = fopen (argv[1], "r");
133 if (torture_file == NULL)
136 "Failed to open \"torture_sdps\" file.\nUsage: %s torture_file [-v]\n",
141 ua_context = (ua_context_t *) osip_malloc (sizeof (ua_context_t));
143 ua_context->m_audio_port = osip_strdup ("20030");
144 ua_context->m_video_port = osip_strdup ("20040");
146 i = osip_negotiation_init (&osip_negotiation);
149 fprintf (stderr, "Failed to initialize the SDP negociator\n");
150 fclose (torture_file);
153 osip_negotiation_set_o_username (osip_negotiation, osip_strdup ("userX"));
154 osip_negotiation_set_o_session_id (osip_negotiation,
155 osip_strdup ("20000001"));
156 osip_negotiation_set_o_session_version (osip_negotiation,
157 osip_strdup ("20000001"));
158 osip_negotiation_set_o_nettype (osip_negotiation, osip_strdup ("IN"));
159 osip_negotiation_set_o_addrtype (osip_negotiation, osip_strdup ("IP4"));
160 osip_negotiation_set_o_addr (osip_negotiation,
161 osip_strdup ("192.168.1.114"));
163 osip_negotiation_set_c_nettype (osip_negotiation, osip_strdup ("IN"));
164 osip_negotiation_set_c_addrtype (osip_negotiation, osip_strdup ("IP4"));
165 osip_negotiation_set_c_addr (osip_negotiation,
166 osip_strdup ("192.168.1.114"));
168 /* ALL CODEC MUST SHARE THE SAME "C=" line and proto as the media
169 will appear on the same "m" line... */
170 osip_negotiation_add_support_for_audio_codec (osip_negotiation,
174 osip_strdup ("RTP/AVP"),
177 osip_strdup ("0 PCMU/8000"));
178 osip_negotiation_add_support_for_audio_codec (osip_negotiation,
179 osip_strdup ("3"), NULL,
180 osip_strdup ("RTP/AVP"), NULL,
181 NULL, NULL, NULL, NULL,
182 osip_strdup ("3 GSM/8000"));
183 osip_negotiation_add_support_for_audio_codec (osip_negotiation,
184 osip_strdup ("7"), NULL,
185 osip_strdup ("RTP/AVP"), NULL,
186 NULL, NULL, NULL, NULL,
187 osip_strdup ("7 LPC/8000"));
188 osip_negotiation_add_support_for_audio_codec (osip_negotiation,
189 osip_strdup ("8"), NULL,
190 osip_strdup ("RTP/AVP"), NULL,
191 NULL, NULL, NULL, NULL,
192 osip_strdup ("8 PCMA/8000"));
193 osip_negotiation_add_support_for_video_codec (osip_negotiation,
194 osip_strdup ("31"), NULL,
195 osip_strdup ("RTP/AVP"), NULL,
196 NULL, NULL, NULL, NULL,
200 osip_negotiation_set_fcn_accept_audio_codec (osip_negotiation,
201 &ua_sdp_accept_audio_codec);
202 osip_negotiation_set_fcn_accept_video_codec (osip_negotiation,
203 &ua_sdp_accept_video_codec);
204 osip_negotiation_set_fcn_accept_other_codec (osip_negotiation,
205 &ua_sdp_accept_other_codec);
206 osip_negotiation_set_fcn_get_audio_port (osip_negotiation,
207 &ua_sdp_get_audio_port);
208 osip_negotiation_set_fcn_get_video_port (osip_negotiation,
209 &ua_sdp_get_video_port);
212 tmp = (char *) osip_malloc (500);
213 marker = fgets (tmp, 500, torture_file); /* lines are under 500 */
214 while (marker != NULL && i < atoi (argv[2]))
216 if (0 == strncmp (tmp, "|", 1))
218 marker = fgets (tmp, 500, torture_file);
223 msg = (char *) osip_malloc (10000); /* msg are under 10000 */
229 "Error! The message's number you specified does not exist\n");
230 fclose (torture_file);
232 exit (1); /* end of file detected! */
234 /* this part reads an entire message, separator is "|" */
235 /* (it is unlinkely that it will appear in messages!) */
236 while (marker != NULL && strncmp (tmp, "|", 1))
238 osip_strncpy (tmpmsg, tmp, strlen (tmp));
239 tmpmsg = tmpmsg + strlen (tmp);
240 marker = fgets (tmp, 500, torture_file);
245 fprintf (stdout, "test %s : ============================ \n", argv[2]);
246 fprintf (stdout, "%s", msg);
248 if (0 == test_sdp_message (msg, verbose))
249 fprintf (stdout, "test %s : ============================ OK\n",
252 fprintf (stdout, "test %s : ============================ FAILED\n",
257 if (0 == test_sdp_message (msg, verbose))
258 fprintf (stdout, "test %s : OK\n", argv[2]);
260 fprintf (stdout, "test %s : FAILED\n", argv[2]);
265 osip_negotiation_free (osip_negotiation);
266 osip_free (ua_context->m_audio_port);
267 osip_free (ua_context->m_video_port);
268 osip_free (ua_context);
269 fclose (torture_file);
274 test_sdp_message (char *msg, int verbose)
281 sdp_message_init (&sdp);
282 if (sdp_message_parse (sdp, msg) != 0)
284 fprintf (stdout, "ERROR: failed while parsing!\n");
285 sdp_message_free (sdp); /* try to free msg, even if it failed! */
286 /* this seems dangerous..... */
293 i = sdp_message_to_str (sdp, &result);
294 test_accessor_get_api (sdp);
297 fprintf (stdout, "ERROR: failed while printing message!\n");
298 sdp_message_free (sdp);
304 fprintf (stdout, "%s", result);
305 if (strlen (result) != strlen (msg))
306 fprintf (stdout, "length differ from original message!\n");
307 if (0 == strncmp (result, msg, strlen (result)))
308 fprintf (stdout, "result equals msg!!\n");
311 osip_negotiation_ctx_t *context;
315 i = osip_negotiation_ctx_init (&context);
317 osip_negotiation_ctx_set_mycontext (context,
318 (void *) ua_context);
323 osip_negotiation_sdp_build_offer (osip_negotiation,
325 ua_context->m_audio_port,
326 ua_context->m_video_port);
327 sdp_message_to_str (sdp, &result);
328 fprintf (stdout, "Here is the offer:\n%s\n", result);
330 osip_negotiation_sdp_message_put_on_hold (sdp);
331 sdp_message_to_str (sdp, &result);
332 fprintf (stdout, "Here is the offer on hold:\n%s\n", result);
334 sdp_message_free (sdp);
339 i = osip_negotiation_ctx_set_remote_sdp (context, sdp);
343 "Initialisation of context failed. Could not negociate\n");
347 fprintf (stdout, "Trying to execute a SIP negotiation:\n");
349 osip_negotiation_ctx_execute_negotiation
350 (osip_negotiation, context);
351 fprintf (stdout, "return code: %i\n", i);
354 dest = osip_negotiation_ctx_get_local_sdp (context);
355 fprintf (stdout, "SDP answer:\n");
356 i = sdp_message_to_str (dest, &result);
359 "Error found in SDP answer while printing\n");
361 fprintf (stdout, "%s\n", result);
364 osip_negotiation_ctx_free (context);
369 sdp_message_free (sdp);
376 test_accessor_set_api (sdp_message_t * sdp)
382 test_accessor_get_api (sdp_message_t * sdp)
392 if (sdp_message_v_version_get (sdp))
393 printf ("v_version: |%s|\n", sdp_message_v_version_get (sdp));
394 if (sdp_message_o_username_get (sdp))
395 printf ("o_originator: |%s|", sdp_message_o_username_get (sdp));
396 if (sdp_message_o_sess_id_get (sdp))
397 printf (" |%s|", sdp_message_o_sess_id_get (sdp));
398 if (sdp_message_o_sess_version_get (sdp) != NULL)
399 printf (" |%s|", sdp_message_o_sess_version_get (sdp));
400 if (sdp_message_o_nettype_get (sdp))
401 printf (" |%s|", sdp_message_o_nettype_get (sdp));
402 if (sdp_message_o_addrtype_get (sdp))
403 printf (" |%s|", sdp_message_o_addrtype_get (sdp));
404 if (sdp_message_o_addr_get (sdp))
405 printf (" |%s|\n", sdp_message_o_addr_get (sdp));
406 if (sdp_message_s_name_get (sdp))
407 printf ("s_name: |%s|\n", sdp_message_s_name_get (sdp));
408 if (sdp_message_i_info_get (sdp, -1))
409 printf ("i_info: |%s|\n", sdp_message_i_info_get (sdp, -1));
410 if (sdp_message_u_uri_get (sdp))
411 printf ("u_uri: |%s|\n", sdp_message_u_uri_get (sdp));
416 tmp = sdp_e_email_get (sdp, i);
418 printf ("e_email: |%s|\n", tmp);
425 tmp = sdp_message_p_phone_get (sdp, i);
427 printf ("p_phone: |%s|\n", tmp);
433 tmp = sdp_message_c_nettype_get (sdp, -1, k);
434 tmp2 = sdp_message_c_addrtype_get (sdp, -1, k);
435 tmp3 = sdp_message_c_addr_get (sdp, -1, k);
436 tmp4 = sdp_message_c_addr_multicast_ttl_get (sdp, -1, k);
437 tmp5 = sdp_message_c_addr_multicast_int_get (sdp, -1, k);
438 if (tmp != NULL && tmp4 != NULL && tmp5 != NULL)
439 printf ("c_connection: |%s| |%s| |%s| |%s| |%s|\n",
440 tmp, tmp2, tmp3, tmp4, tmp5);
441 else if (tmp != NULL && tmp4 != NULL)
442 printf ("c_connection: |%s| |%s| |%s| |%s| |%s|\n",
443 tmp, tmp2, tmp3, tmp4, "(null)");
444 else if (tmp != NULL && tmp5 != NULL)
445 printf ("c_connection: |%s| |%s| |%s| |%s| |%s|\n",
446 tmp, tmp2, tmp3, "(null)", tmp5);
450 tmp = sdp_message_b_bwtype_get (sdp, -1, k);
451 tmp2 = sdp_message_b_bandwidth_get (sdp, -1, k);
452 if (tmp != NULL && tmp2 != NULL)
453 printf ("b_bandwidth: |%s|:|%s|\n", tmp, tmp2);
454 else if (tmp != NULL)
455 printf ("b_bandwidth: |%s|:|%s|\n", tmp, "(null)");
463 tmp = sdp_message_t_start_time_get (sdp, k);
464 tmp2 = sdp_message_t_stop_time_get (sdp, k);
465 if (tmp != NULL && tmp2 != NULL)
466 printf ("t_descr_time: |%s| |%s|\n", tmp, tmp2);
467 else if (tmp != NULL)
468 printf ("t_descr_time: |%s| |%s|\n", tmp, "(null)");
472 tmp2 = sdp_message_r_repeat_get (sdp, k, i);
475 printf ("r_repeat: |%s|\n", tmp2);
477 while (tmp2 != NULL);
484 if (sdp_message_z_adjustments_get (sdp) != NULL)
485 printf ("z_adjustments: |%s|\n", sdp_message_z_adjustments_get (sdp));
487 tmp = sdp_message_k_keytype_get (sdp, -1);
488 tmp2 = sdp_message_k_keydata_get (sdp, -1);
489 if (tmp != NULL && tmp2 != NULL)
490 printf ("k_key: |%s|:|%s|\n", tmp, tmp2);
491 else if (tmp != NULL)
492 printf ("k_key: |%s|:|%s|\n", tmp, "(null)");
497 tmp = sdp_message_a_att_field_get (sdp, -1, k);
498 tmp2 = sdp_message_a_att_value_get (sdp, -1, k);
499 if (tmp != NULL && tmp2 != NULL)
500 printf ("a_attribute: |%s|:|%s|\n", tmp, tmp2);
502 printf ("a_attribute: |%s|:|%s|\n", tmp, "(null)");
508 while (!sdp_message_endof_media (sdp, i))
511 tmp = sdp_message_m_media_get (sdp, i);
512 tmp2 = sdp_message_m_port_get (sdp, i);
513 tmp3 = sdp_message_m_number_of_port_get (sdp, i);
514 tmp4 = sdp_message_m_proto_get (sdp, i);
516 printf ("m_media: |%s|", tmp);
518 printf ("m_media: |%s|", "(null)");
520 printf (" |%s|", tmp2);
522 printf (" |%s|", "(null)");
524 printf (" |%s|", tmp3);
526 printf (" |%s|", "(null)");
528 printf (" |%s|", tmp4);
530 printf (" |%s|", "(null)");
534 tmp = sdp_message_m_payload_get (sdp, i, k);
536 printf (" |%s|", tmp);
544 tmp = sdp_message_c_nettype_get (sdp, i, k);
545 tmp2 = sdp_message_c_addrtype_get (sdp, i, k);
546 tmp3 = sdp_message_c_addr_get (sdp, i, k);
547 tmp4 = sdp_message_c_addr_multicast_ttl_get (sdp, i, k);
548 tmp5 = sdp_message_c_addr_multicast_int_get (sdp, i, k);
550 printf ("c_connection: |%s| |%s| |%s| |%s| |%s|\n",
551 tmp, tmp2, tmp3, tmp4, tmp5);
553 printf ("c_connection: |%s|", "(null)");
555 printf (" |%s|", tmp2);
557 printf (" |%s|", "(null)");
559 printf (" |%s|", tmp3);
561 printf (" |%s|", "(null)");
563 printf (" |%s|", tmp4);
565 printf (" |%s|", "(null)");
567 printf (" |%s|", tmp5);
569 printf (" |%s|", "(null)");
580 tmp = sdp_message_b_bwtype_get (sdp, i, k);
581 tmp2 = sdp_message_b_bandwidth_get (sdp, i, k);
583 printf ("b_bandwidth: |%s|", tmp);
585 printf ("b_bandwidth: |%s|", "(null)");
587 printf (":|%s|\n", tmp2);
589 printf (":|%s|", "(null)");
597 tmp = sdp_message_k_keytype_get (sdp, i);
598 tmp2 = sdp_message_k_keydata_get (sdp, i);
600 printf ("k_key: |%s|", tmp);
602 printf ("k_key: |%s|", "(null)");
604 printf (":|%s|", tmp2);
606 printf (":|%s|", "(null)");
612 tmp = sdp_message_a_att_field_get (sdp, i, k);
613 tmp2 = sdp_message_a_att_value_get (sdp, i, k);
615 printf ("a_attribute: |%s|", tmp);
617 printf ("a_attribute: |%s|", "(null)");
619 printf (":|%s|", tmp2);
621 printf (":|%s|", "(null)");
636 ua_sdp_set_info(osip_negotiation_ctx_t *context, sdp_message_t *dest) {
641 ua_sdp_set_uri(osip_negotiation_ctx_t *context, sdp_message_t *dest) {
646 ua_sdp_add_email(osip_negotiation_ctx_t *context, sdp_message_t *dest) {
651 ua_sdp_add_phone(osip_negotiation_ctx_t *context, sdp_message_t *dest) {
656 ua_sdp_add_attributes(osip_negotiation_ctx_t *context, sdp_message_t *dest, int pos_media) {