comparison libpurple/network.h @ 15373:5fe8042783c1

Rename gtk/ and libgaim/ to pidgin/ and libpurple/
author Sean Egan <seanegan@gmail.com>
date Sat, 20 Jan 2007 02:32:10 +0000
parents
children 32c366eeeb99
comparison
equal deleted inserted replaced
15372:f79e0f4df793 15373:5fe8042783c1
1 /**
2 * @file network.h Network API
3 * @ingroup core
4 *
5 * gaim
6 *
7 * Gaim is the legal property of its developers, whose names are too numerous
8 * to list here. Please refer to the COPYRIGHT file distributed with this
9 * source distribution.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25 #ifndef _GAIM_NETWORK_H_
26 #define _GAIM_NETWORK_H_
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 /**************************************************************************/
33 /** @name Network API */
34 /**************************************************************************/
35 /*@{*/
36
37 typedef struct _GaimNetworkListenData GaimNetworkListenData;
38
39 typedef void (*GaimNetworkListenCallback) (int listenfd, gpointer data);
40
41 /**
42 * Converts a dot-decimal IP address to an array of unsigned
43 * chars. For example, converts 192.168.0.1 to a 4 byte
44 * array containing 192, 168, 0 and 1.
45 *
46 * @param ip An IP address in dot-decimal notiation.
47 * @return An array of 4 bytes containing an IP addresses
48 * equivalent to the given parameter, or NULL if
49 * the given IP address is invalid. This value
50 * is statically allocated and should not be
51 * freed.
52 */
53 const unsigned char *gaim_network_ip_atoi(const char *ip);
54
55 /**
56 * Sets the IP address of the local system in preferences. This
57 * is the IP address that should be used for incoming connections
58 * (file transfer, direct IM, etc.) and should therefore be
59 * publicly accessible.
60 *
61 * @param ip The local IP address.
62 */
63 void gaim_network_set_public_ip(const char *ip);
64
65 /**
66 * Returns the IP address of the local system set in preferences.
67 *
68 * This returns the value set via gaim_network_set_public_ip().
69 * You probably want to use gaim_network_get_my_ip() instead.
70 *
71 * @return The local IP address set in preferences.
72 */
73 const char *gaim_network_get_public_ip(void);
74
75 /**
76 * Returns the IP address of the local system.
77 *
78 * You probably want to use gaim_network_get_my_ip() instead.
79 *
80 * @note The returned string is a pointer to a static buffer. If this
81 * function is called twice, it may be important to make a copy
82 * of the returned string.
83 *
84 * @param fd The fd to use to help figure out the IP, or else -1.
85 * @return The local IP address.
86 */
87 const char *gaim_network_get_local_system_ip(int fd);
88
89 /**
90 * Returns the IP address that should be used anywhere a
91 * public IP addresses is needed (listening for an incoming
92 * file transfer, etc).
93 *
94 * If the user has manually specified an IP address via
95 * preferences, then this IP is returned. Otherwise the
96 * IP address returned by gaim_network_get_local_system_ip()
97 * is returned.
98 *
99 * @note The returned string is a pointer to a static buffer. If this
100 * function is called twice, it may be important to make a copy
101 * of the returned string.
102 *
103 * @param fd The fd to use to help figure out the IP, or -1.
104 * @return The local IP address to be used.
105 */
106 const char *gaim_network_get_my_ip(int fd);
107
108 /**
109 * Attempts to open a listening port ONLY on the specified port number.
110 * You probably want to use gaim_network_listen_range() instead of this.
111 * This function is useful, for example, if you wanted to write a telnet
112 * server as a Gaim plugin, and you HAD to listen on port 23. Why anyone
113 * would want to do that is beyond me.
114 *
115 * This opens a listening port. The caller will want to set up a watcher
116 * of type GAIM_INPUT_READ on the fd returned in cb. It will probably call
117 * accept in the watcher callback, and then possibly remove the watcher and close
118 * the listening socket, and add a new watcher on the new socket accept
119 * returned.
120 *
121 * @param port The port number to bind to. Must be greater than 0.
122 * @param socket_type The type of socket to open for listening.
123 * This will be either SOCK_STREAM for TCP or SOCK_DGRAM for UDP.
124 * @param cb The callback to be invoked when the port to listen on is available.
125 * The file descriptor of the listening socket will be specified in
126 * this callback, or -1 if no socket could be established.
127 * @param cb_data extra data to be returned when cb is called
128 *
129 * @return A pointer to a data structure that can be used to cancel
130 * the pending listener, or NULL if unable to obtain a local
131 * socket to listen on.
132 */
133 GaimNetworkListenData *gaim_network_listen(unsigned short port,
134 int socket_type, GaimNetworkListenCallback cb, gpointer cb_data);
135
136 /**
137 * Opens a listening port selected from a range of ports. The range of
138 * ports used is chosen in the following manner:
139 * If a range is specified in preferences, these values are used.
140 * If a non-0 values are passed to the function as parameters, these
141 * values are used.
142 * Otherwise a port is chosen at random by the operating system.
143 *
144 * This opens a listening port. The caller will want to set up a watcher
145 * of type GAIM_INPUT_READ on the fd returned in cb. It will probably call
146 * accept in the watcher callback, and then possibly remove the watcher and close
147 * the listening socket, and add a new watcher on the new socket accept
148 * returned.
149 *
150 * @param start The port number to bind to, or 0 to pick a random port.
151 * Users are allowed to override this arg in prefs.
152 * @param end The highest possible port in the range of ports to listen on,
153 * or 0 to pick a random port. Users are allowed to override this
154 * arg in prefs.
155 * @param socket_type The type of socket to open for listening.
156 * This will be either SOCK_STREAM for TCP or SOCK_DGRAM for UDP.
157 * @param cb The callback to be invoked when the port to listen on is available.
158 * The file descriptor of the listening socket will be specified in
159 * this callback, or -1 if no socket could be established.
160 * @param cb_data extra data to be returned when cb is called
161 *
162 * @return A pointer to a data structure that can be used to cancel
163 * the pending listener, or NULL if unable to obtain a local
164 * socket to listen on.
165 */
166 GaimNetworkListenData *gaim_network_listen_range(unsigned short start,
167 unsigned short end, int socket_type,
168 GaimNetworkListenCallback cb, gpointer cb_data);
169
170 /**
171 * This can be used to cancel any in-progress listener connection
172 * by passing in the return value from either gaim_network_listen()
173 * or gaim_network_listen_range().
174 *
175 * @param listen_data This listener attempt will be canceled and
176 * the struct will be freed.
177 */
178 void gaim_network_listen_cancel(GaimNetworkListenData *listen_data);
179
180 /**
181 * Gets a port number from a file descriptor.
182 *
183 * @param fd The file descriptor. This should be a tcp socket. The current
184 * implementation probably dies on anything but IPv4. Perhaps this
185 * possible bug will inspire new and valuable contributors to Gaim.
186 * @return The port number, in host byte order.
187 */
188 unsigned short gaim_network_get_port_from_fd(int fd);
189
190 /**
191 * Detects if there is an available Internet connection. Note that this call
192 * could block for the amount of time specified in inet_detect_timeout, so
193 * using it in a UI thread may cause uncomfortableness
194 *
195 * @return TRUE if the Internet is available
196 */
197 gboolean gaim_network_is_available(void);
198
199 /**
200 * Initializes the network subsystem.
201 */
202 void gaim_network_init(void);
203
204 /**
205 * Shuts down the network subsystem.
206 */
207 void gaim_network_uninit(void);
208
209 /*@}*/
210
211 #ifdef __cplusplus
212 }
213 #endif
214
215 #endif /* _GAIM_NETWORK_H_ */