|
Evan@653
|
1 |
/** |
|
Evan@653
|
2 |
* @file network.h Network API |
|
Evan@653
|
3 |
* @ingroup core |
|
Evan@653
|
4 |
*/ |
|
Evan@653
|
5 |
|
|
Evan@653
|
6 |
/* purple |
|
Evan@653
|
7 |
* |
|
Evan@653
|
8 |
* Purple is the legal property of its developers, whose names are too numerous |
|
Evan@653
|
9 |
* to list here. Please refer to the COPYRIGHT file distributed with this |
|
Evan@653
|
10 |
* source distribution. |
|
Evan@653
|
11 |
* |
|
Evan@653
|
12 |
* This program is free software; you can redistribute it and/or modify |
|
Evan@653
|
13 |
* it under the terms of the GNU General Public License as published by |
|
Evan@653
|
14 |
* the Free Software Foundation; either version 2 of the License, or |
|
Evan@653
|
15 |
* (at your option) any later version. |
|
Evan@653
|
16 |
* |
|
Evan@653
|
17 |
* This program is distributed in the hope that it will be useful, |
|
Evan@653
|
18 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
Evan@653
|
19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
Evan@653
|
20 |
* GNU General Public License for more details. |
|
Evan@653
|
21 |
* |
|
Evan@653
|
22 |
* You should have received a copy of the GNU General Public License |
|
Evan@653
|
23 |
* along with this program; if not, write to the Free Software |
|
Evan@653
|
24 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
|
Evan@653
|
25 |
*/ |
|
Evan@653
|
26 |
#ifndef _PURPLE_NETWORK_H_ |
|
Evan@653
|
27 |
#define _PURPLE_NETWORK_H_ |
|
Evan@653
|
28 |
|
|
Evan@653
|
29 |
#ifdef __cplusplus |
|
Evan@653
|
30 |
extern "C" { |
|
Evan@653
|
31 |
#endif |
|
Evan@653
|
32 |
|
|
Evan@653
|
33 |
/**************************************************************************/ |
|
Evan@653
|
34 |
/** @name Network API */ |
|
Evan@653
|
35 |
/**************************************************************************/ |
|
Evan@653
|
36 |
/*@{*/ |
|
Evan@653
|
37 |
|
|
Evan@653
|
38 |
typedef struct _PurpleNetworkListenData PurpleNetworkListenData; |
|
Evan@653
|
39 |
|
|
Evan@653
|
40 |
typedef void (*PurpleNetworkListenCallback) (int listenfd, gpointer data); |
|
Evan@653
|
41 |
|
|
Evan@653
|
42 |
/** |
|
Evan@653
|
43 |
* Converts a dot-decimal IP address to an array of unsigned |
|
Evan@653
|
44 |
* chars. For example, converts 192.168.0.1 to a 4 byte |
|
Evan@653
|
45 |
* array containing 192, 168, 0 and 1. |
|
Evan@653
|
46 |
* |
|
Evan@653
|
47 |
* @param ip An IP address in dot-decimal notiation. |
|
Evan@653
|
48 |
* @return An array of 4 bytes containing an IP addresses |
|
Evan@653
|
49 |
* equivalent to the given parameter, or NULL if |
|
Evan@653
|
50 |
* the given IP address is invalid. This value |
|
Evan@653
|
51 |
* is statically allocated and should not be |
|
Evan@653
|
52 |
* freed. |
|
Evan@653
|
53 |
*/ |
|
Evan@653
|
54 |
const unsigned char *purple_network_ip_atoi(const char *ip); |
|
Evan@653
|
55 |
|
|
Evan@653
|
56 |
/** |
|
Evan@653
|
57 |
* Sets the IP address of the local system in preferences. This |
|
Evan@653
|
58 |
* is the IP address that should be used for incoming connections |
|
Evan@653
|
59 |
* (file transfer, direct IM, etc.) and should therefore be |
|
Evan@653
|
60 |
* publicly accessible. |
|
Evan@653
|
61 |
* |
|
Evan@653
|
62 |
* @param ip The local IP address. |
|
Evan@653
|
63 |
*/ |
|
Evan@653
|
64 |
void purple_network_set_public_ip(const char *ip); |
|
Evan@653
|
65 |
|
|
Evan@653
|
66 |
/** |
|
Evan@653
|
67 |
* Returns the IP address of the local system set in preferences. |
|
Evan@653
|
68 |
* |
|
Evan@653
|
69 |
* This returns the value set via purple_network_set_public_ip(). |
|
Evan@653
|
70 |
* You probably want to use purple_network_get_my_ip() instead. |
|
Evan@653
|
71 |
* |
|
Evan@653
|
72 |
* @return The local IP address set in preferences. |
|
Evan@653
|
73 |
*/ |
|
Evan@653
|
74 |
const char *purple_network_get_public_ip(void); |
|
Evan@653
|
75 |
|
|
Evan@653
|
76 |
/** |
|
Evan@653
|
77 |
* Returns the IP address of the local system. |
|
Evan@653
|
78 |
* |
|
Evan@653
|
79 |
* You probably want to use purple_network_get_my_ip() instead. |
|
Evan@653
|
80 |
* |
|
Evan@653
|
81 |
* @note The returned string is a pointer to a static buffer. If this |
|
Evan@653
|
82 |
* function is called twice, it may be important to make a copy |
|
Evan@653
|
83 |
* of the returned string. |
|
Evan@653
|
84 |
* |
|
Evan@653
|
85 |
* @param fd The fd to use to help figure out the IP, or else -1. |
|
Evan@653
|
86 |
* @return The local IP address. |
|
Evan@653
|
87 |
*/ |
|
Evan@653
|
88 |
const char *purple_network_get_local_system_ip(int fd); |
|
Evan@653
|
89 |
|
|
Evan@653
|
90 |
/** |
|
Evan@653
|
91 |
* Returns the IP address that should be used anywhere a |
|
Evan@653
|
92 |
* public IP addresses is needed (listening for an incoming |
|
Evan@653
|
93 |
* file transfer, etc). |
|
Evan@653
|
94 |
* |
|
Evan@653
|
95 |
* If the user has manually specified an IP address via |
|
Evan@653
|
96 |
* preferences, then this IP is returned. Otherwise the |
|
Evan@653
|
97 |
* IP address returned by purple_network_get_local_system_ip() |
|
Evan@653
|
98 |
* is returned. |
|
Evan@653
|
99 |
* |
|
Evan@653
|
100 |
* @note The returned string is a pointer to a static buffer. If this |
|
Evan@653
|
101 |
* function is called twice, it may be important to make a copy |
|
Evan@653
|
102 |
* of the returned string. |
|
Evan@653
|
103 |
* |
|
Evan@653
|
104 |
* @param fd The fd to use to help figure out the IP, or -1. |
|
Evan@653
|
105 |
* @return The local IP address to be used. |
|
Evan@653
|
106 |
*/ |
|
Evan@653
|
107 |
const char *purple_network_get_my_ip(int fd); |
|
Evan@653
|
108 |
|
|
Evan@653
|
109 |
/** |
|
Evan@653
|
110 |
* Should calls to purple_network_listen() and purple_network_listen_range() |
|
Evan@653
|
111 |
* map the port externally using NAT-PMP or UPnP? |
|
Evan@653
|
112 |
* The default value is TRUE |
|
Evan@653
|
113 |
* |
|
Evan@653
|
114 |
* @param map_external Should the open port be mapped externally? |
|
Evan@653
|
115 |
* @deprecated In 3.0.0 a boolean will be added to the above functions to |
|
Evan@653
|
116 |
* perform the same function. |
|
Evan@653
|
117 |
* @since 2.3.0 |
|
Evan@653
|
118 |
*/ |
|
Evan@653
|
119 |
void purple_network_listen_map_external(gboolean map_external); |
|
Evan@653
|
120 |
|
|
Evan@653
|
121 |
/** |
|
Evan@653
|
122 |
* Attempts to open a listening port ONLY on the specified port number. |
|
Evan@653
|
123 |
* You probably want to use purple_network_listen_range() instead of this. |
|
Evan@653
|
124 |
* This function is useful, for example, if you wanted to write a telnet |
|
Evan@653
|
125 |
* server as a Purple plugin, and you HAD to listen on port 23. Why anyone |
|
Evan@653
|
126 |
* would want to do that is beyond me. |
|
Evan@653
|
127 |
* |
|
Evan@653
|
128 |
* This opens a listening port. The caller will want to set up a watcher |
|
Evan@653
|
129 |
* of type PURPLE_INPUT_READ on the fd returned in cb. It will probably call |
|
Evan@653
|
130 |
* accept in the watcher callback, and then possibly remove the watcher and close |
|
Evan@653
|
131 |
* the listening socket, and add a new watcher on the new socket accept |
|
Evan@653
|
132 |
* returned. |
|
Evan@653
|
133 |
* |
|
Evan@653
|
134 |
* @param port The port number to bind to. Must be greater than 0. |
|
Evan@653
|
135 |
* @param socket_type The type of socket to open for listening. |
|
Evan@653
|
136 |
* This will be either SOCK_STREAM for TCP or SOCK_DGRAM for UDP. |
|
Evan@653
|
137 |
* @param cb The callback to be invoked when the port to listen on is available. |
|
Evan@653
|
138 |
* The file descriptor of the listening socket will be specified in |
|
Evan@653
|
139 |
* this callback, or -1 if no socket could be established. |
|
Evan@653
|
140 |
* @param cb_data extra data to be returned when cb is called |
|
Evan@653
|
141 |
* |
|
Evan@653
|
142 |
* @return A pointer to a data structure that can be used to cancel |
|
Evan@653
|
143 |
* the pending listener, or NULL if unable to obtain a local |
|
Evan@653
|
144 |
* socket to listen on. |
|
Evan@653
|
145 |
*/ |
|
Evan@653
|
146 |
PurpleNetworkListenData *purple_network_listen(unsigned short port, |
|
Evan@653
|
147 |
int socket_type, PurpleNetworkListenCallback cb, gpointer cb_data); |
|
Evan@653
|
148 |
|
|
Evan@653
|
149 |
/** |
|
Evan@653
|
150 |
* Opens a listening port selected from a range of ports. The range of |
|
Evan@653
|
151 |
* ports used is chosen in the following manner: |
|
Evan@653
|
152 |
* If a range is specified in preferences, these values are used. |
|
Evan@653
|
153 |
* If a non-0 values are passed to the function as parameters, these |
|
Evan@653
|
154 |
* values are used. |
|
Evan@653
|
155 |
* Otherwise a port is chosen at random by the operating system. |
|
Evan@653
|
156 |
* |
|
Evan@653
|
157 |
* This opens a listening port. The caller will want to set up a watcher |
|
Evan@653
|
158 |
* of type PURPLE_INPUT_READ on the fd returned in cb. It will probably call |
|
Evan@653
|
159 |
* accept in the watcher callback, and then possibly remove the watcher and close |
|
Evan@653
|
160 |
* the listening socket, and add a new watcher on the new socket accept |
|
Evan@653
|
161 |
* returned. |
|
Evan@653
|
162 |
* |
|
Evan@653
|
163 |
* @param start The port number to bind to, or 0 to pick a random port. |
|
Evan@653
|
164 |
* Users are allowed to override this arg in prefs. |
|
Evan@653
|
165 |
* @param end The highest possible port in the range of ports to listen on, |
|
Evan@653
|
166 |
* or 0 to pick a random port. Users are allowed to override this |
|
Evan@653
|
167 |
* arg in prefs. |
|
Evan@653
|
168 |
* @param socket_type The type of socket to open for listening. |
|
Evan@653
|
169 |
* This will be either SOCK_STREAM for TCP or SOCK_DGRAM for UDP. |
|
Evan@653
|
170 |
* @param cb The callback to be invoked when the port to listen on is available. |
|
Evan@653
|
171 |
* The file descriptor of the listening socket will be specified in |
|
Evan@653
|
172 |
* this callback, or -1 if no socket could be established. |
|
Evan@653
|
173 |
* @param cb_data extra data to be returned when cb is called |
|
Evan@653
|
174 |
* |
|
Evan@653
|
175 |
* @return A pointer to a data structure that can be used to cancel |
|
Evan@653
|
176 |
* the pending listener, or NULL if unable to obtain a local |
|
Evan@653
|
177 |
* socket to listen on. |
|
Evan@653
|
178 |
*/ |
|
Evan@653
|
179 |
PurpleNetworkListenData *purple_network_listen_range(unsigned short start, |
|
Evan@653
|
180 |
unsigned short end, int socket_type, |
|
Evan@653
|
181 |
PurpleNetworkListenCallback cb, gpointer cb_data); |
|
Evan@653
|
182 |
|
|
Evan@653
|
183 |
/** |
|
Evan@653
|
184 |
* This can be used to cancel any in-progress listener connection |
|
Evan@653
|
185 |
* by passing in the return value from either purple_network_listen() |
|
Evan@653
|
186 |
* or purple_network_listen_range(). |
|
Evan@653
|
187 |
* |
|
Evan@653
|
188 |
* @param listen_data This listener attempt will be canceled and |
|
Evan@653
|
189 |
* the struct will be freed. |
|
Evan@653
|
190 |
*/ |
|
Evan@653
|
191 |
void purple_network_listen_cancel(PurpleNetworkListenData *listen_data); |
|
Evan@653
|
192 |
|
|
Evan@653
|
193 |
/** |
|
Evan@653
|
194 |
* Gets a port number from a file descriptor. |
|
Evan@653
|
195 |
* |
|
Evan@653
|
196 |
* @param fd The file descriptor. This should be a tcp socket. The current |
|
Evan@653
|
197 |
* implementation probably dies on anything but IPv4. Perhaps this |
|
Evan@653
|
198 |
* possible bug will inspire new and valuable contributors to Purple. |
|
Evan@653
|
199 |
* @return The port number, in host byte order. |
|
Evan@653
|
200 |
*/ |
|
Evan@653
|
201 |
unsigned short purple_network_get_port_from_fd(int fd); |
|
Evan@653
|
202 |
|
|
Evan@653
|
203 |
/** |
|
Evan@653
|
204 |
* Detects if there is an available network connection. |
|
Evan@653
|
205 |
* |
|
Evan@653
|
206 |
* @return TRUE if the network is available |
|
Evan@653
|
207 |
*/ |
|
Evan@653
|
208 |
gboolean purple_network_is_available(void); |
|
Evan@653
|
209 |
|
|
Evan@653
|
210 |
/** |
|
Evan@1427
|
211 |
* Makes purple_network_is_available() always return @c TRUE. |
|
Evan@1427
|
212 |
* |
|
Evan@1427
|
213 |
* This is what backs the --force-online command line argument in Pidgin, |
|
Evan@1427
|
214 |
* for example. This is useful for offline testing, especially when |
|
Evan@1427
|
215 |
* combined with nullprpl. |
|
Evan@1427
|
216 |
* |
|
Evan@1427
|
217 |
* @since 2.6.0 |
|
Evan@1427
|
218 |
*/ |
|
Evan@1427
|
219 |
void purple_network_force_online(void); |
|
Evan@1427
|
220 |
|
|
Evan@1427
|
221 |
/** |
|
Evan@653
|
222 |
* Get the handle for the network system |
|
Evan@653
|
223 |
* |
|
Evan@653
|
224 |
* @return the handle to the network system |
|
Evan@653
|
225 |
*/ |
|
Evan@653
|
226 |
void *purple_network_get_handle(void); |
|
Evan@653
|
227 |
|
|
zacw@1759
|
228 |
/** |
|
zacw@1759
|
229 |
* Update the STUN server IP given the host name |
|
zacw@1759
|
230 |
* Will result in a DNS query being executed asynchronous |
|
zacw@1759
|
231 |
* |
|
zacw@1759
|
232 |
* @param stun_server The host name of the STUN server to set |
|
zacw@1759
|
233 |
* @since 2.6.0 |
|
zacw@1759
|
234 |
*/ |
|
zacw@1759
|
235 |
void purple_network_set_stun_server(const gchar *stun_server); |
|
zacw@1759
|
236 |
|
|
zacw@1759
|
237 |
/** |
|
zacw@1759
|
238 |
* Get the IP address of the STUN server as a string representation |
|
zacw@1759
|
239 |
* |
|
zacw@1759
|
240 |
* @return the IP address |
|
zacw@1759
|
241 |
* @since 2.6.0 |
|
zacw@1759
|
242 |
*/ |
|
zacw@1759
|
243 |
const gchar *purple_network_get_stun_ip(void); |
|
zacw@1759
|
244 |
|
|
zacw@1759
|
245 |
/** |
|
zacw@1759
|
246 |
* Update the TURN server IP given the host name |
|
zacw@1759
|
247 |
* Will result in a DNS query being executed asynchronous |
|
zacw@1759
|
248 |
* |
|
Evan@2571
|
249 |
* @param turn_server The host name of the TURN server to set |
|
zacw@1759
|
250 |
* @since 2.6.0 |
|
zacw@1759
|
251 |
*/ |
|
zacw@2535
|
252 |
void purple_network_set_turn_server(const gchar *turn_server); |
|
zacw@1759
|
253 |
|
|
zacw@1759
|
254 |
/** |
|
zacw@1759
|
255 |
* Get the IP address of the STUN server as a string representation |
|
zacw@1759
|
256 |
* |
|
zacw@1759
|
257 |
* @return the IP address |
|
zacw@1759
|
258 |
* @since 2.6.0 |
|
zacw@1759
|
259 |
*/ |
|
zacw@1759
|
260 |
const gchar *purple_network_get_turn_ip(void); |
|
zacw@1759
|
261 |
|
|
zacw@2504
|
262 |
/** |
|
zacw@2504
|
263 |
* Remove a port mapping (UPnP or NAT-PMP) associated with listening socket |
|
zacw@2504
|
264 |
* |
|
zacw@2504
|
265 |
* @param fd Socket to remove the port mapping for |
|
zacw@2504
|
266 |
* @since 2.6.0 |
|
zacw@2504
|
267 |
*/ |
|
zacw@2504
|
268 |
void purple_network_remove_port_mapping(gint fd); |
|
zacw@2504
|
269 |
|
|
Evan@653
|
270 |
/** |
|
Evan@2571
|
271 |
* Convert a UTF-8 domain name to ASCII in accordance with the IDNA |
|
Evan@2571
|
272 |
* specification. If libpurple is compiled without IDN support, this function |
|
Evan@2571
|
273 |
* copies the input into the output buffer. |
|
Evan@2571
|
274 |
* |
|
Evan@2571
|
275 |
* Because this function is used by DNS resolver child/threads, it uses no |
|
Evan@2571
|
276 |
* other libpurple API and is threadsafe. |
|
Evan@2571
|
277 |
* |
|
Evan@2571
|
278 |
* In general, a buffer of about 512 bytes is the appropriate size to use. |
|
Evan@2571
|
279 |
* |
|
Evan@2571
|
280 |
* @param in The hostname to be converted. |
|
Evan@2571
|
281 |
* @param out The output buffer where an allocated string will be returned. |
|
Evan@2571
|
282 |
* The caller is responsible for freeing this. |
|
Evan@2571
|
283 |
* @returns 0 on success, -1 if the out is NULL, or an error code |
|
Evan@2571
|
284 |
* that currently corresponds to the Idna_rc enum in libidn. |
|
Evan@2571
|
285 |
* @since 2.6.0 |
|
Evan@2571
|
286 |
*/ |
|
Evan@2571
|
287 |
int purple_network_convert_idn_to_ascii(const gchar *in, gchar **out); |
|
Evan@2571
|
288 |
|
|
Evan@2571
|
289 |
/** |
|
Evan@653
|
290 |
* Initializes the network subsystem. |
|
Evan@653
|
291 |
*/ |
|
Evan@653
|
292 |
void purple_network_init(void); |
|
Evan@653
|
293 |
|
|
Evan@653
|
294 |
/** |
|
Evan@653
|
295 |
* Shuts down the network subsystem. |
|
Evan@653
|
296 |
*/ |
|
Evan@653
|
297 |
void purple_network_uninit(void); |
|
Evan@653
|
298 |
|
|
Evan@653
|
299 |
/*@}*/ |
|
Evan@653
|
300 |
|
|
Evan@653
|
301 |
#ifdef __cplusplus |
|
Evan@653
|
302 |
} |
|
Evan@653
|
303 |
#endif |
|
Evan@653
|
304 |
|
|
Evan@653
|
305 |
#endif /* _PURPLE_NETWORK_H_ */ |