Configurable lookup for network listener creation.
[silc.git] / lib / silcutil / silcnet.h
1 /*
2
3   silcnet.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2006 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 and UDP/IP connections and listeners.
26  * Various utility functions for resolving various information is also
27  * provided.
28  *
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.
33  *
34  ***/
35
36 #ifndef SILCNET_H
37 #define SILCNET_H
38
39 /* Prototypes */
40
41 /****s* silcutil/SilcNetAPI/SilcNetListener
42  *
43  * NAME
44  *
45  *    typedef struct SilcNetListenerStruct *SilcNetListener;
46  *
47  * DESCRIPTION
48  *
49  *    The network listenr context.  This context is created with the
50  *    silc_net_create_listener function and destroyed with
51  *    silc_net_close_listener function.
52  *
53  ***/
54 typedef struct SilcNetListenerStruct *SilcNetListener;
55
56 /****d* silcutil/SilcNetAPI/SilcNetStatus
57  *
58  * NAME
59  *
60  *    typedef enum { ... } SilcNetStatus;
61  *
62  * DESCRIPTION
63  *
64  *    Status to indicate the result of the network operation creation.  This
65  *    type is returned in the SilcNetCallback callback function.
66  *
67  * SOURCE
68  */
69 typedef enum {
70   SILC_NET_OK,                         /* Everything Ok */
71   SILC_NET_UNKNOWN_IP,                 /* Unknown IP address */
72   SILC_NET_UNKNOWN_HOST,               /* Unknown hostname */
73   SILC_NET_HOST_UNREACHABLE,           /* Destination unreachable */
74   SILC_NET_CONNECTION_REFUSED,         /* Connection refused */
75   SILC_NET_CONNECTION_TIMEOUT,         /* Connection timedout */
76   SILC_NET_NO_MEMORY,                  /* System out of memory */
77   SILC_NET_ERROR,                      /* Unknown error */
78 } SilcNetStatus;
79 /***/
80
81 /****f* silcutil/SilcNetAPI/SilcNetCallback
82  *
83  * SYNOPSIS
84  *
85  *    typedef void (*SilcNetCallback)(SilcNetStatus status,
86  *                                    SilcStream stream, void *context);
87  *
88  * DESCRIPTION
89  *
90  *    A callback of this type is returned by silc_net_tcp_create_listener
91  *    and silc_net_tcp_connect functions.  For silc_net_tcp_create_listener
92  *    this callback means that new incoming connection was accepted, and the
93  *    `stream' is the socket stream representing the socket connection.
94  *
95  *    For silc_net_tcp_connect this means that we have connected to the
96  *    remote host and the `stream' is the socket stream for the socket
97  *    connection.  The SILC Stream API (such as silc_stream_read, etc.) can
98  *    be used to read and write to the stream.  The created stream is socket
99  *    stream so various SilcSocketStream API functions can be used with
100  *    the `stream'.
101  *
102  ***/
103 typedef void (*SilcNetCallback)(SilcNetStatus status,
104                                 SilcStream stream, void *context);
105
106 /****f* silcutil/SilcNetAPI/silc_net_tcp_create_listener
107  *
108  * SYNOPSIS
109  *
110  *    SilcNetListener
111  *    silc_net_tcp_create_listener(const char **local_ip_addr,
112  *                                 SilcUInt32 local_ip_count, int port,
113  *                                 SilcBool lookup, SilcBool require_fqdn,
114  *                                 SilcSchedule schedule,
115  *                                 SilcNetCallback callback, void *context);
116  *
117  * DESCRIPTION
118  *
119  *    This function creates TCP listener.  This is used to create network
120  *    listener for incoming connections, and `callback' will be called
121  *    everytime new connection is received.  If `local_ip_addr' is NULL any
122  *    address is used.  If provided it can be used bind the listener to
123  *    `local_ip_count' many IP addresses provided in `local_ip_addr' table.
124  *    On success returns the SilcNetListener context, or NULL on error.
125  *    If `require_fqdn' is TRUE the listener will require that the incoming
126  *    connection has FQDN to be able to connect.  If the `lookup' is TRUE
127  *    then the incoming connection hostname will be resolved.
128  *
129  ***/
130 SilcNetListener
131 silc_net_tcp_create_listener(const char **local_ip_addr,
132                              SilcUInt32 local_ip_count, int port,
133                              SilcBool lookup, SilcBool require_fqdn,
134                              SilcSchedule schedule,
135                              SilcNetCallback callback, void *context);
136
137 /****f* silcutil/SilcNetAPI/silc_net_close_listener
138  *
139  * SYNOPSIS
140  *
141  *    void silc_net_close_listener(SilcNetListener listener);
142  *
143  * DESCRIPTION
144  *
145  *    Closes the network listener indicated by `listener'.
146  *
147  ***/
148 void silc_net_close_listener(SilcNetListener listener);
149
150 /****f* silcutil/SilcNetAPI/silc_net_tcp_connect
151  *
152  * SYNOPSIS
153  *
154  *    SilcAsyncOperation silc_net_tcp_connect(const char *local_ip_addr,
155  *                                            const char *remote_ip_addr,
156  *                                            int remote_port,
157  *                                            SilcSchedule schedule,
158  *                                            SilcNetCallback callback,
159  *                                            void *context);
160  *
161  * DESCRIPTION
162  *
163  *    Creates TCP/IP connection to the remote host indicated by `remote_host'
164  *    which may be hostname or IP address, on the port indicated by
165  *    `remote_port'.  If the `local_ip_addr' is provided the local host is
166  *    bound to that address before creating the connection.  This is
167  *    asynchronous call, and this function returns before the connection is
168  *    actually established.  The `callback' will be called after the
169  *    connection is created to deliver the SilcStream for the created
170  *    connection.
171  *
172  *    The returned SilcAsyncOperation context can be used to control the
173  *    asynchronous connecting, such as to abort it.  If it is aborted
174  *    using silc_async_abort the `callback' will not be called.  If NULL
175  *    is returned the operation cannot be aborted.
176  *
177  */
178 SilcAsyncOperation silc_net_tcp_connect(const char *local_ip_addr,
179                                         const char *remote_ip_addr,
180                                         int remote_port,
181                                         SilcSchedule schedule,
182                                         SilcNetCallback callback,
183                                         void *context);
184
185 /****f* silcutil/SilcNetAPI/silc_net_udp_connect
186  *
187  * SYNOPSIS
188  *
189  *    SilcStream
190  *    silc_net_udp_connect(const char *local_ip_addr, int local_port,
191  *                         const char *remote_ip_addr, int remote_port,
192  *                         SilcSchedule schedule);
193  *
194  * DESCRIPTION
195  *
196  *    This function creates UDP stream.  The UDP stream is bound to the
197  *    `local_ip_addr' if it is specified.  If `local_port' is non-zero the
198  *    stream is bound to that port.  If the `remote_ip_addr' and `remote_port'
199  *    is also provided, packets may be sent to that address using
200  *    silc_stream_write function and packets may be received using
201  *    silc_stream_read function.
202  *
203  *    If the remote address is not provided then packets may only be received
204  *    by using silc_net_udp_receive and sent only by using the function
205  *    silc_net_udp_send.
206  *
207  *    To receive packets the silc_stream_set_notifier must be called for the
208  *    returned SilcStream.  The packets are always received in the notifier
209  *    callback when the SILC_STREAM_CAN_READ is returned to the callback
210  *    To read the packet use silc_stream_read if the remote address was
211  *    provided, and silc_net_udp_receive if it was not.
212  *
213  * EXAMPLE
214  *
215  *    SilcStream udpstream;
216  *
217  *    // Create UDP stream and prepare to receive packets
218  *    udpstream = silc_net_udp_connect("10.2.1.7", 5000,
219  *                                     "10.2.1.100, 5000, schedule);
220  *    silc_stream_set_notifier(udpstream, schedule, receive_callback, context);
221  *
222  *    // Send packet to remote host
223  *    silc_stream_write(udpstream, data, data_len);
224  *
225  ***/
226 SilcStream silc_net_udp_connect(const char *local_ip_addr, int local_port,
227                                 const char *remote_ip_addr, int remote_port,
228                                 SilcSchedule schedule);
229
230 /****f* silcutil/SilcNetAPI/silc_net_udp_receive
231  *
232  * SYNOPSIS
233  *
234  *    int
235  *    silc_net_udp_receive(SilcStream stream, char *remote_ip_addr,
236  *                         SilcUInt32 remote_ip_addr_size, int *remote_port,
237  *                         unsigned char *ret_data, SilcUInt32 data_size)
238  *
239  * DESCRIPTION
240  *
241  *    Receive a UDP packet from the `stream'.  The IP address and port of
242  *    the sender is returned into `remote_ip_addr' buffer and `remote_port'
243  *    pointer.  The packet data is returned into the `ret_data' buffer.
244  *
245  *    Returns the length of the packet, or -1 on error or 0 in case of EOF.
246  *
247  ***/
248 int silc_net_udp_receive(SilcStream stream, char *remote_ip_addr,
249                          SilcUInt32 remote_ip_addr_size, int *remote_port,
250                          unsigned char *ret_data, SilcUInt32 data_size);
251
252 /****f* silcutil/SilcNetAPI/silc_net_udp_send
253  *
254  * SYNOPSIS
255  *
256  *    void silc_net_udp_send(SilcStream stream,
257  *                           const char *remote_ip_addr, int remote_port,
258  *                           const unsigned char *data, SilcUInt32 data_len);
259  *
260  * DESCRIPTION
261  *
262  *    Sends an UDP packet to remote host `remote_ip_addr' on `remote_port'.
263  *    This may be used with UDP streams that are not connected to any
264  *    specific remote host.  With those stream silc_stream_write cannot be
265  *    used.  In those cases, this function must be used.  This may also be
266  *    used even if the stream is connected.
267  *
268  *    This function always succeeds, however there is no guarantee that the
269  *    packet is delivered, as UDP is unreliable transport protocol.
270  *
271  ***/
272 void silc_net_udp_send(SilcStream stream,
273                        const char *remote_ip_addr, int remote_port,
274                        const unsigned char *data, SilcUInt32 data_len);
275
276 /****f* silcutil/SilcNetAPI/silc_net_close_connection
277  *
278  * SYNOPSIS
279  *
280  *    void silc_net_close_connection(int sock);
281  *
282  * DESCRIPTION
283  *
284  *    Closes the connection by closing the socket connection.
285  *
286  ***/
287 void silc_net_close_connection(int sock);
288
289 /****f* silcutil/SilcNetAPI/silc_net_accept_connection
290  *
291  * SYNOPSIS
292  *
293  *    int silc_net_accept_connection(int sock);
294  *
295  * DESCRIPTION
296  *
297  *    Accepts a connection from a particular socket.
298  *
299  ***/
300 int silc_net_accept_connection(int sock);
301
302 /****f* silcutil/SilcNetAPI/silc_net_set_socket_nonblock
303  *
304  * SYNOPSIS
305  *
306  *    int silc_net_set_socket_nonblock(int sock);
307  *
308  * DESCRIPTION
309  *
310  *    Sets the socket to non-blocking mode.
311  *
312  ***/
313 int silc_net_set_socket_nonblock(int sock);
314
315 /****f* silcutil/SilcNetAPI/silc_net_set_socket_opt
316  *
317  * SYNOPSIS
318  *
319  *    int silc_net_set_socket_opt(int sock, int level, int option, int on);
320  *
321  * DESCRIPTION
322  *
323  *    Sets a option for a socket.  This function can be used to set
324  *    various options for the socket.  Some of the options might be
325  *    system specific.
326  *
327  ***/
328 int silc_net_set_socket_opt(int sock, int level, int option, int on);
329
330 /****f* silcutil/SilcNetAPI/silc_net_get_socket_opt
331  *
332  * SYNOPSIS
333  *
334  *    int silc_net_get_socket_opt(int sock, int level, int option,
335  *                                void *optval, int *opt_len);
336  *
337  * DESCRIPTION
338  *
339  *    Return socket options to the `optval' and `opt_len'.
340  *
341  ***/
342 int silc_net_get_socket_opt(int sock, int level, int option,
343                             void *optval, int *opt_len);
344
345 /****f* silcutil/SilcNetAPI/silc_net_is_ip4
346  *
347  * SYNOPSIS
348  *
349  *    SilcBool silc_net_is_ip4(const char *addr);
350  *
351  * DESCRIPTION
352  *
353  *    Checks whether IP address sent as argument is valid IPv4 address.
354  *
355  ***/
356 SilcBool silc_net_is_ip4(const char *addr);
357
358 /****f* silcutil/SilcNetAPI/silc_net_is_ip6
359  *
360  * SYNOPSIS
361  *
362  *    SilcBool silc_net_is_ip6(const char *addr);
363  *
364  * DESCRIPTION
365  *
366  *    Checks whether IP address sent as argument is valid IPv6 address.
367  *
368  ***/
369 SilcBool silc_net_is_ip6(const char *addr);
370
371 /****f* silcutil/SilcNetAPI/silc_net_is_ip
372  *
373  * SYNOPSIS
374  *
375  *    SilcBool silc_net_is_ip(const char *addr);
376  *
377  * DESCRIPTION
378  *
379  *    Checks whether IP address sent as argument is valid IP address.
380  *    This supports both IPv4 and IPv6 addresses.
381  *
382  ***/
383 SilcBool silc_net_is_ip(const char *addr);
384
385 /****f* silcutil/SilcNetAPI/silc_net_addr2bin
386  *
387  * SYNOPSIS
388  *
389  *    SilcBool silc_net_addr2bin(const char *addr, void *bin, SilcUInt32 bin_len);
390  *
391  * DESCRIPTION
392  *
393  *    Converts the IP number string from numbers-and-dots notation to
394  *    binary form in network byte order.  The address can be either
395  *    IPv4 or IPv6 address.
396  *
397  ***/
398 SilcBool silc_net_addr2bin(const char *addr, void *bin, SilcUInt32 bin_len);
399
400 /****f* silcutil/SilcNetAPI/SilcNetResolveCallback
401  *
402  * SYNOPSIS
403  *
404  *    typedef void (*SilcNetResolveCallback)(const char *result,
405  *                                           void *context);
406  *
407  * DESCRIPTION
408  *
409  *    A callback function of this type is called after the asynchronous
410  *    resolving operation has been completed.  This callback is used
411  *    when asynchronously resolving IP addresses and hostnames.
412  *
413  ***/
414 typedef void (*SilcNetResolveCallback)(const char *result, void *context);
415
416 /****f* silcutil/SilcNetAPI/silc_net_gethostbyname
417  *
418  * SYNOPSIS
419  *
420  *    SilcBool silc_net_gethostbyname(const char *name, SilcBool prefer_ipv6,
421  *                                char *address, SilcUInt32 address_len);
422  *
423  * DESCRIPTION
424  *
425  *    Resolves the IP address of the hostname indicated by the `name'.
426  *    This returns TRUE and the IP address of the host to the `address'
427  *    buffer, or FALSE if the address could not be resolved.  This is
428  *    synchronous function and will block the calling process.  If the
429  *    `prefer_ipv6' is TRUE then this will return IPv6 address if it
430  *    finds.  If FALSE if returns IPv4 address even if it found IPv6
431  *    address also.
432  *
433  ***/
434 SilcBool silc_net_gethostbyname(const char *name, SilcBool prefer_ipv6,
435                                 char *address, SilcUInt32 address_len);
436
437 /****f* silcutil/SilcNetAPI/silc_net_gethostbyname_async
438  *
439  * SYNOPSIS
440  *
441  *    void silc_net_gethostbyname_async(const char *name,
442  *                                      SilcBool prefer_ipv6,
443  *                                      SilcSchedule schedule,
444  *                                      SilcNetResolveCallback completion,
445  *                                      void *context)
446  *
447  * DESCRIPTION
448  *
449  *    Asynchronously resolves the IP address of the hostname indicated
450  *    by the `name'.  This function returns immediately, and the
451  *    `completion' callback will be called after the resolving is
452  *    completed.
453  *
454  *    If the `prefer_ipv6' is TRUE then this will return IPv6 address if it
455  *    finds.  If FALSE if returns IPv4 address even if it found IPv6
456  *    address also.
457  *
458  ***/
459 void silc_net_gethostbyname_async(const char *name,
460                                   SilcBool prefer_ipv6,
461                                   SilcSchedule schedule,
462                                   SilcNetResolveCallback completion,
463                                   void *context);
464
465 /****f* silcutil/SilcNetAPI/silc_net_gethostbyaddr
466  *
467  * SYNOPSIS
468  *
469  *   SilcBool silc_net_gethostbyaddr(const char *addr, char *name,
470  *                               SilcUInt32 name_len);
471  *
472  * DESCRIPTION
473  *
474  *    Resolves the hostname for the IP address indicated by the `addr'
475  *    This returns TRUE and the resolved hostname to the `name' buffer,
476  *    or FALSE on error. The `addr' may be either IPv4 or IPv6 address.
477  *    This is synchronous function and will block the calling process.
478  *
479  ***/
480 SilcBool silc_net_gethostbyaddr(const char *addr, char *name,
481                                 SilcUInt32 name_len);
482
483 /****f* silcutil/SilcNetAPI/silc_net_gethostbyaddr_async
484  *
485  * SYNOPSIS
486  *
487  *    void silc_net_gethostbyaddr_async(const char *addr,
488  *                                      SilcSchedule schedule,
489  *                                      SilcNetResolveCallback completion,
490  *                                      void *context)
491  *
492  * DESCRIPTION
493  *
494  *    Asynchronously resolves the hostname for the IP address indicated
495  *    by the `addr'.  This function returns immediately, and the
496  *    `completion' callback will be called after the resolving is
497  *    completed.
498  *
499  ***/
500 void silc_net_gethostbyaddr_async(const char *addr,
501                                   SilcSchedule schedule,
502                                   SilcNetResolveCallback completion,
503                                   void *context);
504
505 /****f* silcutil/SilcNetAPI/silc_net_check_host_by_sock
506  *
507  * SYNOPSIS
508  *
509  *    SilcBool silc_net_check_host_by_sock(int sock, char **hostname,
510  *                                         char **ip);
511  *
512  * DESCRIPTION
513  *
514  *    Performs lookups for remote name and IP address. This peforms reverse
515  *    lookup as well to verify that the IP has FQDN.
516  *
517  ***/
518 SilcBool silc_net_check_host_by_sock(int sock, char **hostname, char **ip);
519
520 /****f* silcutil/SilcNetAPI/silc_net_check_local_by_sock
521  *
522  * SYNOPSIS
523  *
524  *    SilcBool silc_net_check_local_by_sock(int sock, char **hostname,
525  *                                          char **ip);
526  *
527  * DESCRIPTION
528  *
529  *    Performs lookups for local name and IP address. This peforms reverse
530  *    lookup as well to verify that the IP has FQDN.
531  *
532  ***/
533 SilcBool silc_net_check_local_by_sock(int sock, char **hostname, char **ip);
534
535 /****f* silcutil/SilcNetAPI/silc_net_get_remote_port
536  *
537  * SYNOPSIS
538  *
539  *    SilcUInt16 silc_net_get_remote_port(int sock);
540  *
541  * DESCRIPTION
542  *
543  *    Return remote port by socket.
544  *
545  ***/
546 SilcUInt16 silc_net_get_remote_port(int sock);
547
548 /****f* silcutil/SilcNetAPI/silc_net_get_local_port
549  *
550  * SYNOPSIS
551  *
552  *    SilcUInt16 silc_net_get_local_port(int sock);
553  *
554  * DESCRIPTION
555  *
556  *    Return local port by socket.
557  *
558  ***/
559 SilcUInt16 silc_net_get_local_port(int sock);
560
561 /****f* silcutil/SilcNetAPI/silc_net_localhost
562  *
563  * SYNOPSIS
564  *
565  *    char *silc_net_localhost(void);
566  *
567  * DESCRIPTION
568  *
569  *    Return name of localhost.  This will also attempt to resolve
570  *    the real hostname by the local host's IP address.  If unsuccessful
571  *    the first found hostname is returned.  The caller must free
572  *    returned hostname.
573  *
574  ***/
575 char *silc_net_localhost(void);
576
577 /****f* silcutil/SilcNetAPI/silc_net_localip
578  *
579  * SYNOPSIS
580  *
581  *    char *silc_net_localip(void)
582  *
583  * DESCRIPTION
584  *
585  *    Return IP of localhost.  The caller must free the returned IP.
586  *
587  ***/
588 char *silc_net_localip(void);
589
590 /****f* silcutil/SilcNetAPI/silc_net_win32_init
591  *
592  * SYNOPSIS
593  *
594  *    SilcBool silc_net_win32_init(void);
595  *
596  * DESCRIPTION
597  *
598  *    This is WIN32 system specific function and is used to initialize
599  *    the network.  This must be called by all WIN32 applications.  It
600  *    is usually called at the application's main() or WinMain() before
601  *    calling any other SILC routine.  The application must also call
602  *    the silc_net_win32_uninit when exiting the application.  Returns
603  *    FALSE on error.  The network will not work if this function returns
604  *    FALSE.
605  *
606  * NOTES
607  *
608  *    This routines is available only on Win32 platform.
609  *
610  ***/
611 SilcBool silc_net_win32_init(void);
612
613 /****f* silcutil/SilcNetAPI/silc_net_win32_uninit
614  *
615  * SYNOPSIS
616  *
617  *    void silc_net_win32_init(void);
618  *
619  * DESCRIPTION
620  *
621  *    This is WIN32 system specific function and is used to uninitialize
622  *    the network.  This must be called by all WIN32 applications.  It
623  *    is usually called when the application is exiting.  After calling
624  *    this function the SILC Net API routines will not work anymore.
625  *
626  * NOTES
627  *
628  *    This routines is available only on Win32 platform.
629  *
630  ***/
631 void silc_net_win32_uninit(void);
632
633 #include "silcnet_i.h"
634
635 #endif /* SILCNET_H */