f242ad70d6bdad0d51249b1355872e79f93cac29
[platform/upstream/gstreamer.git] / gst-libs / gst / rtsp / gstrtspconnection.c
1 /* GStreamer
2  * Copyright (C) <2005-2009> Wim Taymans <wim.taymans@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 /*
20  * Unless otherwise indicated, Source Code is licensed under MIT license.
21  * See further explanation attached in License Statement (distributed in the file
22  * LICENSE).
23  *
24  * Permission is hereby granted, free of charge, to any person obtaining a copy of
25  * this software and associated documentation files (the "Software"), to deal in
26  * the Software without restriction, including without limitation the rights to
27  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
28  * of the Software, and to permit persons to whom the Software is furnished to do
29  * so, subject to the following conditions:
30  *
31  * The above copyright notice and this permission notice shall be included in all
32  * copies or substantial portions of the Software.
33  *
34  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40  * SOFTWARE.
41  */
42
43 /**
44  * SECTION:gstrtspconnection
45  * @short_description: manage RTSP connections
46  * @see_also: gstrtspurl
47  *  
48  * This object manages the RTSP connection to the server. It provides function
49  * to receive and send bytes and messages.
50  *  
51  * Last reviewed on 2007-07-24 (0.10.14)
52  */
53
54 #ifdef HAVE_CONFIG_H
55 #  include <config.h>
56 #endif
57
58 #include <stdio.h>
59 #include <errno.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <time.h>
63
64 /* we include this here to get the G_OS_* defines */
65 #include <glib.h>
66 #include <gst/gst.h>
67
68 #include "gstrtspconnection.h"
69
70 #ifdef IP_TOS
71 union gst_sockaddr
72 {
73   struct sockaddr sa;
74   struct sockaddr_in sa_in;
75   struct sockaddr_in6 sa_in6;
76   struct sockaddr_storage sa_stor;
77 };
78 #endif
79
80 typedef struct
81 {
82   gint state;
83   guint save;
84   guchar out[3];                /* the size must be evenly divisible by 3 */
85   guint cout;
86   guint coutl;
87 } DecodeCtx;
88
89 #ifdef MSG_NOSIGNAL
90 #define SEND_FLAGS MSG_NOSIGNAL
91 #else
92 #define SEND_FLAGS 0
93 #endif
94
95 typedef enum
96 {
97   TUNNEL_STATE_NONE,
98   TUNNEL_STATE_GET,
99   TUNNEL_STATE_POST,
100   TUNNEL_STATE_COMPLETE
101 } GstRTSPTunnelState;
102
103 #define TUNNELID_LEN   24
104
105 struct _GstRTSPConnection
106 {
107   /*< private > */
108   /* URL for the connection */
109   GstRTSPUrl *url;
110
111   /* connection state */
112   GSocket *read_socket;
113   GSocket *write_socket;
114   gboolean manual_http;
115   GSocket *socket0, *socket1;
116   GCancellable *cancellable;
117
118   gchar tunnelid[TUNNELID_LEN];
119   gboolean tunneled;
120   GstRTSPTunnelState tstate;
121
122   gchar *ip;
123
124   gint read_ahead;
125
126   gchar *initial_buffer;
127   gsize initial_buffer_offset;
128
129   /* Session state */
130   gint cseq;                    /* sequence number */
131   gchar session_id[512];        /* session id */
132   gint timeout;                 /* session timeout in seconds */
133   GTimer *timer;                /* timeout timer */
134
135   /* Authentication */
136   GstRTSPAuthMethod auth_method;
137   gchar *username;
138   gchar *passwd;
139   GHashTable *auth_params;
140
141   DecodeCtx ctx;
142   DecodeCtx *ctxp;
143
144   gchar *proxy_host;
145   guint proxy_port;
146 };
147
148 enum
149 {
150   STATE_START = 0,
151   STATE_DATA_HEADER,
152   STATE_DATA_BODY,
153   STATE_READ_LINES,
154   STATE_END,
155   STATE_LAST
156 };
157
158 enum
159 {
160   READ_AHEAD_EOH = -1,          /* end of headers */
161   READ_AHEAD_CRLF = -2,
162   READ_AHEAD_CRLFCR = -3
163 };
164
165 /* a structure for constructing RTSPMessages */
166 typedef struct
167 {
168   gint state;
169   GstRTSPResult status;
170   guint8 buffer[4096];
171   guint offset;
172
173   guint line;
174   guint8 *body_data;
175   glong body_len;
176 } GstRTSPBuilder;
177
178 static void
179 build_reset (GstRTSPBuilder * builder)
180 {
181   g_free (builder->body_data);
182   memset (builder, 0, sizeof (GstRTSPBuilder));
183 }
184
185 /**
186  * gst_rtsp_connection_create:
187  * @url: a #GstRTSPUrl 
188  * @conn: (out) (transfer full): storage for a #GstRTSPConnection
189  *
190  * Create a newly allocated #GstRTSPConnection from @url and store it in @conn.
191  * The connection will not yet attempt to connect to @url, use
192  * gst_rtsp_connection_connect().
193  *
194  * A copy of @url will be made.
195  *
196  * Returns: #GST_RTSP_OK when @conn contains a valid connection.
197  */
198 GstRTSPResult
199 gst_rtsp_connection_create (const GstRTSPUrl * url, GstRTSPConnection ** conn)
200 {
201   GstRTSPConnection *newconn;
202
203   g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
204
205   newconn = g_new0 (GstRTSPConnection, 1);
206
207   newconn->cancellable = g_cancellable_new ();
208
209   newconn->url = gst_rtsp_url_copy (url);
210   newconn->timer = g_timer_new ();
211   newconn->timeout = 60;
212   newconn->cseq = 1;
213
214   newconn->auth_method = GST_RTSP_AUTH_NONE;
215   newconn->username = NULL;
216   newconn->passwd = NULL;
217   newconn->auth_params = NULL;
218
219   *conn = newconn;
220
221   return GST_RTSP_OK;
222 }
223
224 /**
225  * gst_rtsp_connection_create_from_socket:
226  * @socket: a #GSocket
227  * @ip: the IP address of the other end
228  * @port: the port used by the other end
229  * @initial_buffer: data already read from @fd
230  * @conn: (out) (transfer full): storage for a #GstRTSPConnection
231  *
232  * Create a new #GstRTSPConnection for handling communication on the existing
233  * socket @socket. The @initial_buffer contains any data already read from
234  * @socket which should be used before starting to read new data.
235  *
236  * Returns: #GST_RTSP_OK when @conn contains a valid connection.
237  */
238 GstRTSPResult
239 gst_rtsp_connection_create_from_socket (GSocket * socket, const gchar * ip,
240     guint16 port, const gchar * initial_buffer, GstRTSPConnection ** conn)
241 {
242   GstRTSPConnection *newconn = NULL;
243   GstRTSPUrl *url;
244   GstRTSPResult res;
245
246   g_return_val_if_fail (G_IS_SOCKET (socket), GST_RTSP_EINVAL);
247   g_return_val_if_fail (ip != NULL, GST_RTSP_EINVAL);
248   g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
249
250   /* set to non-blocking mode so that we can cancel the communication */
251   g_socket_set_blocking (socket, FALSE);
252
253   /* create a url for the client address */
254   url = g_new0 (GstRTSPUrl, 1);
255   url->host = g_strdup (ip);
256   url->port = port;
257
258   /* now create the connection object */
259   GST_RTSP_CHECK (gst_rtsp_connection_create (url, &newconn), newconn_failed);
260   gst_rtsp_url_free (url);
261
262   /* both read and write initially */
263   newconn->socket0 = G_SOCKET (g_object_ref (socket));
264   newconn->socket1 = G_SOCKET (g_object_ref (socket));
265   newconn->write_socket = newconn->read_socket = newconn->socket0;
266
267   newconn->ip = g_strdup (ip);
268
269   newconn->initial_buffer = g_strdup (initial_buffer);
270
271   *conn = newconn;
272
273   return GST_RTSP_OK;
274
275   /* ERRORS */
276 newconn_failed:
277   {
278     gst_rtsp_url_free (url);
279     return res;
280   }
281 }
282
283 /**
284  * gst_rtsp_connection_accept:
285  * @socket: a socket
286  * @conn: (out) (transfer full): storage for a #GstRTSPConnection
287  * @cancellable: a #GCancellable to cancel the operation
288  *
289  * Accept a new connection on @socket and create a new #GstRTSPConnection for
290  * handling communication on new socket.
291  *
292  * Returns: #GST_RTSP_OK when @conn contains a valid connection.
293  */
294 GstRTSPResult
295 gst_rtsp_connection_accept (GSocket * socket, GstRTSPConnection ** conn,
296     GCancellable * cancellable)
297 {
298   GError *err = NULL;
299   gchar *ip;
300   guint16 port;
301   GSocket *client_sock;
302   GSocketAddress *addr;
303   GstRTSPResult ret;
304
305   g_return_val_if_fail (G_IS_SOCKET (socket), GST_RTSP_EINVAL);
306   g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
307
308   client_sock = g_socket_accept (socket, cancellable, &err);
309   if (!client_sock)
310     goto accept_failed;
311
312   addr = g_socket_get_remote_address (client_sock, &err);
313   if (!addr)
314     goto getnameinfo_failed;
315
316   ip = g_inet_address_to_string (g_inet_socket_address_get_address
317       (G_INET_SOCKET_ADDRESS (addr)));
318   port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
319   g_object_unref (addr);
320
321   ret =
322       gst_rtsp_connection_create_from_socket (client_sock, ip, port, NULL,
323       conn);
324   g_object_unref (client_sock);
325   g_free (ip);
326
327   return ret;
328
329   /* ERRORS */
330 accept_failed:
331   {
332     GST_DEBUG ("Accepting client failed: %s", err->message);
333     g_clear_error (&err);
334     return GST_RTSP_ESYS;
335   }
336 getnameinfo_failed:
337   {
338     if (!g_socket_close (client_sock, &err)) {
339       GST_DEBUG ("Closing socket failed: %s", err->message);
340       g_clear_error (&err);
341     }
342     g_object_unref (client_sock);
343     return GST_RTSP_ERROR;
344   }
345 }
346
347 static gchar *
348 do_resolve (const gchar * host, GCancellable * cancellable)
349 {
350   GResolver *resolver;
351   GInetAddress *addr;
352   GError *err = NULL;
353   gchar *ip;
354
355   addr = g_inet_address_new_from_string (host);
356   if (!addr) {
357     GList *results, *l;
358
359     resolver = g_resolver_get_default ();
360
361     results = g_resolver_lookup_by_name (resolver, host, cancellable, &err);
362     if (!results)
363       goto name_resolve;
364
365     for (l = results; l; l = l->next) {
366       GInetAddress *tmp = l->data;
367
368       if (g_inet_address_get_family (tmp) == G_SOCKET_FAMILY_IPV4 ||
369           g_inet_address_get_family (tmp) == G_SOCKET_FAMILY_IPV6) {
370         addr = G_INET_ADDRESS (g_object_ref (tmp));
371         break;
372       }
373     }
374
375     g_resolver_free_addresses (results);
376     g_object_unref (resolver);
377   }
378
379   if (!addr)
380     return NULL;
381
382   ip = g_inet_address_to_string (addr);
383   g_object_unref (addr);
384
385   return ip;
386
387   /* ERRORS */
388 name_resolve:
389   {
390     GST_ERROR ("failed to resolve %s: %s", host, err->message);
391     g_clear_error (&err);
392     g_object_unref (resolver);
393     return NULL;
394   }
395 }
396
397 static GstRTSPResult
398 do_connect (const gchar * ip, guint16 port, GSocket ** socket_out,
399     GTimeVal * timeout, GCancellable * cancellable)
400 {
401   GSocket *socket;
402   GstClockTime to;
403   GInetAddress *addr;
404   GSocketAddress *saddr;
405   GError *err = NULL;
406
407   addr = g_inet_address_new_from_string (ip);
408   g_assert (addr);
409   saddr = g_inet_socket_address_new (addr, port);
410   g_object_unref (addr);
411
412   socket =
413       g_socket_new (g_socket_address_get_family (saddr), G_SOCKET_TYPE_STREAM,
414       G_SOCKET_PROTOCOL_TCP, &err);
415   if (socket == NULL)
416     goto no_socket;
417
418   /* set to non-blocking mode so that we can cancel the connect */
419   g_socket_set_blocking (socket, FALSE);
420
421   /* we are going to connect ASYNC now */
422   if (!g_socket_connect (socket, saddr, cancellable, &err)) {
423     if (!g_error_matches (err, G_IO_ERROR, G_IO_ERROR_PENDING))
424       goto sys_error;
425     g_clear_error (&err);
426   } else {
427     goto done;
428   }
429
430   /* wait for connect to complete up to the specified timeout or until we got
431    * interrupted. */
432   to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
433
434   g_socket_set_timeout (socket, (to + GST_SECOND - 1) / GST_SECOND);
435   if (!g_socket_condition_wait (socket, G_IO_OUT, cancellable, &err)) {
436     g_socket_set_timeout (socket, 0);
437     if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_TIMED_OUT))
438       goto timeout;
439     else
440       goto sys_error;
441   }
442   g_socket_set_timeout (socket, 0);
443
444   if (!g_socket_check_connect_result (socket, &err))
445     goto sys_error;
446
447 done:
448   g_object_unref (saddr);
449
450   *socket_out = socket;
451
452   return GST_RTSP_OK;
453
454   /* ERRORS */
455 no_socket:
456   {
457     GST_ERROR ("no socket: %s", err->message);
458     g_clear_error (&err);
459     g_object_unref (saddr);
460     return GST_RTSP_ESYS;
461   }
462 sys_error:
463   {
464     GST_ERROR ("system error: %s", err->message);
465     g_clear_error (&err);
466     g_object_unref (saddr);
467     g_object_unref (socket);
468     return GST_RTSP_ESYS;
469   }
470 timeout:
471   {
472     GST_ERROR ("timeout");
473     g_clear_error (&err);
474     g_object_unref (saddr);
475     g_object_unref (socket);
476     return GST_RTSP_ETIMEOUT;
477   }
478 }
479
480 static GstRTSPResult
481 setup_tunneling (GstRTSPConnection * conn, GTimeVal * timeout)
482 {
483   gint i;
484   GstRTSPResult res;
485   gchar *ip;
486   gchar *uri;
487   gchar *value;
488   guint16 port, url_port;
489   GstRTSPUrl *url;
490   gchar *hostparam;
491   GstRTSPMessage *msg;
492   GstRTSPMessage response;
493   gboolean old_http;
494
495   memset (&response, 0, sizeof (response));
496   gst_rtsp_message_init (&response);
497
498   /* create a random sessionid */
499   for (i = 0; i < TUNNELID_LEN; i++)
500     conn->tunnelid[i] = g_random_int_range ('a', 'z');
501   conn->tunnelid[TUNNELID_LEN - 1] = '\0';
502
503   url = conn->url;
504   /* get the port from the url */
505   gst_rtsp_url_get_port (url, &url_port);
506
507   if (conn->proxy_host) {
508     uri = g_strdup_printf ("http://%s:%d%s%s%s", url->host, url_port,
509         url->abspath, url->query ? "?" : "", url->query ? url->query : "");
510     hostparam = g_strdup_printf ("%s:%d", url->host, url_port);
511     ip = conn->proxy_host;
512     port = conn->proxy_port;
513   } else {
514     uri = g_strdup_printf ("%s%s%s", url->abspath, url->query ? "?" : "",
515         url->query ? url->query : "");
516     hostparam = NULL;
517     ip = conn->ip;
518     port = url_port;
519   }
520
521   /* create the GET request for the read connection */
522   GST_RTSP_CHECK (gst_rtsp_message_new_request (&msg, GST_RTSP_GET, uri),
523       no_message);
524   msg->type = GST_RTSP_MESSAGE_HTTP_REQUEST;
525
526   if (hostparam != NULL)
527     gst_rtsp_message_add_header (msg, GST_RTSP_HDR_HOST, hostparam);
528   gst_rtsp_message_add_header (msg, GST_RTSP_HDR_X_SESSIONCOOKIE,
529       conn->tunnelid);
530   gst_rtsp_message_add_header (msg, GST_RTSP_HDR_ACCEPT,
531       "application/x-rtsp-tunnelled");
532   gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CACHE_CONTROL, "no-cache");
533   gst_rtsp_message_add_header (msg, GST_RTSP_HDR_PRAGMA, "no-cache");
534
535   /* we start by writing to this fd */
536   conn->write_socket = conn->socket0;
537
538   /* we need to temporarily set conn->tunneled to FALSE to prevent the HTTP
539    * request from being base64 encoded */
540   conn->tunneled = FALSE;
541   GST_RTSP_CHECK (gst_rtsp_connection_send (conn, msg, timeout), write_failed);
542   gst_rtsp_message_free (msg);
543   conn->tunneled = TRUE;
544
545   /* receive the response to the GET request */
546   /* we need to temporarily set manual_http to TRUE since
547    * gst_rtsp_connection_receive() will treat the HTTP response as a parsing
548    * failure otherwise */
549   old_http = conn->manual_http;
550   conn->manual_http = TRUE;
551   GST_RTSP_CHECK (gst_rtsp_connection_receive (conn, &response, timeout),
552       read_failed);
553   conn->manual_http = old_http;
554
555   if (response.type != GST_RTSP_MESSAGE_HTTP_RESPONSE ||
556       response.type_data.response.code != GST_RTSP_STS_OK)
557     goto wrong_result;
558
559   if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_X_SERVER_IP_ADDRESS,
560           &value, 0) == GST_RTSP_OK) {
561     if (conn->proxy_host) {
562       /* if we use a proxy we need to change the destination url */
563       g_free (url->host);
564       url->host = g_strdup (value);
565       g_free (hostparam);
566       hostparam = g_strdup_printf ("%s:%d", url->host, url_port);
567     } else {
568       /* and resolve the new ip address */
569       if (!(ip = do_resolve (value, conn->cancellable)))
570         goto not_resolved;
571       g_free (conn->ip);
572       conn->ip = ip;
573     }
574   }
575
576   /* connect to the host/port */
577   res = do_connect (ip, port, &conn->socket1, timeout, conn->cancellable);
578   if (res != GST_RTSP_OK)
579     goto connect_failed;
580
581   /* this is now our writing socket */
582   conn->write_socket = conn->socket1;
583
584   /* create the POST request for the write connection */
585   GST_RTSP_CHECK (gst_rtsp_message_new_request (&msg, GST_RTSP_POST, uri),
586       no_message);
587   msg->type = GST_RTSP_MESSAGE_HTTP_REQUEST;
588
589   if (hostparam != NULL)
590     gst_rtsp_message_add_header (msg, GST_RTSP_HDR_HOST, hostparam);
591   gst_rtsp_message_add_header (msg, GST_RTSP_HDR_X_SESSIONCOOKIE,
592       conn->tunnelid);
593   gst_rtsp_message_add_header (msg, GST_RTSP_HDR_ACCEPT,
594       "application/x-rtsp-tunnelled");
595   gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CACHE_CONTROL, "no-cache");
596   gst_rtsp_message_add_header (msg, GST_RTSP_HDR_PRAGMA, "no-cache");
597   gst_rtsp_message_add_header (msg, GST_RTSP_HDR_EXPIRES,
598       "Sun, 9 Jan 1972 00:00:00 GMT");
599   gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CONTENT_LENGTH, "32767");
600
601   /* we need to temporarily set conn->tunneled to FALSE to prevent the HTTP
602    * request from being base64 encoded */
603   conn->tunneled = FALSE;
604   GST_RTSP_CHECK (gst_rtsp_connection_send (conn, msg, timeout), write_failed);
605   gst_rtsp_message_free (msg);
606   conn->tunneled = TRUE;
607
608 exit:
609   gst_rtsp_message_unset (&response);
610   g_free (hostparam);
611   g_free (uri);
612
613   return res;
614
615   /* ERRORS */
616 no_message:
617   {
618     GST_ERROR ("failed to create request (%d)", res);
619     goto exit;
620   }
621 write_failed:
622   {
623     GST_ERROR ("write failed (%d)", res);
624     gst_rtsp_message_free (msg);
625     conn->tunneled = TRUE;
626     goto exit;
627   }
628 read_failed:
629   {
630     GST_ERROR ("read failed (%d)", res);
631     conn->manual_http = FALSE;
632     goto exit;
633   }
634 wrong_result:
635   {
636     GST_ERROR ("got failure response %d %s", response.type_data.response.code,
637         response.type_data.response.reason);
638     res = GST_RTSP_ERROR;
639     goto exit;
640   }
641 not_resolved:
642   {
643     GST_ERROR ("could not resolve %s", conn->ip);
644     res = GST_RTSP_ENET;
645     goto exit;
646   }
647 connect_failed:
648   {
649     GST_ERROR ("failed to connect");
650     goto exit;
651   }
652 }
653
654 /**
655  * gst_rtsp_connection_connect:
656  * @conn: a #GstRTSPConnection 
657  * @timeout: a #GTimeVal timeout
658  *
659  * Attempt to connect to the url of @conn made with
660  * gst_rtsp_connection_create(). If @timeout is #NULL this function can block
661  * forever. If @timeout contains a valid timeout, this function will return
662  * #GST_RTSP_ETIMEOUT after the timeout expired.
663  *
664  * This function can be cancelled with gst_rtsp_connection_flush().
665  *
666  * Returns: #GST_RTSP_OK when a connection could be made.
667  */
668 GstRTSPResult
669 gst_rtsp_connection_connect (GstRTSPConnection * conn, GTimeVal * timeout)
670 {
671   GstRTSPResult res;
672   gchar *ip;
673   guint16 port;
674   GstRTSPUrl *url;
675
676   g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
677   g_return_val_if_fail (conn->url != NULL, GST_RTSP_EINVAL);
678   g_return_val_if_fail (conn->socket0 == NULL, GST_RTSP_EINVAL);
679
680   url = conn->url;
681
682   if (conn->proxy_host && conn->tunneled) {
683     if (!(ip = do_resolve (conn->proxy_host, conn->cancellable))) {
684       GST_ERROR ("could not resolve %s", conn->proxy_host);
685       goto not_resolved;
686     }
687     port = conn->proxy_port;
688     g_free (conn->proxy_host);
689     conn->proxy_host = ip;
690   } else {
691     if (!(ip = do_resolve (url->host, conn->cancellable))) {
692       GST_ERROR ("could not resolve %s", url->host);
693       goto not_resolved;
694     }
695     /* get the port from the url */
696     gst_rtsp_url_get_port (url, &port);
697
698     g_free (conn->ip);
699     conn->ip = ip;
700   }
701
702   /* connect to the host/port */
703   res = do_connect (ip, port, &conn->socket0, timeout, conn->cancellable);
704   if (res != GST_RTSP_OK)
705     goto connect_failed;
706
707   /* this is our read URL */
708   conn->read_socket = conn->socket0;
709
710   if (conn->tunneled) {
711     res = setup_tunneling (conn, timeout);
712     if (res != GST_RTSP_OK)
713       goto tunneling_failed;
714   } else {
715     conn->write_socket = conn->socket0;
716   }
717
718   return GST_RTSP_OK;
719
720 not_resolved:
721   {
722     return GST_RTSP_ENET;
723   }
724 connect_failed:
725   {
726     GST_ERROR ("failed to connect");
727     return res;
728   }
729 tunneling_failed:
730   {
731     GST_ERROR ("failed to setup tunneling");
732     return res;
733   }
734 }
735
736 static void
737 auth_digest_compute_hex_urp (const gchar * username,
738     const gchar * realm, const gchar * password, gchar hex_urp[33])
739 {
740   GChecksum *md5_context = g_checksum_new (G_CHECKSUM_MD5);
741   const gchar *digest_string;
742
743   g_checksum_update (md5_context, (const guchar *) username, strlen (username));
744   g_checksum_update (md5_context, (const guchar *) ":", 1);
745   g_checksum_update (md5_context, (const guchar *) realm, strlen (realm));
746   g_checksum_update (md5_context, (const guchar *) ":", 1);
747   g_checksum_update (md5_context, (const guchar *) password, strlen (password));
748   digest_string = g_checksum_get_string (md5_context);
749
750   memset (hex_urp, 0, 33);
751   memcpy (hex_urp, digest_string, strlen (digest_string));
752
753   g_checksum_free (md5_context);
754 }
755
756 static void
757 auth_digest_compute_response (const gchar * method,
758     const gchar * uri, const gchar * hex_a1, const gchar * nonce,
759     gchar response[33])
760 {
761   char hex_a2[33] = { 0, };
762   GChecksum *md5_context = g_checksum_new (G_CHECKSUM_MD5);
763   const gchar *digest_string;
764
765   /* compute A2 */
766   g_checksum_update (md5_context, (const guchar *) method, strlen (method));
767   g_checksum_update (md5_context, (const guchar *) ":", 1);
768   g_checksum_update (md5_context, (const guchar *) uri, strlen (uri));
769   digest_string = g_checksum_get_string (md5_context);
770   memcpy (hex_a2, digest_string, strlen (digest_string));
771
772   /* compute KD */
773   g_checksum_reset (md5_context);
774   g_checksum_update (md5_context, (const guchar *) hex_a1, strlen (hex_a1));
775   g_checksum_update (md5_context, (const guchar *) ":", 1);
776   g_checksum_update (md5_context, (const guchar *) nonce, strlen (nonce));
777   g_checksum_update (md5_context, (const guchar *) ":", 1);
778
779   g_checksum_update (md5_context, (const guchar *) hex_a2, 32);
780   digest_string = g_checksum_get_string (md5_context);
781   memset (response, 0, 33);
782   memcpy (response, digest_string, strlen (digest_string));
783
784   g_checksum_free (md5_context);
785 }
786
787 static void
788 add_auth_header (GstRTSPConnection * conn, GstRTSPMessage * message)
789 {
790   switch (conn->auth_method) {
791     case GST_RTSP_AUTH_BASIC:{
792       gchar *user_pass;
793       gchar *user_pass64;
794       gchar *auth_string;
795
796       if (conn->username == NULL || conn->passwd == NULL)
797         break;
798
799       user_pass = g_strdup_printf ("%s:%s", conn->username, conn->passwd);
800       user_pass64 = g_base64_encode ((guchar *) user_pass, strlen (user_pass));
801       auth_string = g_strdup_printf ("Basic %s", user_pass64);
802
803       gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
804           auth_string);
805
806       g_free (user_pass);
807       g_free (user_pass64);
808       break;
809     }
810     case GST_RTSP_AUTH_DIGEST:{
811       gchar response[33], hex_urp[33];
812       gchar *auth_string, *auth_string2;
813       gchar *realm;
814       gchar *nonce;
815       gchar *opaque;
816       const gchar *uri;
817       const gchar *method;
818
819       /* we need to have some params set */
820       if (conn->auth_params == NULL || conn->username == NULL ||
821           conn->passwd == NULL)
822         break;
823
824       /* we need the realm and nonce */
825       realm = (gchar *) g_hash_table_lookup (conn->auth_params, "realm");
826       nonce = (gchar *) g_hash_table_lookup (conn->auth_params, "nonce");
827       if (realm == NULL || nonce == NULL)
828         break;
829
830       auth_digest_compute_hex_urp (conn->username, realm, conn->passwd,
831           hex_urp);
832
833       method = gst_rtsp_method_as_text (message->type_data.request.method);
834       uri = message->type_data.request.uri;
835
836       /* Assume no qop, algorithm=md5, stale=false */
837       /* For algorithm MD5, a1 = urp. */
838       auth_digest_compute_response (method, uri, hex_urp, nonce, response);
839       auth_string = g_strdup_printf ("Digest username=\"%s\", "
840           "realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"",
841           conn->username, realm, nonce, uri, response);
842
843       opaque = (gchar *) g_hash_table_lookup (conn->auth_params, "opaque");
844       if (opaque) {
845         auth_string2 = g_strdup_printf ("%s, opaque=\"%s\"", auth_string,
846             opaque);
847         g_free (auth_string);
848         auth_string = auth_string2;
849       }
850       gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
851           auth_string);
852       break;
853     }
854     default:
855       /* Nothing to do */
856       break;
857   }
858 }
859
860 static void
861 gen_date_string (gchar * date_string, guint len)
862 {
863   static const char wkdays[7][4] =
864       { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
865   static const char months[12][4] =
866       { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
867     "Nov", "Dec"
868   };
869   struct tm tm;
870   time_t t;
871
872   time (&t);
873
874 #ifdef HAVE_GMTIME_R
875   gmtime_r (&t, &tm);
876 #else
877   tm = *gmtime (&t);
878 #endif
879
880   g_snprintf (date_string, len, "%s, %02d %s %04d %02d:%02d:%02d GMT",
881       wkdays[tm.tm_wday], tm.tm_mday, months[tm.tm_mon], tm.tm_year + 1900,
882       tm.tm_hour, tm.tm_min, tm.tm_sec);
883 }
884
885 static GstRTSPResult
886 write_bytes (GSocket * socket, const guint8 * buffer, guint * idx, guint size,
887     GCancellable * cancellable)
888 {
889   guint left;
890
891   if (G_UNLIKELY (*idx > size))
892     return GST_RTSP_ERROR;
893
894   left = size - *idx;
895
896   while (left) {
897     GError *err = NULL;
898     gssize r;
899
900     r = g_socket_send (socket, (gchar *) & buffer[*idx], left, cancellable,
901         &err);
902     if (G_UNLIKELY (r == 0)) {
903       return GST_RTSP_EINTR;
904     } else if (G_UNLIKELY (r < 0)) {
905       if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) {
906         g_clear_error (&err);
907         return GST_RTSP_EINTR;
908       }
909       g_clear_error (&err);
910       return GST_RTSP_ESYS;
911     } else {
912       left -= r;
913       *idx += r;
914     }
915   }
916   return GST_RTSP_OK;
917 }
918
919 static gint
920 fill_raw_bytes (GstRTSPConnection * conn, guint8 * buffer, guint size,
921     GError ** err)
922 {
923   gint out = 0;
924
925   if (G_UNLIKELY (conn->initial_buffer != NULL)) {
926     gsize left = strlen (&conn->initial_buffer[conn->initial_buffer_offset]);
927
928     out = MIN (left, size);
929     memcpy (buffer, &conn->initial_buffer[conn->initial_buffer_offset], out);
930
931     if (left == (gsize) out) {
932       g_free (conn->initial_buffer);
933       conn->initial_buffer = NULL;
934       conn->initial_buffer_offset = 0;
935     } else
936       conn->initial_buffer_offset += out;
937   }
938
939   if (G_LIKELY (size > (guint) out)) {
940     gssize r;
941
942     r = g_socket_receive (conn->read_socket, (gchar *) & buffer[out],
943         size - out, conn->cancellable, err);
944     if (r <= 0) {
945       if (out == 0)
946         out = r;
947     } else
948       out += r;
949   }
950
951   return out;
952 }
953
954 static gint
955 fill_bytes (GstRTSPConnection * conn, guint8 * buffer, guint size,
956     GError ** err)
957 {
958   DecodeCtx *ctx = conn->ctxp;
959   gint out = 0;
960
961   if (ctx) {
962     while (size > 0) {
963       guint8 in[sizeof (ctx->out) * 4 / 3];
964       gint r;
965
966       while (size > 0 && ctx->cout < ctx->coutl) {
967         /* we have some leftover bytes */
968         *buffer++ = ctx->out[ctx->cout++];
969         size--;
970         out++;
971       }
972
973       /* got what we needed? */
974       if (size == 0)
975         break;
976
977       /* try to read more bytes */
978       r = fill_raw_bytes (conn, in, sizeof (in), err);
979       if (r <= 0) {
980         if (out == 0)
981           out = r;
982         break;
983       }
984
985       ctx->cout = 0;
986       ctx->coutl =
987           g_base64_decode_step ((gchar *) in, r, ctx->out, &ctx->state,
988           &ctx->save);
989     }
990   } else {
991     out = fill_raw_bytes (conn, buffer, size, err);
992   }
993
994   return out;
995 }
996
997 static GstRTSPResult
998 read_bytes (GstRTSPConnection * conn, guint8 * buffer, guint * idx, guint size)
999 {
1000   guint left;
1001   GError *err = NULL;
1002
1003   if (G_UNLIKELY (*idx > size))
1004     return GST_RTSP_ERROR;
1005
1006   left = size - *idx;
1007
1008   while (left) {
1009     gint r;
1010
1011     r = fill_bytes (conn, &buffer[*idx], left, &err);
1012     if (G_UNLIKELY (r == 0)) {
1013       return GST_RTSP_EEOF;
1014     } else if (G_UNLIKELY (r < 0)) {
1015       if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) {
1016         g_clear_error (&err);
1017         return GST_RTSP_EINTR;
1018       }
1019       g_clear_error (&err);
1020       return GST_RTSP_ESYS;
1021     } else {
1022       left -= r;
1023       *idx += r;
1024     }
1025   }
1026   return GST_RTSP_OK;
1027 }
1028
1029 /* The code below tries to handle clients using \r, \n or \r\n to indicate the
1030  * end of a line. It even does its best to handle clients which mix them (even
1031  * though this is a really stupid idea (tm).) It also handles Line White Space
1032  * (LWS), where a line end followed by whitespace is considered LWS. This is
1033  * the method used in RTSP (and HTTP) to break long lines.
1034  */
1035 static GstRTSPResult
1036 read_line (GstRTSPConnection * conn, guint8 * buffer, guint * idx, guint size)
1037 {
1038   GError *err = NULL;
1039
1040   while (TRUE) {
1041     guint8 c;
1042     gint r;
1043
1044     if (conn->read_ahead == READ_AHEAD_EOH) {
1045       /* the last call to read_line() already determined that we have reached
1046        * the end of the headers, so convey that information now */
1047       conn->read_ahead = 0;
1048       break;
1049     } else if (conn->read_ahead == READ_AHEAD_CRLF) {
1050       /* the last call to read_line() left off after having read \r\n */
1051       c = '\n';
1052     } else if (conn->read_ahead == READ_AHEAD_CRLFCR) {
1053       /* the last call to read_line() left off after having read \r\n\r */
1054       c = '\r';
1055     } else if (conn->read_ahead != 0) {
1056       /* the last call to read_line() left us with a character to start with */
1057       c = (guint8) conn->read_ahead;
1058       conn->read_ahead = 0;
1059     } else {
1060       /* read the next character */
1061       r = fill_bytes (conn, &c, 1, &err);
1062       if (G_UNLIKELY (r == 0)) {
1063         return GST_RTSP_EEOF;
1064       } else if (G_UNLIKELY (r < 0)) {
1065         if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) {
1066           g_clear_error (&err);
1067           return GST_RTSP_EINTR;
1068         }
1069
1070         g_clear_error (&err);
1071         return GST_RTSP_ESYS;
1072       }
1073     }
1074
1075     /* special treatment of line endings */
1076     if (c == '\r' || c == '\n') {
1077       guint8 read_ahead;
1078
1079     retry:
1080       /* need to read ahead one more character to know what to do... */
1081       r = fill_bytes (conn, &read_ahead, 1, &err);
1082       if (G_UNLIKELY (r == 0)) {
1083         return GST_RTSP_EEOF;
1084       } else if (G_UNLIKELY (r < 0)) {
1085         if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) {
1086           /* remember the original character we read and try again next time */
1087           if (conn->read_ahead == 0)
1088             conn->read_ahead = c;
1089           return GST_RTSP_EINTR;
1090           g_clear_error (&err);
1091           return GST_RTSP_EINTR;
1092         }
1093
1094         g_clear_error (&err);
1095         return GST_RTSP_ESYS;
1096       }
1097
1098       if (read_ahead == ' ' || read_ahead == '\t') {
1099         if (conn->read_ahead == READ_AHEAD_CRLFCR) {
1100           /* got \r\n\r followed by whitespace, treat it as a normal line
1101            * followed by one starting with LWS */
1102           conn->read_ahead = read_ahead;
1103           break;
1104         } else {
1105           /* got LWS, change the line ending to a space and continue */
1106           c = ' ';
1107           conn->read_ahead = read_ahead;
1108         }
1109       } else if (conn->read_ahead == READ_AHEAD_CRLFCR) {
1110         if (read_ahead == '\r' || read_ahead == '\n') {
1111           /* got \r\n\r\r or \r\n\r\n, treat it as the end of the headers */
1112           conn->read_ahead = READ_AHEAD_EOH;
1113           break;
1114         } else {
1115           /* got \r\n\r followed by something else, this is not really
1116            * supported since we have probably just eaten the first character
1117            * of the body or the next message, so just ignore the second \r
1118            * and live with it... */
1119           conn->read_ahead = read_ahead;
1120           break;
1121         }
1122       } else if (conn->read_ahead == READ_AHEAD_CRLF) {
1123         if (read_ahead == '\r') {
1124           /* got \r\n\r so far, need one more character... */
1125           conn->read_ahead = READ_AHEAD_CRLFCR;
1126           goto retry;
1127         } else if (read_ahead == '\n') {
1128           /* got \r\n\n, treat it as the end of the headers */
1129           conn->read_ahead = READ_AHEAD_EOH;
1130           break;
1131         } else {
1132           /* found the end of a line, keep read_ahead for the next line */
1133           conn->read_ahead = read_ahead;
1134           break;
1135         }
1136       } else if (c == read_ahead) {
1137         /* got double \r or \n, treat it as the end of the headers */
1138         conn->read_ahead = READ_AHEAD_EOH;
1139         break;
1140       } else if (c == '\r' && read_ahead == '\n') {
1141         /* got \r\n so far, still need more to know what to do... */
1142         conn->read_ahead = READ_AHEAD_CRLF;
1143         goto retry;
1144       } else {
1145         /* found the end of a line, keep read_ahead for the next line */
1146         conn->read_ahead = read_ahead;
1147         break;
1148       }
1149     }
1150
1151     if (G_LIKELY (*idx < size - 1))
1152       buffer[(*idx)++] = c;
1153   }
1154   buffer[*idx] = '\0';
1155
1156   return GST_RTSP_OK;
1157 }
1158
1159 /**
1160  * gst_rtsp_connection_write:
1161  * @conn: a #GstRTSPConnection
1162  * @data: the data to write
1163  * @size: the size of @data
1164  * @timeout: a timeout value or #NULL
1165  *
1166  * Attempt to write @size bytes of @data to the connected @conn, blocking up to
1167  * the specified @timeout. @timeout can be #NULL, in which case this function
1168  * might block forever.
1169  * 
1170  * This function can be cancelled with gst_rtsp_connection_flush().
1171  *
1172  * Returns: #GST_RTSP_OK on success.
1173  */
1174 GstRTSPResult
1175 gst_rtsp_connection_write (GstRTSPConnection * conn, const guint8 * data,
1176     guint size, GTimeVal * timeout)
1177 {
1178   guint offset;
1179   GstClockTime to;
1180   GstRTSPResult res;
1181   GError *err = NULL;
1182
1183   g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1184   g_return_val_if_fail (data != NULL || size == 0, GST_RTSP_EINVAL);
1185   g_return_val_if_fail (conn->write_socket != NULL, GST_RTSP_EINVAL);
1186
1187   to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1188
1189   offset = 0;
1190
1191   while (TRUE) {
1192     /* try to write */
1193     res =
1194         write_bytes (conn->write_socket, data, &offset, size,
1195         conn->cancellable);
1196     if (G_LIKELY (res == GST_RTSP_OK))
1197       break;
1198     if (G_UNLIKELY (res != GST_RTSP_EINTR))
1199       goto write_error;
1200
1201     /* not all is written, wait until we can write more */
1202     g_socket_set_timeout (conn->write_socket,
1203         (to + GST_SECOND - 1) / GST_SECOND);
1204     if (!g_socket_condition_wait (conn->write_socket,
1205             G_IO_OUT | G_IO_ERR | G_IO_HUP, conn->cancellable, &err)) {
1206       g_socket_set_timeout (conn->write_socket, 0);
1207       if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_BUSY)) {
1208         g_clear_error (&err);
1209         goto stopped;
1210       } else if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_TIMED_OUT)) {
1211         g_clear_error (&err);
1212         goto timeout;
1213       }
1214       g_clear_error (&err);
1215       goto select_error;
1216     }
1217     g_socket_set_timeout (conn->write_socket, 0);
1218   }
1219   return GST_RTSP_OK;
1220
1221   /* ERRORS */
1222 timeout:
1223   {
1224     return GST_RTSP_ETIMEOUT;
1225   }
1226 select_error:
1227   {
1228     return GST_RTSP_ESYS;
1229   }
1230 stopped:
1231   {
1232     return GST_RTSP_EINTR;
1233   }
1234 write_error:
1235   {
1236     return res;
1237   }
1238 }
1239
1240 static GString *
1241 message_to_string (GstRTSPConnection * conn, GstRTSPMessage * message)
1242 {
1243   GString *str = NULL;
1244
1245   str = g_string_new ("");
1246
1247   switch (message->type) {
1248     case GST_RTSP_MESSAGE_REQUEST:
1249       /* create request string, add CSeq */
1250       g_string_append_printf (str, "%s %s RTSP/1.0\r\n"
1251           "CSeq: %d\r\n",
1252           gst_rtsp_method_as_text (message->type_data.request.method),
1253           message->type_data.request.uri, conn->cseq++);
1254       /* add session id if we have one */
1255       if (conn->session_id[0] != '\0') {
1256         gst_rtsp_message_remove_header (message, GST_RTSP_HDR_SESSION, -1);
1257         gst_rtsp_message_add_header (message, GST_RTSP_HDR_SESSION,
1258             conn->session_id);
1259       }
1260       /* add any authentication headers */
1261       add_auth_header (conn, message);
1262       break;
1263     case GST_RTSP_MESSAGE_RESPONSE:
1264       /* create response string */
1265       g_string_append_printf (str, "RTSP/1.0 %d %s\r\n",
1266           message->type_data.response.code, message->type_data.response.reason);
1267       break;
1268     case GST_RTSP_MESSAGE_HTTP_REQUEST:
1269       /* create request string */
1270       g_string_append_printf (str, "%s %s HTTP/%s\r\n",
1271           gst_rtsp_method_as_text (message->type_data.request.method),
1272           message->type_data.request.uri,
1273           gst_rtsp_version_as_text (message->type_data.request.version));
1274       /* add any authentication headers */
1275       add_auth_header (conn, message);
1276       break;
1277     case GST_RTSP_MESSAGE_HTTP_RESPONSE:
1278       /* create response string */
1279       g_string_append_printf (str, "HTTP/%s %d %s\r\n",
1280           gst_rtsp_version_as_text (message->type_data.request.version),
1281           message->type_data.response.code, message->type_data.response.reason);
1282       break;
1283     case GST_RTSP_MESSAGE_DATA:
1284     {
1285       guint8 data_header[4];
1286
1287       /* prepare data header */
1288       data_header[0] = '$';
1289       data_header[1] = message->type_data.data.channel;
1290       data_header[2] = (message->body_size >> 8) & 0xff;
1291       data_header[3] = message->body_size & 0xff;
1292
1293       /* create string with header and data */
1294       str = g_string_append_len (str, (gchar *) data_header, 4);
1295       str =
1296           g_string_append_len (str, (gchar *) message->body,
1297           message->body_size);
1298       break;
1299     }
1300     default:
1301       g_string_free (str, TRUE);
1302       g_return_val_if_reached (NULL);
1303       break;
1304   }
1305
1306   /* append headers and body */
1307   if (message->type != GST_RTSP_MESSAGE_DATA) {
1308     gchar date_string[100];
1309
1310     gen_date_string (date_string, sizeof (date_string));
1311
1312     /* add date header */
1313     gst_rtsp_message_remove_header (message, GST_RTSP_HDR_DATE, -1);
1314     gst_rtsp_message_add_header (message, GST_RTSP_HDR_DATE, date_string);
1315
1316     /* append headers */
1317     gst_rtsp_message_append_headers (message, str);
1318
1319     /* append Content-Length and body if needed */
1320     if (message->body != NULL && message->body_size > 0) {
1321       gchar *len;
1322
1323       len = g_strdup_printf ("%d", message->body_size);
1324       g_string_append_printf (str, "%s: %s\r\n",
1325           gst_rtsp_header_as_text (GST_RTSP_HDR_CONTENT_LENGTH), len);
1326       g_free (len);
1327       /* header ends here */
1328       g_string_append (str, "\r\n");
1329       str =
1330           g_string_append_len (str, (gchar *) message->body,
1331           message->body_size);
1332     } else {
1333       /* just end headers */
1334       g_string_append (str, "\r\n");
1335     }
1336   }
1337
1338   return str;
1339 }
1340
1341 /**
1342  * gst_rtsp_connection_send:
1343  * @conn: a #GstRTSPConnection
1344  * @message: the message to send
1345  * @timeout: a timeout value or #NULL
1346  *
1347  * Attempt to send @message to the connected @conn, blocking up to
1348  * the specified @timeout. @timeout can be #NULL, in which case this function
1349  * might block forever.
1350  * 
1351  * This function can be cancelled with gst_rtsp_connection_flush().
1352  *
1353  * Returns: #GST_RTSP_OK on success.
1354  */
1355 GstRTSPResult
1356 gst_rtsp_connection_send (GstRTSPConnection * conn, GstRTSPMessage * message,
1357     GTimeVal * timeout)
1358 {
1359   GString *string = NULL;
1360   GstRTSPResult res;
1361   gchar *str;
1362   gsize len;
1363
1364   g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1365   g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1366
1367   if (G_UNLIKELY (!(string = message_to_string (conn, message))))
1368     goto no_message;
1369
1370   if (conn->tunneled) {
1371     str = g_base64_encode ((const guchar *) string->str, string->len);
1372     g_string_free (string, TRUE);
1373     len = strlen (str);
1374   } else {
1375     str = string->str;
1376     len = string->len;
1377     g_string_free (string, FALSE);
1378   }
1379
1380   /* write request */
1381   res = gst_rtsp_connection_write (conn, (guint8 *) str, len, timeout);
1382
1383   g_free (str);
1384
1385   return res;
1386
1387 no_message:
1388   {
1389     g_warning ("Wrong message");
1390     return GST_RTSP_EINVAL;
1391   }
1392 }
1393
1394 static GstRTSPResult
1395 parse_string (gchar * dest, gint size, gchar ** src)
1396 {
1397   GstRTSPResult res = GST_RTSP_OK;
1398   gint idx;
1399
1400   idx = 0;
1401   /* skip spaces */
1402   while (g_ascii_isspace (**src))
1403     (*src)++;
1404
1405   while (!g_ascii_isspace (**src) && **src != '\0') {
1406     if (idx < size - 1)
1407       dest[idx++] = **src;
1408     else
1409       res = GST_RTSP_EPARSE;
1410     (*src)++;
1411   }
1412   if (size > 0)
1413     dest[idx] = '\0';
1414
1415   return res;
1416 }
1417
1418 static GstRTSPResult
1419 parse_protocol_version (gchar * protocol, GstRTSPMsgType * type,
1420     GstRTSPVersion * version)
1421 {
1422   GstRTSPResult res = GST_RTSP_OK;
1423   gchar *ver;
1424
1425   if (G_LIKELY ((ver = strchr (protocol, '/')) != NULL)) {
1426     guint major;
1427     guint minor;
1428     gchar dummychar;
1429
1430     *ver++ = '\0';
1431
1432     /* the version number must be formatted as X.Y with nothing following */
1433     if (sscanf (ver, "%u.%u%c", &major, &minor, &dummychar) != 2)
1434       res = GST_RTSP_EPARSE;
1435
1436     if (g_ascii_strcasecmp (protocol, "RTSP") == 0) {
1437       if (major != 1 || minor != 0) {
1438         *version = GST_RTSP_VERSION_INVALID;
1439         res = GST_RTSP_ERROR;
1440       }
1441     } else if (g_ascii_strcasecmp (protocol, "HTTP") == 0) {
1442       if (*type == GST_RTSP_MESSAGE_REQUEST)
1443         *type = GST_RTSP_MESSAGE_HTTP_REQUEST;
1444       else if (*type == GST_RTSP_MESSAGE_RESPONSE)
1445         *type = GST_RTSP_MESSAGE_HTTP_RESPONSE;
1446
1447       if (major == 1 && minor == 1) {
1448         *version = GST_RTSP_VERSION_1_1;
1449       } else if (major != 1 || minor != 0) {
1450         *version = GST_RTSP_VERSION_INVALID;
1451         res = GST_RTSP_ERROR;
1452       }
1453     } else
1454       res = GST_RTSP_EPARSE;
1455   } else
1456     res = GST_RTSP_EPARSE;
1457
1458   return res;
1459 }
1460
1461 static GstRTSPResult
1462 parse_response_status (guint8 * buffer, GstRTSPMessage * msg)
1463 {
1464   GstRTSPResult res = GST_RTSP_OK;
1465   GstRTSPResult res2;
1466   gchar versionstr[20];
1467   gchar codestr[4];
1468   gint code;
1469   gchar *bptr;
1470
1471   bptr = (gchar *) buffer;
1472
1473   if (parse_string (versionstr, sizeof (versionstr), &bptr) != GST_RTSP_OK)
1474     res = GST_RTSP_EPARSE;
1475
1476   if (parse_string (codestr, sizeof (codestr), &bptr) != GST_RTSP_OK)
1477     res = GST_RTSP_EPARSE;
1478   code = atoi (codestr);
1479   if (G_UNLIKELY (*codestr == '\0' || code < 0 || code >= 600))
1480     res = GST_RTSP_EPARSE;
1481
1482   while (g_ascii_isspace (*bptr))
1483     bptr++;
1484
1485   if (G_UNLIKELY (gst_rtsp_message_init_response (msg, code, bptr,
1486               NULL) != GST_RTSP_OK))
1487     res = GST_RTSP_EPARSE;
1488
1489   res2 = parse_protocol_version (versionstr, &msg->type,
1490       &msg->type_data.response.version);
1491   if (G_LIKELY (res == GST_RTSP_OK))
1492     res = res2;
1493
1494   return res;
1495 }
1496
1497 static GstRTSPResult
1498 parse_request_line (guint8 * buffer, GstRTSPMessage * msg)
1499 {
1500   GstRTSPResult res = GST_RTSP_OK;
1501   GstRTSPResult res2;
1502   gchar versionstr[20];
1503   gchar methodstr[20];
1504   gchar urlstr[4096];
1505   gchar *bptr;
1506   GstRTSPMethod method;
1507
1508   bptr = (gchar *) buffer;
1509
1510   if (parse_string (methodstr, sizeof (methodstr), &bptr) != GST_RTSP_OK)
1511     res = GST_RTSP_EPARSE;
1512   method = gst_rtsp_find_method (methodstr);
1513
1514   if (parse_string (urlstr, sizeof (urlstr), &bptr) != GST_RTSP_OK)
1515     res = GST_RTSP_EPARSE;
1516   if (G_UNLIKELY (*urlstr == '\0'))
1517     res = GST_RTSP_EPARSE;
1518
1519   if (parse_string (versionstr, sizeof (versionstr), &bptr) != GST_RTSP_OK)
1520     res = GST_RTSP_EPARSE;
1521
1522   if (G_UNLIKELY (*bptr != '\0'))
1523     res = GST_RTSP_EPARSE;
1524
1525   if (G_UNLIKELY (gst_rtsp_message_init_request (msg, method,
1526               urlstr) != GST_RTSP_OK))
1527     res = GST_RTSP_EPARSE;
1528
1529   res2 = parse_protocol_version (versionstr, &msg->type,
1530       &msg->type_data.request.version);
1531   if (G_LIKELY (res == GST_RTSP_OK))
1532     res = res2;
1533
1534   if (G_LIKELY (msg->type == GST_RTSP_MESSAGE_REQUEST)) {
1535     /* GET and POST are not allowed as RTSP methods */
1536     if (msg->type_data.request.method == GST_RTSP_GET ||
1537         msg->type_data.request.method == GST_RTSP_POST) {
1538       msg->type_data.request.method = GST_RTSP_INVALID;
1539       if (res == GST_RTSP_OK)
1540         res = GST_RTSP_ERROR;
1541     }
1542   } else if (msg->type == GST_RTSP_MESSAGE_HTTP_REQUEST) {
1543     /* only GET and POST are allowed as HTTP methods */
1544     if (msg->type_data.request.method != GST_RTSP_GET &&
1545         msg->type_data.request.method != GST_RTSP_POST) {
1546       msg->type_data.request.method = GST_RTSP_INVALID;
1547       if (res == GST_RTSP_OK)
1548         res = GST_RTSP_ERROR;
1549     }
1550   }
1551
1552   return res;
1553 }
1554
1555 /* parsing lines means reading a Key: Value pair */
1556 static GstRTSPResult
1557 parse_line (guint8 * buffer, GstRTSPMessage * msg)
1558 {
1559   GstRTSPHeaderField field;
1560   gchar *line = (gchar *) buffer;
1561   gchar *value;
1562
1563   if ((value = strchr (line, ':')) == NULL || value == line)
1564     goto parse_error;
1565
1566   /* trim space before the colon */
1567   if (value[-1] == ' ')
1568     value[-1] = '\0';
1569
1570   /* replace the colon with a NUL */
1571   *value++ = '\0';
1572
1573   /* find the header */
1574   field = gst_rtsp_find_header_field (line);
1575   if (field == GST_RTSP_HDR_INVALID)
1576     goto done;
1577
1578   /* split up the value in multiple key:value pairs if it contains comma(s) */
1579   while (*value != '\0') {
1580     gchar *next_value;
1581     gchar *comma = NULL;
1582     gboolean quoted = FALSE;
1583     guint comment = 0;
1584
1585     /* trim leading space */
1586     if (*value == ' ')
1587       value++;
1588
1589     /* for headers which may not appear multiple times, and thus may not
1590      * contain multiple values on the same line, we can short-circuit the loop
1591      * below and the entire value results in just one key:value pair*/
1592     if (!gst_rtsp_header_allow_multiple (field))
1593       next_value = value + strlen (value);
1594     else
1595       next_value = value;
1596
1597     /* find the next value, taking special care of quotes and comments */
1598     while (*next_value != '\0') {
1599       if ((quoted || comment != 0) && *next_value == '\\' &&
1600           next_value[1] != '\0')
1601         next_value++;
1602       else if (comment == 0 && *next_value == '"')
1603         quoted = !quoted;
1604       else if (!quoted && *next_value == '(')
1605         comment++;
1606       else if (comment != 0 && *next_value == ')')
1607         comment--;
1608       else if (!quoted && comment == 0) {
1609         /* To quote RFC 2068: "User agents MUST take special care in parsing
1610          * the WWW-Authenticate field value if it contains more than one
1611          * challenge, or if more than one WWW-Authenticate header field is
1612          * provided, since the contents of a challenge may itself contain a
1613          * comma-separated list of authentication parameters."
1614          *
1615          * What this means is that we cannot just look for an unquoted comma
1616          * when looking for multiple values in Proxy-Authenticate and
1617          * WWW-Authenticate headers. Instead we need to look for the sequence
1618          * "comma [space] token space token" before we can split after the
1619          * comma...
1620          */
1621         if (field == GST_RTSP_HDR_PROXY_AUTHENTICATE ||
1622             field == GST_RTSP_HDR_WWW_AUTHENTICATE) {
1623           if (*next_value == ',') {
1624             if (next_value[1] == ' ') {
1625               /* skip any space following the comma so we do not mistake it for
1626                * separating between two tokens */
1627               next_value++;
1628             }
1629             comma = next_value;
1630           } else if (*next_value == ' ' && next_value[1] != ',' &&
1631               next_value[1] != '=' && comma != NULL) {
1632             next_value = comma;
1633             comma = NULL;
1634             break;
1635           }
1636         } else if (*next_value == ',')
1637           break;
1638       }
1639
1640       next_value++;
1641     }
1642
1643     /* trim space */
1644     if (value != next_value && next_value[-1] == ' ')
1645       next_value[-1] = '\0';
1646
1647     if (*next_value != '\0')
1648       *next_value++ = '\0';
1649
1650     /* add the key:value pair */
1651     if (*value != '\0')
1652       gst_rtsp_message_add_header (msg, field, value);
1653
1654     value = next_value;
1655   }
1656
1657 done:
1658   return GST_RTSP_OK;
1659
1660   /* ERRORS */
1661 parse_error:
1662   {
1663     return GST_RTSP_EPARSE;
1664   }
1665 }
1666
1667 /* convert all consecutive whitespace to a single space */
1668 static void
1669 normalize_line (guint8 * buffer)
1670 {
1671   while (*buffer) {
1672     if (g_ascii_isspace (*buffer)) {
1673       guint8 *tmp;
1674
1675       *buffer++ = ' ';
1676       for (tmp = buffer; g_ascii_isspace (*tmp); tmp++) {
1677       }
1678       if (buffer != tmp)
1679         memmove (buffer, tmp, strlen ((gchar *) tmp) + 1);
1680     } else {
1681       buffer++;
1682     }
1683   }
1684 }
1685
1686 /* returns:
1687  *  GST_RTSP_OK when a complete message was read.
1688  *  GST_RTSP_EEOF: when the read socket is closed
1689  *  GST_RTSP_EINTR: when more data is needed.
1690  *  GST_RTSP_..: some other error occured.
1691  */
1692 static GstRTSPResult
1693 build_next (GstRTSPBuilder * builder, GstRTSPMessage * message,
1694     GstRTSPConnection * conn)
1695 {
1696   GstRTSPResult res;
1697
1698   while (TRUE) {
1699     switch (builder->state) {
1700       case STATE_START:
1701       {
1702         guint8 c;
1703
1704         builder->offset = 0;
1705         res =
1706             read_bytes (conn, (guint8 *) builder->buffer, &builder->offset, 1);
1707         if (res != GST_RTSP_OK)
1708           goto done;
1709
1710         c = builder->buffer[0];
1711
1712         /* we have 1 bytes now and we can see if this is a data message or
1713          * not */
1714         if (c == '$') {
1715           /* data message, prepare for the header */
1716           builder->state = STATE_DATA_HEADER;
1717         } else if (c == '\n' || c == '\r') {
1718           /* skip \n and \r */
1719           builder->offset = 0;
1720         } else {
1721           builder->line = 0;
1722           builder->state = STATE_READ_LINES;
1723         }
1724         break;
1725       }
1726       case STATE_DATA_HEADER:
1727       {
1728         res =
1729             read_bytes (conn, (guint8 *) builder->buffer, &builder->offset, 4);
1730         if (res != GST_RTSP_OK)
1731           goto done;
1732
1733         gst_rtsp_message_init_data (message, builder->buffer[1]);
1734
1735         builder->body_len = (builder->buffer[2] << 8) | builder->buffer[3];
1736         builder->body_data = g_malloc (builder->body_len + 1);
1737         builder->body_data[builder->body_len] = '\0';
1738         builder->offset = 0;
1739         builder->state = STATE_DATA_BODY;
1740         break;
1741       }
1742       case STATE_DATA_BODY:
1743       {
1744         res =
1745             read_bytes (conn, builder->body_data, &builder->offset,
1746             builder->body_len);
1747         if (res != GST_RTSP_OK)
1748           goto done;
1749
1750         /* we have the complete body now, store in the message adjusting the
1751          * length to include the trailing '\0' */
1752         gst_rtsp_message_take_body (message,
1753             (guint8 *) builder->body_data, builder->body_len + 1);
1754         builder->body_data = NULL;
1755         builder->body_len = 0;
1756
1757         builder->state = STATE_END;
1758         break;
1759       }
1760       case STATE_READ_LINES:
1761       {
1762         res = read_line (conn, builder->buffer, &builder->offset,
1763             sizeof (builder->buffer));
1764         if (res != GST_RTSP_OK)
1765           goto done;
1766
1767         /* we have a regular response */
1768         if (builder->buffer[0] == '\0') {
1769           gchar *hdrval;
1770
1771           /* empty line, end of message header */
1772           /* see if there is a Content-Length header, but ignore it if this
1773            * is a POST request with an x-sessioncookie header */
1774           if (gst_rtsp_message_get_header (message,
1775                   GST_RTSP_HDR_CONTENT_LENGTH, &hdrval, 0) == GST_RTSP_OK &&
1776               (message->type != GST_RTSP_MESSAGE_HTTP_REQUEST ||
1777                   message->type_data.request.method != GST_RTSP_POST ||
1778                   gst_rtsp_message_get_header (message,
1779                       GST_RTSP_HDR_X_SESSIONCOOKIE, NULL, 0) != GST_RTSP_OK)) {
1780             /* there is, prepare to read the body */
1781             builder->body_len = atol (hdrval);
1782             builder->body_data = g_try_malloc (builder->body_len + 1);
1783             /* we can't do much here, we need the length to know how many bytes
1784              * we need to read next and when allocation fails, something is
1785              * probably wrong with the length. */
1786             if (builder->body_data == NULL)
1787               goto invalid_body_len;
1788
1789             builder->body_data[builder->body_len] = '\0';
1790             builder->offset = 0;
1791             builder->state = STATE_DATA_BODY;
1792           } else {
1793             builder->state = STATE_END;
1794           }
1795           break;
1796         }
1797
1798         /* we have a line */
1799         normalize_line (builder->buffer);
1800         if (builder->line == 0) {
1801           /* first line, check for response status */
1802           if (memcmp (builder->buffer, "RTSP", 4) == 0 ||
1803               memcmp (builder->buffer, "HTTP", 4) == 0) {
1804             builder->status = parse_response_status (builder->buffer, message);
1805           } else {
1806             builder->status = parse_request_line (builder->buffer, message);
1807           }
1808         } else {
1809           /* else just parse the line */
1810           res = parse_line (builder->buffer, message);
1811           if (res != GST_RTSP_OK)
1812             builder->status = res;
1813         }
1814         builder->line++;
1815         builder->offset = 0;
1816         break;
1817       }
1818       case STATE_END:
1819       {
1820         gchar *session_cookie;
1821         gchar *session_id;
1822
1823         if (message->type == GST_RTSP_MESSAGE_DATA) {
1824           /* data messages don't have headers */
1825           res = GST_RTSP_OK;
1826           goto done;
1827         }
1828
1829         /* save the tunnel session in the connection */
1830         if (message->type == GST_RTSP_MESSAGE_HTTP_REQUEST &&
1831             !conn->manual_http &&
1832             conn->tstate == TUNNEL_STATE_NONE &&
1833             gst_rtsp_message_get_header (message, GST_RTSP_HDR_X_SESSIONCOOKIE,
1834                 &session_cookie, 0) == GST_RTSP_OK) {
1835           strncpy (conn->tunnelid, session_cookie, TUNNELID_LEN);
1836           conn->tunnelid[TUNNELID_LEN - 1] = '\0';
1837           conn->tunneled = TRUE;
1838         }
1839
1840         /* save session id in the connection for further use */
1841         if (message->type == GST_RTSP_MESSAGE_RESPONSE &&
1842             gst_rtsp_message_get_header (message, GST_RTSP_HDR_SESSION,
1843                 &session_id, 0) == GST_RTSP_OK) {
1844           gint maxlen, i;
1845
1846           maxlen = sizeof (conn->session_id) - 1;
1847           /* the sessionid can have attributes marked with ;
1848            * Make sure we strip them */
1849           for (i = 0; session_id[i] != '\0'; i++) {
1850             if (session_id[i] == ';') {
1851               maxlen = i;
1852               /* parse timeout */
1853               do {
1854                 i++;
1855               } while (g_ascii_isspace (session_id[i]));
1856               if (g_str_has_prefix (&session_id[i], "timeout=")) {
1857                 gint to;
1858
1859                 /* if we parsed something valid, configure */
1860                 if ((to = atoi (&session_id[i + 8])) > 0)
1861                   conn->timeout = to;
1862               }
1863               break;
1864             }
1865           }
1866
1867           /* make sure to not overflow */
1868           strncpy (conn->session_id, session_id, maxlen);
1869           conn->session_id[maxlen] = '\0';
1870         }
1871         res = builder->status;
1872         goto done;
1873       }
1874       default:
1875         res = GST_RTSP_ERROR;
1876         break;
1877     }
1878   }
1879 done:
1880   return res;
1881
1882   /* ERRORS */
1883 invalid_body_len:
1884   {
1885     GST_DEBUG ("could not allocate body");
1886     return GST_RTSP_ERROR;
1887   }
1888 }
1889
1890 /**
1891  * gst_rtsp_connection_read:
1892  * @conn: a #GstRTSPConnection
1893  * @data: the data to read
1894  * @size: the size of @data
1895  * @timeout: a timeout value or #NULL
1896  *
1897  * Attempt to read @size bytes into @data from the connected @conn, blocking up to
1898  * the specified @timeout. @timeout can be #NULL, in which case this function
1899  * might block forever.
1900  *
1901  * This function can be cancelled with gst_rtsp_connection_flush().
1902  *
1903  * Returns: #GST_RTSP_OK on success.
1904  */
1905 GstRTSPResult
1906 gst_rtsp_connection_read (GstRTSPConnection * conn, guint8 * data, guint size,
1907     GTimeVal * timeout)
1908 {
1909   guint offset;
1910   GstClockTime to;
1911   GstRTSPResult res;
1912   GError *err = NULL;
1913
1914   g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1915   g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
1916   g_return_val_if_fail (conn->read_socket != NULL, GST_RTSP_EINVAL);
1917
1918   if (G_UNLIKELY (size == 0))
1919     return GST_RTSP_OK;
1920
1921   offset = 0;
1922
1923   /* configure timeout if any */
1924   to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1925
1926   while (TRUE) {
1927     res = read_bytes (conn, data, &offset, size);
1928     if (G_UNLIKELY (res == GST_RTSP_EEOF))
1929       goto eof;
1930     if (G_LIKELY (res == GST_RTSP_OK))
1931       break;
1932     if (G_UNLIKELY (res != GST_RTSP_EINTR))
1933       goto read_error;
1934
1935     g_socket_set_timeout (conn->read_socket,
1936         (to + GST_SECOND - 1) / GST_SECOND);
1937     if (!g_socket_condition_wait (conn->read_socket,
1938             G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, conn->cancellable,
1939             &err)) {
1940       g_socket_set_timeout (conn->read_socket, 0);
1941       if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_BUSY)) {
1942         g_clear_error (&err);
1943         goto stopped;
1944       } else if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_TIMED_OUT)) {
1945         g_clear_error (&err);
1946         goto select_timeout;
1947       }
1948       g_clear_error (&err);
1949       goto select_error;
1950     }
1951     g_socket_set_timeout (conn->read_socket, 0);
1952   }
1953   return GST_RTSP_OK;
1954
1955   /* ERRORS */
1956 select_error:
1957   {
1958     return GST_RTSP_ESYS;
1959   }
1960 select_timeout:
1961   {
1962     return GST_RTSP_ETIMEOUT;
1963   }
1964 stopped:
1965   {
1966     return GST_RTSP_EINTR;
1967   }
1968 eof:
1969   {
1970     return GST_RTSP_EEOF;
1971   }
1972 read_error:
1973   {
1974     return res;
1975   }
1976 }
1977
1978 static GstRTSPMessage *
1979 gen_tunnel_reply (GstRTSPConnection * conn, GstRTSPStatusCode code,
1980     const GstRTSPMessage * request)
1981 {
1982   GstRTSPMessage *msg;
1983   GstRTSPResult res;
1984
1985   if (gst_rtsp_status_as_text (code) == NULL)
1986     code = GST_RTSP_STS_INTERNAL_SERVER_ERROR;
1987
1988   GST_RTSP_CHECK (gst_rtsp_message_new_response (&msg, code, NULL, request),
1989       no_message);
1990
1991   gst_rtsp_message_add_header (msg, GST_RTSP_HDR_SERVER,
1992       "GStreamer RTSP Server");
1993   gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CONNECTION, "close");
1994   gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CACHE_CONTROL, "no-store");
1995   gst_rtsp_message_add_header (msg, GST_RTSP_HDR_PRAGMA, "no-cache");
1996
1997   if (code == GST_RTSP_STS_OK) {
1998     if (conn->ip)
1999       gst_rtsp_message_add_header (msg, GST_RTSP_HDR_X_SERVER_IP_ADDRESS,
2000           conn->ip);
2001     gst_rtsp_message_add_header (msg, GST_RTSP_HDR_CONTENT_TYPE,
2002         "application/x-rtsp-tunnelled");
2003   }
2004
2005   return msg;
2006
2007   /* ERRORS */
2008 no_message:
2009   {
2010     return NULL;
2011   }
2012 }
2013
2014 /**
2015  * gst_rtsp_connection_receive:
2016  * @conn: a #GstRTSPConnection
2017  * @message: the message to read
2018  * @timeout: a timeout value or #NULL
2019  *
2020  * Attempt to read into @message from the connected @conn, blocking up to
2021  * the specified @timeout. @timeout can be #NULL, in which case this function
2022  * might block forever.
2023  * 
2024  * This function can be cancelled with gst_rtsp_connection_flush().
2025  *
2026  * Returns: #GST_RTSP_OK on success.
2027  */
2028 GstRTSPResult
2029 gst_rtsp_connection_receive (GstRTSPConnection * conn, GstRTSPMessage * message,
2030     GTimeVal * timeout)
2031 {
2032   GstRTSPResult res;
2033   GstRTSPBuilder builder;
2034   GstClockTime to;
2035   GError *err = NULL;
2036
2037   g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2038   g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2039   g_return_val_if_fail (conn->read_socket != NULL, GST_RTSP_EINVAL);
2040
2041   /* configure timeout if any */
2042   to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
2043
2044   memset (&builder, 0, sizeof (GstRTSPBuilder));
2045   while (TRUE) {
2046     res = build_next (&builder, message, conn);
2047     if (G_UNLIKELY (res == GST_RTSP_EEOF))
2048       goto eof;
2049     else if (G_LIKELY (res == GST_RTSP_OK)) {
2050       if (!conn->manual_http) {
2051         if (message->type == GST_RTSP_MESSAGE_HTTP_REQUEST) {
2052           if (conn->tstate == TUNNEL_STATE_NONE &&
2053               message->type_data.request.method == GST_RTSP_GET) {
2054             GstRTSPMessage *response;
2055
2056             conn->tstate = TUNNEL_STATE_GET;
2057
2058             /* tunnel GET request, we can reply now */
2059             response = gen_tunnel_reply (conn, GST_RTSP_STS_OK, message);
2060             res = gst_rtsp_connection_send (conn, response, timeout);
2061             gst_rtsp_message_free (response);
2062             if (res == GST_RTSP_OK)
2063               res = GST_RTSP_ETGET;
2064             goto cleanup;
2065           } else if (conn->tstate == TUNNEL_STATE_NONE &&
2066               message->type_data.request.method == GST_RTSP_POST) {
2067             conn->tstate = TUNNEL_STATE_POST;
2068
2069             /* tunnel POST request, the caller now has to link the two
2070              * connections. */
2071             res = GST_RTSP_ETPOST;
2072             goto cleanup;
2073           } else {
2074             res = GST_RTSP_EPARSE;
2075             goto cleanup;
2076           }
2077         } else if (message->type == GST_RTSP_MESSAGE_HTTP_RESPONSE) {
2078           res = GST_RTSP_EPARSE;
2079           goto cleanup;
2080         }
2081       }
2082
2083       break;
2084     } else if (G_UNLIKELY (res != GST_RTSP_EINTR))
2085       goto read_error;
2086
2087     g_socket_set_timeout (conn->read_socket,
2088         (to + GST_SECOND - 1) / GST_SECOND);
2089     if (!g_socket_condition_wait (conn->read_socket,
2090             G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, conn->cancellable,
2091             &err)) {
2092       g_socket_set_timeout (conn->read_socket, 0);
2093       if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
2094         g_clear_error (&err);
2095         goto stopped;
2096       } else if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_TIMED_OUT)) {
2097         g_clear_error (&err);
2098         goto select_timeout;
2099       }
2100       g_clear_error (&err);
2101       goto select_error;
2102     }
2103     g_socket_set_timeout (conn->read_socket, 0);
2104   }
2105
2106   /* we have a message here */
2107   build_reset (&builder);
2108
2109   return GST_RTSP_OK;
2110
2111   /* ERRORS */
2112 select_error:
2113   {
2114     res = GST_RTSP_ESYS;
2115     goto cleanup;
2116   }
2117 select_timeout:
2118   {
2119     res = GST_RTSP_ETIMEOUT;
2120     goto cleanup;
2121   }
2122 stopped:
2123   {
2124     res = GST_RTSP_EINTR;
2125     goto cleanup;
2126   }
2127 eof:
2128   {
2129     res = GST_RTSP_EEOF;
2130     goto cleanup;
2131   }
2132 read_error:
2133 cleanup:
2134   {
2135     build_reset (&builder);
2136     gst_rtsp_message_unset (message);
2137     return res;
2138   }
2139 }
2140
2141 /**
2142  * gst_rtsp_connection_close:
2143  * @conn: a #GstRTSPConnection
2144  *
2145  * Close the connected @conn. After this call, the connection is in the same
2146  * state as when it was first created.
2147  * 
2148  * Returns: #GST_RTSP_OK on success.
2149  */
2150 GstRTSPResult
2151 gst_rtsp_connection_close (GstRTSPConnection * conn)
2152 {
2153   g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2154
2155   /* last unref closes the connection we don't want to explicitly close here
2156    * because these sockets might have been provided at construction */
2157   if (conn->socket0) {
2158     g_object_unref (conn->socket0);
2159     conn->socket0 = NULL;
2160   }
2161   if (conn->socket1) {
2162     g_object_unref (conn->socket1);
2163     conn->socket1 = NULL;
2164   }
2165
2166   g_free (conn->ip);
2167   conn->ip = NULL;
2168
2169   conn->read_ahead = 0;
2170
2171   g_free (conn->initial_buffer);
2172   conn->initial_buffer = NULL;
2173   conn->initial_buffer_offset = 0;
2174
2175   conn->write_socket = NULL;
2176   conn->read_socket = NULL;
2177   conn->tunneled = FALSE;
2178   conn->tstate = TUNNEL_STATE_NONE;
2179   conn->ctxp = NULL;
2180   g_free (conn->username);
2181   conn->username = NULL;
2182   g_free (conn->passwd);
2183   conn->passwd = NULL;
2184   gst_rtsp_connection_clear_auth_params (conn);
2185   conn->timeout = 60;
2186   conn->cseq = 0;
2187   conn->session_id[0] = '\0';
2188
2189   return GST_RTSP_OK;
2190 }
2191
2192 /**
2193  * gst_rtsp_connection_free:
2194  * @conn: a #GstRTSPConnection
2195  *
2196  * Close and free @conn.
2197  * 
2198  * Returns: #GST_RTSP_OK on success.
2199  */
2200 GstRTSPResult
2201 gst_rtsp_connection_free (GstRTSPConnection * conn)
2202 {
2203   GstRTSPResult res;
2204
2205   g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2206
2207   res = gst_rtsp_connection_close (conn);
2208
2209   if (conn->cancellable)
2210     g_object_unref (conn->cancellable);
2211
2212   g_timer_destroy (conn->timer);
2213   gst_rtsp_url_free (conn->url);
2214   g_free (conn->proxy_host);
2215   g_free (conn);
2216
2217   return res;
2218 }
2219
2220 /**
2221  * gst_rtsp_connection_poll:
2222  * @conn: a #GstRTSPConnection
2223  * @events: a bitmask of #GstRTSPEvent flags to check
2224  * @revents: location for result flags 
2225  * @timeout: a timeout
2226  *
2227  * Wait up to the specified @timeout for the connection to become available for
2228  * at least one of the operations specified in @events. When the function returns
2229  * with #GST_RTSP_OK, @revents will contain a bitmask of available operations on
2230  * @conn.
2231  *
2232  * @timeout can be #NULL, in which case this function might block forever.
2233  *
2234  * This function can be cancelled with gst_rtsp_connection_flush().
2235  * 
2236  * Returns: #GST_RTSP_OK on success.
2237  */
2238 GstRTSPResult
2239 gst_rtsp_connection_poll (GstRTSPConnection * conn, GstRTSPEvent events,
2240     GstRTSPEvent * revents, GTimeVal * timeout)
2241 {
2242   GstClockTime to;
2243   GMainContext *ctx;
2244   GSource *rs, *ws, *ts;
2245   GIOCondition condition;
2246
2247   g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2248   g_return_val_if_fail (events != 0, GST_RTSP_EINVAL);
2249   g_return_val_if_fail (revents != NULL, GST_RTSP_EINVAL);
2250   g_return_val_if_fail (conn->read_socket != NULL, GST_RTSP_EINVAL);
2251   g_return_val_if_fail (conn->write_socket != NULL, GST_RTSP_EINVAL);
2252
2253   ctx = g_main_context_new ();
2254
2255   /* configure timeout if any */
2256   to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
2257
2258   if (timeout) {
2259     ts = g_timeout_source_new (to / GST_MSECOND);
2260     g_source_set_dummy_callback (ts);
2261     g_source_attach (ts, ctx);
2262     g_source_unref (ts);
2263   }
2264
2265   rs = g_socket_create_source (conn->read_socket,
2266       G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, conn->cancellable);
2267   g_source_set_dummy_callback (rs);
2268   g_source_attach (rs, ctx);
2269   g_source_unref (rs);
2270
2271   ws = g_socket_create_source (conn->write_socket,
2272       G_IO_OUT | G_IO_ERR | G_IO_HUP, conn->cancellable);
2273   g_source_set_dummy_callback (ws);
2274   g_source_attach (ws, ctx);
2275   g_source_unref (ws);
2276
2277   /* Returns after handling all pending events */
2278   g_main_context_iteration (ctx, TRUE);
2279
2280   g_main_context_unref (ctx);
2281
2282   condition =
2283       g_socket_condition_check (conn->read_socket,
2284       G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP);
2285   condition |=
2286       g_socket_condition_check (conn->write_socket,
2287       G_IO_OUT | G_IO_ERR | G_IO_HUP);
2288
2289   *revents = 0;
2290   if (events & GST_RTSP_EV_READ) {
2291     if ((condition & G_IO_IN) || (condition & G_IO_PRI))
2292       *revents |= GST_RTSP_EV_READ;
2293   }
2294   if (events & GST_RTSP_EV_WRITE) {
2295     if ((condition & G_IO_OUT))
2296       *revents |= GST_RTSP_EV_WRITE;
2297   }
2298
2299   if (*revents == 0)
2300     return GST_RTSP_ETIMEOUT;
2301
2302   return GST_RTSP_OK;
2303 }
2304
2305 /**
2306  * gst_rtsp_connection_next_timeout:
2307  * @conn: a #GstRTSPConnection
2308  * @timeout: a timeout
2309  *
2310  * Calculate the next timeout for @conn, storing the result in @timeout.
2311  *
2312  * Returns: #GST_RTSP_OK.
2313  */
2314 GstRTSPResult
2315 gst_rtsp_connection_next_timeout (GstRTSPConnection * conn, GTimeVal * timeout)
2316 {
2317   gdouble elapsed;
2318   glong sec;
2319   gulong usec;
2320   gint ctimeout;
2321
2322   g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2323   g_return_val_if_fail (timeout != NULL, GST_RTSP_EINVAL);
2324
2325   ctimeout = conn->timeout;
2326   if (ctimeout >= 20) {
2327     /* Because we should act before the timeout we timeout 5
2328      * seconds in advance. */
2329     ctimeout -= 5;
2330   } else if (ctimeout >= 5) {
2331     /* else timeout 20% earlier */
2332     ctimeout -= ctimeout / 5;
2333   } else if (ctimeout >= 1) {
2334     /* else timeout 1 second earlier */
2335     ctimeout -= 1;
2336   }
2337
2338   elapsed = g_timer_elapsed (conn->timer, &usec);
2339   if (elapsed >= ctimeout) {
2340     sec = 0;
2341     usec = 0;
2342   } else {
2343     sec = ctimeout - elapsed;
2344     if (usec <= G_USEC_PER_SEC)
2345       usec = G_USEC_PER_SEC - usec;
2346     else
2347       usec = 0;
2348   }
2349
2350   timeout->tv_sec = sec;
2351   timeout->tv_usec = usec;
2352
2353   return GST_RTSP_OK;
2354 }
2355
2356 /**
2357  * gst_rtsp_connection_reset_timeout:
2358  * @conn: a #GstRTSPConnection
2359  *
2360  * Reset the timeout of @conn.
2361  *
2362  * Returns: #GST_RTSP_OK.
2363  */
2364 GstRTSPResult
2365 gst_rtsp_connection_reset_timeout (GstRTSPConnection * conn)
2366 {
2367   g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2368
2369   g_timer_start (conn->timer);
2370
2371   return GST_RTSP_OK;
2372 }
2373
2374 /**
2375  * gst_rtsp_connection_flush:
2376  * @conn: a #GstRTSPConnection
2377  * @flush: start or stop the flush
2378  *
2379  * Start or stop the flushing action on @conn. When flushing, all current
2380  * and future actions on @conn will return #GST_RTSP_EINTR until the connection
2381  * is set to non-flushing mode again.
2382  * 
2383  * Returns: #GST_RTSP_OK.
2384  */
2385 GstRTSPResult
2386 gst_rtsp_connection_flush (GstRTSPConnection * conn, gboolean flush)
2387 {
2388   g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2389
2390   if (flush)
2391     g_cancellable_cancel (conn->cancellable);
2392   else
2393     g_cancellable_reset (conn->cancellable);
2394
2395   return GST_RTSP_OK;
2396 }
2397
2398 /**
2399  * gst_rtsp_connection_set_proxy:
2400  * @conn: a #GstRTSPConnection
2401  * @host: the proxy host
2402  * @port: the proxy port
2403  *
2404  * Set the proxy host and port.
2405  * 
2406  * Returns: #GST_RTSP_OK.
2407  */
2408 GstRTSPResult
2409 gst_rtsp_connection_set_proxy (GstRTSPConnection * conn,
2410     const gchar * host, guint port)
2411 {
2412   g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2413
2414   g_free (conn->proxy_host);
2415   conn->proxy_host = g_strdup (host);
2416   conn->proxy_port = port;
2417
2418   return GST_RTSP_OK;
2419 }
2420
2421 /**
2422  * gst_rtsp_connection_set_auth:
2423  * @conn: a #GstRTSPConnection
2424  * @method: authentication method
2425  * @user: the user
2426  * @pass: the password
2427  *
2428  * Configure @conn for authentication mode @method with @user and @pass as the
2429  * user and password respectively.
2430  * 
2431  * Returns: #GST_RTSP_OK.
2432  */
2433 GstRTSPResult
2434 gst_rtsp_connection_set_auth (GstRTSPConnection * conn,
2435     GstRTSPAuthMethod method, const gchar * user, const gchar * pass)
2436 {
2437   g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2438
2439   if (method == GST_RTSP_AUTH_DIGEST && ((user == NULL || pass == NULL)
2440           || g_strrstr (user, ":") != NULL))
2441     return GST_RTSP_EINVAL;
2442
2443   /* Make sure the username and passwd are being set for authentication */
2444   if (method == GST_RTSP_AUTH_NONE && (user == NULL || pass == NULL))
2445     return GST_RTSP_EINVAL;
2446
2447   /* ":" chars are not allowed in usernames for basic auth */
2448   if (method == GST_RTSP_AUTH_BASIC && g_strrstr (user, ":") != NULL)
2449     return GST_RTSP_EINVAL;
2450
2451   g_free (conn->username);
2452   g_free (conn->passwd);
2453
2454   conn->auth_method = method;
2455   conn->username = g_strdup (user);
2456   conn->passwd = g_strdup (pass);
2457
2458   return GST_RTSP_OK;
2459 }
2460
2461 /**
2462  * str_case_hash:
2463  * @key: ASCII string to hash
2464  *
2465  * Hashes @key in a case-insensitive manner.
2466  *
2467  * Returns: the hash code.
2468  **/
2469 static guint
2470 str_case_hash (gconstpointer key)
2471 {
2472   const char *p = key;
2473   guint h = g_ascii_toupper (*p);
2474
2475   if (h)
2476     for (p += 1; *p != '\0'; p++)
2477       h = (h << 5) - h + g_ascii_toupper (*p);
2478
2479   return h;
2480 }
2481
2482 /**
2483  * str_case_equal:
2484  * @v1: an ASCII string
2485  * @v2: another ASCII string
2486  *
2487  * Compares @v1 and @v2 in a case-insensitive manner
2488  *
2489  * Returns: %TRUE if they are equal (modulo case)
2490  **/
2491 static gboolean
2492 str_case_equal (gconstpointer v1, gconstpointer v2)
2493 {
2494   const char *string1 = v1;
2495   const char *string2 = v2;
2496
2497   return g_ascii_strcasecmp (string1, string2) == 0;
2498 }
2499
2500 /**
2501  * gst_rtsp_connection_set_auth_param:
2502  * @conn: a #GstRTSPConnection
2503  * @param: authentication directive
2504  * @value: value
2505  *
2506  * Setup @conn with authentication directives. This is not necesary for
2507  * methods #GST_RTSP_AUTH_NONE and #GST_RTSP_AUTH_BASIC. For
2508  * #GST_RTSP_AUTH_DIGEST, directives should be taken from the digest challenge
2509  * in the WWW-Authenticate response header and can include realm, domain,
2510  * nonce, opaque, stale, algorithm, qop as per RFC2617.
2511  */
2512 void
2513 gst_rtsp_connection_set_auth_param (GstRTSPConnection * conn,
2514     const gchar * param, const gchar * value)
2515 {
2516   g_return_if_fail (conn != NULL);
2517   g_return_if_fail (param != NULL);
2518
2519   if (conn->auth_params == NULL) {
2520     conn->auth_params =
2521         g_hash_table_new_full (str_case_hash, str_case_equal, g_free, g_free);
2522   }
2523   g_hash_table_insert (conn->auth_params, g_strdup (param), g_strdup (value));
2524 }
2525
2526 /**
2527  * gst_rtsp_connection_clear_auth_params:
2528  * @conn: a #GstRTSPConnection
2529  *
2530  * Clear the list of authentication directives stored in @conn.
2531  */
2532 void
2533 gst_rtsp_connection_clear_auth_params (GstRTSPConnection * conn)
2534 {
2535   g_return_if_fail (conn != NULL);
2536
2537   if (conn->auth_params != NULL) {
2538     g_hash_table_destroy (conn->auth_params);
2539     conn->auth_params = NULL;
2540   }
2541 }
2542
2543 static GstRTSPResult
2544 set_qos_dscp (GSocket * socket, guint qos_dscp)
2545 {
2546 #ifndef IP_TOS
2547   return GST_RTSP_OK;
2548 #else
2549   gint fd;
2550   union gst_sockaddr sa;
2551   socklen_t slen = sizeof (sa);
2552   gint af;
2553   gint tos;
2554
2555   if (!socket)
2556     return GST_RTSP_OK;
2557
2558   fd = g_socket_get_fd (socket);
2559   if (getsockname (fd, &sa.sa, &slen) < 0)
2560     goto no_getsockname;
2561
2562   af = sa.sa.sa_family;
2563
2564   /* if this is an IPv4-mapped address then do IPv4 QoS */
2565   if (af == AF_INET6) {
2566     if (IN6_IS_ADDR_V4MAPPED (&sa.sa_in6.sin6_addr))
2567       af = AF_INET;
2568   }
2569
2570   /* extract and shift 6 bits of the DSCP */
2571   tos = (qos_dscp & 0x3f) << 2;
2572
2573   switch (af) {
2574     case AF_INET:
2575       if (setsockopt (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0)
2576         goto no_setsockopt;
2577       break;
2578     case AF_INET6:
2579 #ifdef IPV6_TCLASS
2580       if (setsockopt (fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof (tos)) < 0)
2581         goto no_setsockopt;
2582       break;
2583 #endif
2584     default:
2585       goto wrong_family;
2586   }
2587
2588   return GST_RTSP_OK;
2589
2590   /* ERRORS */
2591 no_getsockname:
2592 no_setsockopt:
2593   {
2594     return GST_RTSP_ESYS;
2595   }
2596 wrong_family:
2597   {
2598     return GST_RTSP_ERROR;
2599   }
2600 #endif
2601 }
2602
2603 /**
2604  * gst_rtsp_connection_set_qos_dscp:
2605  * @conn: a #GstRTSPConnection
2606  * @qos_dscp: DSCP value
2607  *
2608  * Configure @conn to use the specified DSCP value.
2609  *
2610  * Returns: #GST_RTSP_OK on success.
2611  */
2612 GstRTSPResult
2613 gst_rtsp_connection_set_qos_dscp (GstRTSPConnection * conn, guint qos_dscp)
2614 {
2615   GstRTSPResult res;
2616
2617   g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2618   g_return_val_if_fail (conn->read_socket != NULL, GST_RTSP_EINVAL);
2619   g_return_val_if_fail (conn->write_socket != NULL, GST_RTSP_EINVAL);
2620
2621   res = set_qos_dscp (conn->socket0, qos_dscp);
2622   if (res == GST_RTSP_OK)
2623     res = set_qos_dscp (conn->socket1, qos_dscp);
2624
2625   return res;
2626 }
2627
2628
2629 /**
2630  * gst_rtsp_connection_get_url:
2631  * @conn: a #GstRTSPConnection
2632  *
2633  * Retrieve the URL of the other end of @conn.
2634  *
2635  * Returns: The URL. This value remains valid until the
2636  * connection is freed.
2637  */
2638 GstRTSPUrl *
2639 gst_rtsp_connection_get_url (const GstRTSPConnection * conn)
2640 {
2641   g_return_val_if_fail (conn != NULL, NULL);
2642
2643   return conn->url;
2644 }
2645
2646 /**
2647  * gst_rtsp_connection_get_ip:
2648  * @conn: a #GstRTSPConnection
2649  *
2650  * Retrieve the IP address of the other end of @conn.
2651  *
2652  * Returns: The IP address as a string. this value remains valid until the
2653  * connection is closed.
2654  */
2655 const gchar *
2656 gst_rtsp_connection_get_ip (const GstRTSPConnection * conn)
2657 {
2658   g_return_val_if_fail (conn != NULL, NULL);
2659
2660   return conn->ip;
2661 }
2662
2663 /**
2664  * gst_rtsp_connection_set_ip:
2665  * @conn: a #GstRTSPConnection
2666  * @ip: an ip address
2667  *
2668  * Set the IP address of the server.
2669  */
2670 void
2671 gst_rtsp_connection_set_ip (GstRTSPConnection * conn, const gchar * ip)
2672 {
2673   g_return_if_fail (conn != NULL);
2674
2675   g_free (conn->ip);
2676   conn->ip = g_strdup (ip);
2677 }
2678
2679 /**
2680  * gst_rtsp_connection_get_readfd:
2681  * @conn: a #GstRTSPConnection
2682  *
2683  * Get the file descriptor for reading.
2684  *
2685  * Returns: the file descriptor used for reading or %NULL on error. The file
2686  * descriptor remains valid until the connection is closed.
2687  */
2688 GSocket *
2689 gst_rtsp_connection_get_read_socket (const GstRTSPConnection * conn)
2690 {
2691   g_return_val_if_fail (conn != NULL, NULL);
2692   g_return_val_if_fail (conn->read_socket != NULL, NULL);
2693
2694   return conn->read_socket;
2695 }
2696
2697 /**
2698  * gst_rtsp_connection_get_write_socket:
2699  * @conn: a #GstRTSPConnection
2700  *
2701  * Get the file descriptor for writing.
2702  *
2703  * Returns: the file descriptor used for writing or NULL on error. The file
2704  * descriptor remains valid until the connection is closed.
2705  */
2706 GSocket *
2707 gst_rtsp_connection_get_write_socket (const GstRTSPConnection * conn)
2708 {
2709   g_return_val_if_fail (conn != NULL, NULL);
2710   g_return_val_if_fail (conn->write_socket != NULL, NULL);
2711
2712   return conn->write_socket;
2713 }
2714
2715 /**
2716  * gst_rtsp_connection_set_http_mode:
2717  * @conn: a #GstRTSPConnection
2718  * @enable: %TRUE to enable manual HTTP mode
2719  *
2720  * By setting the HTTP mode to %TRUE the message parsing will support HTTP
2721  * messages in addition to the RTSP messages. It will also disable the
2722  * automatic handling of setting up an HTTP tunnel.
2723  */
2724 void
2725 gst_rtsp_connection_set_http_mode (GstRTSPConnection * conn, gboolean enable)
2726 {
2727   g_return_if_fail (conn != NULL);
2728
2729   conn->manual_http = enable;
2730 }
2731
2732 /**
2733  * gst_rtsp_connection_set_tunneled:
2734  * @conn: a #GstRTSPConnection
2735  * @tunneled: the new state
2736  *
2737  * Set the HTTP tunneling state of the connection. This must be configured before
2738  * the @conn is connected.
2739  */
2740 void
2741 gst_rtsp_connection_set_tunneled (GstRTSPConnection * conn, gboolean tunneled)
2742 {
2743   g_return_if_fail (conn != NULL);
2744   g_return_if_fail (conn->read_socket == NULL);
2745   g_return_if_fail (conn->write_socket == NULL);
2746
2747   conn->tunneled = tunneled;
2748 }
2749
2750 /**
2751  * gst_rtsp_connection_is_tunneled:
2752  * @conn: a #GstRTSPConnection
2753  *
2754  * Get the tunneling state of the connection. 
2755  *
2756  * Returns: if @conn is using HTTP tunneling.
2757  */
2758 gboolean
2759 gst_rtsp_connection_is_tunneled (const GstRTSPConnection * conn)
2760 {
2761   g_return_val_if_fail (conn != NULL, FALSE);
2762
2763   return conn->tunneled;
2764 }
2765
2766 /**
2767  * gst_rtsp_connection_get_tunnelid:
2768  * @conn: a #GstRTSPConnection
2769  *
2770  * Get the tunnel session id the connection. 
2771  *
2772  * Returns: returns a non-empty string if @conn is being tunneled over HTTP.
2773  */
2774 const gchar *
2775 gst_rtsp_connection_get_tunnelid (const GstRTSPConnection * conn)
2776 {
2777   g_return_val_if_fail (conn != NULL, NULL);
2778
2779   if (!conn->tunneled)
2780     return NULL;
2781
2782   return conn->tunnelid;
2783 }
2784
2785 /**
2786  * gst_rtsp_connection_do_tunnel:
2787  * @conn: a #GstRTSPConnection
2788  * @conn2: a #GstRTSPConnection or %NULL
2789  *
2790  * If @conn received the first tunnel connection and @conn2 received
2791  * the second tunnel connection, link the two connections together so that
2792  * @conn manages the tunneled connection.
2793  *
2794  * After this call, @conn2 cannot be used anymore and must be freed with
2795  * gst_rtsp_connection_free().
2796  *
2797  * If @conn2 is %NULL then only the base64 decoding context will be setup for
2798  * @conn.
2799  *
2800  * Returns: return GST_RTSP_OK on success.
2801  */
2802 GstRTSPResult
2803 gst_rtsp_connection_do_tunnel (GstRTSPConnection * conn,
2804     GstRTSPConnection * conn2)
2805 {
2806   g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2807
2808   if (conn2 != NULL) {
2809     g_return_val_if_fail (conn->tstate == TUNNEL_STATE_GET, GST_RTSP_EINVAL);
2810     g_return_val_if_fail (conn2->tstate == TUNNEL_STATE_POST, GST_RTSP_EINVAL);
2811     g_return_val_if_fail (!memcmp (conn2->tunnelid, conn->tunnelid,
2812             TUNNELID_LEN), GST_RTSP_EINVAL);
2813
2814     /* both connections have socket0 as the read/write socket. start by taking the
2815      * socket from conn2 and set it as the socket in conn */
2816     conn->socket1 = conn2->socket0;
2817
2818     /* clean up some of the state of conn2 */
2819     g_cancellable_cancel (conn2->cancellable);
2820     conn2->socket0 = 0;
2821     g_object_unref (conn2->socket1);
2822     conn2->socket1 = NULL;
2823     conn2->write_socket = conn2->read_socket = NULL;
2824     g_cancellable_reset (conn2->cancellable);
2825
2826     /* We make socket0 the write socket and socket1 the read socket. */
2827     conn->write_socket = conn->socket0;
2828     conn->read_socket = conn->socket1;
2829
2830     conn->tstate = TUNNEL_STATE_COMPLETE;
2831
2832     g_free (conn->initial_buffer);
2833     conn->initial_buffer = conn2->initial_buffer;
2834     conn2->initial_buffer = NULL;
2835     conn->initial_buffer_offset = conn2->initial_buffer_offset;
2836   }
2837
2838   /* we need base64 decoding for the readfd */
2839   conn->ctx.state = 0;
2840   conn->ctx.save = 0;
2841   conn->ctx.cout = 0;
2842   conn->ctx.coutl = 0;
2843   conn->ctxp = &conn->ctx;
2844
2845   return GST_RTSP_OK;
2846 }
2847
2848 #define READ_ERR    (G_IO_HUP | G_IO_ERR | G_IO_NVAL)
2849 #define READ_COND   (G_IO_IN | READ_ERR)
2850 #define WRITE_ERR   (G_IO_HUP | G_IO_ERR | G_IO_NVAL)
2851 #define WRITE_COND  (G_IO_OUT | WRITE_ERR)
2852
2853 typedef struct
2854 {
2855   guint8 *data;
2856   guint size;
2857   guint id;
2858 } GstRTSPRec;
2859
2860 /* async functions */
2861 struct _GstRTSPWatch
2862 {
2863   GSource source;
2864
2865   GstRTSPConnection *conn;
2866
2867   GstRTSPBuilder builder;
2868   GstRTSPMessage message;
2869
2870   GPollFD readfd;
2871   GPollFD writefd;
2872
2873   /* queued message for transmission */
2874   guint id;
2875   GMutex mutex;
2876   GQueue *messages;
2877   guint8 *write_data;
2878   guint write_off;
2879   guint write_size;
2880   guint write_id;
2881
2882   GstRTSPWatchFuncs funcs;
2883
2884   gpointer user_data;
2885   GDestroyNotify notify;
2886 };
2887
2888 static gboolean
2889 gst_rtsp_source_prepare (GSource * source, gint * timeout)
2890 {
2891   GstRTSPWatch *watch = (GstRTSPWatch *) source;
2892
2893   if (watch->conn->initial_buffer != NULL)
2894     return TRUE;
2895
2896   *timeout = (watch->conn->timeout * 1000);
2897
2898   return FALSE;
2899 }
2900
2901 static gboolean
2902 gst_rtsp_source_check (GSource * source)
2903 {
2904   GstRTSPWatch *watch = (GstRTSPWatch *) source;
2905
2906   if (watch->readfd.revents & READ_COND)
2907     return TRUE;
2908
2909   if (watch->writefd.revents & WRITE_COND)
2910     return TRUE;
2911
2912   return FALSE;
2913 }
2914
2915 static gboolean
2916 gst_rtsp_source_dispatch (GSource * source, GSourceFunc callback G_GNUC_UNUSED,
2917     gpointer user_data G_GNUC_UNUSED)
2918 {
2919   GstRTSPWatch *watch = (GstRTSPWatch *) source;
2920   GstRTSPResult res = GST_RTSP_ERROR;
2921   gboolean keep_running = TRUE;
2922
2923   /* first read as much as we can */
2924   if (watch->readfd.revents & READ_COND || watch->conn->initial_buffer != NULL) {
2925     do {
2926       if (watch->readfd.revents & READ_ERR)
2927         goto read_error;
2928
2929       res = build_next (&watch->builder, &watch->message, watch->conn);
2930       if (res == GST_RTSP_EINTR)
2931         break;
2932       else if (G_UNLIKELY (res == GST_RTSP_EEOF)) {
2933         watch->readfd.events = 0;
2934         watch->readfd.revents = 0;
2935         g_source_remove_poll ((GSource *) watch, &watch->readfd);
2936         /* When we are in tunnelled mode, the read socket can be closed and we
2937          * should be prepared for a new POST method to reopen it */
2938         if (watch->conn->tstate == TUNNEL_STATE_COMPLETE) {
2939           /* remove the read connection for the tunnel */
2940           /* we accept a new POST request */
2941           watch->conn->tstate = TUNNEL_STATE_GET;
2942           /* and signal that we lost our tunnel */
2943           if (watch->funcs.tunnel_lost)
2944             res = watch->funcs.tunnel_lost (watch, watch->user_data);
2945           goto read_done;
2946         } else
2947           goto eof;
2948       } else if (G_LIKELY (res == GST_RTSP_OK)) {
2949         if (!watch->conn->manual_http &&
2950             watch->message.type == GST_RTSP_MESSAGE_HTTP_REQUEST) {
2951           if (watch->conn->tstate == TUNNEL_STATE_NONE &&
2952               watch->message.type_data.request.method == GST_RTSP_GET) {
2953             GstRTSPMessage *response;
2954             GstRTSPStatusCode code;
2955
2956             watch->conn->tstate = TUNNEL_STATE_GET;
2957
2958             if (watch->funcs.tunnel_start)
2959               code = watch->funcs.tunnel_start (watch, watch->user_data);
2960             else
2961               code = GST_RTSP_STS_OK;
2962
2963             /* queue the response */
2964             response = gen_tunnel_reply (watch->conn, code, &watch->message);
2965             gst_rtsp_watch_send_message (watch, response, NULL);
2966             gst_rtsp_message_free (response);
2967             goto read_done;
2968           } else if (watch->conn->tstate == TUNNEL_STATE_NONE &&
2969               watch->message.type_data.request.method == GST_RTSP_POST) {
2970             watch->conn->tstate = TUNNEL_STATE_POST;
2971
2972             /* in the callback the connection should be tunneled with the
2973              * GET connection */
2974             if (watch->funcs.tunnel_complete) {
2975               watch->funcs.tunnel_complete (watch, watch->user_data);
2976               keep_running = !(watch->conn->read_socket == NULL &&
2977                   watch->conn->write_socket == NULL);
2978               if (!keep_running)
2979                 goto done;
2980             }
2981             goto read_done;
2982           }
2983         }
2984       }
2985
2986       if (!watch->conn->manual_http) {
2987         /* if manual HTTP support is not enabled, then restore the message to
2988          * what it would have looked like without the support for parsing HTTP
2989          * messages being present */
2990         if (watch->message.type == GST_RTSP_MESSAGE_HTTP_REQUEST) {
2991           watch->message.type = GST_RTSP_MESSAGE_REQUEST;
2992           watch->message.type_data.request.method = GST_RTSP_INVALID;
2993           if (watch->message.type_data.request.version != GST_RTSP_VERSION_1_0)
2994             watch->message.type_data.request.version = GST_RTSP_VERSION_INVALID;
2995           res = GST_RTSP_EPARSE;
2996         } else if (watch->message.type == GST_RTSP_MESSAGE_HTTP_RESPONSE) {
2997           watch->message.type = GST_RTSP_MESSAGE_RESPONSE;
2998           if (watch->message.type_data.response.version != GST_RTSP_VERSION_1_0)
2999             watch->message.type_data.response.version =
3000                 GST_RTSP_VERSION_INVALID;
3001           res = GST_RTSP_EPARSE;
3002         }
3003       }
3004
3005       if (G_LIKELY (res == GST_RTSP_OK)) {
3006         if (watch->funcs.message_received)
3007           watch->funcs.message_received (watch, &watch->message,
3008               watch->user_data);
3009       } else {
3010         goto read_error;
3011       }
3012
3013     read_done:
3014       gst_rtsp_message_unset (&watch->message);
3015       build_reset (&watch->builder);
3016     } while (FALSE);
3017   }
3018
3019   if (watch->writefd.revents & WRITE_COND) {
3020     if (watch->writefd.revents & WRITE_ERR)
3021       goto write_error;
3022
3023     g_mutex_lock (&watch->mutex);
3024     do {
3025       if (watch->write_data == NULL) {
3026         GstRTSPRec *rec;
3027
3028         /* get a new message from the queue */
3029         rec = g_queue_pop_tail (watch->messages);
3030         if (rec == NULL)
3031           break;
3032
3033         watch->write_off = 0;
3034         watch->write_data = rec->data;
3035         watch->write_size = rec->size;
3036         watch->write_id = rec->id;
3037
3038         g_slice_free (GstRTSPRec, rec);
3039       }
3040
3041       res = write_bytes (watch->conn->write_socket, watch->write_data,
3042           &watch->write_off, watch->write_size, watch->conn->cancellable);
3043       g_mutex_unlock (&watch->mutex);
3044
3045       if (res == GST_RTSP_EINTR)
3046         goto write_blocked;
3047       else if (G_LIKELY (res == GST_RTSP_OK)) {
3048         if (watch->funcs.message_sent)
3049           watch->funcs.message_sent (watch, watch->write_id, watch->user_data);
3050       } else {
3051         goto write_error;
3052       }
3053       g_mutex_lock (&watch->mutex);
3054
3055       g_free (watch->write_data);
3056       watch->write_data = NULL;
3057     } while (TRUE);
3058
3059     watch->writefd.events = WRITE_ERR;
3060
3061     g_mutex_unlock (&watch->mutex);
3062   }
3063
3064 done:
3065 write_blocked:
3066   return keep_running;
3067
3068   /* ERRORS */
3069 eof:
3070   {
3071     if (watch->funcs.closed)
3072       watch->funcs.closed (watch, watch->user_data);
3073
3074     /* always stop when the readfd returns EOF in non-tunneled mode */
3075     return FALSE;
3076   }
3077 read_error:
3078   {
3079     watch->readfd.events = 0;
3080     watch->readfd.revents = 0;
3081     g_source_remove_poll ((GSource *) watch, &watch->readfd);
3082     keep_running = (watch->writefd.events != 0);
3083
3084     if (keep_running) {
3085       if (watch->funcs.error_full)
3086         GST_RTSP_CHECK (watch->funcs.error_full (watch, res, &watch->message,
3087                 0, watch->user_data), error);
3088       else
3089         goto error;
3090     } else
3091       goto eof;
3092   }
3093 write_error:
3094   {
3095     watch->writefd.events = 0;
3096     watch->writefd.revents = 0;
3097     g_source_remove_poll ((GSource *) watch, &watch->writefd);
3098     keep_running = (watch->readfd.events != 0);
3099
3100     if (keep_running) {
3101       if (watch->funcs.error_full)
3102         GST_RTSP_CHECK (watch->funcs.error_full (watch, res, NULL,
3103                 watch->write_id, watch->user_data), error);
3104       else
3105         goto error;
3106     } else
3107       goto eof;
3108   }
3109 error:
3110   {
3111     if (watch->funcs.error)
3112       watch->funcs.error (watch, res, watch->user_data);
3113
3114     return keep_running;
3115   }
3116 }
3117
3118 static void
3119 gst_rtsp_rec_free (gpointer data)
3120 {
3121   GstRTSPRec *rec = data;
3122
3123   g_free (rec->data);
3124   g_slice_free (GstRTSPRec, rec);
3125 }
3126
3127 static void
3128 gst_rtsp_source_finalize (GSource * source)
3129 {
3130   GstRTSPWatch *watch = (GstRTSPWatch *) source;
3131
3132   build_reset (&watch->builder);
3133   gst_rtsp_message_unset (&watch->message);
3134
3135   g_queue_foreach (watch->messages, (GFunc) gst_rtsp_rec_free, NULL);
3136   g_queue_free (watch->messages);
3137   watch->messages = NULL;
3138   g_free (watch->write_data);
3139
3140   g_mutex_clear (&watch->mutex);
3141
3142   if (watch->notify)
3143     watch->notify (watch->user_data);
3144 }
3145
3146 static GSourceFuncs gst_rtsp_source_funcs = {
3147   gst_rtsp_source_prepare,
3148   gst_rtsp_source_check,
3149   gst_rtsp_source_dispatch,
3150   gst_rtsp_source_finalize,
3151   NULL,
3152   NULL
3153 };
3154
3155 /**
3156  * gst_rtsp_watch_new:
3157  * @conn: a #GstRTSPConnection
3158  * @funcs: watch functions
3159  * @user_data: user data to pass to @funcs
3160  * @notify: notify when @user_data is not referenced anymore
3161  *
3162  * Create a watch object for @conn. The functions provided in @funcs will be
3163  * called with @user_data when activity happened on the watch.
3164  *
3165  * The new watch is usually created so that it can be attached to a
3166  * maincontext with gst_rtsp_watch_attach(). 
3167  *
3168  * @conn must exist for the entire lifetime of the watch.
3169  *
3170  * Returns: a #GstRTSPWatch that can be used for asynchronous RTSP
3171  * communication. Free with gst_rtsp_watch_unref () after usage.
3172  */
3173 GstRTSPWatch *
3174 gst_rtsp_watch_new (GstRTSPConnection * conn,
3175     GstRTSPWatchFuncs * funcs, gpointer user_data, GDestroyNotify notify)
3176 {
3177   GstRTSPWatch *result;
3178
3179   g_return_val_if_fail (conn != NULL, NULL);
3180   g_return_val_if_fail (funcs != NULL, NULL);
3181   g_return_val_if_fail (conn->read_socket != NULL, NULL);
3182   g_return_val_if_fail (conn->write_socket != NULL, NULL);
3183
3184   result = (GstRTSPWatch *) g_source_new (&gst_rtsp_source_funcs,
3185       sizeof (GstRTSPWatch));
3186
3187   result->conn = conn;
3188   result->builder.state = STATE_START;
3189
3190   g_mutex_init (&result->mutex);
3191   result->messages = g_queue_new ();
3192
3193   result->readfd.fd = -1;
3194   result->writefd.fd = -1;
3195
3196   gst_rtsp_watch_reset (result);
3197
3198   result->funcs = *funcs;
3199   result->user_data = user_data;
3200   result->notify = notify;
3201
3202   return result;
3203 }
3204
3205 /**
3206  * gst_rtsp_watch_reset:
3207  * @watch: a #GstRTSPWatch
3208  *
3209  * Reset @watch, this is usually called after gst_rtsp_connection_do_tunnel()
3210  * when the file descriptors of the connection might have changed.
3211  */
3212 void
3213 gst_rtsp_watch_reset (GstRTSPWatch * watch)
3214 {
3215   if (watch->readfd.fd != -1)
3216     g_source_remove_poll ((GSource *) watch, &watch->readfd);
3217   if (watch->writefd.fd != -1)
3218     g_source_remove_poll ((GSource *) watch, &watch->writefd);
3219
3220   watch->readfd.fd = g_socket_get_fd (watch->conn->read_socket);
3221   watch->readfd.events = READ_COND;
3222   watch->readfd.revents = 0;
3223
3224   watch->writefd.fd = g_socket_get_fd (watch->conn->write_socket);
3225   watch->writefd.events = WRITE_ERR;
3226   watch->writefd.revents = 0;
3227
3228   if (watch->readfd.fd != -1)
3229     g_source_add_poll ((GSource *) watch, &watch->readfd);
3230   if (watch->writefd.fd != -1)
3231     g_source_add_poll ((GSource *) watch, &watch->writefd);
3232 }
3233
3234 /**
3235  * gst_rtsp_watch_attach:
3236  * @watch: a #GstRTSPWatch
3237  * @context: a GMainContext (if NULL, the default context will be used)
3238  *
3239  * Adds a #GstRTSPWatch to a context so that it will be executed within that context.
3240  *
3241  * Returns: the ID (greater than 0) for the watch within the GMainContext. 
3242  */
3243 guint
3244 gst_rtsp_watch_attach (GstRTSPWatch * watch, GMainContext * context)
3245 {
3246   g_return_val_if_fail (watch != NULL, 0);
3247
3248   return g_source_attach ((GSource *) watch, context);
3249 }
3250
3251 /**
3252  * gst_rtsp_watch_unref:
3253  * @watch: a #GstRTSPWatch
3254  *
3255  * Decreases the reference count of @watch by one. If the resulting reference
3256  * count is zero the watch and associated memory will be destroyed.
3257  */
3258 void
3259 gst_rtsp_watch_unref (GstRTSPWatch * watch)
3260 {
3261   g_return_if_fail (watch != NULL);
3262
3263   g_source_unref ((GSource *) watch);
3264 }
3265
3266 /**
3267  * gst_rtsp_watch_write_data:
3268  * @watch: a #GstRTSPWatch
3269  * @data: the data to queue
3270  * @size: the size of @data
3271  * @id: (out) (allow-none): location for a message ID or %NULL
3272  *
3273  * Write @data using the connection of the @watch. If it cannot be sent
3274  * immediately, it will be queued for transmission in @watch. The contents of
3275  * @message will then be serialized and transmitted when the connection of the
3276  * @watch becomes writable. In case the @message is queued, the ID returned in
3277  * @id will be non-zero and used as the ID argument in the message_sent
3278  * callback.
3279  *
3280  * This function will take ownership of @data and g_free() it after use.
3281  *
3282  * Returns: #GST_RTSP_OK on success.
3283  */
3284 GstRTSPResult
3285 gst_rtsp_watch_write_data (GstRTSPWatch * watch, const guint8 * data,
3286     guint size, guint * id)
3287 {
3288   GstRTSPResult res;
3289   GstRTSPRec *rec;
3290   guint off = 0;
3291   GMainContext *context = NULL;
3292
3293   g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
3294   g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
3295   g_return_val_if_fail (size != 0, GST_RTSP_EINVAL);
3296
3297   g_mutex_lock (&watch->mutex);
3298
3299   /* try to send the message synchronously first */
3300   if (watch->messages->length == 0 && watch->write_data == NULL) {
3301     res =
3302         write_bytes (watch->conn->write_socket, data, &off, size,
3303         watch->conn->cancellable);
3304     if (res != GST_RTSP_EINTR) {
3305       if (id != NULL)
3306         *id = 0;
3307       g_free ((gpointer) data);
3308       goto done;
3309     }
3310   }
3311
3312   /* make a record with the data and id for sending async */
3313   rec = g_slice_new (GstRTSPRec);
3314   if (off == 0) {
3315     rec->data = (guint8 *) data;
3316     rec->size = size;
3317   } else {
3318     rec->data = g_memdup (data + off, size - off);
3319     rec->size = size - off;
3320     g_free ((gpointer) data);
3321   }
3322
3323   do {
3324     /* make sure rec->id is never 0 */
3325     rec->id = ++watch->id;
3326   } while (G_UNLIKELY (rec->id == 0));
3327
3328   /* add the record to a queue. FIXME we would like to have an upper limit here */
3329   g_queue_push_head (watch->messages, rec);
3330
3331   /* make sure the main context will now also check for writability on the
3332    * socket */
3333   if (watch->writefd.events != WRITE_COND) {
3334     watch->writefd.events = WRITE_COND;
3335     context = ((GSource *) watch)->context;
3336   }
3337
3338   if (id != NULL)
3339     *id = rec->id;
3340   res = GST_RTSP_OK;
3341
3342 done:
3343   g_mutex_unlock (&watch->mutex);
3344
3345   if (context)
3346     g_main_context_wakeup (context);
3347
3348   return res;
3349 }
3350
3351 /**
3352  * gst_rtsp_watch_send_message:
3353  * @watch: a #GstRTSPWatch
3354  * @message: a #GstRTSPMessage
3355  * @id: (out) (allow-none): location for a message ID or %NULL
3356  *
3357  * Send a @message using the connection of the @watch. If it cannot be sent
3358  * immediately, it will be queued for transmission in @watch. The contents of
3359  * @message will then be serialized and transmitted when the connection of the
3360  * @watch becomes writable. In case the @message is queued, the ID returned in
3361  * @id will be non-zero and used as the ID argument in the message_sent
3362  * callback.
3363  *
3364  * Returns: #GST_RTSP_OK on success.
3365  */
3366 GstRTSPResult
3367 gst_rtsp_watch_send_message (GstRTSPWatch * watch, GstRTSPMessage * message,
3368     guint * id)
3369 {
3370   GString *str;
3371   guint size;
3372
3373   g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
3374   g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
3375
3376   /* make a record with the message as a string and id */
3377   str = message_to_string (watch->conn, message);
3378   size = str->len;
3379   return gst_rtsp_watch_write_data (watch,
3380       (guint8 *) g_string_free (str, FALSE), size, id);
3381 }