Frameworks/libpurple.framework/Versions/0.6.2/Headers/network.h
author Zachary West <zacw@adium.im>
Fri Aug 21 13:25:11 2009 -0700 (2009-08-21)
changeset 2592 e8d15275025e
parent 2571 Frameworks/libpurple.framework/Versions/0.6.0/Headers/network.h@75fb8ee8f2e6
permissions -rw-r--r--
im.pidgin.adium.1-4 at 267c6165e02e34318a1823960bd04c0639952f73
     1 /**
     2  * @file network.h Network API
     3  * @ingroup core
     4  */
     5 
     6 /* purple
     7  *
     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.
    11  *
    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.
    16  *
    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.
    21  *
    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
    25  */
    26 #ifndef _PURPLE_NETWORK_H_
    27 #define _PURPLE_NETWORK_H_
    28 
    29 #ifdef __cplusplus
    30 extern "C" {
    31 #endif
    32 
    33 /**************************************************************************/
    34 /** @name Network API                                                     */
    35 /**************************************************************************/
    36 /*@{*/
    37 
    38 typedef struct _PurpleNetworkListenData PurpleNetworkListenData;
    39 
    40 typedef void (*PurpleNetworkListenCallback) (int listenfd, gpointer data);
    41 
    42 /**
    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.
    46  *
    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
    52  *         freed.
    53  */
    54 const unsigned char *purple_network_ip_atoi(const char *ip);
    55 
    56 /**
    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.
    61  *
    62  * @param ip The local IP address.
    63  */
    64 void purple_network_set_public_ip(const char *ip);
    65 
    66 /**
    67  * Returns the IP address of the local system set in preferences.
    68  *
    69  * This returns the value set via purple_network_set_public_ip().
    70  * You probably want to use purple_network_get_my_ip() instead.
    71  *
    72  * @return The local IP address set in preferences.
    73  */
    74 const char *purple_network_get_public_ip(void);
    75 
    76 /**
    77  * Returns the IP address of the local system.
    78  *
    79  * You probably want to use purple_network_get_my_ip() instead.
    80  *
    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.
    84  *
    85  * @param fd The fd to use to help figure out the IP, or else -1.
    86  * @return The local IP address.
    87  */
    88 const char *purple_network_get_local_system_ip(int fd);
    89 
    90 /**
    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).
    94  *
    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()
    98  * is returned.
    99  *
   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.
   103  *
   104  * @param fd The fd to use to help figure out the IP, or -1.
   105  * @return The local IP address to be used.
   106  */
   107 const char *purple_network_get_my_ip(int fd);
   108 
   109 /**
   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
   113  *
   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.
   117  * @since 2.3.0
   118  */
   119 void purple_network_listen_map_external(gboolean map_external);
   120 
   121 /**
   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.
   127  *
   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
   132  * returned.
   133  *
   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
   141  *
   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.
   145  */
   146 PurpleNetworkListenData *purple_network_listen(unsigned short port,
   147 		int socket_type, PurpleNetworkListenCallback cb, gpointer cb_data);
   148 
   149 /**
   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
   154  * values are used.
   155  * Otherwise a port is chosen at random by the operating system.
   156  *
   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
   161  * returned.
   162  *
   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
   167  *            arg in prefs.
   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
   174  *
   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.
   178  */
   179 PurpleNetworkListenData *purple_network_listen_range(unsigned short start,
   180 		unsigned short end, int socket_type,
   181 		PurpleNetworkListenCallback cb, gpointer cb_data);
   182 
   183 /**
   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().
   187  *
   188  * @param listen_data This listener attempt will be canceled and
   189  *        the struct will be freed.
   190  */
   191 void purple_network_listen_cancel(PurpleNetworkListenData *listen_data);
   192 
   193 /**
   194  * Gets a port number from a file descriptor.
   195  *
   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.
   200  */
   201 unsigned short purple_network_get_port_from_fd(int fd);
   202 
   203 /**
   204  * Detects if there is an available network connection.
   205  *
   206  * @return TRUE if the network is available
   207  */
   208 gboolean purple_network_is_available(void);
   209 
   210 /**
   211  * Makes purple_network_is_available() always return @c TRUE.
   212  *
   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.
   216  *
   217  * @since 2.6.0
   218  */
   219 void purple_network_force_online(void);
   220 
   221 /**
   222  * Get the handle for the network system
   223  *
   224  * @return the handle to the network system
   225  */
   226 void *purple_network_get_handle(void);
   227 
   228 /**	
   229  * Update the STUN server IP given the host name
   230  * Will result in a DNS query being executed asynchronous
   231  * 
   232  * @param stun_server The host name of the STUN server to set
   233  * @since 2.6.0
   234  */
   235 void purple_network_set_stun_server(const gchar *stun_server);
   236 	
   237 /**
   238  * Get the IP address of the STUN server as a string representation
   239  *
   240  * @return the IP address
   241  * @since 2.6.0
   242  */
   243 const gchar *purple_network_get_stun_ip(void);
   244 	
   245 /**	
   246  * Update the TURN server IP given the host name
   247  * Will result in a DNS query being executed asynchronous
   248  * 
   249  * @param turn_server The host name of the TURN server to set
   250  * @since 2.6.0
   251  */
   252 void purple_network_set_turn_server(const gchar *turn_server);
   253 	
   254 /**
   255  * Get the IP address of the STUN server as a string representation
   256  *
   257  * @return the IP address
   258  * @since 2.6.0
   259  */
   260 const gchar *purple_network_get_turn_ip(void);
   261 		
   262 /**
   263  * Remove a port mapping (UPnP or NAT-PMP) associated with listening socket
   264  *
   265  * @param fd Socket to remove the port mapping for
   266  * @since 2.6.0
   267  */
   268 void purple_network_remove_port_mapping(gint fd);	
   269 
   270 /**
   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.
   274  *
   275  * Because this function is used by DNS resolver child/threads, it uses no
   276  * other libpurple API and is threadsafe.
   277  *
   278  * In general, a buffer of about 512 bytes is the appropriate size to use.
   279  *
   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.
   285  * @since 2.6.0
   286  */
   287 int purple_network_convert_idn_to_ascii(const gchar *in, gchar **out);
   288 
   289 /**
   290  * Initializes the network subsystem.
   291  */
   292 void purple_network_init(void);
   293 
   294 /**
   295  * Shuts down the network subsystem.
   296  */
   297 void purple_network_uninit(void);
   298 
   299 /*@}*/
   300 
   301 #ifdef __cplusplus
   302 }
   303 #endif
   304 
   305 #endif /* _PURPLE_NETWORK_H_ */