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"
98 static GstRTSPResult read_line (gint fd, guint8 * buffer, guint * idx,
100 static GstRTSPResult parse_key_value (guint8 * buffer, gchar * key,
101 guint keysize, gchar ** value);
102 static void parse_string (gchar * dest, gint size, gchar ** src);
105 #define READ_SOCKET(fd, buf, len) recv (fd, (char *)buf, len, 0)
106 #define WRITE_SOCKET(fd, buf, len) send (fd, (const char *)buf, len, 0)
107 #define SETSOCKOPT(sock, level, name, val, len) setsockopt (sock, level, name, (const char *)val, len)
108 #define CLOSE_SOCKET(sock) closesocket (sock)
109 #define ERRNO_IS_EAGAIN (WSAGetLastError () == WSAEWOULDBLOCK)
110 #define ERRNO_IS_EINTR (WSAGetLastError () == WSAEINTR)
111 /* According to Microsoft's connect() documentation this one returns
112 * WSAEWOULDBLOCK and not WSAEINPROGRESS. */
113 #define ERRNO_IS_EINPROGRESS (WSAGetLastError () == WSAEWOULDBLOCK)
115 #define READ_SOCKET(fd, buf, len) read (fd, buf, len)
116 #define WRITE_SOCKET(fd, buf, len) write (fd, buf, len)
117 #define SETSOCKOPT(sock, level, name, val, len) setsockopt (sock, level, name, val, len)
118 #define CLOSE_SOCKET(sock) close (sock)
119 #define ERRNO_IS_EAGAIN (errno == EAGAIN)
120 #define ERRNO_IS_EINTR (errno == EINTR)
121 #define ERRNO_IS_EINPROGRESS (errno == EINPROGRESS)
124 #define ADD_POLLFD(fdset, pfd, fd) \
127 gst_poll_add_fd (fdset, pfd); \
130 #define REMOVE_POLLFD(fdset, pfd) \
132 if ((pfd)->fd != -1) { \
133 GST_DEBUG ("remove fd %d", (pfd)->fd); \
134 gst_poll_remove_fd (fdset, pfd); \
135 CLOSE_SOCKET ((pfd)->fd); \
140 struct _GstRTSPConnection
143 /* URL for the connection */
146 /* connection state */
159 gint cseq; /* sequence number */
160 gchar session_id[512]; /* session id */
161 gint timeout; /* session timeout in seconds */
162 GTimer *timer; /* timeout timer */
165 GstRTSPAuthMethod auth_method;
168 GHashTable *auth_params;
173 inet_aton (const char *c, struct in_addr *paddr)
175 /* note that inet_addr is deprecated on unix because
176 * inet_addr returns -1 (INADDR_NONE) for the valid 255.255.255.255
178 paddr->s_addr = inet_addr (c);
180 if (paddr->s_addr == INADDR_NONE)
197 /* a structure for constructing RTSPMessages */
210 build_reset (GstRTSPBuilder * builder)
212 g_free (builder->body_data);
213 memset (builder, 0, sizeof (builder));
217 * gst_rtsp_connection_create:
218 * @url: a #GstRTSPUrl
219 * @conn: storage for a #GstRTSPConnection
221 * Create a newly allocated #GstRTSPConnection from @url and store it in @conn.
222 * The connection will not yet attempt to connect to @url, use
223 * gst_rtsp_connection_connect().
225 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
228 gst_rtsp_connection_create (GstRTSPUrl * url, GstRTSPConnection ** conn)
230 GstRTSPConnection *newconn;
236 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
239 error = WSAStartup (0x0202, &w);
244 if (w.wVersion != 0x0202)
248 newconn = g_new0 (GstRTSPConnection, 1);
250 if ((newconn->fdset = gst_poll_new (TRUE)) == NULL)
254 newconn->fd0.fd = -1;
255 newconn->fd1.fd = -1;
256 newconn->timer = g_timer_new ();
257 newconn->timeout = 60;
258 newconn->tunneled = FALSE;
260 newconn->auth_method = GST_RTSP_AUTH_NONE;
261 newconn->username = NULL;
262 newconn->passwd = NULL;
263 newconn->auth_params = NULL;
273 g_warning ("Error %d on WSAStartup", error);
274 return GST_RTSP_EWSASTART;
278 g_warning ("Windows sockets are not version 0x202 (current 0x%x)",
281 return GST_RTSP_EWSAVERSION;
290 return GST_RTSP_ESYS;
295 * gst_rtsp_connection_accept:
297 * @conn: storage for a #GstRTSPConnection
299 * Accept a new connection on @sock and create a new #GstRTSPConnection for
300 * handling communication on new socket.
302 * Returns: #GST_RTSP_OK when @conn contains a valid connection.
307 gst_rtsp_connection_accept (gint sock, GstRTSPConnection ** conn)
310 unsigned int address_len;
311 GstRTSPConnection *newconn = NULL;
312 struct sockaddr_in address;
318 address_len = sizeof (address);
319 memset (&address, 0, address_len);
322 fd = accept (sock, (struct sockaddr *) &address, &address_len);
324 fd = accept (sock, (struct sockaddr *) &address, (gint *) & address_len);
325 #endif /* G_OS_WIN32 */
329 /* set to non-blocking mode so that we can cancel the communication */
331 fcntl (fd, F_SETFL, O_NONBLOCK);
333 ioctlsocket (fd, FIONBIO, &flags);
334 #endif /* G_OS_WIN32 */
336 /* create a url for the client address */
337 url = g_new0 (GstRTSPUrl, 1);
338 url->host = g_strdup_printf ("%s", inet_ntoa (address.sin_addr));
339 url->port = address.sin_port;
341 /* now create the connection object */
342 gst_rtsp_connection_create (url, &newconn);
343 ADD_POLLFD (newconn->fdset, &newconn->fd0, fd);
345 newconn->readfd = &newconn->fd0;
346 newconn->writefd = &newconn->fd0;
355 return GST_RTSP_ESYS;
360 do_resolve (const gchar * host)
362 struct hostent *hostinfo;
366 struct in_addr *addrp;
369 gchar ipbuf[INET_ADDRSTRLEN];
370 #endif /* G_OS_WIN32 */
374 /* first check if it already is an IP address */
375 if (inet_aton (host, &addr)) {
378 hostinfo = gethostbyname (host);
380 goto not_resolved; /* h_errno set */
382 if (hostinfo->h_addrtype != AF_INET)
383 goto not_ip; /* host not an IP host */
385 addrp = (struct in_addr *) hostinfo->h_addr_list[0];
386 /* this is not threadsafe */
387 ip = inet_ntoa (*addrp);
389 addrs = hostinfo->h_addr_list;
390 ip = inet_ntop (AF_INET, (struct in_addr *) addrs[0], ipbuf,
392 #endif /* G_OS_WIN32 */
399 GST_ERROR ("could not resolve %s", host);
404 GST_ERROR ("not an IP address");
410 do_connect (const gchar * ip, guint16 port, GstPollFD * fdout,
411 GstPoll * fdset, GTimeVal * timeout)
414 struct sockaddr_in sa_in;
417 unsigned long flags = 1;
418 #endif /* G_OS_WIN32 */
422 memset (&sa_in, 0, sizeof (sa_in));
423 sa_in.sin_family = AF_INET; /* network socket */
424 sa_in.sin_port = htons (port); /* on port */
425 sa_in.sin_addr.s_addr = inet_addr (ip); /* on host ip */
427 fd = socket (AF_INET, SOCK_STREAM, 0);
431 /* set to non-blocking mode so that we can cancel the connect */
433 fcntl (fd, F_SETFL, O_NONBLOCK);
435 ioctlsocket (fd, FIONBIO, &flags);
436 #endif /* G_OS_WIN32 */
438 /* add the socket to our fdset */
439 ADD_POLLFD (fdset, fdout, fd);
441 /* we are going to connect ASYNC now */
442 ret = connect (fd, (struct sockaddr *) &sa_in, sizeof (sa_in));
445 if (!ERRNO_IS_EINPROGRESS)
448 /* wait for connect to complete up to the specified timeout or until we got
450 gst_poll_fd_ctl_write (fdset, fdout, TRUE);
452 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
455 retval = gst_poll_wait (fdset, to);
456 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
460 else if (retval == -1)
463 /* we can still have an error connecting on windows */
464 if (gst_poll_fd_has_error (fdset, fdout)) {
465 socklen_t len = sizeof (errno);
467 getsockopt (fd, SOL_SOCKET, SO_ERROR, &errno, &len);
469 getsockopt (fd, SOL_SOCKET, SO_ERROR, (char *) &errno, &len);
474 gst_poll_fd_ignored (fdset, fdout);
482 GST_ERROR ("no socket %d (%s)", errno, g_strerror (errno));
483 return GST_RTSP_ESYS;
487 GST_ERROR ("system error %d (%s)", errno, g_strerror (errno));
488 REMOVE_POLLFD (fdset, fdout);
489 return GST_RTSP_ESYS;
493 GST_ERROR ("timeout");
494 REMOVE_POLLFD (fdset, fdout);
495 return GST_RTSP_ETIMEOUT;
500 setup_tunneling (GstRTSPConnection * conn, GTimeVal * timeout)
514 /* create a random sessionid */
515 for (i = 0; i < 24; i++)
516 sessionid[i] = g_random_int_range ('a', 'z');
517 sessionid[23] = '\0';
520 str = g_strdup_printf ("GET %s HTTP/1.0\r\n"
521 "x-sessioncookie: %s\r\n"
522 "Accept: application/x-rtsp-tunnelled\r\n"
523 "Pragma: no-cache\r\n"
524 "Cache-Control: no-cache\r\n" "\r\n", conn->url->host, sessionid);
526 /* we start by writing to this fd */
527 conn->writefd = &conn->fd0;
529 res = gst_rtsp_connection_write (conn, (guint8 *) str, strlen (str), timeout);
531 if (res != GST_RTSP_OK)
534 gst_poll_fd_ctl_write (conn->fdset, &conn->fd0, FALSE);
535 gst_poll_fd_ctl_read (conn->fdset, &conn->fd0, TRUE);
537 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
545 res = read_line (conn->fd0.fd, buffer, &idx, sizeof (buffer));
546 if (res == GST_RTSP_EEOF)
548 if (res == GST_RTSP_OK)
550 if (res != GST_RTSP_EINTR)
554 retval = gst_poll_wait (conn->fdset, to);
555 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
557 /* check for timeout */
569 /* check for last line */
570 if (buffer[0] == '\r')
572 if (buffer[0] == '\0')
576 /* first line, parse response */
577 gchar versionstr[20];
580 bptr = (gchar *) buffer;
582 parse_string (versionstr, sizeof (versionstr), &bptr);
583 parse_string (codestr, sizeof (codestr), &bptr);
584 code = atoi (codestr);
592 /* other lines, parse key/value */
593 res = parse_key_value (buffer, key, sizeof (key), &value);
594 if (res == GST_RTSP_OK) {
595 /* we got a new ip address */
596 if (g_ascii_strcasecmp (key, "x-server-ip-address") == 0) {
598 conn->ip = g_strdup (value);
605 if (!(ip = do_resolve (conn->ip)))
608 /* get the port from the url */
609 gst_rtsp_url_get_port (conn->url, &port);
611 /* connect to the host/port */
612 res = do_connect (ip, port, &conn->fd1, conn->fdset, timeout);
613 if (res != GST_RTSP_OK)
616 /* this is now our writing socket */
617 conn->writefd = &conn->fd1;
620 str = g_strdup_printf ("POST %s HTTP/1.0\r\n"
621 "x-sessioncookie: %s\r\n"
622 "Content-Type: application/x-rtsp-tunnelled\r\n"
623 "Pragma: no-cache\r\n"
624 "Cache-Control: no-cache\r\n"
625 "Content-Length: 32767\r\n"
626 "Expires: Sun, 9 Jan 1972 00:00:00 GMT\r\n"
627 "\r\n", conn->url->host, sessionid);
629 /* we start by writing to this fd */
630 conn->writefd = &conn->fd1;
632 res = gst_rtsp_connection_write (conn, (guint8 *) str, strlen (str), timeout);
634 if (res != GST_RTSP_OK)
642 GST_ERROR ("write failed", res);
647 return GST_RTSP_EEOF;
655 return GST_RTSP_ETIMEOUT;
659 return GST_RTSP_ESYS;
663 return GST_RTSP_EINTR;
667 GST_ERROR ("got failure response %d %s", code, codestr);
668 return GST_RTSP_ERROR;
672 GST_ERROR ("could not resolve %s", conn->ip);
673 return GST_RTSP_ENET;
677 GST_ERROR ("failed to connect");
683 * gst_rtsp_connection_connect:
684 * @conn: a #GstRTSPConnection
685 * @timeout: a #GTimeVal timeout
687 * Attempt to connect to the url of @conn made with
688 * gst_rtsp_connection_create(). If @timeout is #NULL this function can block
689 * forever. If @timeout contains a valid timeout, this function will return
690 * #GST_RTSP_ETIMEOUT after the timeout expired.
692 * This function can be cancelled with gst_rtsp_connection_flush().
694 * Returns: #GST_RTSP_OK when a connection could be made.
697 gst_rtsp_connection_connect (GstRTSPConnection * conn, GTimeVal * timeout)
704 unsigned long flags = 1;
705 #endif /* G_OS_WIN32 */
707 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
708 g_return_val_if_fail (conn->url != NULL, GST_RTSP_EINVAL);
709 g_return_val_if_fail (conn->fd0.fd < 0, GST_RTSP_EINVAL);
713 if (!(ip = do_resolve (url->host)))
716 /* get the port from the url */
717 gst_rtsp_url_get_port (url, &port);
719 /* connect to the host/port */
720 res = do_connect (ip, port, &conn->fd0, conn->fdset, timeout);
721 if (res != GST_RTSP_OK)
725 conn->ip = g_strdup (ip);
727 /* this is our read URL */
728 conn->readfd = &conn->fd0;
730 if (conn->tunneled) {
731 res = setup_tunneling (conn, timeout);
732 if (res != GST_RTSP_OK)
733 goto tunneling_failed;
735 conn->writefd = &conn->fd0;
742 GST_ERROR ("could not resolve %s", url->host);
743 return GST_RTSP_ENET;
747 GST_ERROR ("failed to connect");
752 GST_ERROR ("failed to setup tunneling");
758 md5_digest_to_hex_string (unsigned char digest[16], char string[33])
760 static const char hexdigits[] = "0123456789abcdef";
763 for (i = 0; i < 16; i++) {
764 string[i * 2] = hexdigits[(digest[i] >> 4) & 0x0f];
765 string[i * 2 + 1] = hexdigits[digest[i] & 0x0f];
771 auth_digest_compute_hex_urp (const gchar * username,
772 const gchar * realm, const gchar * password, gchar hex_urp[33])
774 struct MD5Context md5_context;
775 unsigned char digest[16];
777 MD5Init (&md5_context);
778 MD5Update (&md5_context, username, strlen (username));
779 MD5Update (&md5_context, ":", 1);
780 MD5Update (&md5_context, realm, strlen (realm));
781 MD5Update (&md5_context, ":", 1);
782 MD5Update (&md5_context, password, strlen (password));
783 MD5Final (digest, &md5_context);
784 md5_digest_to_hex_string (digest, hex_urp);
788 auth_digest_compute_response (const gchar * method,
789 const gchar * uri, const gchar * hex_a1, const gchar * nonce,
793 struct MD5Context md5_context;
794 unsigned char digest[16];
797 MD5Init (&md5_context);
798 MD5Update (&md5_context, method, strlen (method));
799 MD5Update (&md5_context, ":", 1);
800 MD5Update (&md5_context, uri, strlen (uri));
801 MD5Final (digest, &md5_context);
802 md5_digest_to_hex_string (digest, hex_a2);
805 MD5Init (&md5_context);
806 MD5Update (&md5_context, hex_a1, strlen (hex_a1));
807 MD5Update (&md5_context, ":", 1);
808 MD5Update (&md5_context, nonce, strlen (nonce));
809 MD5Update (&md5_context, ":", 1);
811 MD5Update (&md5_context, hex_a2, 32);
812 MD5Final (digest, &md5_context);
813 md5_digest_to_hex_string (digest, response);
817 add_auth_header (GstRTSPConnection * conn, GstRTSPMessage * message)
819 switch (conn->auth_method) {
820 case GST_RTSP_AUTH_BASIC:{
822 g_strdup_printf ("%s:%s", conn->username, conn->passwd);
824 gst_rtsp_base64_encode (user_pass, strlen (user_pass));
825 gchar *auth_string = g_strdup_printf ("Basic %s", user_pass64);
827 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
831 g_free (user_pass64);
834 case GST_RTSP_AUTH_DIGEST:{
835 gchar response[33], hex_urp[33];
836 gchar *auth_string, *auth_string2;
843 /* we need to have some params set */
844 if (conn->auth_params == NULL)
847 /* we need the realm and nonce */
848 realm = (gchar *) g_hash_table_lookup (conn->auth_params, "realm");
849 nonce = (gchar *) g_hash_table_lookup (conn->auth_params, "nonce");
850 if (realm == NULL || nonce == NULL)
853 auth_digest_compute_hex_urp (conn->username, realm, conn->passwd,
856 method = gst_rtsp_method_as_text (message->type_data.request.method);
857 uri = message->type_data.request.uri;
859 /* Assume no qop, algorithm=md5, stale=false */
860 /* For algorithm MD5, a1 = urp. */
861 auth_digest_compute_response (method, uri, hex_urp, nonce, response);
862 auth_string = g_strdup_printf ("Digest username=\"%s\", "
863 "realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"",
864 conn->username, realm, nonce, uri, response);
866 opaque = (gchar *) g_hash_table_lookup (conn->auth_params, "opaque");
868 auth_string2 = g_strdup_printf ("%s, opaque=\"%s\"", auth_string,
870 g_free (auth_string);
871 auth_string = auth_string2;
873 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
884 add_date_header (GstRTSPMessage * message)
887 gchar date_string[100];
894 g_get_current_time (&tv);
895 t = (time_t) tv.tv_sec;
898 strftime (date_string, sizeof (date_string), "%a, %d %b %Y %H:%M:%S GMT",
899 gmtime_r (&t, &tm_));
901 strftime (date_string, sizeof (date_string), "%a, %d %b %Y %H:%M:%S GMT",
905 gst_rtsp_message_add_header (message, GST_RTSP_HDR_DATE, date_string);
909 write_bytes (gint fd, const guint8 * buffer, guint * idx, guint size)
914 return GST_RTSP_ERROR;
921 r = WRITE_SOCKET (fd, &buffer[*idx], left);
923 return GST_RTSP_EINTR;
926 return GST_RTSP_EINTR;
928 return GST_RTSP_ESYS;
938 read_bytes (gint fd, guint8 * buffer, guint * idx, guint size)
943 return GST_RTSP_ERROR;
950 r = READ_SOCKET (fd, &buffer[*idx], left);
952 return GST_RTSP_EEOF;
955 return GST_RTSP_EINTR;
957 return GST_RTSP_ESYS;
967 read_line (gint fd, guint8 * buffer, guint * idx, guint size)
973 r = READ_SOCKET (fd, &c, 1);
975 return GST_RTSP_EEOF;
978 return GST_RTSP_EINTR;
980 return GST_RTSP_ESYS;
982 if (c == '\n') /* end on \n */
984 if (c == '\r') /* ignore \r */
988 buffer[(*idx)++] = c;
997 * gst_rtsp_connection_write:
998 * @conn: a #GstRTSPConnection
999 * @data: the data to write
1000 * @size: the size of @data
1001 * @timeout: a timeout value or #NULL
1003 * Attempt to write @size bytes of @data to the connected @conn, blocking up to
1004 * the specified @timeout. @timeout can be #NULL, in which case this function
1005 * might block forever.
1007 * This function can be cancelled with gst_rtsp_connection_flush().
1009 * Returns: #GST_RTSP_OK on success.
1012 gst_rtsp_connection_write (GstRTSPConnection * conn, const guint8 * data,
1013 guint size, GTimeVal * timeout)
1020 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1021 g_return_val_if_fail (data != NULL || size == 0, GST_RTSP_EINVAL);
1022 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
1024 gst_poll_set_controllable (conn->fdset, TRUE);
1025 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, TRUE);
1026 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, FALSE);
1027 /* clear all previous poll results */
1028 gst_poll_fd_ignored (conn->fdset, conn->writefd);
1029 gst_poll_fd_ignored (conn->fdset, conn->readfd);
1031 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1037 res = write_bytes (conn->writefd->fd, data, &offset, size);
1038 if (res == GST_RTSP_OK)
1040 if (res != GST_RTSP_EINTR)
1043 /* not all is written, wait until we can write more */
1045 retval = gst_poll_wait (conn->fdset, to);
1046 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1063 return GST_RTSP_ETIMEOUT;
1067 return GST_RTSP_ESYS;
1071 return GST_RTSP_EINTR;
1080 message_to_string (GstRTSPConnection * conn, GstRTSPMessage * message)
1082 GString *str = NULL;
1084 str = g_string_new ("");
1086 switch (message->type) {
1087 case GST_RTSP_MESSAGE_REQUEST:
1088 /* create request string, add CSeq */
1089 g_string_append_printf (str, "%s %s RTSP/1.0\r\n"
1091 gst_rtsp_method_as_text (message->type_data.request.method),
1092 message->type_data.request.uri, conn->cseq++);
1093 /* add session id if we have one */
1094 if (conn->session_id[0] != '\0') {
1095 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SESSION,
1098 /* add any authentication headers */
1099 add_auth_header (conn, message);
1101 case GST_RTSP_MESSAGE_RESPONSE:
1102 /* create response string */
1103 g_string_append_printf (str, "RTSP/1.0 %d %s\r\n",
1104 message->type_data.response.code, message->type_data.response.reason);
1106 case GST_RTSP_MESSAGE_DATA:
1108 guint8 data_header[4];
1110 /* prepare data header */
1111 data_header[0] = '$';
1112 data_header[1] = message->type_data.data.channel;
1113 data_header[2] = (message->body_size >> 8) & 0xff;
1114 data_header[3] = message->body_size & 0xff;
1116 /* create string with header and data */
1117 str = g_string_append_len (str, (gchar *) data_header, 4);
1119 g_string_append_len (str, (gchar *) message->body,
1120 message->body_size);
1124 g_string_free (str, TRUE);
1125 g_return_val_if_reached (NULL);
1129 /* append headers and body */
1130 if (message->type != GST_RTSP_MESSAGE_DATA) {
1131 /* add date header */
1132 add_date_header (message);
1134 /* append headers */
1135 gst_rtsp_message_append_headers (message, str);
1137 /* append Content-Length and body if needed */
1138 if (message->body != NULL && message->body_size > 0) {
1141 len = g_strdup_printf ("%d", message->body_size);
1142 g_string_append_printf (str, "%s: %s\r\n",
1143 gst_rtsp_header_as_text (GST_RTSP_HDR_CONTENT_LENGTH), len);
1145 /* header ends here */
1146 g_string_append (str, "\r\n");
1148 g_string_append_len (str, (gchar *) message->body,
1149 message->body_size);
1151 /* just end headers */
1152 g_string_append (str, "\r\n");
1160 * gst_rtsp_connection_send:
1161 * @conn: a #GstRTSPConnection
1162 * @message: the message to send
1163 * @timeout: a timeout value or #NULL
1165 * Attempt to send @message to the connected @conn, blocking up to
1166 * the specified @timeout. @timeout can be #NULL, in which case this function
1167 * might block forever.
1169 * This function can be cancelled with gst_rtsp_connection_flush().
1171 * Returns: #GST_RTSP_OK on success.
1174 gst_rtsp_connection_send (GstRTSPConnection * conn, GstRTSPMessage * message,
1177 GString *string = NULL;
1182 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1183 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1185 if (!(string = message_to_string (conn, message)))
1188 if (conn->tunneled) {
1189 str = g_base64_encode ((const guchar *) string->str, string->len);
1190 g_string_free (string, TRUE);
1195 g_string_free (string, FALSE);
1199 res = gst_rtsp_connection_write (conn, (guint8 *) str, len, timeout);
1207 g_warning ("Wrong message");
1208 return GST_RTSP_EINVAL;
1213 parse_string (gchar * dest, gint size, gchar ** src)
1219 while (g_ascii_isspace (**src))
1222 while (!g_ascii_isspace (**src) && **src != '\0') {
1224 dest[idx++] = **src;
1232 parse_key (gchar * dest, gint size, gchar ** src)
1237 while (**src != ':' && **src != '\0') {
1239 dest[idx++] = **src;
1246 static GstRTSPResult
1247 parse_response_status (guint8 * buffer, GstRTSPMessage * msg)
1250 gchar versionstr[20];
1255 bptr = (gchar *) buffer;
1257 parse_string (versionstr, sizeof (versionstr), &bptr);
1258 parse_string (codestr, sizeof (codestr), &bptr);
1259 code = atoi (codestr);
1261 while (g_ascii_isspace (*bptr))
1264 if (strcmp (versionstr, "RTSP/1.0") == 0)
1265 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
1267 else if (strncmp (versionstr, "RTSP/", 5) == 0) {
1268 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
1270 msg->type_data.response.version = GST_RTSP_VERSION_INVALID;
1278 return GST_RTSP_EPARSE;
1282 static GstRTSPResult
1283 parse_request_line (guint8 * buffer, GstRTSPMessage * msg)
1285 GstRTSPResult res = GST_RTSP_OK;
1286 gchar versionstr[20];
1287 gchar methodstr[20];
1290 GstRTSPMethod method;
1292 bptr = (gchar *) buffer;
1294 parse_string (methodstr, sizeof (methodstr), &bptr);
1295 method = gst_rtsp_find_method (methodstr);
1297 parse_string (urlstr, sizeof (urlstr), &bptr);
1298 if (*urlstr == '\0')
1299 res = GST_RTSP_EPARSE;
1301 parse_string (versionstr, sizeof (versionstr), &bptr);
1304 res = GST_RTSP_EPARSE;
1306 if (strcmp (versionstr, "RTSP/1.0") == 0) {
1307 if (gst_rtsp_message_init_request (msg, method, urlstr) != GST_RTSP_OK)
1308 res = GST_RTSP_EPARSE;
1309 } else if (strncmp (versionstr, "RTSP/", 5) == 0) {
1310 if (gst_rtsp_message_init_request (msg, method, urlstr) != GST_RTSP_OK)
1311 res = GST_RTSP_EPARSE;
1312 msg->type_data.request.version = GST_RTSP_VERSION_INVALID;
1314 gst_rtsp_message_init_request (msg, method, urlstr);
1315 msg->type_data.request.version = GST_RTSP_VERSION_INVALID;
1316 res = GST_RTSP_EPARSE;
1322 static GstRTSPResult
1323 parse_key_value (guint8 * buffer, gchar * key, guint keysize, gchar ** value)
1327 bptr = (gchar *) buffer;
1330 parse_key (key, keysize, &bptr);
1335 while (g_ascii_isspace (*bptr))
1345 return GST_RTSP_EPARSE;
1349 /* parsing lines means reading a Key: Value pair */
1350 static GstRTSPResult
1351 parse_line (guint8 * buffer, GstRTSPMessage * msg)
1356 GstRTSPHeaderField field;
1358 res = parse_key_value (buffer, key, sizeof (key), &value);
1359 if (res != GST_RTSP_OK)
1362 field = gst_rtsp_find_header_field (key);
1363 if (field != GST_RTSP_HDR_INVALID)
1364 gst_rtsp_message_add_header (msg, field, value);
1376 * GST_RTSP_OK when a complete message was read.
1377 * GST_RTSP_EEOF: when the socket is closed
1378 * GST_RTSP_EINTR: when more data is needed.
1379 * GST_RTSP_..: some other error occured.
1381 static GstRTSPResult
1382 build_next (GstRTSPBuilder * builder, GstRTSPMessage * message,
1383 GstRTSPConnection * conn)
1388 switch (builder->state) {
1390 builder->offset = 0;
1392 read_bytes (conn->readfd->fd, (guint8 *) builder->buffer,
1393 &builder->offset, 1);
1394 if (res != GST_RTSP_OK)
1397 /* we have 1 bytes now and we can see if this is a data message or
1399 if (builder->buffer[0] == '$') {
1400 /* data message, prepare for the header */
1401 builder->state = STATE_DATA_HEADER;
1404 builder->state = STATE_READ_LINES;
1407 case STATE_DATA_HEADER:
1410 read_bytes (conn->readfd->fd, (guint8 *) builder->buffer,
1411 &builder->offset, 4);
1412 if (res != GST_RTSP_OK)
1415 gst_rtsp_message_init_data (message, builder->buffer[1]);
1417 builder->body_len = (builder->buffer[2] << 8) | builder->buffer[3];
1418 builder->body_data = g_malloc (builder->body_len + 1);
1419 builder->body_data[builder->body_len] = '\0';
1420 builder->offset = 0;
1421 builder->state = STATE_DATA_BODY;
1424 case STATE_DATA_BODY:
1427 read_bytes (conn->readfd->fd, builder->body_data, &builder->offset,
1429 if (res != GST_RTSP_OK)
1432 /* we have the complete body now, store in the message adjusting the
1433 * length to include the traling '\0' */
1434 gst_rtsp_message_take_body (message,
1435 (guint8 *) builder->body_data, builder->body_len + 1);
1436 builder->body_data = NULL;
1437 builder->body_len = 0;
1439 builder->state = STATE_END;
1442 case STATE_READ_LINES:
1444 res = read_line (conn->readfd->fd, builder->buffer, &builder->offset,
1445 sizeof (builder->buffer));
1446 if (res != GST_RTSP_OK)
1449 /* we have a regular response */
1450 if (builder->buffer[0] == '\r') {
1451 builder->buffer[0] = '\0';
1454 if (builder->buffer[0] == '\0') {
1457 /* empty line, end of message header */
1458 /* see if there is a Content-Length header */
1459 if (gst_rtsp_message_get_header (message,
1460 GST_RTSP_HDR_CONTENT_LENGTH, &hdrval, 0) == GST_RTSP_OK) {
1461 /* there is, prepare to read the body */
1462 builder->body_len = atol (hdrval);
1463 builder->body_data = g_malloc (builder->body_len + 1);
1464 builder->body_data[builder->body_len] = '\0';
1465 builder->offset = 0;
1466 builder->state = STATE_DATA_BODY;
1468 builder->state = STATE_END;
1473 /* we have a line */
1474 if (builder->line == 0) {
1475 /* first line, check for response status */
1476 if (memcmp (builder->buffer, "RTSP", 4) == 0) {
1477 res = parse_response_status (builder->buffer, message);
1479 res = parse_request_line (builder->buffer, message);
1482 /* else just parse the line */
1483 parse_line (builder->buffer, message);
1486 builder->offset = 0;
1493 if (message->type == GST_RTSP_MESSAGE_DATA) {
1494 /* data messages don't have headers */
1499 /* save session id in the connection for further use */
1500 if (gst_rtsp_message_get_header (message, GST_RTSP_HDR_SESSION,
1501 &session_id, 0) == GST_RTSP_OK) {
1504 maxlen = sizeof (conn->session_id) - 1;
1505 /* the sessionid can have attributes marked with ;
1506 * Make sure we strip them */
1507 for (i = 0; session_id[i] != '\0'; i++) {
1508 if (session_id[i] == ';') {
1513 } while (g_ascii_isspace (session_id[i]));
1514 if (g_str_has_prefix (&session_id[i], "timeout=")) {
1517 /* if we parsed something valid, configure */
1518 if ((to = atoi (&session_id[i + 9])) > 0)
1525 /* make sure to not overflow */
1526 strncpy (conn->session_id, session_id, maxlen);
1527 conn->session_id[maxlen] = '\0';
1533 res = GST_RTSP_ERROR;
1542 * gst_rtsp_connection_read:
1543 * @conn: a #GstRTSPConnection
1544 * @data: the data to read
1545 * @size: the size of @data
1546 * @timeout: a timeout value or #NULL
1548 * Attempt to read @size bytes into @data from the connected @conn, blocking up to
1549 * the specified @timeout. @timeout can be #NULL, in which case this function
1550 * might block forever.
1552 * This function can be cancelled with gst_rtsp_connection_flush().
1554 * Returns: #GST_RTSP_OK on success.
1557 gst_rtsp_connection_read (GstRTSPConnection * conn, guint8 * data, guint size,
1565 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1566 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
1567 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
1574 /* configure timeout if any */
1575 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1577 gst_poll_set_controllable (conn->fdset, TRUE);
1578 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
1579 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
1582 res = read_bytes (conn->readfd->fd, data, &offset, size);
1583 if (res == GST_RTSP_EEOF)
1585 if (res == GST_RTSP_OK)
1587 if (res != GST_RTSP_EINTR)
1591 retval = gst_poll_wait (conn->fdset, to);
1592 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1594 /* check for timeout */
1596 goto select_timeout;
1604 gst_poll_set_controllable (conn->fdset, FALSE);
1611 return GST_RTSP_ESYS;
1615 return GST_RTSP_ETIMEOUT;
1619 return GST_RTSP_EINTR;
1623 return GST_RTSP_EEOF;
1633 * gst_rtsp_connection_receive:
1634 * @conn: a #GstRTSPConnection
1635 * @message: the message to read
1636 * @timeout: a timeout value or #NULL
1638 * Attempt to read into @message from the connected @conn, blocking up to
1639 * the specified @timeout. @timeout can be #NULL, in which case this function
1640 * might block forever.
1642 * This function can be cancelled with gst_rtsp_connection_flush().
1644 * Returns: #GST_RTSP_OK on success.
1647 gst_rtsp_connection_receive (GstRTSPConnection * conn, GstRTSPMessage * message,
1651 GstRTSPBuilder builder = { 0 };
1655 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1656 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1657 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
1659 /* configure timeout if any */
1660 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1662 gst_poll_set_controllable (conn->fdset, TRUE);
1663 gst_poll_fd_ctl_write (conn->fdset, conn->writefd, FALSE);
1664 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, TRUE);
1667 res = build_next (&builder, message, conn);
1668 if (res == GST_RTSP_EEOF)
1670 if (res == GST_RTSP_OK)
1672 if (res != GST_RTSP_EINTR)
1676 retval = gst_poll_wait (conn->fdset, to);
1677 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1679 /* check for timeout */
1681 goto select_timeout;
1689 gst_poll_set_controllable (conn->fdset, FALSE);
1692 /* we have a message here */
1693 build_reset (&builder);
1700 res = GST_RTSP_ESYS;
1705 res = GST_RTSP_ETIMEOUT;
1710 res = GST_RTSP_EINTR;
1715 res = GST_RTSP_EEOF;
1721 build_reset (&builder);
1722 gst_rtsp_message_unset (message);
1728 * gst_rtsp_connection_close:
1729 * @conn: a #GstRTSPConnection
1731 * Close the connected @conn.
1733 * Returns: #GST_RTSP_OK on success.
1736 gst_rtsp_connection_close (GstRTSPConnection * conn)
1738 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1743 REMOVE_POLLFD (conn->fdset, &conn->fd0);
1744 REMOVE_POLLFD (conn->fdset, &conn->fd1);
1745 conn->writefd = NULL;
1746 conn->readfd = NULL;
1752 * gst_rtsp_connection_free:
1753 * @conn: a #GstRTSPConnection
1755 * Close and free @conn.
1757 * Returns: #GST_RTSP_OK on success.
1760 gst_rtsp_connection_free (GstRTSPConnection * conn)
1764 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1766 res = gst_rtsp_connection_close (conn);
1767 gst_poll_free (conn->fdset);
1768 g_timer_destroy (conn->timer);
1769 g_free (conn->username);
1770 g_free (conn->passwd);
1771 gst_rtsp_connection_clear_auth_params (conn);
1781 * gst_rtsp_connection_poll:
1782 * @conn: a #GstRTSPConnection
1783 * @events: a bitmask of #GstRTSPEvent flags to check
1784 * @revents: location for result flags
1785 * @timeout: a timeout
1787 * Wait up to the specified @timeout for the connection to become available for
1788 * at least one of the operations specified in @events. When the function returns
1789 * with #GST_RTSP_OK, @revents will contain a bitmask of available operations on
1792 * @timeout can be #NULL, in which case this function might block forever.
1794 * This function can be cancelled with gst_rtsp_connection_flush().
1796 * Returns: #GST_RTSP_OK on success.
1801 gst_rtsp_connection_poll (GstRTSPConnection * conn, GstRTSPEvent events,
1802 GstRTSPEvent * revents, GTimeVal * timeout)
1807 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1808 g_return_val_if_fail (events != 0, GST_RTSP_EINVAL);
1809 g_return_val_if_fail (revents != NULL, GST_RTSP_EINVAL);
1810 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
1811 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
1813 gst_poll_set_controllable (conn->fdset, TRUE);
1815 /* add fd to writer set when asked to */
1816 gst_poll_fd_ctl_write (conn->fdset, conn->writefd,
1817 events & GST_RTSP_EV_WRITE);
1819 /* add fd to reader set when asked to */
1820 gst_poll_fd_ctl_read (conn->fdset, conn->readfd, events & GST_RTSP_EV_READ);
1822 /* configure timeout if any */
1823 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1826 retval = gst_poll_wait (conn->fdset, to);
1827 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1830 goto select_timeout;
1840 if (events & GST_RTSP_EV_READ) {
1841 if (gst_poll_fd_can_read (conn->fdset, conn->readfd))
1842 *revents |= GST_RTSP_EV_READ;
1844 if (events & GST_RTSP_EV_WRITE) {
1845 if (gst_poll_fd_can_write (conn->fdset, conn->writefd))
1846 *revents |= GST_RTSP_EV_WRITE;
1853 return GST_RTSP_ETIMEOUT;
1857 return GST_RTSP_ESYS;
1861 return GST_RTSP_EINTR;
1866 * gst_rtsp_connection_next_timeout:
1867 * @conn: a #GstRTSPConnection
1868 * @timeout: a timeout
1870 * Calculate the next timeout for @conn, storing the result in @timeout.
1872 * Returns: #GST_RTSP_OK.
1875 gst_rtsp_connection_next_timeout (GstRTSPConnection * conn, GTimeVal * timeout)
1881 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1882 g_return_val_if_fail (timeout != NULL, GST_RTSP_EINVAL);
1884 elapsed = g_timer_elapsed (conn->timer, &usec);
1885 if (elapsed >= conn->timeout) {
1889 sec = conn->timeout - elapsed;
1892 timeout->tv_sec = sec;
1893 timeout->tv_usec = usec;
1899 * gst_rtsp_connection_reset_timeout:
1900 * @conn: a #GstRTSPConnection
1902 * Reset the timeout of @conn.
1904 * Returns: #GST_RTSP_OK.
1907 gst_rtsp_connection_reset_timeout (GstRTSPConnection * conn)
1909 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1911 g_timer_start (conn->timer);
1917 * gst_rtsp_connection_flush:
1918 * @conn: a #GstRTSPConnection
1919 * @flush: start or stop the flush
1921 * Start or stop the flushing action on @conn. When flushing, all current
1922 * and future actions on @conn will return #GST_RTSP_EINTR until the connection
1923 * is set to non-flushing mode again.
1925 * Returns: #GST_RTSP_OK.
1928 gst_rtsp_connection_flush (GstRTSPConnection * conn, gboolean flush)
1930 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1932 gst_poll_set_flushing (conn->fdset, flush);
1938 * gst_rtsp_connection_set_auth:
1939 * @conn: a #GstRTSPConnection
1940 * @method: authentication method
1942 * @pass: the password
1944 * Configure @conn for authentication mode @method with @user and @pass as the
1945 * user and password respectively.
1947 * Returns: #GST_RTSP_OK.
1950 gst_rtsp_connection_set_auth (GstRTSPConnection * conn,
1951 GstRTSPAuthMethod method, const gchar * user, const gchar * pass)
1953 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1955 if (method == GST_RTSP_AUTH_DIGEST && ((user == NULL || pass == NULL)
1956 || g_strrstr (user, ":") != NULL))
1957 return GST_RTSP_EINVAL;
1959 /* Make sure the username and passwd are being set for authentication */
1960 if (method == GST_RTSP_AUTH_NONE && (user == NULL || pass == NULL))
1961 return GST_RTSP_EINVAL;
1963 /* ":" chars are not allowed in usernames for basic auth */
1964 if (method == GST_RTSP_AUTH_BASIC && g_strrstr (user, ":") != NULL)
1965 return GST_RTSP_EINVAL;
1967 g_free (conn->username);
1968 g_free (conn->passwd);
1970 conn->auth_method = method;
1971 conn->username = g_strdup (user);
1972 conn->passwd = g_strdup (pass);
1979 * @key: ASCII string to hash
1981 * Hashes @key in a case-insensitive manner.
1983 * Returns: the hash code.
1986 str_case_hash (gconstpointer key)
1988 const char *p = key;
1989 guint h = g_ascii_toupper (*p);
1992 for (p += 1; *p != '\0'; p++)
1993 h = (h << 5) - h + g_ascii_toupper (*p);
2000 * @v1: an ASCII string
2001 * @v2: another ASCII string
2003 * Compares @v1 and @v2 in a case-insensitive manner
2005 * Returns: %TRUE if they are equal (modulo case)
2008 str_case_equal (gconstpointer v1, gconstpointer v2)
2010 const char *string1 = v1;
2011 const char *string2 = v2;
2013 return g_ascii_strcasecmp (string1, string2) == 0;
2017 * gst_rtsp_connection_set_auth_param:
2018 * @conn: a #GstRTSPConnection
2019 * @param: authentication directive
2022 * Setup @conn with authentication directives. This is not necesary for
2023 * methods #GST_RTSP_AUTH_NONE and #GST_RTSP_AUTH_BASIC. For
2024 * #GST_RTSP_AUTH_DIGEST, directives should be taken from the digest challenge
2025 * in the WWW-Authenticate response header and can include realm, domain,
2026 * nonce, opaque, stale, algorithm, qop as per RFC2617.
2031 gst_rtsp_connection_set_auth_param (GstRTSPConnection * conn,
2032 const gchar * param, const gchar * value)
2034 g_return_if_fail (conn != NULL);
2035 g_return_if_fail (param != NULL);
2037 if (conn->auth_params == NULL) {
2039 g_hash_table_new_full (str_case_hash, str_case_equal, g_free, g_free);
2041 g_hash_table_insert (conn->auth_params, g_strdup (param), g_strdup (value));
2045 * gst_rtsp_connection_clear_auth_params:
2046 * @conn: a #GstRTSPConnection
2048 * Clear the list of authentication directives stored in @conn.
2053 gst_rtsp_connection_clear_auth_params (GstRTSPConnection * conn)
2055 g_return_if_fail (conn != NULL);
2057 if (conn->auth_params != NULL) {
2058 g_hash_table_destroy (conn->auth_params);
2059 conn->auth_params = NULL;
2063 static GstRTSPResult
2064 set_qos_dscp (gint fd, guint qos_dscp)
2069 struct sockaddr_in6 sa_in6;
2070 struct sockaddr_storage sa_stor;
2072 socklen_t slen = sizeof (sa);
2079 if (getsockname (fd, &sa.sa, &slen) < 0)
2080 goto no_getsockname;
2082 af = sa.sa.sa_family;
2084 /* if this is an IPv4-mapped address then do IPv4 QoS */
2085 if (af == AF_INET6) {
2086 if (IN6_IS_ADDR_V4MAPPED (&sa.sa_in6.sin6_addr))
2090 /* extract and shift 6 bits of the DSCP */
2091 tos = (qos_dscp & 0x3f) << 2;
2095 if (SETSOCKOPT (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0)
2100 if (SETSOCKOPT (fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof (tos)) < 0)
2114 return GST_RTSP_ESYS;
2119 return GST_RTSP_ERROR;
2124 * gst_rtsp_connection_set_qos_dscp:
2125 * @conn: a #GstRTSPConnection
2126 * @qos_dscp: DSCP value
2128 * Configure @conn to use the specified DSCP value.
2130 * Returns: #GST_RTSP_OK on success.
2135 gst_rtsp_connection_set_qos_dscp (GstRTSPConnection * conn, guint qos_dscp)
2139 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
2140 g_return_val_if_fail (conn->readfd != NULL, GST_RTSP_EINVAL);
2141 g_return_val_if_fail (conn->writefd != NULL, GST_RTSP_EINVAL);
2143 res = set_qos_dscp (conn->fd0.fd, qos_dscp);
2144 if (res == GST_RTSP_OK)
2145 res = set_qos_dscp (conn->fd1.fd, qos_dscp);
2152 * gst_rtsp_connection_get_url:
2153 * @conn: a #GstRTSPConnection
2155 * Retrieve the URL of the other end of @conn.
2157 * Returns: The URL. This value remains valid until the
2158 * connection is closed.
2163 gst_rtsp_connection_get_url (const GstRTSPConnection * conn)
2165 g_return_val_if_fail (conn != NULL, NULL);
2171 * gst_rtsp_connection_get_ip:
2172 * @conn: a #GstRTSPConnection
2174 * Retrieve the IP address of the other end of @conn.
2176 * Returns: The IP address as a string. this value remains valid until the
2177 * connection is closed.
2182 gst_rtsp_connection_get_ip (const GstRTSPConnection * conn)
2184 g_return_val_if_fail (conn != NULL, NULL);
2190 * gst_rtsp_connection_set_tunneled:
2191 * @conn: a #GstRTSPConnection
2192 * @tunneled: the new state
2194 * Set the HTTP tunneling state of the connection. This must be configured before
2195 * the @conn is connected.
2200 gst_rtsp_connection_set_tunneled (GstRTSPConnection * conn, gboolean tunneled)
2202 g_return_if_fail (conn != NULL);
2203 g_return_if_fail (conn->readfd == NULL);
2204 g_return_if_fail (conn->writefd == NULL);
2206 conn->tunneled = tunneled;
2210 * gst_rtsp_connection_is_tunneled:
2211 * @conn: a #GstRTSPConnection
2213 * Get the tunneling state of the connection.
2215 * Returns: if @conn is using HTTP tunneling.
2220 gst_rtsp_connection_is_tunneled (GstRTSPConnection * conn)
2222 g_return_val_if_fail (conn != NULL, FALSE);
2224 return conn->tunneled;
2228 #define READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR)
2229 #define WRITE_COND (G_IO_OUT | G_IO_ERR)
2237 /* async functions */
2238 struct _GstRTSPWatch
2242 GstRTSPConnection *conn;
2244 GstRTSPBuilder builder;
2245 GstRTSPMessage message;
2249 gboolean write_added;
2251 /* queued message for transmission */
2258 GstRTSPWatchFuncs funcs;
2261 GDestroyNotify notify;
2265 gst_rtsp_source_prepare (GSource * source, gint * timeout)
2267 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2269 *timeout = (watch->conn->timeout * 1000);
2275 gst_rtsp_source_check (GSource * source)
2277 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2279 if (watch->readfd.revents & READ_COND)
2282 if (watch->writefd.revents & WRITE_COND)
2289 gst_rtsp_source_dispatch (GSource * source, GSourceFunc callback,
2292 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2295 /* first read as much as we can */
2296 if (watch->readfd.revents & READ_COND) {
2298 res = build_next (&watch->builder, &watch->message, watch->conn);
2299 if (res == GST_RTSP_EINTR)
2301 if (res == GST_RTSP_EEOF)
2303 if (res != GST_RTSP_OK)
2306 if (watch->funcs.message_received)
2307 watch->funcs.message_received (watch, &watch->message,
2310 gst_rtsp_message_unset (&watch->message);
2311 build_reset (&watch->builder);
2315 if (watch->writefd.revents & WRITE_COND) {
2317 if (watch->write_data == NULL) {
2320 if (!watch->messages)
2323 /* no data, get a new message from the queue */
2324 data = watch->messages->data;
2325 watch->messages = g_list_delete_link (watch->messages, watch->messages);
2327 watch->write_off = 0;
2328 watch->write_len = data->str->len;
2329 watch->write_data = (guint8 *) g_string_free (data->str, FALSE);
2330 watch->write_cseq = data->cseq;
2332 g_slice_free (GstRTSPRec, data);
2335 res = write_bytes (watch->writefd.fd, watch->write_data,
2336 &watch->write_off, watch->write_len);
2337 if (res == GST_RTSP_EINTR)
2339 if (res != GST_RTSP_OK)
2342 if (watch->funcs.message_sent)
2343 watch->funcs.message_sent (watch, watch->write_cseq, watch->user_data);
2346 if (watch->messages == NULL && watch->write_added) {
2347 g_source_remove_poll ((GSource *) watch, &watch->writefd);
2348 watch->write_added = FALSE;
2349 watch->writefd.revents = 0;
2351 g_free (watch->write_data);
2352 watch->write_data = NULL;
2361 if (watch->funcs.closed)
2362 watch->funcs.closed (watch, watch->user_data);
2367 if (watch->funcs.error)
2368 watch->funcs.error (watch, res, watch->user_data);
2374 gst_rtsp_source_finalize (GSource * source)
2376 GstRTSPWatch *watch = (GstRTSPWatch *) source;
2379 build_reset (&watch->builder);
2381 for (walk = watch->messages; walk; walk = g_list_next (walk)) {
2382 GstRTSPRec *data = walk->data;
2384 g_string_free (data->str, TRUE);
2385 g_slice_free (GstRTSPRec, data);
2387 g_list_free (watch->messages);
2388 g_free (watch->write_data);
2391 watch->notify (watch->user_data);
2394 static GSourceFuncs gst_rtsp_source_funcs = {
2395 gst_rtsp_source_prepare,
2396 gst_rtsp_source_check,
2397 gst_rtsp_source_dispatch,
2398 gst_rtsp_source_finalize
2402 * gst_rtsp_watch_new:
2403 * @conn: a #GstRTSPConnection
2404 * @funcs: watch functions
2405 * @user_data: user data to pass to @funcs
2406 * @notify: notify when @user_data is not referenced anymore
2408 * Create a watch object for @conn. The functions provided in @funcs will be
2409 * called with @user_data when activity happened on the watch.
2411 * The new watch is usually created so that it can be attached to a
2412 * maincontext with gst_rtsp_watch_attach().
2414 * @conn must exist for the entire lifetime of the watch.
2416 * Returns: a #GstRTSPWatch that can be used for asynchronous RTSP
2417 * communication. Free with gst_rtsp_watch_unref () after usage.
2422 gst_rtsp_watch_new (GstRTSPConnection * conn,
2423 GstRTSPWatchFuncs * funcs, gpointer user_data, GDestroyNotify notify)
2425 GstRTSPWatch *result;
2427 g_return_val_if_fail (conn != NULL, NULL);
2428 g_return_val_if_fail (funcs != NULL, NULL);
2429 g_return_val_if_fail (conn->readfd != NULL, NULL);
2430 g_return_val_if_fail (conn->writefd != NULL, NULL);
2432 result = (GstRTSPWatch *) g_source_new (&gst_rtsp_source_funcs,
2433 sizeof (GstRTSPWatch));
2435 result->conn = conn;
2436 result->builder.state = STATE_START;
2438 result->readfd.fd = conn->readfd->fd;
2439 result->readfd.events = READ_COND;
2440 result->readfd.revents = 0;
2442 result->writefd.fd = conn->writefd->fd;
2443 result->writefd.events = WRITE_COND;
2444 result->writefd.revents = 0;
2445 result->write_added = FALSE;
2447 result->funcs = *funcs;
2448 result->user_data = user_data;
2449 result->notify = notify;
2451 /* only add the read fd, the write fd is only added when we have data
2453 g_source_add_poll ((GSource *) result, &result->readfd);
2459 * gst_rtsp_watch_attach:
2460 * @watch: a #GstRTSPWatch
2461 * @context: a GMainContext (if NULL, the default context will be used)
2463 * Adds a #GstRTSPWatch to a context so that it will be executed within that context.
2465 * Returns: the ID (greater than 0) for the watch within the GMainContext.
2470 gst_rtsp_watch_attach (GstRTSPWatch * watch, GMainContext * context)
2472 g_return_val_if_fail (watch != NULL, 0);
2474 return g_source_attach ((GSource *) watch, context);
2478 * gst_rtsp_watch_unref:
2479 * @watch: a #GstRTSPWatch
2481 * Decreases the reference count of @watch by one. If the resulting reference
2482 * count is zero the watch and associated memory will be destroyed.
2487 gst_rtsp_watch_unref (GstRTSPWatch * watch)
2489 g_return_if_fail (watch != NULL);
2491 g_source_unref ((GSource *) watch);
2495 * gst_rtsp_watch_queue_message:
2496 * @watch: a #GstRTSPWatch
2497 * @message: a #GstRTSPMessage
2499 * Queue a @message for transmission in @watch. The contents of this
2500 * message will be serialized and transmitted when the connection of the
2501 * watch becomes writable.
2503 * The return value of this function will be returned as the cseq argument in
2504 * the message_sent callback.
2506 * Returns: the sequence number of the message or -1 if the cseq could not be
2512 gst_rtsp_watch_queue_message (GstRTSPWatch * watch, GstRTSPMessage * message)
2518 g_return_val_if_fail (watch != NULL, GST_RTSP_EINVAL);
2519 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
2521 /* get the cseq from the message, when we finish writing this message on the
2522 * socket we will have to pass the cseq to the callback. */
2523 if (gst_rtsp_message_get_header (message, GST_RTSP_HDR_CSEQ, &header,
2524 0) == GST_RTSP_OK) {
2525 cseq = atoi (header);
2530 /* make a record with the message as a string ans cseq */
2531 data = g_slice_new (GstRTSPRec);
2532 data->str = message_to_string (watch->conn, message);
2535 /* add the record to a queue */
2536 watch->messages = g_list_append (watch->messages, data);
2538 /* make sure the main context will now also check for writability on the
2540 if (!watch->write_added) {
2541 g_source_add_poll ((GSource *) watch, &watch->writefd);
2542 watch->write_added = TRUE;