2 * Copyright (C) <2005,2006> Wim Taymans <wim@fluendo.com>
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.
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.
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.
20 * Unless otherwise indicated, Source Code is licensed under MIT license.
21 * See further explanation attached in License Statement (distributed in the file
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:
31 * The above copyright notice and this permission notice shall be included in all
32 * copies or substantial portions of the Software.
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
44 * SECTION:gstrtspconnection
45 * @short_description: manage RTSP connections
46 * @see_also: gstrtspurl
50 * This object manages the RTSP connection to the server. It provides function
51 * to receive and send bytes and messages.
55 * Last reviewed on 2007-07-24 (0.10.14)
73 /* we include this here to get the G_OS_* defines */
80 #define EINPROGRESS WSAEINPROGRESS
82 #include <sys/ioctl.h>
84 #include <sys/socket.h>
85 #include <netinet/in.h>
86 #include <arpa/inet.h>
90 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
91 #include <sys/filio.h>
94 #include "gstrtspconnection.h"
95 #include "gstrtspbase64.h"
99 #define FIONREAD_TYPE gulong
100 #define IOCTL_SOCKET ioctlsocket
101 #define READ_SOCKET(fd, buf, len) recv (fd, (char *)buf, len, 0)
102 #define WRITE_SOCKET(fd, buf, len) send (fd, (const char *)buf, len, 0)
103 #define SETSOCKOPT(sock, level, name, val, len) setsockopt (sock, level, name, (const char *)val, len)
104 #define CLOSE_SOCKET(sock) closesocket (sock)
105 #define ERRNO_IS_NOT_EAGAIN (WSAGetLastError () != WSAEWOULDBLOCK)
106 #define ERRNO_IS_NOT_EINTR (WSAGetLastError () != WSAEINTR)
107 /* According to Microsoft's connect() documentation this one returns
108 * WSAEWOULDBLOCK and not WSAEINPROGRESS. */
109 #define ERRNO_IS_NOT_EINPROGRESS (WSAGetLastError () != WSAEWOULDBLOCK)
111 #define FIONREAD_TYPE gint
112 #define IOCTL_SOCKET ioctl
113 #define READ_SOCKET(fd, buf, len) read (fd, buf, len)
114 #define WRITE_SOCKET(fd, buf, len) write (fd, buf, len)
115 #define SETSOCKOPT(sock, level, name, val, len) setsockopt (sock, level, name, val, len)
116 #define CLOSE_SOCKET(sock) close (sock)
117 #define ERRNO_IS_NOT_EAGAIN (errno != EAGAIN)
118 #define ERRNO_IS_NOT_EINTR (errno != EINTR)
119 #define ERRNO_IS_NOT_EINPROGRESS (errno != EINPROGRESS)
124 inet_aton (const char *c, struct in_addr *paddr)
126 /* note that inet_addr is deprecated on unix because
127 * inet_addr returns -1 (INADDR_NONE) for the valid 255.255.255.255
129 paddr->s_addr = inet_addr (c);
131 if (paddr->s_addr == INADDR_NONE)
139 * gst_rtsp_connection_create:
140 * @url: a #GstRTSPUrl
141 * @conn: a #GstRTSPConnection
143 * Create a newly allocated #GstRTSPConnection from @url and store it in @conn.
144 * The connection will not yet attempt to connect to @url, use
145 * gst_rtsp_connection_connect().
147 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
150 gst_rtsp_connection_create (GstRTSPUrl * url, GstRTSPConnection ** conn)
152 GstRTSPConnection *newconn;
154 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
156 newconn = g_new0 (GstRTSPConnection, 1);
158 if ((newconn->fdset = gst_poll_new (TRUE)) == NULL)
163 newconn->timer = g_timer_new ();
165 newconn->auth_method = GST_RTSP_AUTH_NONE;
166 newconn->username = NULL;
167 newconn->passwd = NULL;
168 newconn->auth_params = NULL;
178 return GST_RTSP_ESYS;
183 * gst_rtsp_connection_connect:
184 * @conn: a #GstRTSPConnection
185 * @timeout: a #GTimeVal timeout
187 * Attempt to connect to the url of @conn made with
188 * gst_rtsp_connection_create(). If @timeout is #NULL this function can block
189 * forever. If @timeout contains a valid timeout, this function will return
190 * #GST_RTSP_ETIMEOUT after the timeout expired.
192 * This function can be cancelled with gst_rtsp_connection_flush().
194 * Returns: #GST_RTSP_OK when a connection could be made.
197 gst_rtsp_connection_connect (GstRTSPConnection * conn, GTimeVal * timeout)
200 struct sockaddr_in sa_in;
201 struct hostent *hostinfo;
211 unsigned long flags = 1;
212 struct in_addr *addrp;
215 gchar ipbuf[INET_ADDRSTRLEN];
216 #endif /* G_OS_WIN32 */
218 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
219 g_return_val_if_fail (conn->url != NULL, GST_RTSP_EINVAL);
220 g_return_val_if_fail (conn->fd.fd < 0, GST_RTSP_EINVAL);
224 /* first check if it already is an IP address */
225 if (inet_aton (url->host, &addr)) {
228 hostinfo = gethostbyname (url->host);
230 goto not_resolved; /* h_errno set */
232 if (hostinfo->h_addrtype != AF_INET)
233 goto not_ip; /* host not an IP host */
235 addrp = (struct in_addr *) hostinfo->h_addr_list[0];
236 /* this is not threadsafe */
237 ip = inet_ntoa (*addrp);
239 addrs = hostinfo->h_addr_list;
240 ip = inet_ntop (AF_INET, (struct in_addr *) addrs[0], ipbuf,
242 #endif /* G_OS_WIN32 */
245 /* get the port from the url */
246 gst_rtsp_url_get_port (url, &port);
248 memset (&sa_in, 0, sizeof (sa_in));
249 sa_in.sin_family = AF_INET; /* network socket */
250 sa_in.sin_port = htons (port); /* on port */
251 sa_in.sin_addr.s_addr = inet_addr (ip); /* on host ip */
253 fd = socket (AF_INET, SOCK_STREAM, 0);
257 /* set to non-blocking mode so that we can cancel the connect */
259 fcntl (fd, F_SETFL, O_NONBLOCK);
261 ioctlsocket (fd, FIONBIO, &flags);
262 #endif /* G_OS_WIN32 */
264 /* add the socket to our fdset */
266 gst_poll_add_fd (conn->fdset, &conn->fd);
268 /* we are going to connect ASYNC now */
269 ret = connect (fd, (struct sockaddr *) &sa_in, sizeof (sa_in));
272 if (ERRNO_IS_NOT_EINPROGRESS)
275 /* wait for connect to complete up to the specified timeout or until we got
277 gst_poll_fd_ctl_write (conn->fdset, &conn->fd, TRUE);
279 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
282 retval = gst_poll_wait (conn->fdset, to);
283 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
287 else if (retval == -1)
290 /* we can still have an error connecting on windows */
291 if (gst_poll_fd_has_error (conn->fdset, &conn->fd))
294 gst_poll_fd_ignored (conn->fdset, &conn->fd);
297 conn->ip = g_strdup (ip);
303 GST_ERROR ("system error %d (%s)", errno, g_strerror (errno));
304 if (conn->fd.fd >= 0) {
305 GST_DEBUG ("remove fd %d", conn->fd.fd);
306 gst_poll_remove_fd (conn->fdset, &conn->fd);
311 return GST_RTSP_ESYS;
315 GST_ERROR ("could not resolve %s", url->host);
316 return GST_RTSP_ENET;
320 GST_ERROR ("not an IP address");
321 return GST_RTSP_ENOTIP;
325 GST_ERROR ("timeout");
326 if (conn->fd.fd >= 0) {
327 GST_DEBUG ("remove fd %d", conn->fd.fd);
328 gst_poll_remove_fd (conn->fdset, &conn->fd);
333 return GST_RTSP_ETIMEOUT;
338 md5_digest_to_hex_string (unsigned char digest[16], char string[33])
340 static const char hexdigits[] = "0123456789abcdef";
343 for (i = 0; i < 16; i++) {
344 string[i * 2] = hexdigits[(digest[i] >> 4) & 0x0f];
345 string[i * 2 + 1] = hexdigits[digest[i] & 0x0f];
351 auth_digest_compute_hex_urp (const gchar * username,
352 const gchar * realm, const gchar * password, gchar hex_urp[33])
354 struct MD5Context md5_context;
355 unsigned char digest[16];
357 MD5Init (&md5_context);
358 MD5Update (&md5_context, username, strlen (username));
359 MD5Update (&md5_context, ":", 1);
360 MD5Update (&md5_context, realm, strlen (realm));
361 MD5Update (&md5_context, ":", 1);
362 MD5Update (&md5_context, password, strlen (password));
363 MD5Final (digest, &md5_context);
364 md5_digest_to_hex_string (digest, hex_urp);
368 auth_digest_compute_response (const gchar * method,
369 const gchar * uri, const gchar * hex_a1, const gchar * nonce,
373 struct MD5Context md5_context;
374 unsigned char digest[16];
377 MD5Init (&md5_context);
378 MD5Update (&md5_context, method, strlen (method));
379 MD5Update (&md5_context, ":", 1);
380 MD5Update (&md5_context, uri, strlen (uri));
381 MD5Final (digest, &md5_context);
382 md5_digest_to_hex_string (digest, hex_a2);
385 MD5Init (&md5_context);
386 MD5Update (&md5_context, hex_a1, strlen (hex_a1));
387 MD5Update (&md5_context, ":", 1);
388 MD5Update (&md5_context, nonce, strlen (nonce));
389 MD5Update (&md5_context, ":", 1);
391 MD5Update (&md5_context, hex_a2, 32);
392 MD5Final (digest, &md5_context);
393 md5_digest_to_hex_string (digest, response);
397 add_auth_header (GstRTSPConnection * conn, GstRTSPMessage * message)
399 switch (conn->auth_method) {
400 case GST_RTSP_AUTH_BASIC:{
402 g_strdup_printf ("%s:%s", conn->username, conn->passwd);
404 gst_rtsp_base64_encode (user_pass, strlen (user_pass));
405 gchar *auth_string = g_strdup_printf ("Basic %s", user_pass64);
407 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
411 g_free (user_pass64);
414 case GST_RTSP_AUTH_DIGEST:{
415 gchar response[33], hex_urp[33];
416 gchar *auth_string, *auth_string2;
423 /* we need to have some params set */
424 if (conn->auth_params == NULL)
427 /* we need the realm and nonce */
428 realm = (gchar *) g_hash_table_lookup (conn->auth_params, "realm");
429 nonce = (gchar *) g_hash_table_lookup (conn->auth_params, "nonce");
430 if (realm == NULL || nonce == NULL)
433 auth_digest_compute_hex_urp (conn->username, realm, conn->passwd,
436 method = gst_rtsp_method_as_text (message->type_data.request.method);
437 uri = message->type_data.request.uri;
439 /* Assume no qop, algorithm=md5, stale=false */
440 /* For algorithm MD5, a1 = urp. */
441 auth_digest_compute_response (method, uri, hex_urp, nonce, response);
442 auth_string = g_strdup_printf ("Digest username=\"%s\", "
443 "realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"",
444 conn->username, realm, nonce, uri, response);
446 opaque = (gchar *) g_hash_table_lookup (conn->auth_params, "opaque");
448 auth_string2 = g_strdup_printf ("%s, opaque=\"%s\"", auth_string,
450 g_free (auth_string);
451 auth_string = auth_string2;
453 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
464 add_date_header (GstRTSPMessage * message)
467 gchar date_string[100];
474 g_get_current_time (&tv);
475 t = (time_t) tv.tv_sec;
478 strftime (date_string, sizeof (date_string), "%a, %d %b %Y %H:%M:%S GMT",
479 gmtime_r (&t, &tm_));
481 strftime (date_string, sizeof (date_string), "%a, %d %b %Y %H:%M:%S GMT",
485 gst_rtsp_message_add_header (message, GST_RTSP_HDR_DATE, date_string);
489 * gst_rtsp_connection_write:
490 * @conn: a #GstRTSPConnection
491 * @data: the data to write
492 * @size: the size of @data
493 * @timeout: a timeout value or #NULL
495 * Attempt to write @size bytes of @data to the connected @conn, blocking up to
496 * the specified @timeout. @timeout can be #NULL, in which case this function
497 * might block forever.
499 * This function can be cancelled with gst_rtsp_connection_flush().
501 * Returns: #GST_RTSP_OK on success.
504 gst_rtsp_connection_write (GstRTSPConnection * conn, const guint8 * data,
505 guint size, GTimeVal * timeout)
511 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
512 g_return_val_if_fail (data != NULL || size == 0, GST_RTSP_EINVAL);
513 g_return_val_if_fail (conn->fd.fd >= 0, GST_RTSP_EINVAL);
515 gst_poll_set_controllable (conn->fdset, TRUE);
516 gst_poll_fd_ctl_write (conn->fdset, &conn->fd, TRUE);
517 gst_poll_fd_ctl_read (conn->fdset, &conn->fd, FALSE);
518 /* clear all previous poll results */
519 gst_poll_fd_ignored (conn->fdset, &conn->fd);
521 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
525 while (towrite > 0) {
529 retval = gst_poll_wait (conn->fdset, to);
530 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
542 /* now we can write */
543 written = WRITE_SOCKET (conn->fd.fd, data, towrite);
545 if (ERRNO_IS_NOT_EAGAIN && ERRNO_IS_NOT_EINTR)
557 return GST_RTSP_ETIMEOUT;
561 return GST_RTSP_ESYS;
565 return GST_RTSP_EINTR;
569 return GST_RTSP_ESYS;
574 * gst_rtsp_connection_send:
575 * @conn: a #GstRTSPConnection
576 * @message: the message to send
577 * @timeout: a timeout value or #NULL
579 * Attempt to send @message to the connected @conn, blocking up to
580 * the specified @timeout. @timeout can be #NULL, in which case this function
581 * might block forever.
583 * This function can be cancelled with gst_rtsp_connection_flush().
585 * Returns: #GST_RTSP_OK on success.
588 gst_rtsp_connection_send (GstRTSPConnection * conn, GstRTSPMessage * message,
599 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
600 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
603 error = WSAStartup (0x0202, &w);
608 if (w.wVersion != 0x0202)
612 str = g_string_new ("");
614 switch (message->type) {
615 case GST_RTSP_MESSAGE_REQUEST:
616 /* create request string, add CSeq */
617 g_string_append_printf (str, "%s %s RTSP/1.0\r\n"
619 gst_rtsp_method_as_text (message->type_data.request.method),
620 message->type_data.request.uri, conn->cseq++);
621 /* add session id if we have one */
622 if (conn->session_id[0] != '\0') {
623 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SESSION,
626 /* add any authentication headers */
627 add_auth_header (conn, message);
629 case GST_RTSP_MESSAGE_RESPONSE:
630 /* create response string */
631 g_string_append_printf (str, "RTSP/1.0 %d %s\r\n",
632 message->type_data.response.code, message->type_data.response.reason);
634 case GST_RTSP_MESSAGE_DATA:
636 guint8 data_header[4];
638 /* prepare data header */
639 data_header[0] = '$';
640 data_header[1] = message->type_data.data.channel;
641 data_header[2] = (message->body_size >> 8) & 0xff;
642 data_header[3] = message->body_size & 0xff;
644 /* create string with header and data */
645 str = g_string_append_len (str, (gchar *) data_header, 4);
647 g_string_append_len (str, (gchar *) message->body,
652 g_return_val_if_reached (GST_RTSP_EINVAL);
656 /* append headers and body */
657 if (message->type != GST_RTSP_MESSAGE_DATA) {
658 /* add date header */
659 add_date_header (message);
662 gst_rtsp_message_append_headers (message, str);
664 /* append Content-Length and body if needed */
665 if (message->body != NULL && message->body_size > 0) {
668 len = g_strdup_printf ("%d", message->body_size);
669 g_string_append_printf (str, "%s: %s\r\n",
670 gst_rtsp_header_as_text (GST_RTSP_HDR_CONTENT_LENGTH), len);
672 /* header ends here */
673 g_string_append (str, "\r\n");
675 g_string_append_len (str, (gchar *) message->body,
678 /* just end headers */
679 g_string_append (str, "\r\n");
685 gst_rtsp_connection_write (conn, (guint8 *) str->str, str->len, timeout);
687 g_string_free (str, TRUE);
694 g_warning ("Error %d on WSAStartup", error);
695 return GST_RTSP_EWSASTART;
699 g_warning ("Windows sockets are not version 0x202 (current 0x%x)",
702 return GST_RTSP_EWSAVERSION;
708 read_line (gint fd, gchar * buffer, guint size)
716 r = READ_SOCKET (fd, &c, 1);
720 if (ERRNO_IS_NOT_EAGAIN && ERRNO_IS_NOT_EINTR)
723 if (c == '\n') /* end on \n */
725 if (c == '\r') /* ignore \r */
738 return GST_RTSP_EEOF;
742 return GST_RTSP_ESYS;
747 read_string (gchar * dest, gint size, gchar ** src)
753 while (g_ascii_isspace (**src))
756 while (!g_ascii_isspace (**src) && **src != '\0') {
766 read_key (gchar * dest, gint size, gchar ** src)
771 while (**src != ':' && **src != '\0') {
781 parse_response_status (gchar * buffer, GstRTSPMessage * msg)
784 gchar versionstr[20];
791 read_string (versionstr, sizeof (versionstr), &bptr);
792 read_string (codestr, sizeof (codestr), &bptr);
793 code = atoi (codestr);
795 while (g_ascii_isspace (*bptr))
798 if (strcmp (versionstr, "RTSP/1.0") == 0)
799 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
801 else if (strncmp (versionstr, "RTSP/", 5) == 0) {
802 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
804 msg->type_data.response.version = GST_RTSP_VERSION_INVALID;
812 return GST_RTSP_EPARSE;
817 parse_request_line (gchar * buffer, GstRTSPMessage * msg)
819 GstRTSPResult res = GST_RTSP_OK;
820 gchar versionstr[20];
824 GstRTSPMethod method;
828 read_string (methodstr, sizeof (methodstr), &bptr);
829 method = gst_rtsp_find_method (methodstr);
831 read_string (urlstr, sizeof (urlstr), &bptr);
833 res = GST_RTSP_EPARSE;
835 read_string (versionstr, sizeof (versionstr), &bptr);
838 res = GST_RTSP_EPARSE;
840 if (strcmp (versionstr, "RTSP/1.0") == 0) {
841 if (gst_rtsp_message_init_request (msg, method, urlstr) != GST_RTSP_OK)
842 res = GST_RTSP_EPARSE;
843 } else if (strncmp (versionstr, "RTSP/", 5) == 0) {
844 if (gst_rtsp_message_init_request (msg, method, urlstr) != GST_RTSP_OK)
845 res = GST_RTSP_EPARSE;
846 msg->type_data.request.version = GST_RTSP_VERSION_INVALID;
848 gst_rtsp_message_init_request (msg, method, urlstr);
849 msg->type_data.request.version = GST_RTSP_VERSION_INVALID;
850 res = GST_RTSP_EPARSE;
856 /* parsing lines means reading a Key: Value pair */
858 parse_line (gchar * buffer, GstRTSPMessage * msg)
862 GstRTSPHeaderField field;
867 read_key (key, sizeof (key), &bptr);
873 field = gst_rtsp_find_header_field (key);
874 if (field != GST_RTSP_HDR_INVALID) {
875 while (g_ascii_isspace (*bptr))
877 gst_rtsp_message_add_header (msg, field, bptr);
884 return GST_RTSP_EPARSE;
889 * gst_rtsp_connection_read_internal:
890 * @conn: a #GstRTSPConnection
891 * @data: the data to read
892 * @size: the size of @data
893 * @timeout: a timeout value or #NULL
894 * @allow_interrupt: can the pending read be interrupted
896 * Attempt to read @size bytes into @data from the connected @conn, blocking up to
897 * the specified @timeout. @timeout can be #NULL, in which case this function
898 * might block forever.
900 * This function can be cancelled with gst_rtsp_connection_flush() only if
901 * @allow_interrupt is set.
903 * Returns: #GST_RTSP_OK on success.
906 gst_rtsp_connection_read_internal (GstRTSPConnection * conn, guint8 * data,
907 guint size, GTimeVal * timeout, gboolean allow_interrupt)
914 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
915 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
916 g_return_val_if_fail (conn->fd.fd >= 0, GST_RTSP_EINVAL);
923 /* configure timeout if any */
924 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
926 /* if the call fails, just go in the select.. it should not fail. Else if
927 * there is enough data to read, skip the select call al together.*/
928 if (IOCTL_SOCKET (conn->fd.fd, FIONREAD, &avail) < 0)
930 else if (avail >= toread)
933 gst_poll_set_controllable (conn->fdset, allow_interrupt);
934 gst_poll_fd_ctl_write (conn->fdset, &conn->fd, FALSE);
935 gst_poll_fd_ctl_read (conn->fdset, &conn->fd, TRUE);
941 retval = gst_poll_wait (conn->fdset, to);
942 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
951 /* check for timeout */
956 /* if we get here there is activity on the real fd since the select
957 * completed and the control socket was not readable. */
958 bytes = READ_SOCKET (conn->fd.fd, data, toread);
961 } else if (bytes < 0) {
962 if (ERRNO_IS_NOT_EAGAIN && ERRNO_IS_NOT_EINTR)
974 return GST_RTSP_ESYS;
978 return GST_RTSP_ETIMEOUT;
982 return GST_RTSP_EINTR;
986 return GST_RTSP_EEOF;
990 return GST_RTSP_ESYS;
995 * gst_rtsp_connection_read:
996 * @conn: a #GstRTSPConnection
997 * @data: the data to read
998 * @size: the size of @data
999 * @timeout: a timeout value or #NULL
1001 * Attempt to read @size bytes into @data from the connected @conn, blocking up to
1002 * the specified @timeout. @timeout can be #NULL, in which case this function
1003 * might block forever.
1005 * This function can be cancelled with gst_rtsp_connection_flush().
1007 * Returns: #GST_RTSP_OK on success.
1010 gst_rtsp_connection_read (GstRTSPConnection * conn, guint8 * data, guint size,
1013 return gst_rtsp_connection_read_internal (conn, data, size, timeout, TRUE);
1017 static GstRTSPResult
1018 read_body (GstRTSPConnection * conn, glong content_length, GstRTSPMessage * msg,
1024 if (content_length <= 0) {
1030 body = g_malloc (content_length + 1);
1031 body[content_length] = '\0';
1033 GST_RTSP_CHECK (gst_rtsp_connection_read_internal (conn, body, content_length,
1034 timeout, FALSE), read_error);
1036 content_length += 1;
1039 gst_rtsp_message_take_body (msg, (guint8 *) body, content_length);
1052 * gst_rtsp_connection_receive:
1053 * @conn: a #GstRTSPConnection
1054 * @message: the message to read
1055 * @timeout: a timeout value or #NULL
1057 * Attempt to read into @message from the connected @conn, blocking up to
1058 * the specified @timeout. @timeout can be #NULL, in which case this function
1059 * might block forever.
1061 * This function can be cancelled with gst_rtsp_connection_flush().
1063 * Returns: #GST_RTSP_OK on success.
1066 gst_rtsp_connection_receive (GstRTSPConnection * conn, GstRTSPMessage * message,
1071 glong content_length;
1075 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1076 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1083 /* parse first line and headers */
1084 while (res == GST_RTSP_OK) {
1087 /* read first character, this identifies data messages */
1088 /* This is the only read() that we allow to be interrupted */
1089 GST_RTSP_CHECK (gst_rtsp_connection_read_internal (conn, &c, 1, timeout,
1092 /* check for data packet, first character is $ */
1096 /* data packets are $<1 byte channel><2 bytes length,BE><data bytes> */
1098 /* read channel, which is the next char */
1099 GST_RTSP_CHECK (gst_rtsp_connection_read_internal (conn, &c, 1, timeout,
1100 FALSE), read_error);
1102 /* now we create a data message */
1103 gst_rtsp_message_init_data (message, c);
1105 /* next two bytes are the length of the data */
1106 GST_RTSP_CHECK (gst_rtsp_connection_read_internal (conn,
1107 (guint8 *) & size, 2, timeout, FALSE), read_error);
1109 size = GUINT16_FROM_BE (size);
1111 /* and read the body */
1112 res = read_body (conn, size, message, timeout);
1118 /* we have a regular response */
1123 /* should not happen */
1128 GST_RTSP_CHECK (read_line (conn->fd.fd, buffer + offset,
1129 sizeof (buffer) - offset), read_error);
1131 if (buffer[0] == '\0')
1135 /* first line, check for response status */
1136 if (g_str_has_prefix (buffer, "RTSP")) {
1137 res = parse_response_status (buffer, message);
1139 res = parse_request_line (buffer, message);
1142 /* else just parse the line */
1143 parse_line (buffer, message);
1149 /* read the rest of the body if needed */
1154 /* see if there is a Content-Length header */
1155 if (gst_rtsp_message_get_header (message, GST_RTSP_HDR_CONTENT_LENGTH,
1156 &hdrval, 0) == GST_RTSP_OK) {
1157 /* there is, read the body */
1158 content_length = atol (hdrval);
1159 GST_RTSP_CHECK (read_body (conn, content_length, message, timeout),
1163 /* save session id in the connection for further use */
1164 if (gst_rtsp_message_get_header (message, GST_RTSP_HDR_SESSION,
1165 &session_id, 0) == GST_RTSP_OK) {
1168 /* default session timeout */
1171 maxlen = sizeof (conn->session_id) - 1;
1172 /* the sessionid can have attributes marked with ;
1173 * Make sure we strip them */
1174 for (i = 0; session_id[i] != '\0'; i++) {
1175 if (session_id[i] == ';') {
1180 } while (g_ascii_isspace (session_id[i]));
1181 if (g_str_has_prefix (&session_id[i], "timeout=")) {
1184 /* if we parsed something valid, configure */
1185 if ((to = atoi (&session_id[i + 9])) > 0)
1192 /* make sure to not overflow */
1193 strncpy (conn->session_id, session_id, maxlen);
1194 conn->session_id[maxlen] = '\0';
1206 * gst_rtsp_connection_close:
1207 * @conn: a #GstRTSPConnection
1209 * Close the connected @conn.
1211 * Returns: #GST_RTSP_OK on success.
1214 gst_rtsp_connection_close (GstRTSPConnection * conn)
1218 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1223 if (conn->fd.fd != -1) {
1224 gst_poll_remove_fd (conn->fdset, &conn->fd);
1225 res = CLOSE_SOCKET (conn->fd.fd);
1238 return GST_RTSP_ESYS;
1243 * gst_rtsp_connection_free:
1244 * @conn: a #GstRTSPConnection
1246 * Close and free @conn.
1248 * Returns: #GST_RTSP_OK on success.
1251 gst_rtsp_connection_free (GstRTSPConnection * conn)
1255 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1257 res = gst_rtsp_connection_close (conn);
1258 gst_poll_free (conn->fdset);
1262 g_timer_destroy (conn->timer);
1263 g_free (conn->username);
1264 g_free (conn->passwd);
1265 gst_rtsp_connection_clear_auth_params (conn);
1272 * gst_rtsp_connection_poll:
1273 * @conn: a #GstRTSPConnection
1274 * @events: a bitmask of #GstRTSPEvent flags to check
1275 * @revents: location for result flags
1276 * @timeout: a timeout
1278 * Wait up to the specified @timeout for the connection to become available for
1279 * at least one of the operations specified in @events. When the function returns
1280 * with #GST_RTSP_OK, @revents will contain a bitmask of available operations on
1283 * @timeout can be #NULL, in which case this function might block forever.
1285 * This function can be cancelled with gst_rtsp_connection_flush().
1287 * Returns: #GST_RTSP_OK on success.
1292 gst_rtsp_connection_poll (GstRTSPConnection * conn, GstRTSPEvent events,
1293 GstRTSPEvent * revents, GTimeVal * timeout)
1298 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1299 g_return_val_if_fail (events != 0, GST_RTSP_EINVAL);
1300 g_return_val_if_fail (revents != NULL, GST_RTSP_EINVAL);
1301 g_return_val_if_fail (conn->fd.fd >= 0, GST_RTSP_EINVAL);
1303 gst_poll_set_controllable (conn->fdset, TRUE);
1305 /* add fd to writer set when asked to */
1306 gst_poll_fd_ctl_write (conn->fdset, &conn->fd, events & GST_RTSP_EV_WRITE);
1308 /* add fd to reader set when asked to */
1309 gst_poll_fd_ctl_read (conn->fdset, &conn->fd, events & GST_RTSP_EV_READ);
1311 /* configure timeout if any */
1312 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1315 retval = gst_poll_wait (conn->fdset, to);
1316 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1319 goto select_timeout;
1329 if (events & GST_RTSP_EV_READ) {
1330 if (gst_poll_fd_can_read (conn->fdset, &conn->fd))
1331 *revents |= GST_RTSP_EV_READ;
1333 if (events & GST_RTSP_EV_WRITE) {
1334 if (gst_poll_fd_can_write (conn->fdset, &conn->fd))
1335 *revents |= GST_RTSP_EV_WRITE;
1342 return GST_RTSP_ETIMEOUT;
1346 return GST_RTSP_ESYS;
1350 return GST_RTSP_EINTR;
1355 * gst_rtsp_connection_next_timeout:
1356 * @conn: a #GstRTSPConnection
1357 * @timeout: a timeout
1359 * Calculate the next timeout for @conn, storing the result in @timeout.
1361 * Returns: #GST_RTSP_OK.
1364 gst_rtsp_connection_next_timeout (GstRTSPConnection * conn, GTimeVal * timeout)
1370 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1371 g_return_val_if_fail (timeout != NULL, GST_RTSP_EINVAL);
1373 elapsed = g_timer_elapsed (conn->timer, &usec);
1374 if (elapsed >= conn->timeout) {
1378 sec = conn->timeout - elapsed;
1381 timeout->tv_sec = sec;
1382 timeout->tv_usec = usec;
1388 * gst_rtsp_connection_reset_timeout:
1389 * @conn: a #GstRTSPConnection
1391 * Reset the timeout of @conn.
1393 * Returns: #GST_RTSP_OK.
1396 gst_rtsp_connection_reset_timeout (GstRTSPConnection * conn)
1398 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1400 g_timer_start (conn->timer);
1406 * gst_rtsp_connection_flush:
1407 * @conn: a #GstRTSPConnection
1408 * @flush: start or stop the flush
1410 * Start or stop the flushing action on @conn. When flushing, all current
1411 * and future actions on @conn will return #GST_RTSP_EINTR until the connection
1412 * is set to non-flushing mode again.
1414 * Returns: #GST_RTSP_OK.
1417 gst_rtsp_connection_flush (GstRTSPConnection * conn, gboolean flush)
1419 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1421 gst_poll_set_flushing (conn->fdset, flush);
1427 * gst_rtsp_connection_set_auth:
1428 * @conn: a #GstRTSPConnection
1429 * @method: authentication method
1431 * @pass: the password
1433 * Configure @conn for authentication mode @method with @user and @pass as the
1434 * user and password respectively.
1436 * Returns: #GST_RTSP_OK.
1439 gst_rtsp_connection_set_auth (GstRTSPConnection * conn,
1440 GstRTSPAuthMethod method, const gchar * user, const gchar * pass)
1442 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1444 if (method == GST_RTSP_AUTH_DIGEST && ((user == NULL || pass == NULL)
1445 || g_strrstr (user, ":") != NULL))
1446 return GST_RTSP_EINVAL;
1448 /* Make sure the username and passwd are being set for authentication */
1449 if (method == GST_RTSP_AUTH_NONE && (user == NULL || pass == NULL))
1450 return GST_RTSP_EINVAL;
1452 /* ":" chars are not allowed in usernames for basic auth */
1453 if (method == GST_RTSP_AUTH_BASIC && g_strrstr (user, ":") != NULL)
1454 return GST_RTSP_EINVAL;
1456 g_free (conn->username);
1457 g_free (conn->passwd);
1459 conn->auth_method = method;
1460 conn->username = g_strdup (user);
1461 conn->passwd = g_strdup (pass);
1468 * @key: ASCII string to hash
1470 * Hashes @key in a case-insensitive manner.
1472 * Returns: the hash code.
1475 str_case_hash (gconstpointer key)
1477 const char *p = key;
1478 guint h = g_ascii_toupper (*p);
1481 for (p += 1; *p != '\0'; p++)
1482 h = (h << 5) - h + g_ascii_toupper (*p);
1489 * @v1: an ASCII string
1490 * @v2: another ASCII string
1492 * Compares @v1 and @v2 in a case-insensitive manner
1494 * Returns: %TRUE if they are equal (modulo case)
1497 str_case_equal (gconstpointer v1, gconstpointer v2)
1499 const char *string1 = v1;
1500 const char *string2 = v2;
1502 return g_ascii_strcasecmp (string1, string2) == 0;
1506 * gst_rtsp_connection_set_auth_param:
1507 * @conn: a #GstRTSPConnection
1508 * @param: authentication directive
1511 * Setup @conn with authentication directives. This is not necesary for
1512 * methods #GST_RTSP_AUTH_NONE and #GST_RTSP_AUTH_BASIC. For
1513 * #GST_RTSP_AUTH_DIGEST, directives should be taken from the digest challenge
1514 * in the WWW-Authenticate response header and can include realm, domain,
1515 * nonce, opaque, stale, algorithm, qop as per RFC2617.
1520 gst_rtsp_connection_set_auth_param (GstRTSPConnection * conn,
1521 const gchar * param, const gchar * value)
1523 g_return_if_fail (conn != NULL);
1524 g_return_if_fail (param != NULL);
1526 if (conn->auth_params == NULL) {
1528 g_hash_table_new_full (str_case_hash, str_case_equal, g_free, g_free);
1530 g_hash_table_insert (conn->auth_params, g_strdup (param), g_strdup (value));
1534 * gst_rtsp_connection_clear_auth_params:
1535 * @conn: a #GstRTSPConnection
1537 * Clear the list of authentication directives stored in @conn.
1542 gst_rtsp_connection_clear_auth_params (GstRTSPConnection * conn)
1544 g_return_if_fail (conn != NULL);
1546 if (conn->auth_params != NULL) {
1547 g_hash_table_destroy (conn->auth_params);
1548 conn->auth_params = NULL;
1553 * gst_rtsp_connection_set_qos_dscp:
1554 * @conn: a #GstRTSPConnection
1555 * @qos_dscp: DSCP value
1557 * Configure @conn to use the specified DSCP value.
1559 * Returns: #GST_RTSP_OK on success.
1564 gst_rtsp_connection_set_qos_dscp (GstRTSPConnection * conn, guint qos_dscp)
1566 struct sockaddr_storage sa_s;
1567 socklen_t sa_sl = sizeof (sa_s);
1571 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1572 g_return_val_if_fail (conn->fd.fd >= 0, GST_RTSP_EINVAL);
1574 if (getsockname (conn->fd.fd, (struct sockaddr *) &sa_s, &sa_sl) < 0)
1575 goto no_getsockname;
1577 af = sa_s.ss_family;
1579 /* if this is an IPv4-mapped address then do IPv4 QoS */
1580 if (af == AF_INET6) {
1581 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *) &sa_s;
1583 if (IN6_IS_ADDR_V4MAPPED (&saddr6->sin6_addr))
1587 /* extract and shift 6 bits of the DSCP */
1588 tos = (qos_dscp & 0x3f) << 2;
1592 if (SETSOCKOPT (conn->fd.fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0)
1597 if (SETSOCKOPT (conn->fd.fd, IPPROTO_IPV6, IPV6_TCLASS, &tos,
1612 return GST_RTSP_ESYS;
1617 return GST_RTSP_ERROR;
1622 * gst_rtsp_connection_get_ip:
1623 * @conn: a #GstRTSPConnection
1625 * Retrieve the IP address of the other end of @conn.
1627 * Returns: The IP address as a string. this value remains valid until the
1628 * connection is closed.
1633 gst_rtsp_connection_get_ip (const GstRTSPConnection * conn)
1635 g_return_val_if_fail (conn != NULL, NULL);