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