1 /* GNet - Networking library
2  * Copyright (C) 2000, 2002  David Helder
3  * Copyright (C) 2000  Andrew Lanoix
4  * Copyright (C) 2007  Tim-Philipp Müller <tim centricular net>
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., 59 Temple Place - Suite 330,
19  * Boston, MA  02111-1307, USA.
20  */
21 
22 
23 #ifndef _GNET_INETADDR_H
24 #define _GNET_INETADDR_H
25 
26 #include <glib.h>
27 
28 G_BEGIN_DECLS
29 
30 /**
31  *  GInetAddr
32  *
33  *  GInetAddr is an internet address.
34  *
35  **/
36 typedef struct _GInetAddr GInetAddr;
37 
38 
39 /* ********** */
40 
41 GInetAddr* gnet_inetaddr_new (const gchar* hostname, gint port);
42 GInetAddr* gnet_inetaddr_new_nonblock (const gchar* hostname, gint port);
43 GInetAddr* gnet_inetaddr_new_bytes (const gchar* bytes, const guint length);
44 
45 GList*     gnet_inetaddr_new_list (const gchar* hostname, gint port);
46 void	   gnet_inetaddr_delete_list (GList* list);
47 
48 GInetAddr* gnet_inetaddr_clone (const GInetAddr* inetaddr);
49 
50 void       gnet_inetaddr_delete (GInetAddr* inetaddr);
51 void 	   gnet_inetaddr_ref (GInetAddr* inetaddr);
52 void 	   gnet_inetaddr_unref (GInetAddr* inetaddr);
53 
54 gchar*     gnet_inetaddr_get_name (/* const */ GInetAddr* inetaddr);
55 gchar*     gnet_inetaddr_get_name_nonblock (GInetAddr* inetaddr);
56 
57 gint	   gnet_inetaddr_get_length (const GInetAddr* inetaddr);
58 void	   gnet_inetaddr_get_bytes (const GInetAddr* inetaddr, gchar* buffer);
59 void	   gnet_inetaddr_set_bytes (GInetAddr* inetaddr, const gchar* bytes, gint length);
60 
61 gchar*     gnet_inetaddr_get_canonical_name (const GInetAddr* inetaddr);
62 gboolean   gnet_inetaddr_is_canonical (const gchar* hostname);
63 
64 gint 	   gnet_inetaddr_get_port (const GInetAddr* inetaddr);
65 void 	   gnet_inetaddr_set_port (const GInetAddr* inetaddr, gint port);
66 gboolean   gnet_inetaddr_is_internet  (const GInetAddr* inetaddr);
67 gboolean   gnet_inetaddr_is_private   (const GInetAddr* inetaddr);
68 gboolean   gnet_inetaddr_is_reserved  (const GInetAddr* inetaddr);
69 gboolean   gnet_inetaddr_is_loopback  (const GInetAddr* inetaddr);
70 gboolean   gnet_inetaddr_is_multicast (const GInetAddr* inetaddr);
71 gboolean   gnet_inetaddr_is_broadcast (const GInetAddr* inetaddr);
72 
73 gboolean   gnet_inetaddr_is_ipv4      (const GInetAddr* inetaddr);
74 gboolean   gnet_inetaddr_is_ipv6      (const GInetAddr* inetaddr);
75 
76 
77 guint 	   gnet_inetaddr_hash (gconstpointer p);
78 gboolean   gnet_inetaddr_equal (gconstpointer p1, gconstpointer p2);
79 gboolean   gnet_inetaddr_noport_equal (gconstpointer p1, gconstpointer p2);
80 
81 gchar*     gnet_inetaddr_get_host_name (void);
82 GInetAddr* gnet_inetaddr_get_host_addr (void);
83 
84 
85 GInetAddr* gnet_inetaddr_autodetect_internet_interface (void);
86 GInetAddr* gnet_inetaddr_get_interface_to (const GInetAddr* inetaddr);
87 GInetAddr* gnet_inetaddr_get_internet_interface (void);
88 
89 gboolean   gnet_inetaddr_is_internet_domainname (const gchar* name);
90 
91 GList*     gnet_inetaddr_list_interfaces (void);
92 
93 
94 /**
95  *  GNET_INETADDR_MAX_LEN
96  *
97  *  Maximum length of a #GInetAddr's address in bytes.  This can be
98  *  used to allocate a buffer large enough for
99  *  gnet_inetaddr_get_bytes().
100  *
101  **/
102 #define GNET_INETADDR_MAX_LEN 16
103 
104 
105 
106 /* **************************************** */
107 /* Asynchronous functions */
108 
109 
110 /**
111  *   GInetAddrNewAsyncID:
112  *
113  *   ID of an asynchronous GInetAddr creation/lookup started with
114  *   gnet_inetaddr_new_async().  The creation can be canceled by
115  *   calling gnet_inetaddr_new_async_cancel() with the ID.
116  *
117  **/
118 typedef struct _GInetAddrNewState * GInetAddrNewAsyncID;
119 
120 
121 
122 /**
123  *   GInetAddrNewAsyncFunc:
124  *   @inetaddr: InetAddr that was looked up (callee owned)
125  *   @data: User data
126  *
127  *   Callback for gnet_inetaddr_new_async().  Callee owns the address.
128  *   The address will be NULL if the lookup failed.
129  *
130  **/
131 typedef void (*GInetAddrNewAsyncFunc) (GInetAddr * inetaddr, gpointer data);
132 
133 
134 GInetAddrNewAsyncID  gnet_inetaddr_new_async      (const gchar          * hostname,
135                                                    gint                   port,
136                                                    GInetAddrNewAsyncFunc  func,
137                                                    gpointer               data);
138 
139 GInetAddrNewAsyncID  gnet_inetaddr_new_async_full (const gchar          * hostname,
140                                                    gint                   port,
141                                                    GInetAddrNewAsyncFunc  func,
142                                                    gpointer               data,
143                                                    GDestroyNotify         notify,
144                                                    GMainContext         * context,
145                                                    gint                   priority);
146 
147 void                 gnet_inetaddr_new_async_cancel (GInetAddrNewAsyncID id);
148 
149 
150 
151 /**
152  *   GInetAddrNewListAsyncID:
153  *
154  *   ID of an asynchronous GInetAddr list creation/lookup started with
155  *   gnet_inetaddr_new_list_async().  The creation can be canceled by
156  *   calling gnet_inetaddr_new_list_async_cancel() with the ID.
157  *
158  **/
159 typedef struct _GInetAddrNewListState * GInetAddrNewListAsyncID;
160 
161 
162 
163 /**
164  *   GInetAddrNewListAsyncFunc:
165  *   @list: List of GInetAddr's (callee owned)
166  *   @data: User data
167  *
168  *   Callback for gnet_inetaddr_new_list_async().  Callee owns the
169  *   list of GInetAddrs.  The list is NULL if the lookup failed.
170  *
171  **/
172 typedef void (*GInetAddrNewListAsyncFunc) (GList * list, gpointer data);
173 
174 GInetAddrNewListAsyncID  gnet_inetaddr_new_list_async      (const gchar              * hostname,
175                                                             gint                       port,
176                                                             GInetAddrNewListAsyncFunc  func,
177                                                             gpointer                   data);
178 
179 GInetAddrNewListAsyncID  gnet_inetaddr_new_list_async_full (const gchar              * hostname,
180                                                             gint                       port,
181                                                             GInetAddrNewListAsyncFunc  func,
182                                                             gpointer                   data,
183                                                             GDestroyNotify             notify,
184                                                             GMainContext             * context,
185                                                             gint                       priority);
186 
187 void                     gnet_inetaddr_new_list_async_cancel (GInetAddrNewListAsyncID id);
188 
189 
190 
191 /**
192  *   GInetAddrGetNameAsyncID:
193  *
194  *   ID of an asynchronous InetAddr name lookup started with
195  *   gnet_inetaddr_get_name_async().  The lookup can be canceled by
196  *   calling gnet_inetaddr_get_name_async_cancel() with the ID.
197  *
198  **/
199 typedef struct _GInetAddrReverseAsyncState * GInetAddrGetNameAsyncID;
200 
201 
202 
203 /**
204  *   GInetAddrGetNameAsyncFunc:
205  *   @hostname: Canonical name of the address (callee owned), NULL on failure
206  *   @data: User data
207  *
208  *   Callback for gnet_inetaddr_get_name_async().  Callee (that is: you) owns
209  *   the name.  Free it with g_free() when no longer needed.  The name will be
210  *   NULL if the lookup failed.
211  *
212  **/
213 typedef void (*GInetAddrGetNameAsyncFunc) (gchar * hostname, gpointer data);
214 
215 
216 GInetAddrGetNameAsyncID  gnet_inetaddr_get_name_async      (GInetAddr                 * inetaddr,
217                                                             GInetAddrGetNameAsyncFunc   func,
218                                                             gpointer                    data);
219 
220 GInetAddrGetNameAsyncID  gnet_inetaddr_get_name_async_full (GInetAddr                 * inetaddr,
221                                                             GInetAddrGetNameAsyncFunc   func,
222                                                             gpointer                    data,
223                                                             GDestroyNotify              notify,
224                                                             GMainContext              * context,
225                                                             gint                        priority);
226 
227 void                     gnet_inetaddr_get_name_async_cancel (GInetAddrGetNameAsyncID id);
228 
229 G_END_DECLS
230 
231 #endif /* _GNET_INETADDR_H */
232