comparison libpurple/network.h @ 16177:d88f0f320c9b

merge of '07fc4db9a3c2c12596e0354b8e7959aa847f966b' and '2e6d324c725b3e6a2c803589bca3f0ac9b9790bf'
author Richard Laager <rlaager@wiktel.com>
date Mon, 16 Apr 2007 00:44:33 +0000
parents 1a12ce76c4f4
children 44b4e8bd759b
comparison
equal deleted inserted replaced
15938:2e3eba412412 16177:d88f0f320c9b
1 /**
2 * @file network.h Network API
3 * @ingroup core
4 *
5 * purple
6 *
7 * Purple 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 _PURPLE_NETWORK_H_
26 #define _PURPLE_NETWORK_H_
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 /**************************************************************************/
33 /** @name Network API */
34 /**************************************************************************/
35 /*@{*/
36
37 typedef struct _PurpleNetworkListenData PurpleNetworkListenData;
38
39 typedef void (*PurpleNetworkListenCallback) (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 *purple_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 purple_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 purple_network_set_public_ip().
69 * You probably want to use purple_network_get_my_ip() instead.
70 *
71 * @return The local IP address set in preferences.
72 */
73 const char *purple_network_get_public_ip(void);
74
75 /**
76 * Returns the IP address of the local system.
77 *
78 * You probably want to use purple_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 *purple_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 purple_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 *purple_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 purple_network_listen_range() instead of this.
111 * This function is useful, for example, if you wanted to write a telnet
112 * server as a Purple 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 PURPLE_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 PurpleNetworkListenData *purple_network_listen(unsigned short port,
134 int socket_type, PurpleNetworkListenCallback 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 PURPLE_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 PurpleNetworkListenData *purple_network_listen_range(unsigned short start,
167 unsigned short end, int socket_type,
168 PurpleNetworkListenCallback 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 purple_network_listen()
173 * or purple_network_listen_range().
174 *
175 * @param listen_data This listener attempt will be canceled and
176 * the struct will be freed.
177 */
178 void purple_network_listen_cancel(PurpleNetworkListenData *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 Purple.
186 * @return The port number, in host byte order.
187 */
188 unsigned short purple_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 purple_network_is_available(void);
198
199 /**
200 * Get the handle for the network system
201 *
202 * @return the handle to the network system
203 */
204 void *purple_network_get_handle(void);
205
206 /**
207 * Initializes the network subsystem.
208 */
209 void purple_network_init(void);
210
211 /**
212 * Shuts down the network subsystem.
213 */
214 void purple_network_uninit(void);
215
216 /*@}*/
217
218 #ifdef __cplusplus
219 }
220 #endif
221
222 #endif /* _PURPLE_NETWORK_H_ */