• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) 2020 Huawei Technologies Co., Ltd.
4  *   @Author: Stéphane Cerveau <scerveau@collabora.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 "gsttcpelements.h"
27 
28 GST_DEBUG_CATEGORY (tcp_debug);
29 #define GST_CAT_DEFAULT tcp_debug
30 
31 void
tcp_element_init(GstPlugin * plugin)32 tcp_element_init (GstPlugin * plugin)
33 {
34   static gsize res = FALSE;
35   if (g_once_init_enter (&res)) {
36     GST_DEBUG_CATEGORY_INIT (tcp_debug, "tcp", 0, "TCP calls");
37     g_once_init_leave (&res, TRUE);
38   }
39 }
40 
41 GList *
tcp_get_addresses(GstElement * obj,const char * host,GCancellable * cancellable,GError ** err)42 tcp_get_addresses (GstElement * obj, const char *host,
43     GCancellable * cancellable, GError ** err)
44 {
45   GList *addrs = NULL;
46   GInetAddress *addr;
47 
48   g_return_val_if_fail (GST_IS_ELEMENT (obj), NULL);
49   g_return_val_if_fail (host != NULL, NULL);
50   g_return_val_if_fail (err == NULL || *err == NULL, NULL);
51 
52   /* look up name if we need to */
53   addr = g_inet_address_new_from_string (host);
54   if (addr) {
55     addrs = g_list_append (addrs, addr);
56   } else {
57     GResolver *resolver = g_resolver_get_default ();
58 
59     GST_DEBUG_OBJECT (obj, "Looking up IP address(es) for host '%s'", host);
60     addrs = g_resolver_lookup_by_name (resolver, host, cancellable, err);
61     g_object_unref (resolver);
62   }
63 
64   return addrs;
65 }
66 
67 /*
68  * Loops over available addresses until successfully creates a socket
69  *
70  * iter: updated to contain current list position or NULL if finished
71  * saddr: contains current address is successful
72  */
73 GSocket *
tcp_create_socket(GstElement * obj,GList ** iter,guint16 port,GSocketAddress ** saddr,GError ** err)74 tcp_create_socket (GstElement * obj, GList ** iter, guint16 port,
75     GSocketAddress ** saddr, GError ** err)
76 {
77   GSocket *sock = NULL;
78 
79   g_return_val_if_fail (GST_IS_ELEMENT (obj), NULL);
80   g_return_val_if_fail (iter != NULL, NULL);
81   g_return_val_if_fail (saddr != NULL, NULL);
82   g_return_val_if_fail (err == NULL || *err == NULL, NULL);
83 
84   *saddr = NULL;
85   while (*iter) {
86     GInetAddress *addr = G_INET_ADDRESS ((*iter)->data);
87 
88 #ifndef GST_DISABLE_GST_DEBUG
89     {
90       gchar *ip = g_inet_address_to_string (addr);
91       GST_DEBUG_OBJECT (obj, "Trying IP address %s", ip);
92       g_free (ip);
93     }
94 #endif
95     /* clean up from possible previous iterations */
96     g_clear_error (err);
97     /* update iter in case we get called again */
98     *iter = (*iter)->next;
99 
100     *saddr = g_inet_socket_address_new (addr, port);
101     sock =
102         g_socket_new (g_socket_address_get_family (*saddr),
103         G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_TCP, err);
104     if (sock)
105       break;
106 
107     /* release and try next... */
108     g_clear_object (saddr);
109   }
110 
111   return sock;
112 }
113