Merged silc_1_0_branch to trunk.
[silc.git] / lib / silcutil / silcnet.h
1 /*
2
3   silcnet.h
4  
5   Author: Pekka Riikonen <priikone@silcnet.org>
6  
7   Copyright (C) 1997 - 2005 Pekka Riikonen
8  
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; version 2 of the License.
12  
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 /****h* silcutil/SILC Net Interface
21  *
22  * DESCRIPTION
23  *
24  * SILC Net API provides various network routines for applications. It
25  * can be used to create TCP/IP connections and servers. Various utility
26  * functions for resolving various information is also provided.
27  *
28  * On WIN32 systems the SILC Net API must initialized by calling the
29  * silc_net_win32_init and uninitialized when the application ends by
30  * calling the silc_net_win32_uninit function. The initializing must be
31  * done in order to assure that the SILC Net API works correctly.
32  *
33  ***/
34
35 #ifndef SILCNET_H
36 #define SILCNET_H
37
38 /* Prototypes */
39
40 /****f* silcutil/SilcNetAPI/silc_net_create_server
41  *
42  * SYNOPSIS
43  *
44  *    int silc_net_create_server(int port, char *ip_addr);
45  *
46  * DESCRIPTION
47  *
48  *    This function creates server or daemon or listener or what ever. This
49  *    does not fork a new process, it must be done by the caller if caller
50  *    wants to create a child process. This is used by the SILC server. 
51  *    If argument `ip_addr' is NULL `any' address will be used. Returns 
52  *    the created socket or -1 on error.
53  *
54  ***/
55 int silc_net_create_server(int port, const char *ip_addr);
56
57 /****f* silcutil/SilcNetAPI/silc_net_close_server
58  *
59  * SYNOPSIS
60  *
61  *    void silc_net_close_server(int sock);
62  *
63  * DESCRIPTION
64  *
65  *    Closes the server by closing the socket connection.
66  *
67  ***/
68 void silc_net_close_server(int sock);
69
70 /****f* silcutil/SilcNetAPI/silc_net_create_connection
71  *
72  * SYNOPSIS
73  *
74  *    int silc_net_create_connection(const char *local_ip, int port, 
75  *                                   const char *host);
76  *
77  * DESCRIPTION
78  *
79  *    Creates a connection (TCP/IP) to a remote host. Returns the connection
80  *    socket or -1 on error. This blocks the process while trying to create
81  *    the connection. If the `local_ip' is not NULL then this will bind
82  *    the `local_ip' address to a port before creating the connection.  If
83  *    it is NULL then this will directly create the connection.
84  *
85  ***/
86 int silc_net_create_connection(const char *localhost, int port, 
87                                const char *host);
88
89 /****f* silcutil/SilcNetAPI/silc_net_create_connection_async
90  *
91  * SYNOPSIS
92  *
93  *    int silc_net_create_connection_async(const char *local_ip, int port, 
94  *                                         const char *host);
95  *
96  * DESCRIPTION
97  *
98  *    Creates a connection (TCP/IP) to a remote host. Returns the connection
99  *    socket or -1 on error. This creates non-blocking socket hence the
100  *    connection returns directly. To get the result of the connect() one
101  *    must select() the socket and read the result after it's ready. If the
102  *    `local_ip' is not NULL then this will bind the `local_ip' address to
103  *    a port before creating the connection.  If it is NULL then this will
104  *    directly create the connection.
105  *
106  ***/
107 int silc_net_create_connection_async(const char *local_ip, int port, 
108                                      const char *host);
109
110 /****f* silcutil/SilcNetAPI/silc_net_close_connection
111  *
112  * SYNOPSIS
113  *
114  *    void silc_net_close_connection(int sock);
115  *
116  * DESCRIPTION
117  *
118  *    Closes the connection by closing the socket connection.
119  *
120  ***/
121 void silc_net_close_connection(int sock);
122
123 /****f* silcutil/SilcNetAPI/silc_net_accept_connection
124  *
125  * SYNOPSIS
126  *
127  *    int silc_net_accept_connection(int sock);
128  *
129  * DESCRIPTION
130  *
131  *    Accepts a connection from a particular socket.
132  *
133  ***/
134 int silc_net_accept_connection(int sock);
135
136 /****f* silcutil/SilcNetAPI/silc_net_set_socket_nonblock
137  *
138  * SYNOPSIS
139  *
140  *    int silc_net_set_socket_nonblock(int sock);
141  *
142  * DESCRIPTION
143  *
144  *    Sets the socket to non-blocking mode.
145  *
146  ***/
147 int silc_net_set_socket_nonblock(int sock);
148
149 /****f* silcutil/SilcNetAPI/silc_net_set_socket_opt
150  *
151  * SYNOPSIS
152  *
153  *    int silc_net_set_socket_opt(int sock, int level, int option, int on);
154  *
155  * DESCRIPTION
156  *
157  *    Sets a option for a socket.  This function can be used to set
158  *    various options for the socket.  Some of the options might be
159  *    system specific.
160  *
161  ***/
162 int silc_net_set_socket_opt(int sock, int level, int option, int on);
163
164 /****f* silcutil/SilcNetAPI/silc_net_get_socket_opt
165  *
166  * SYNOPSIS
167  *
168  *    int silc_net_get_socket_opt(int sock, int level, int option, 
169  *                                void *optval, int *opt_len);
170  *
171  * DESCRIPTION
172  *
173  *    Return socket options to the `optval' and `opt_len'.
174  *
175  ***/
176 int silc_net_get_socket_opt(int sock, int level, int option, 
177                             void *optval, int *opt_len);
178
179 /****f* silcutil/SilcNetAPI/silc_net_is_ip4
180  *
181  * SYNOPSIS
182  *
183  *    bool silc_net_is_ip4(const char *addr);
184  *
185  * DESCRIPTION
186  *
187  *    Checks whether IP address sent as argument is valid IPv4 address.
188  *
189  ***/
190 bool silc_net_is_ip4(const char *addr);
191
192 /****f* silcutil/SilcNetAPI/silc_net_is_ip6
193  *
194  * SYNOPSIS
195  *
196  *    bool silc_net_is_ip6(const char *addr);
197  *
198  * DESCRIPTION
199  *
200  *    Checks whether IP address sent as argument is valid IPv6 address.
201  *
202  ***/
203 bool silc_net_is_ip6(const char *addr);
204
205 /****f* silcutil/SilcNetAPI/silc_net_is_ip
206  *
207  * SYNOPSIS
208  *
209  *    bool silc_net_is_ip(const char *addr);
210  *
211  * DESCRIPTION
212  *
213  *    Checks whether IP address sent as argument is valid IP address.
214  *    This supports both IPv4 and IPv6 addresses.
215  *
216  ***/
217 bool silc_net_is_ip(const char *addr);
218
219 /****f* silcutil/SilcNetAPI/silc_net_addr2bin
220  *
221  * SYNOPSIS
222  *
223  *    bool silc_net_addr2bin(const char *addr, void *bin, SilcUInt32 bin_len);
224  *
225  * DESCRIPTION
226  *
227  *    Converts the IP number string from numbers-and-dots notation to
228  *    binary form in network byte order.  The address can be either
229  *    IPv4 or IPv6 address.
230  *
231  ***/
232 bool silc_net_addr2bin(const char *addr, void *bin, SilcUInt32 bin_len);
233
234 /****f* silcutil/SilcNetAPI/SilcNetResolveCallback
235  *
236  * SYNOPSIS
237  *
238  *    typedef void (*SilcNetResolveCallback)(const char *result, 
239  *                                           void *context);
240  *
241  * DESCRIPTION
242  *
243  *    A callback function of this type is called after the asynchronous
244  *    resolving operation has been completed.  This callback is used
245  *    when asynchronously resolving IP addresses and hostnames.
246  *
247  ***/
248 typedef void (*SilcNetResolveCallback)(const char *result, void *context);
249
250 /****f* silcutil/SilcNetAPI/silc_net_gethostbyname
251  *
252  * SYNOPSIS
253  *
254  *    bool silc_net_gethostbyname(const char *name, bool prefer_ipv6, 
255  *                                char *address, SilcUInt32 address_len);
256  *
257  * DESCRIPTION
258  *
259  *    Resolves the IP address of the hostname indicated by the `name'.
260  *    This returns TRUE and the IP address of the host to the `address'
261  *    buffer, or FALSE if the address could not be resolved.  This is
262  *    synchronous function and will block the calling process.  If the
263  *    `prefer_ipv6' is TRUE then this will return IPv6 address if it
264  *    finds.  If FALSE if returns IPv4 address even if it found IPv6
265  *    address also.
266  *
267  ***/
268 bool silc_net_gethostbyname(const char *name, bool prefer_ipv6, char *address, 
269                             SilcUInt32 address_len);
270
271 /****f* silcutil/SilcNetAPI/silc_net_gethostbyname_async
272  *
273  * SYNOPSIS
274  *
275  *    void silc_net_gethostbyname_async(const char *name, 
276  *                                      bool prefer_ipv6,
277  *                                      SilcSchedule schedule,
278  *                                      SilcNetResolveCallback completion,
279  *                                      void *context)
280  *
281  * DESCRIPTION
282  *
283  *    Asynchronously resolves the IP address of the hostname indicated
284  *    by the `name'.  This function returns immediately, and the
285  *    `completion' callback will be called after the resolving is
286  *    completed.
287  *
288  *    If the `prefer_ipv6' is TRUE then this will return IPv6 address if it
289  *    finds.  If FALSE if returns IPv4 address even if it found IPv6
290  *    address also.
291  *
292  ***/
293 void silc_net_gethostbyname_async(const char *name, 
294                                   bool prefer_ipv6,
295                                   SilcSchedule schedule,
296                                   SilcNetResolveCallback completion,
297                                   void *context);
298
299 /****f* silcutil/SilcNetAPI/silc_net_gethostbyaddr
300  *
301  * SYNOPSIS
302  *
303  *   bool silc_net_gethostbyaddr(const char *addr, char *name, 
304  *                               SilcUInt32 name_len);
305  *
306  * DESCRIPTION
307  *
308  *    Resolves the hostname for the IP address indicated by the `addr'
309  *    This returns TRUE and the resolved hostname to the `name' buffer, 
310  *    or FALSE on error. The `addr' may be either IPv4 or IPv6 address.
311  *    This is synchronous function and will block the calling process.
312  *
313  ***/
314 bool silc_net_gethostbyaddr(const char *addr, char *name, SilcUInt32 name_len);
315
316 /****f* silcutil/SilcNetAPI/silc_net_gethostbyaddr_async
317  *
318  * SYNOPSIS
319  *
320  *    void silc_net_gethostbyaddr_async(const char *addr, 
321  *                                      SilcSchedule schedule,
322  *                                      SilcNetResolveCallback completion,
323  *                                      void *context)
324  *
325  * DESCRIPTION
326  *
327  *    Asynchronously resolves the hostname for the IP address indicated
328  *    by the `addr'.  This function returns immediately, and the
329  *    `completion' callback will be called after the resolving is
330  *    completed.
331  *
332  ***/
333 void silc_net_gethostbyaddr_async(const char *addr, 
334                                   SilcSchedule schedule,
335                                   SilcNetResolveCallback completion,
336                                   void *context);
337
338 /****f* silcutil/SilcNetAPI/silc_net_check_host_by_sock
339  *
340  * SYNOPSIS
341  *
342  *    bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip);
343  *
344  * DESCRIPTION
345  *
346  *    Performs lookups for remote name and IP address. This peforms reverse
347  *    lookup as well to verify that the IP has FQDN.
348  *
349  ***/
350 bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip);
351
352 /****f* silcutil/SilcNetAPI/silc_net_check_local_by_sock
353  *
354  * SYNOPSIS
355  *
356  *    bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip);
357  *
358  * DESCRIPTION
359  *
360  *    Performs lookups for local name and IP address. This peforms reverse
361  *    lookup as well to verify that the IP has FQDN.
362  *
363  ***/
364 bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip);
365
366 /****f* silcutil/SilcNetAPI/silc_net_get_remote_port
367  *
368  * SYNOPSIS
369  *
370  *    SilcUInt16 silc_net_get_remote_port(int sock);
371  *
372  * DESCRIPTION
373  *
374  *    Return remote port by socket.
375  *
376  ***/
377 SilcUInt16 silc_net_get_remote_port(int sock);
378
379 /****f* silcutil/SilcNetAPI/silc_net_get_local_port
380  *
381  * SYNOPSIS
382  *
383  *    SilcUInt16 silc_net_get_local_port(int sock);
384  *
385  * DESCRIPTION
386  *
387  *    Return local port by socket.
388  *
389  ***/
390 SilcUInt16 silc_net_get_local_port(int sock);
391
392 /****f* silcutil/SilcNetAPI/silc_net_localhost
393  *
394  * SYNOPSIS
395  *
396  *    char *silc_net_localhost(void);
397  *
398  * DESCRIPTION
399  *
400  *    Return name of localhost.  This will also attempt to resolve
401  *    the real hostname by the local host's IP address.  If unsuccessful
402  *    the first found hostname is returned.  The caller must free
403  *    returned hostname.
404  *
405  ***/
406 char *silc_net_localhost(void);
407
408 /****f* silcutil/SilcNetAPI/silc_net_localip
409  *
410  * SYNOPSIS
411  *
412  *    char *silc_net_localip(void)
413  *
414  * DESCRIPTION
415  *
416  *    Return IP of localhost.  The caller must free the returned IP.
417  *
418  ***/
419 char *silc_net_localip(void);
420
421 /****f* silcutil/SilcNetAPI/silc_net_win32_init
422  *
423  * SYNOPSIS
424  *
425  *    bool silc_net_win32_init(void);
426  *
427  * DESCRIPTION
428  *
429  *    This is WIN32 system specific function and is used to initialize
430  *    the network.  This must be called by all WIN32 applications.  It
431  *    is usually called at the application's main() or WinMain() before
432  *    calling any other SILC routine.  The application must also call
433  *    the silc_net_win32_uninit when exiting the application.  Returns
434  *    FALSE on error.  The network will not work if this function returns
435  *    FALSE.
436  *
437  * NOTES
438  *
439  *    This routines is available only on Win32 platform.
440  *
441  ***/
442 bool silc_net_win32_init(void);
443
444 /****f* silcutil/SilcNetAPI/silc_net_win32_uninit
445  *
446  * SYNOPSIS
447  *
448  *    void silc_net_win32_init(void);
449  *
450  * DESCRIPTION
451  *
452  *    This is WIN32 system specific function and is used to uninitialize
453  *    the network.  This must be called by all WIN32 applications.  It
454  *    is usually called when the application is exiting.  After calling
455  *    this function the SILC Net API routines will not work anymore.
456  *
457  * NOTES
458  *
459  *    This routines is available only on Win32 platform.
460  *
461  ***/
462 void silc_net_win32_uninit(void);
463
464 #endif