1 /* GStreamer UDP network utility functions
2  * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
3  * Copyright (C) 2006 Joni Valtanen <joni.valtanen@movial.fi>
4  * Copyright (C) 2009 Jarkko Palviainen <jarkko.palviainen@sesca.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include <gst/gst.h>
27 #include <string.h>
28 
29 #include "gstudpnetutils.h"
30 
31 gboolean
gst_udp_parse_uri(const gchar * uristr,gchar ** host,guint16 * port)32 gst_udp_parse_uri (const gchar * uristr, gchar ** host, guint16 * port)
33 {
34   gchar *protocol, *location_start;
35   gchar *location, *location_end;
36   gchar *colptr;
37 
38   /* consider no protocol to be udp:// */
39   protocol = gst_uri_get_protocol (uristr);
40   if (!protocol)
41     goto no_protocol;
42   if (strcmp (protocol, "udp") != 0)
43     goto wrong_protocol;
44   g_free (protocol);
45 
46   location_start = gst_uri_get_location (uristr);
47   if (!location_start)
48     return FALSE;
49 
50   GST_DEBUG ("got location '%s'", location_start);
51 
52   /* VLC compatibility, strip everything before the @ sign. VLC uses that as the
53    * remote address. */
54   location = g_strstr_len (location_start, -1, "@");
55   if (location == NULL)
56     location = location_start;
57   else
58     location += 1;
59 
60   if (location[0] == '[') {
61     GST_DEBUG ("parse IPV6 address '%s'", location);
62     location_end = strchr (location, ']');
63     if (location_end == NULL)
64       goto wrong_address;
65 
66     *host = g_strndup (location + 1, location_end - location - 1);
67     colptr = strrchr (location_end, ':');
68   } else {
69     GST_DEBUG ("parse IPV4 address '%s'", location);
70     colptr = strrchr (location, ':');
71 
72     if (colptr != NULL) {
73       *host = g_strndup (location, colptr - location);
74     } else {
75       *host = g_strdup (location);
76     }
77   }
78   GST_DEBUG ("host set to '%s'", *host);
79 
80   if (colptr != NULL) {
81     *port = g_ascii_strtoll (colptr + 1, NULL, 10);
82   } else {
83     *port = 0;
84   }
85   g_free (location_start);
86 
87   return TRUE;
88 
89   /* ERRORS */
90 no_protocol:
91   {
92     GST_ERROR ("error parsing uri %s: no protocol", uristr);
93     return FALSE;
94   }
95 wrong_protocol:
96   {
97     GST_ERROR ("error parsing uri %s: wrong protocol (%s != udp)", uristr,
98         protocol);
99     g_free (protocol);
100     return FALSE;
101   }
102 wrong_address:
103   {
104     GST_ERROR ("error parsing uri %s", uristr);
105     g_free (location);
106     return FALSE;
107   }
108 }
109