5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2001 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 /****h* silcutil/SilcNetAPI
25 * SILC Net API provides various network routines for applications. It
26 * can be used to create TCP/IP connections and servers. Various utility
27 * functions for resolving various information is also provided.
29 * On WIN32 systems the SILC Net API must initialized by calling the
30 * silc_net_win32_init and uninitialized when the application ends by
31 * calling the silc_net_win32_uninit function. The initializing must be
32 * done in order to assure that the SILC Net API works correctly.
41 /****f* silcutil/SilcNetAPI/silc_net_create_server
45 * int silc_net_create_server(int port, char *ip_addr);
49 * This function creates server or daemon or listener or what ever. This
50 * does not fork a new process, it must be done by the caller if caller
51 * wants to create a child process. This is used by the SILC server.
52 * If argument `ip_addr' is NULL `any' address will be used. Returns
53 * the created socket or -1 on error.
56 int silc_net_create_server(int port, const char *ip_addr);
58 /****f* silcutil/SilcNetAPI/silc_net_close_server
62 * void silc_net_close_server(int sock);
66 * Closes the server by closing the socket connection.
69 void silc_net_close_server(int sock);
71 /****f* silcutil/SilcNetAPI/silc_net_create_connection
75 * int silc_net_create_connection(const char *local_ip, int port,
80 * Creates a connection (TCP/IP) to a remote host. Returns the connection
81 * socket or -1 on error. This blocks the process while trying to create
82 * the connection. If the `local_ip' is not NULL then this will bind
83 * the `local_ip' address to a port before creating the connection. If
84 * it is NULL then this will directly create the connection.
87 int silc_net_create_connection(const char *localhost, int port,
90 /****f* silcutil/SilcNetAPI/silc_net_create_connection_async
94 * int silc_net_create_connection_async(const char *local_ip, int port,
99 * Creates a connection (TCP/IP) to a remote host. Returns the connection
100 * socket or -1 on error. This creates non-blocking socket hence the
101 * connection returns directly. To get the result of the connect() one
102 * must select() the socket and read the result after it's ready. If the
103 * `local_ip' is not NULL then this will bind the `local_ip' address to
104 * a port before creating the connection. If it is NULL then this will
105 * directly create the connection.
108 int silc_net_create_connection_async(const char *local_ip, int port,
111 /****f* silcutil/SilcNetAPI/silc_net_close_connection
115 * void silc_net_close_connection(int sock);
119 * Closes the connection by closing the socket connection.
122 void silc_net_close_connection(int sock);
124 /****f* silcutil/SilcNetAPI/silc_net_accept_connection
128 * int silc_net_accept_connection(int sock);
132 * Accepts a connection from a particular socket.
135 int silc_net_accept_connection(int sock);
137 /****f* silcutil/SilcNetAPI/silc_net_set_socket_nonblock
141 * int silc_net_set_socket_nonblock(int sock);
145 * Sets the socket to non-blocking mode.
148 int silc_net_set_socket_nonblock(int sock);
150 /****f* silcutil/SilcNetAPI/silc_net_set_socket_opt
154 * int silc_net_set_socket_opt(int sock, int level, int option, int on);
158 * Sets a option for a socket. This function can be used to set
159 * various options for the socket. Some of the options might be
163 int silc_net_set_socket_opt(int sock, int level, int option, int on);
165 /****f* silcutil/SilcNetAPI/silc_net_get_socket_opt
169 * int silc_net_get_socket_opt(int sock, int level, int option,
170 * void *optval, int *opt_len);
174 * Return socket options to the `optval' and `opt_len'.
177 int silc_net_get_socket_opt(int sock, int level, int option,
178 void *optval, int *opt_len);
180 /****f* silcutil/SilcNetAPI/silc_net_is_ip4
184 * bool silc_net_is_ip4(const char *addr);
188 * Checks whether IP address sent as argument is valid IPv4 address.
191 bool silc_net_is_ip4(const char *addr);
193 /****f* silcutil/SilcNetAPI/silc_net_is_ip6
197 * bool silc_net_is_ip6(const char *addr);
201 * Checks whether IP address sent as argument is valid IPv6 address.
204 bool silc_net_is_ip6(const char *addr);
206 /****f* silcutil/SilcNetAPI/silc_net_is_ip
210 * bool silc_net_is_ip(const char *addr);
214 * Checks whether IP address sent as argument is valid IP address.
215 * This supports both IPv4 and IPv6 addresses.
218 bool silc_net_is_ip(const char *addr);
220 /****f* silcutil/SilcNetAPI/silc_net_addr2bin
224 * bool silc_net_addr2bin(const char *addr, void *bin, SilcUInt32 bin_len);
228 * Converts the IP number string from numbers-and-dots notation to
229 * binary form in network byte order. The address can be either
230 * IPv4 or IPv6 address.
233 bool silc_net_addr2bin(const char *addr, void *bin, SilcUInt32 bin_len);
235 /****f* silcutil/SilcNetAPI/SilcNetResolveCallback
239 * typedef void (*SilcNetResolveCallback)(const char *result,
244 * A callback function of this type is called after the asynchronous
245 * resolving operation has been completed. This callback is used
246 * when asynchronously resolving IP addresses and hostnames.
249 typedef void (*SilcNetResolveCallback)(const char *result, void *context);
251 /****f* silcutil/SilcNetAPI/silc_net_gethostbyname
255 * bool silc_net_gethostbyname(const char *name, bool prefer_ipv6,
256 * char *address, SilcUInt32 address_len);
260 * Resolves the IP address of the hostname indicated by the `name'.
261 * This returns TRUE and the IP address of the host to the `address'
262 * buffer, or FALSE if the address could not be resolved. This is
263 * synchronous function and will block the calling process. If the
264 * `prefer_ipv6' is TRUE then this will return IPv6 address if it
265 * finds. If FALSE if returns IPv4 address even if it found IPv6
269 bool silc_net_gethostbyname(const char *name, bool prefer_ipv6, char *address,
270 SilcUInt32 address_len);
272 /****f* silcutil/SilcNetAPI/silc_net_gethostbyname_async
276 * void silc_net_gethostbyname_async(const char *name,
278 * SilcSchedule schedule,
279 * SilcNetResolveCallback completion,
284 * Asynchronously resolves the IP address of the hostname indicated
285 * by the `name'. This function returns immediately, and the
286 * `completion' callback will be called after the resolving is
289 * If the `prefer_ipv6' is TRUE then this will return IPv6 address if it
290 * finds. If FALSE if returns IPv4 address even if it found IPv6
294 void silc_net_gethostbyname_async(const char *name,
296 SilcSchedule schedule,
297 SilcNetResolveCallback completion,
300 /****f* silcutil/SilcNetAPI/silc_net_gethostbyaddr
304 * bool silc_net_gethostbyaddr(const char *addr, char *name,
305 * SilcUInt32 name_len);
309 * Resolves the hostname for the IP address indicated by the `addr'
310 * This returns TRUE and the resolved hostname to the `name' buffer,
311 * or FALSE on error. The `addr' may be either IPv4 or IPv6 address.
312 * This is synchronous function and will block the calling process.
315 bool silc_net_gethostbyaddr(const char *addr, char *name, SilcUInt32 name_len);
317 /****f* silcutil/SilcNetAPI/silc_net_gethostbyaddr_async
321 * void silc_net_gethostbyaddr_async(const char *addr,
322 * SilcSchedule schedule,
323 * SilcNetResolveCallback completion,
328 * Asynchronously resolves the hostname for the IP address indicated
329 * by the `addr'. This function returns immediately, and the
330 * `completion' callback will be called after the resolving is
334 void silc_net_gethostbyaddr_async(const char *addr,
335 SilcSchedule schedule,
336 SilcNetResolveCallback completion,
339 /****f* silcutil/SilcNetAPI/silc_net_check_host_by_sock
343 * bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip);
347 * Performs lookups for remote name and IP address. This peforms reverse
348 * lookup as well to verify that the IP has FQDN.
351 bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip);
353 /****f* silcutil/SilcNetAPI/silc_net_check_local_by_sock
357 * bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip);
361 * Performs lookups for local name and IP address. This peforms reverse
362 * lookup as well to verify that the IP has FQDN.
365 bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip);
367 /****f* silcutil/SilcNetAPI/silc_net_get_remote_port
371 * SilcUInt16 silc_net_get_remote_port(int sock);
375 * Return remote port by socket.
378 SilcUInt16 silc_net_get_remote_port(int sock);
380 /****f* silcutil/SilcNetAPI/silc_net_get_local_port
384 * SilcUInt16 silc_net_get_local_port(int sock);
388 * Return local port by socket.
391 SilcUInt16 silc_net_get_local_port(int sock);
393 /****f* silcutil/SilcNetAPI/silc_net_localhost
397 * char *silc_net_localhost(void);
401 * Return name of localhost. This will also attempt to resolve
402 * the real hostname by the local host's IP address. If unsuccessful
403 * the first found hostname is returned.
406 char *silc_net_localhost(void);
408 /****f* silcutil/SilcNetAPI/silc_net_localip
412 * char *silc_net_localip(void)
416 * Return IP of localhost.
419 char *silc_net_localip(void);
423 /****f* silcutil/SilcNetAPI/silc_net_win32_init
427 * bool silc_net_win32_init(void);
431 * This is WIN32 system specific function and is used to initialize
432 * the network. This must be called by all WIN32 applications. It
433 * is usually called at the application's main() or WinMain() before
434 * calling any other SILC routine. The application must also call
435 * the silc_net_win32_uninit when exiting the application. Returns
436 * FALSE on error. The network will not work if this function returns
440 bool silc_net_win32_init(void);
442 /****f* silcutil/SilcNetAPI/silc_net_win32_uninit
446 * void silc_net_win32_init(void);
450 * This is WIN32 system specific function and is used to uninitialize
451 * the network. This must be called by all WIN32 applications. It
452 * is usually called when the application is exiting. After calling
453 * this function the SILC Net API routines will not work anymore.
456 void silc_net_win32_uninit(void);