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 if (conn->fd.fd >= 0) {
304 gst_poll_remove_fd (conn->fdset, &conn->fd);
309 return GST_RTSP_ESYS;
313 return GST_RTSP_ENET;
317 return GST_RTSP_ENOTIP;
321 if (conn->fd.fd >= 0) {
322 gst_poll_remove_fd (conn->fdset, &conn->fd);
327 return GST_RTSP_ETIMEOUT;
332 md5_digest_to_hex_string (unsigned char digest[16], char string[33])
334 static const char hexdigits[] = "0123456789abcdef";
337 for (i = 0; i < 16; i++) {
338 string[i * 2] = hexdigits[(digest[i] >> 4) & 0x0f];
339 string[i * 2 + 1] = hexdigits[digest[i] & 0x0f];
345 auth_digest_compute_hex_urp (const gchar * username,
346 const gchar * realm, const gchar * password, gchar hex_urp[33])
348 struct MD5Context md5_context;
349 unsigned char digest[16];
351 MD5Init (&md5_context);
352 MD5Update (&md5_context, username, strlen (username));
353 MD5Update (&md5_context, ":", 1);
354 MD5Update (&md5_context, realm, strlen (realm));
355 MD5Update (&md5_context, ":", 1);
356 MD5Update (&md5_context, password, strlen (password));
357 MD5Final (digest, &md5_context);
358 md5_digest_to_hex_string (digest, hex_urp);
362 auth_digest_compute_response (const gchar * method,
363 const gchar * uri, const gchar * hex_a1, const gchar * nonce,
367 struct MD5Context md5_context;
368 unsigned char digest[16];
371 MD5Init (&md5_context);
372 MD5Update (&md5_context, method, strlen (method));
373 MD5Update (&md5_context, ":", 1);
374 MD5Update (&md5_context, uri, strlen (uri));
375 MD5Final (digest, &md5_context);
376 md5_digest_to_hex_string (digest, hex_a2);
379 MD5Init (&md5_context);
380 MD5Update (&md5_context, hex_a1, strlen (hex_a1));
381 MD5Update (&md5_context, ":", 1);
382 MD5Update (&md5_context, nonce, strlen (nonce));
383 MD5Update (&md5_context, ":", 1);
385 MD5Update (&md5_context, hex_a2, 32);
386 MD5Final (digest, &md5_context);
387 md5_digest_to_hex_string (digest, response);
391 add_auth_header (GstRTSPConnection * conn, GstRTSPMessage * message)
393 switch (conn->auth_method) {
394 case GST_RTSP_AUTH_BASIC:{
396 g_strdup_printf ("%s:%s", conn->username, conn->passwd);
398 gst_rtsp_base64_encode (user_pass, strlen (user_pass));
399 gchar *auth_string = g_strdup_printf ("Basic %s", user_pass64);
401 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
405 g_free (user_pass64);
408 case GST_RTSP_AUTH_DIGEST:{
409 gchar response[33], hex_urp[33];
410 gchar *auth_string, *auth_string2;
417 /* we need to have some params set */
418 if (conn->auth_params == NULL)
421 /* we need the realm and nonce */
422 realm = (gchar *) g_hash_table_lookup (conn->auth_params, "realm");
423 nonce = (gchar *) g_hash_table_lookup (conn->auth_params, "nonce");
424 if (realm == NULL || nonce == NULL)
427 auth_digest_compute_hex_urp (conn->username, realm, conn->passwd,
430 method = gst_rtsp_method_as_text (message->type_data.request.method);
431 uri = message->type_data.request.uri;
433 /* Assume no qop, algorithm=md5, stale=false */
434 /* For algorithm MD5, a1 = urp. */
435 auth_digest_compute_response (method, uri, hex_urp, nonce, response);
436 auth_string = g_strdup_printf ("Digest username=\"%s\", "
437 "realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"",
438 conn->username, realm, nonce, uri, response);
440 opaque = (gchar *) g_hash_table_lookup (conn->auth_params, "opaque");
442 auth_string2 = g_strdup_printf ("%s, opaque=\"%s\"", auth_string,
444 g_free (auth_string);
445 auth_string = auth_string2;
447 gst_rtsp_message_take_header (message, GST_RTSP_HDR_AUTHORIZATION,
458 add_date_header (GstRTSPMessage * message)
461 gchar date_string[100];
468 g_get_current_time (&tv);
469 t = (time_t) tv.tv_sec;
472 strftime (date_string, sizeof (date_string), "%a, %d %b %Y %H:%M:%S GMT",
473 gmtime_r (&t, &tm_));
475 strftime (date_string, sizeof (date_string), "%a, %d %b %Y %H:%M:%S GMT",
479 gst_rtsp_message_add_header (message, GST_RTSP_HDR_DATE, date_string);
483 * gst_rtsp_connection_write:
484 * @conn: a #GstRTSPConnection
485 * @data: the data to write
486 * @size: the size of @data
487 * @timeout: a timeout value or #NULL
489 * Attempt to write @size bytes of @data to the connected @conn, blocking up to
490 * the specified @timeout. @timeout can be #NULL, in which case this function
491 * might block forever.
493 * This function can be cancelled with gst_rtsp_connection_flush().
495 * Returns: #GST_RTSP_OK on success.
498 gst_rtsp_connection_write (GstRTSPConnection * conn, const guint8 * data,
499 guint size, GTimeVal * timeout)
505 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
506 g_return_val_if_fail (data != NULL || size == 0, GST_RTSP_EINVAL);
508 gst_poll_set_controllable (conn->fdset, TRUE);
509 gst_poll_fd_ctl_write (conn->fdset, &conn->fd, TRUE);
510 gst_poll_fd_ctl_read (conn->fdset, &conn->fd, FALSE);
511 /* clear all previous poll results */
512 gst_poll_fd_ignored (conn->fdset, &conn->fd);
514 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
518 while (towrite > 0) {
522 retval = gst_poll_wait (conn->fdset, to);
523 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
535 /* now we can write */
536 written = WRITE_SOCKET (conn->fd.fd, data, towrite);
538 if (ERRNO_IS_NOT_EAGAIN && ERRNO_IS_NOT_EINTR)
550 return GST_RTSP_ETIMEOUT;
554 return GST_RTSP_ESYS;
558 return GST_RTSP_EINTR;
562 return GST_RTSP_ESYS;
567 * gst_rtsp_connection_send:
568 * @conn: a #GstRTSPConnection
569 * @message: the message to send
570 * @timeout: a timeout value or #NULL
572 * Attempt to send @message to the connected @conn, blocking up to
573 * the specified @timeout. @timeout can be #NULL, in which case this function
574 * might block forever.
576 * This function can be cancelled with gst_rtsp_connection_flush().
578 * Returns: #GST_RTSP_OK on success.
581 gst_rtsp_connection_send (GstRTSPConnection * conn, GstRTSPMessage * message,
592 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
593 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
596 error = WSAStartup (0x0202, &w);
601 if (w.wVersion != 0x0202)
605 str = g_string_new ("");
607 switch (message->type) {
608 case GST_RTSP_MESSAGE_REQUEST:
609 /* create request string, add CSeq */
610 g_string_append_printf (str, "%s %s RTSP/1.0\r\n"
612 gst_rtsp_method_as_text (message->type_data.request.method),
613 message->type_data.request.uri, conn->cseq++);
614 /* add session id if we have one */
615 if (conn->session_id[0] != '\0') {
616 gst_rtsp_message_add_header (message, GST_RTSP_HDR_SESSION,
619 /* add any authentication headers */
620 add_auth_header (conn, message);
622 case GST_RTSP_MESSAGE_RESPONSE:
623 /* create response string */
624 g_string_append_printf (str, "RTSP/1.0 %d %s\r\n",
625 message->type_data.response.code, message->type_data.response.reason);
627 case GST_RTSP_MESSAGE_DATA:
629 guint8 data_header[4];
631 /* prepare data header */
632 data_header[0] = '$';
633 data_header[1] = message->type_data.data.channel;
634 data_header[2] = (message->body_size >> 8) & 0xff;
635 data_header[3] = message->body_size & 0xff;
637 /* create string with header and data */
638 str = g_string_append_len (str, (gchar *) data_header, 4);
640 g_string_append_len (str, (gchar *) message->body,
645 g_return_val_if_reached (GST_RTSP_EINVAL);
649 /* append headers and body */
650 if (message->type != GST_RTSP_MESSAGE_DATA) {
651 /* add date header */
652 add_date_header (message);
655 gst_rtsp_message_append_headers (message, str);
657 /* append Content-Length and body if needed */
658 if (message->body != NULL && message->body_size > 0) {
661 len = g_strdup_printf ("%d", message->body_size);
662 g_string_append_printf (str, "%s: %s\r\n",
663 gst_rtsp_header_as_text (GST_RTSP_HDR_CONTENT_LENGTH), len);
665 /* header ends here */
666 g_string_append (str, "\r\n");
668 g_string_append_len (str, (gchar *) message->body,
671 /* just end headers */
672 g_string_append (str, "\r\n");
678 gst_rtsp_connection_write (conn, (guint8 *) str->str, str->len, timeout);
680 g_string_free (str, TRUE);
687 g_warning ("Error %d on WSAStartup", error);
688 return GST_RTSP_EWSASTART;
692 g_warning ("Windows sockets are not version 0x202 (current 0x%x)",
695 return GST_RTSP_EWSAVERSION;
701 read_line (gint fd, gchar * buffer, guint size)
709 r = READ_SOCKET (fd, &c, 1);
713 if (ERRNO_IS_NOT_EAGAIN && ERRNO_IS_NOT_EINTR)
716 if (c == '\n') /* end on \n */
718 if (c == '\r') /* ignore \r */
731 return GST_RTSP_EEOF;
735 return GST_RTSP_ESYS;
740 read_string (gchar * dest, gint size, gchar ** src)
746 while (g_ascii_isspace (**src))
749 while (!g_ascii_isspace (**src) && **src != '\0') {
759 read_key (gchar * dest, gint size, gchar ** src)
764 while (**src != ':' && **src != '\0') {
774 parse_response_status (gchar * buffer, GstRTSPMessage * msg)
777 gchar versionstr[20];
784 read_string (versionstr, sizeof (versionstr), &bptr);
785 read_string (codestr, sizeof (codestr), &bptr);
786 code = atoi (codestr);
788 while (g_ascii_isspace (*bptr))
791 if (strcmp (versionstr, "RTSP/1.0") == 0)
792 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
794 else if (strncmp (versionstr, "RTSP/", 5) == 0) {
795 GST_RTSP_CHECK (gst_rtsp_message_init_response (msg, code, bptr, NULL),
797 msg->type_data.response.version = GST_RTSP_VERSION_INVALID;
805 return GST_RTSP_EPARSE;
810 parse_request_line (gchar * buffer, GstRTSPMessage * msg)
812 GstRTSPResult res = GST_RTSP_OK;
813 gchar versionstr[20];
817 GstRTSPMethod method;
821 read_string (methodstr, sizeof (methodstr), &bptr);
822 method = gst_rtsp_find_method (methodstr);
824 read_string (urlstr, sizeof (urlstr), &bptr);
826 res = GST_RTSP_EPARSE;
828 read_string (versionstr, sizeof (versionstr), &bptr);
831 res = GST_RTSP_EPARSE;
833 if (strcmp (versionstr, "RTSP/1.0") == 0) {
834 if (gst_rtsp_message_init_request (msg, method, urlstr) != GST_RTSP_OK)
835 res = GST_RTSP_EPARSE;
836 } else if (strncmp (versionstr, "RTSP/", 5) == 0) {
837 if (gst_rtsp_message_init_request (msg, method, urlstr) != GST_RTSP_OK)
838 res = GST_RTSP_EPARSE;
839 msg->type_data.request.version = GST_RTSP_VERSION_INVALID;
841 gst_rtsp_message_init_request (msg, method, urlstr);
842 msg->type_data.request.version = GST_RTSP_VERSION_INVALID;
843 res = GST_RTSP_EPARSE;
849 /* parsing lines means reading a Key: Value pair */
851 parse_line (gchar * buffer, GstRTSPMessage * msg)
855 GstRTSPHeaderField field;
860 read_key (key, sizeof (key), &bptr);
866 field = gst_rtsp_find_header_field (key);
867 if (field != GST_RTSP_HDR_INVALID) {
868 while (g_ascii_isspace (*bptr))
870 gst_rtsp_message_add_header (msg, field, bptr);
877 return GST_RTSP_EPARSE;
882 * gst_rtsp_connection_read_internal:
883 * @conn: a #GstRTSPConnection
884 * @data: the data to read
885 * @size: the size of @data
886 * @timeout: a timeout value or #NULL
887 * @allow_interrupt: can the pending read be interrupted
889 * Attempt to read @size bytes into @data from the connected @conn, blocking up to
890 * the specified @timeout. @timeout can be #NULL, in which case this function
891 * might block forever.
893 * This function can be cancelled with gst_rtsp_connection_flush() only if
894 * @allow_interrupt is set.
896 * Returns: #GST_RTSP_OK on success.
899 gst_rtsp_connection_read_internal (GstRTSPConnection * conn, guint8 * data,
900 guint size, GTimeVal * timeout, gboolean allow_interrupt)
907 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
908 g_return_val_if_fail (data != NULL, GST_RTSP_EINVAL);
915 /* configure timeout if any */
916 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
918 /* if the call fails, just go in the select.. it should not fail. Else if
919 * there is enough data to read, skip the select call al together.*/
920 if (IOCTL_SOCKET (conn->fd.fd, FIONREAD, &avail) < 0)
922 else if (avail >= toread)
925 gst_poll_set_controllable (conn->fdset, allow_interrupt);
926 gst_poll_fd_ctl_write (conn->fdset, &conn->fd, FALSE);
927 gst_poll_fd_ctl_read (conn->fdset, &conn->fd, TRUE);
933 retval = gst_poll_wait (conn->fdset, to);
934 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
943 /* check for timeout */
948 /* if we get here there is activity on the real fd since the select
949 * completed and the control socket was not readable. */
950 bytes = READ_SOCKET (conn->fd.fd, data, toread);
953 } else if (bytes < 0) {
954 if (ERRNO_IS_NOT_EAGAIN && ERRNO_IS_NOT_EINTR)
966 return GST_RTSP_ESYS;
970 return GST_RTSP_ETIMEOUT;
974 return GST_RTSP_EINTR;
978 return GST_RTSP_EEOF;
982 return GST_RTSP_ESYS;
987 * gst_rtsp_connection_read:
988 * @conn: a #GstRTSPConnection
989 * @data: the data to read
990 * @size: the size of @data
991 * @timeout: a timeout value or #NULL
993 * Attempt to read @size bytes into @data from the connected @conn, blocking up to
994 * the specified @timeout. @timeout can be #NULL, in which case this function
995 * might block forever.
997 * This function can be cancelled with gst_rtsp_connection_flush().
999 * Returns: #GST_RTSP_OK on success.
1002 gst_rtsp_connection_read (GstRTSPConnection * conn, guint8 * data, guint size,
1005 return gst_rtsp_connection_read_internal (conn, data, size, timeout, TRUE);
1009 static GstRTSPResult
1010 read_body (GstRTSPConnection * conn, glong content_length, GstRTSPMessage * msg,
1016 if (content_length <= 0) {
1022 body = g_malloc (content_length + 1);
1023 body[content_length] = '\0';
1025 GST_RTSP_CHECK (gst_rtsp_connection_read_internal (conn, body, content_length,
1026 timeout, FALSE), read_error);
1028 content_length += 1;
1031 gst_rtsp_message_take_body (msg, (guint8 *) body, content_length);
1044 * gst_rtsp_connection_receive:
1045 * @conn: a #GstRTSPConnection
1046 * @message: the message to read
1047 * @timeout: a timeout value or #NULL
1049 * Attempt to read into @message from the connected @conn, blocking up to
1050 * the specified @timeout. @timeout can be #NULL, in which case this function
1051 * might block forever.
1053 * This function can be cancelled with gst_rtsp_connection_flush().
1055 * Returns: #GST_RTSP_OK on success.
1058 gst_rtsp_connection_receive (GstRTSPConnection * conn, GstRTSPMessage * message,
1063 glong content_length;
1067 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1068 g_return_val_if_fail (message != NULL, GST_RTSP_EINVAL);
1075 /* parse first line and headers */
1076 while (res == GST_RTSP_OK) {
1079 /* read first character, this identifies data messages */
1080 /* This is the only read() that we allow to be interrupted */
1081 GST_RTSP_CHECK (gst_rtsp_connection_read_internal (conn, &c, 1, timeout,
1084 /* check for data packet, first character is $ */
1088 /* data packets are $<1 byte channel><2 bytes length,BE><data bytes> */
1090 /* read channel, which is the next char */
1091 GST_RTSP_CHECK (gst_rtsp_connection_read_internal (conn, &c, 1, timeout,
1092 FALSE), read_error);
1094 /* now we create a data message */
1095 gst_rtsp_message_init_data (message, c);
1097 /* next two bytes are the length of the data */
1098 GST_RTSP_CHECK (gst_rtsp_connection_read_internal (conn,
1099 (guint8 *) & size, 2, timeout, FALSE), read_error);
1101 size = GUINT16_FROM_BE (size);
1103 /* and read the body */
1104 res = read_body (conn, size, message, timeout);
1110 /* we have a regular response */
1115 /* should not happen */
1120 GST_RTSP_CHECK (read_line (conn->fd.fd, buffer + offset,
1121 sizeof (buffer) - offset), read_error);
1123 if (buffer[0] == '\0')
1127 /* first line, check for response status */
1128 if (g_str_has_prefix (buffer, "RTSP")) {
1129 res = parse_response_status (buffer, message);
1131 res = parse_request_line (buffer, message);
1134 /* else just parse the line */
1135 parse_line (buffer, message);
1141 /* read the rest of the body if needed */
1146 /* see if there is a Content-Length header */
1147 if (gst_rtsp_message_get_header (message, GST_RTSP_HDR_CONTENT_LENGTH,
1148 &hdrval, 0) == GST_RTSP_OK) {
1149 /* there is, read the body */
1150 content_length = atol (hdrval);
1151 GST_RTSP_CHECK (read_body (conn, content_length, message, timeout),
1155 /* save session id in the connection for further use */
1156 if (gst_rtsp_message_get_header (message, GST_RTSP_HDR_SESSION,
1157 &session_id, 0) == GST_RTSP_OK) {
1160 /* default session timeout */
1163 maxlen = sizeof (conn->session_id) - 1;
1164 /* the sessionid can have attributes marked with ;
1165 * Make sure we strip them */
1166 for (i = 0; session_id[i] != '\0'; i++) {
1167 if (session_id[i] == ';') {
1172 } while (g_ascii_isspace (session_id[i]));
1173 if (g_str_has_prefix (&session_id[i], "timeout=")) {
1176 /* if we parsed something valid, configure */
1177 if ((to = atoi (&session_id[i + 9])) > 0)
1184 /* make sure to not overflow */
1185 strncpy (conn->session_id, session_id, maxlen);
1186 conn->session_id[maxlen] = '\0';
1198 * gst_rtsp_connection_close:
1199 * @conn: a #GstRTSPConnection
1201 * Close the connected @conn.
1203 * Returns: #GST_RTSP_OK on success.
1206 gst_rtsp_connection_close (GstRTSPConnection * conn)
1210 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1215 if (conn->fd.fd != -1) {
1216 gst_poll_remove_fd (conn->fdset, &conn->fd);
1217 res = CLOSE_SOCKET (conn->fd.fd);
1230 return GST_RTSP_ESYS;
1235 * gst_rtsp_connection_free:
1236 * @conn: a #GstRTSPConnection
1238 * Close and free @conn.
1240 * Returns: #GST_RTSP_OK on success.
1243 gst_rtsp_connection_free (GstRTSPConnection * conn)
1247 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1249 res = gst_rtsp_connection_close (conn);
1250 gst_poll_free (conn->fdset);
1254 g_timer_destroy (conn->timer);
1255 g_free (conn->username);
1256 g_free (conn->passwd);
1257 gst_rtsp_connection_clear_auth_params (conn);
1264 * gst_rtsp_connection_poll:
1265 * @conn: a #GstRTSPConnection
1266 * @events: a bitmask of #GstRTSPEvent flags to check
1267 * @revents: location for result flags
1268 * @timeout: a timeout
1270 * Wait up to the specified @timeout for the connection to become available for
1271 * at least one of the operations specified in @events. When the function returns
1272 * with #GST_RTSP_OK, @revents will contain a bitmask of available operations on
1275 * @timeout can be #NULL, in which case this function might block forever.
1277 * This function can be cancelled with gst_rtsp_connection_flush().
1279 * Returns: #GST_RTSP_OK on success.
1284 gst_rtsp_connection_poll (GstRTSPConnection * conn, GstRTSPEvent events,
1285 GstRTSPEvent * revents, GTimeVal * timeout)
1290 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1291 g_return_val_if_fail (events != 0, GST_RTSP_EINVAL);
1292 g_return_val_if_fail (revents != NULL, GST_RTSP_EINVAL);
1294 gst_poll_set_controllable (conn->fdset, TRUE);
1296 /* add fd to writer set when asked to */
1297 gst_poll_fd_ctl_write (conn->fdset, &conn->fd, events & GST_RTSP_EV_WRITE);
1299 /* add fd to reader set when asked to */
1300 gst_poll_fd_ctl_read (conn->fdset, &conn->fd, events & GST_RTSP_EV_READ);
1302 /* configure timeout if any */
1303 to = timeout ? GST_TIMEVAL_TO_TIME (*timeout) : GST_CLOCK_TIME_NONE;
1306 retval = gst_poll_wait (conn->fdset, to);
1307 } while (retval == -1 && (errno == EINTR || errno == EAGAIN));
1310 goto select_timeout;
1320 if (events & GST_RTSP_EV_READ) {
1321 if (gst_poll_fd_can_read (conn->fdset, &conn->fd))
1322 *revents |= GST_RTSP_EV_READ;
1324 if (events & GST_RTSP_EV_WRITE) {
1325 if (gst_poll_fd_can_write (conn->fdset, &conn->fd))
1326 *revents |= GST_RTSP_EV_WRITE;
1333 return GST_RTSP_ETIMEOUT;
1337 return GST_RTSP_ESYS;
1341 return GST_RTSP_EINTR;
1346 * gst_rtsp_connection_next_timeout:
1347 * @conn: a #GstRTSPConnection
1348 * @timeout: a timeout
1350 * Calculate the next timeout for @conn, storing the result in @timeout.
1352 * Returns: #GST_RTSP_OK.
1355 gst_rtsp_connection_next_timeout (GstRTSPConnection * conn, GTimeVal * timeout)
1361 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1362 g_return_val_if_fail (timeout != NULL, GST_RTSP_EINVAL);
1364 elapsed = g_timer_elapsed (conn->timer, &usec);
1365 if (elapsed >= conn->timeout) {
1369 sec = conn->timeout - elapsed;
1372 timeout->tv_sec = sec;
1373 timeout->tv_usec = usec;
1379 * gst_rtsp_connection_reset_timeout:
1380 * @conn: a #GstRTSPConnection
1382 * Reset the timeout of @conn.
1384 * Returns: #GST_RTSP_OK.
1387 gst_rtsp_connection_reset_timeout (GstRTSPConnection * conn)
1389 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1391 g_timer_start (conn->timer);
1397 * gst_rtsp_connection_flush:
1398 * @conn: a #GstRTSPConnection
1399 * @flush: start or stop the flush
1401 * Start or stop the flushing action on @conn. When flushing, all current
1402 * and future actions on @conn will return #GST_RTSP_EINTR until the connection
1403 * is set to non-flushing mode again.
1405 * Returns: #GST_RTSP_OK.
1408 gst_rtsp_connection_flush (GstRTSPConnection * conn, gboolean flush)
1410 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1412 gst_poll_set_flushing (conn->fdset, flush);
1418 * gst_rtsp_connection_set_auth:
1419 * @conn: a #GstRTSPConnection
1420 * @method: authentication method
1422 * @pass: the password
1424 * Configure @conn for authentication mode @method with @user and @pass as the
1425 * user and password respectively.
1427 * Returns: #GST_RTSP_OK.
1430 gst_rtsp_connection_set_auth (GstRTSPConnection * conn,
1431 GstRTSPAuthMethod method, const gchar * user, const gchar * pass)
1433 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1435 if (method == GST_RTSP_AUTH_DIGEST && ((user == NULL || pass == NULL)
1436 || g_strrstr (user, ":") != NULL))
1437 return GST_RTSP_EINVAL;
1439 /* Make sure the username and passwd are being set for authentication */
1440 if (method == GST_RTSP_AUTH_NONE && (user == NULL || pass == NULL))
1441 return GST_RTSP_EINVAL;
1443 /* ":" chars are not allowed in usernames for basic auth */
1444 if (method == GST_RTSP_AUTH_BASIC && g_strrstr (user, ":") != NULL)
1445 return GST_RTSP_EINVAL;
1447 g_free (conn->username);
1448 g_free (conn->passwd);
1450 conn->auth_method = method;
1451 conn->username = g_strdup (user);
1452 conn->passwd = g_strdup (pass);
1459 * @key: ASCII string to hash
1461 * Hashes @key in a case-insensitive manner.
1463 * Returns: the hash code.
1466 str_case_hash (gconstpointer key)
1468 const char *p = key;
1469 guint h = g_ascii_toupper (*p);
1472 for (p += 1; *p != '\0'; p++)
1473 h = (h << 5) - h + g_ascii_toupper (*p);
1480 * @v1: an ASCII string
1481 * @v2: another ASCII string
1483 * Compares @v1 and @v2 in a case-insensitive manner
1485 * Returns: %TRUE if they are equal (modulo case)
1488 str_case_equal (gconstpointer v1, gconstpointer v2)
1490 const char *string1 = v1;
1491 const char *string2 = v2;
1493 return g_ascii_strcasecmp (string1, string2) == 0;
1497 * gst_rtsp_connection_set_auth_param:
1498 * @conn: a #GstRTSPConnection
1499 * @param: authentication directive
1502 * Setup @conn with authentication directives. This is not necesary for
1503 * methods #GST_RTSP_AUTH_NONE and #GST_RTSP_AUTH_BASIC. For
1504 * #GST_RTSP_AUTH_DIGEST, directives should be taken from the digest challenge
1505 * in the WWW-Authenticate response header and can include realm, domain,
1506 * nonce, opaque, stale, algorithm, qop as per RFC2617.
1511 gst_rtsp_connection_set_auth_param (GstRTSPConnection * conn,
1512 const gchar * param, const gchar * value)
1514 g_return_if_fail (conn != NULL);
1515 g_return_if_fail (param != NULL);
1517 if (conn->auth_params == NULL) {
1519 g_hash_table_new_full (str_case_hash, str_case_equal, g_free, g_free);
1521 g_hash_table_insert (conn->auth_params, g_strdup (param), g_strdup (value));
1525 * gst_rtsp_connection_clear_auth_params:
1526 * @conn: a #GstRTSPConnection
1528 * Clear the list of authentication directives stored in @conn.
1533 gst_rtsp_connection_clear_auth_params (GstRTSPConnection * conn)
1535 g_return_if_fail (conn != NULL);
1537 if (conn->auth_params != NULL) {
1538 g_hash_table_destroy (conn->auth_params);
1539 conn->auth_params = NULL;
1544 * gst_rtsp_connection_set_qos_dscp:
1545 * @conn: a #GstRTSPConnection
1546 * @qos_dscp: DSCP value
1548 * Configure @conn to use the specified DSCP value.
1550 * Returns: #GST_RTSP_OK on success.
1555 gst_rtsp_connection_set_qos_dscp (GstRTSPConnection * conn, guint qos_dscp)
1557 struct sockaddr_storage sa_s;
1558 socklen_t sa_sl = sizeof (sa_s);
1562 g_return_val_if_fail (conn != NULL, GST_RTSP_EINVAL);
1563 g_return_val_if_fail (conn->fd.fd >= 0, GST_RTSP_EINVAL);
1565 if (getsockname (conn->fd.fd, (struct sockaddr *) &sa_s, &sa_sl) < 0)
1566 goto no_getsockname;
1568 af = sa_s.ss_family;
1570 /* if this is an IPv4-mapped address then do IPv4 QoS */
1571 if (af == AF_INET6) {
1572 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *) &sa_s;
1574 if (IN6_IS_ADDR_V4MAPPED (&saddr6->sin6_addr))
1578 /* extract and shift 6 bits of the DSCP */
1579 tos = (qos_dscp & 0x3f) << 2;
1583 if (SETSOCKOPT (conn->fd.fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0)
1588 if (SETSOCKOPT (conn->fd.fd, IPPROTO_IPV6, IPV6_TCLASS, &tos,
1603 return GST_RTSP_ESYS;
1608 return GST_RTSP_ERROR;
1613 * gst_rtsp_connection_get_ip:
1614 * @conn: a #GstRTSPConnection
1616 * Retrieve the IP address of the other end of @conn.
1618 * Returns: The IP address as a string.
1623 gst_rtsp_connection_get_ip (const GstRTSPConnection * conn)
1625 g_return_val_if_fail (conn != NULL, NULL);