Initial code commit for Toolkit 1.1.
[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 /****s* silcutil/SilcNetAPI/SilcNetServer
41  *
42  * NAME
43  *
44  *    typedef struct SilcNetServerStruct *SilcNetServer;
45  *
46  * DESCRIPTION
47  *
48  *    The network server (daemon, listener, etc.) context.  This context
49  *    is created with the silc_net_create_server function and destroyed
50  *    with silc_net_close_server function.
51  *
52  ***/
53 typedef struct SilcNetServerStruct *SilcNetServer;
54
55 /****d* silcutil/SilcNetAPI/SilcNetStatus
56  *
57  * NAME
58  *
59  *    typedef enum { ... } SilcNetStatus;
60  *
61  * DESCRIPTION
62  *
63  *    Status to indicate the result of the network operation creation.  This
64  *    type is returned in the SilcNetCallback callback function.
65  *
66  * SOURCE
67  */
68 typedef enum {
69   SILC_NET_OK,                         /* Everything Ok */
70   SILC_NET_UNKNOWN_IP,                 /* Unknown IP address */
71   SILC_NET_UNKNOWN_HOST,               /* Unknown hostname */
72   SILC_NET_HOST_UNREACHABLE,           /* Destination unreachable */
73   SILC_NET_CONNECTION_REFUSED,         /* Connection refused */
74   SILC_NET_CONNECTION_TIMEOUT,         /* Connection timedout */
75   SILC_NET_NO_MEMORY,                  /* System out of memory */
76   SILC_NET_ERROR,                      /* Unknown error */
77 } SilcNetStatus;
78 /***/
79
80 /****f* silcutil/SilcNetAPI/SilcNetCallback
81  *
82  * SYNOPSIS
83  *
84  *    typedef void (*SilcNetCallback)(SilcNetStatus status,
85  *                                    SilcStream stream, void *context);
86  *
87  * DESCRIPTION
88  *
89  *    A callback function of this type is returned by silc_net_create_server
90  *    and silc_net_connect_async functions.  For silc_net_create_server this
91  *    callback means that new incoming connection was accepted, and the
92  *    `stream' is the socket stream representing the socket connection.
93  *
94  *    For silc_net_connect_async this means that we have connected to the
95  *    remote host and the `stream' is the socket stream for the socket
96  *    connection.  The SILC Stream API (such as silc_stream_read, etc.)
97  *    can be used to read and write to the stream.  The created stream
98  *    is socket stream so various SilcSocketStream API functions can be
99  *    used with the `stream'.
100  *
101  ***/
102 typedef void (*SilcNetCallback)(SilcNetStatus status,
103                                 SilcStream stream, void *context);
104
105 /****f* silcutil/SilcNetAPI/silc_net_create_server
106  *
107  * SYNOPSIS
108  *
109  *    SilcNetServer
110  *    silc_net_create_server(const char **local_ip_addr,
111  *                           SilcUInt32 local_ip_count,
112  *                           int port, bool require_fqdn,
113  *                           SilcSchedule schedule,
114  *                           SilcNetCallback callback, void *context);
115  *
116  * DESCRIPTION
117  *
118  *    This function creates server or daemon or listener etc.  This is used
119  *    to create network listener for incoming connections, and `callback'
120  *    will be called everytime new connection is received.  If `local_ip_addr'
121  *    is NULL any address is used.  If provided it can be used bind the
122  *    server to `local_ip_count' many IP addresses provided in `local_ip_addr'
123  *    table.  On success returns the SilcNetServer context, or NULL on error.
124  *    If `require_fqdn' is TRUE the server will require that the incoming
125  *    connection has FQDN to be able to connect.
126  *
127  ***/
128 SilcNetServer
129 silc_net_create_server(const char **local_ip_addr, SilcUInt32 local_ip_count,
130                        int port, bool require_fqdn, SilcSchedule schedule,
131                        SilcNetCallback callback, void *context);
132
133 /****f* silcutil/SilcNetAPI/silc_net_close_server
134  *
135  * SYNOPSIS
136  *
137  *    void silc_net_close_server(SilcNetServer server);
138  *
139  * DESCRIPTION
140  *
141  *    Closes the network server listener indicated by `server'.
142  *
143  ***/
144 void silc_net_close_server(SilcNetServer server);
145
146 /****f* silcutil/SilcNetAPI/silc_net_connect
147  *
148  * SYNOPSIS
149  *
150  *    SilcAsyncOperation silc_net_tcp_connect(const char *local_ip_addr,
151  *                                            const char *remote_ip_addr,
152  *                                            int remote_port,
153  *                                            SilcSchedule schedule,
154  *                                            SilcNetCallback callback,
155  *                                            void *context);
156  *
157  * DESCRIPTION
158  *
159  *    Creates TCP/IP connection to the remote host indicated by `remote_host'
160  *    which may be hostname or IP address, on the port indicated by
161  *    `remote_port'.  If the `local_ip_addr' is provided the local host is
162  *    bound to that address before creating the connection.  This is
163  *    asynchronous call, and this function returns before the connection is
164  *    actually established.  The `callback' will be called after the
165  *    connection is created to deliver the SilcStream for the created
166  *    connection.
167  *
168  *    The returned SilcAsyncOperation context can be used to control the
169  *    asynchronous connecting, such as to abort it.  If it is aborted
170  *    using silc_async_abort the `callback' will not be called.  If NULL
171  *    is returned the operation cannot be aborted and the `callback' will
172  *    be called eventually.
173  *
174  */
175 SilcAsyncOperation silc_net_tcp_connect(const char *local_ip_addr,
176                                         const char *remote_ip_addr,
177                                         int remote_port,
178                                         SilcSchedule schedule,
179                                         SilcNetCallback callback,
180                                         void *context);
181
182 SilcAsyncOperation silc_net_udp_connect(const char *local_ip_addr,
183                                         const char *remote_ip_addr,
184                                         int remote_port,
185                                         SilcSchedule schedule,
186                                         SilcNetCallback callback,
187                                         void *context);
188
189 /****f* silcutil/SilcNetAPI/silc_net_close_connection
190  *
191  * SYNOPSIS
192  *
193  *    void silc_net_close_connection(int sock);
194  *
195  * DESCRIPTION
196  *
197  *    Closes the connection by closing the socket connection.
198  *
199  ***/
200 void silc_net_close_connection(int sock);
201
202 /****f* silcutil/SilcNetAPI/silc_net_accept_connection
203  *
204  * SYNOPSIS
205  *
206  *    int silc_net_accept_connection(int sock);
207  *
208  * DESCRIPTION
209  *
210  *    Accepts a connection from a particular socket.
211  *
212  ***/
213 int silc_net_accept_connection(int sock);
214
215 /****f* silcutil/SilcNetAPI/silc_net_set_socket_nonblock
216  *
217  * SYNOPSIS
218  *
219  *    int silc_net_set_socket_nonblock(int sock);
220  *
221  * DESCRIPTION
222  *
223  *    Sets the socket to non-blocking mode.
224  *
225  ***/
226 int silc_net_set_socket_nonblock(int sock);
227
228 /****f* silcutil/SilcNetAPI/silc_net_set_socket_opt
229  *
230  * SYNOPSIS
231  *
232  *    int silc_net_set_socket_opt(int sock, int level, int option, int on);
233  *
234  * DESCRIPTION
235  *
236  *    Sets a option for a socket.  This function can be used to set
237  *    various options for the socket.  Some of the options might be
238  *    system specific.
239  *
240  ***/
241 int silc_net_set_socket_opt(int sock, int level, int option, int on);
242
243 /****f* silcutil/SilcNetAPI/silc_net_get_socket_opt
244  *
245  * SYNOPSIS
246  *
247  *    int silc_net_get_socket_opt(int sock, int level, int option,
248  *                                void *optval, int *opt_len);
249  *
250  * DESCRIPTION
251  *
252  *    Return socket options to the `optval' and `opt_len'.
253  *
254  ***/
255 int silc_net_get_socket_opt(int sock, int level, int option,
256                             void *optval, int *opt_len);
257
258 /****f* silcutil/SilcNetAPI/silc_net_is_ip4
259  *
260  * SYNOPSIS
261  *
262  *    bool silc_net_is_ip4(const char *addr);
263  *
264  * DESCRIPTION
265  *
266  *    Checks whether IP address sent as argument is valid IPv4 address.
267  *
268  ***/
269 bool silc_net_is_ip4(const char *addr);
270
271 /****f* silcutil/SilcNetAPI/silc_net_is_ip6
272  *
273  * SYNOPSIS
274  *
275  *    bool silc_net_is_ip6(const char *addr);
276  *
277  * DESCRIPTION
278  *
279  *    Checks whether IP address sent as argument is valid IPv6 address.
280  *
281  ***/
282 bool silc_net_is_ip6(const char *addr);
283
284 /****f* silcutil/SilcNetAPI/silc_net_is_ip
285  *
286  * SYNOPSIS
287  *
288  *    bool silc_net_is_ip(const char *addr);
289  *
290  * DESCRIPTION
291  *
292  *    Checks whether IP address sent as argument is valid IP address.
293  *    This supports both IPv4 and IPv6 addresses.
294  *
295  ***/
296 bool silc_net_is_ip(const char *addr);
297
298 /****f* silcutil/SilcNetAPI/silc_net_addr2bin
299  *
300  * SYNOPSIS
301  *
302  *    bool silc_net_addr2bin(const char *addr, void *bin, SilcUInt32 bin_len);
303  *
304  * DESCRIPTION
305  *
306  *    Converts the IP number string from numbers-and-dots notation to
307  *    binary form in network byte order.  The address can be either
308  *    IPv4 or IPv6 address.
309  *
310  ***/
311 bool silc_net_addr2bin(const char *addr, void *bin, SilcUInt32 bin_len);
312
313 /****f* silcutil/SilcNetAPI/SilcNetResolveCallback
314  *
315  * SYNOPSIS
316  *
317  *    typedef void (*SilcNetResolveCallback)(const char *result,
318  *                                           void *context);
319  *
320  * DESCRIPTION
321  *
322  *    A callback function of this type is called after the asynchronous
323  *    resolving operation has been completed.  This callback is used
324  *    when asynchronously resolving IP addresses and hostnames.
325  *
326  ***/
327 typedef void (*SilcNetResolveCallback)(const char *result, void *context);
328
329 /****f* silcutil/SilcNetAPI/silc_net_gethostbyname
330  *
331  * SYNOPSIS
332  *
333  *    bool silc_net_gethostbyname(const char *name, bool prefer_ipv6,
334  *                                char *address, SilcUInt32 address_len);
335  *
336  * DESCRIPTION
337  *
338  *    Resolves the IP address of the hostname indicated by the `name'.
339  *    This returns TRUE and the IP address of the host to the `address'
340  *    buffer, or FALSE if the address could not be resolved.  This is
341  *    synchronous function and will block the calling process.  If the
342  *    `prefer_ipv6' is TRUE then this will return IPv6 address if it
343  *    finds.  If FALSE if returns IPv4 address even if it found IPv6
344  *    address also.
345  *
346  ***/
347 bool silc_net_gethostbyname(const char *name, bool prefer_ipv6, char *address,
348                             SilcUInt32 address_len);
349
350 /****f* silcutil/SilcNetAPI/silc_net_gethostbyname_async
351  *
352  * SYNOPSIS
353  *
354  *    void silc_net_gethostbyname_async(const char *name,
355  *                                      bool prefer_ipv6,
356  *                                      SilcSchedule schedule,
357  *                                      SilcNetResolveCallback completion,
358  *                                      void *context)
359  *
360  * DESCRIPTION
361  *
362  *    Asynchronously resolves the IP address of the hostname indicated
363  *    by the `name'.  This function returns immediately, and the
364  *    `completion' callback will be called after the resolving is
365  *    completed.
366  *
367  *    If the `prefer_ipv6' is TRUE then this will return IPv6 address if it
368  *    finds.  If FALSE if returns IPv4 address even if it found IPv6
369  *    address also.
370  *
371  ***/
372 void silc_net_gethostbyname_async(const char *name,
373                                   bool prefer_ipv6,
374                                   SilcSchedule schedule,
375                                   SilcNetResolveCallback completion,
376                                   void *context);
377
378 /****f* silcutil/SilcNetAPI/silc_net_gethostbyaddr
379  *
380  * SYNOPSIS
381  *
382  *   bool silc_net_gethostbyaddr(const char *addr, char *name,
383  *                               SilcUInt32 name_len);
384  *
385  * DESCRIPTION
386  *
387  *    Resolves the hostname for the IP address indicated by the `addr'
388  *    This returns TRUE and the resolved hostname to the `name' buffer,
389  *    or FALSE on error. The `addr' may be either IPv4 or IPv6 address.
390  *    This is synchronous function and will block the calling process.
391  *
392  ***/
393 bool silc_net_gethostbyaddr(const char *addr, char *name, SilcUInt32 name_len);
394
395 /****f* silcutil/SilcNetAPI/silc_net_gethostbyaddr_async
396  *
397  * SYNOPSIS
398  *
399  *    void silc_net_gethostbyaddr_async(const char *addr,
400  *                                      SilcSchedule schedule,
401  *                                      SilcNetResolveCallback completion,
402  *                                      void *context)
403  *
404  * DESCRIPTION
405  *
406  *    Asynchronously resolves the hostname for the IP address indicated
407  *    by the `addr'.  This function returns immediately, and the
408  *    `completion' callback will be called after the resolving is
409  *    completed.
410  *
411  ***/
412 void silc_net_gethostbyaddr_async(const char *addr,
413                                   SilcSchedule schedule,
414                                   SilcNetResolveCallback completion,
415                                   void *context);
416
417 /****f* silcutil/SilcNetAPI/silc_net_check_host_by_sock
418  *
419  * SYNOPSIS
420  *
421  *    bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip);
422  *
423  * DESCRIPTION
424  *
425  *    Performs lookups for remote name and IP address. This peforms reverse
426  *    lookup as well to verify that the IP has FQDN.
427  *
428  ***/
429 bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip);
430
431 /****f* silcutil/SilcNetAPI/silc_net_check_local_by_sock
432  *
433  * SYNOPSIS
434  *
435  *    bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip);
436  *
437  * DESCRIPTION
438  *
439  *    Performs lookups for local name and IP address. This peforms reverse
440  *    lookup as well to verify that the IP has FQDN.
441  *
442  ***/
443 bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip);
444
445 /****f* silcutil/SilcNetAPI/silc_net_get_remote_port
446  *
447  * SYNOPSIS
448  *
449  *    SilcUInt16 silc_net_get_remote_port(int sock);
450  *
451  * DESCRIPTION
452  *
453  *    Return remote port by socket.
454  *
455  ***/
456 SilcUInt16 silc_net_get_remote_port(int sock);
457
458 /****f* silcutil/SilcNetAPI/silc_net_get_local_port
459  *
460  * SYNOPSIS
461  *
462  *    SilcUInt16 silc_net_get_local_port(int sock);
463  *
464  * DESCRIPTION
465  *
466  *    Return local port by socket.
467  *
468  ***/
469 SilcUInt16 silc_net_get_local_port(int sock);
470
471 /****f* silcutil/SilcNetAPI/silc_net_localhost
472  *
473  * SYNOPSIS
474  *
475  *    char *silc_net_localhost(void);
476  *
477  * DESCRIPTION
478  *
479  *    Return name of localhost.  This will also attempt to resolve
480  *    the real hostname by the local host's IP address.  If unsuccessful
481  *    the first found hostname is returned.  The caller must free
482  *    returned hostname.
483  *
484  ***/
485 char *silc_net_localhost(void);
486
487 /****f* silcutil/SilcNetAPI/silc_net_localip
488  *
489  * SYNOPSIS
490  *
491  *    char *silc_net_localip(void)
492  *
493  * DESCRIPTION
494  *
495  *    Return IP of localhost.  The caller must free the returned IP.
496  *
497  ***/
498 char *silc_net_localip(void);
499
500 /****f* silcutil/SilcNetAPI/silc_net_win32_init
501  *
502  * SYNOPSIS
503  *
504  *    bool silc_net_win32_init(void);
505  *
506  * DESCRIPTION
507  *
508  *    This is WIN32 system specific function and is used to initialize
509  *    the network.  This must be called by all WIN32 applications.  It
510  *    is usually called at the application's main() or WinMain() before
511  *    calling any other SILC routine.  The application must also call
512  *    the silc_net_win32_uninit when exiting the application.  Returns
513  *    FALSE on error.  The network will not work if this function returns
514  *    FALSE.
515  *
516  * NOTES
517  *
518  *    This routines is available only on Win32 platform.
519  *
520  ***/
521 bool silc_net_win32_init(void);
522
523 /****f* silcutil/SilcNetAPI/silc_net_win32_uninit
524  *
525  * SYNOPSIS
526  *
527  *    void silc_net_win32_init(void);
528  *
529  * DESCRIPTION
530  *
531  *    This is WIN32 system specific function and is used to uninitialize
532  *    the network.  This must be called by all WIN32 applications.  It
533  *    is usually called when the application is exiting.  After calling
534  *    this function the SILC Net API routines will not work anymore.
535  *
536  * NOTES
537  *
538  *    This routines is available only on Win32 platform.
539  *
540  ***/
541 void silc_net_win32_uninit(void);
542
543 #include "silcnet_i.h"
544
545 #endif /* SILCNET_H */