updates.
[silc.git] / lib / silcutil / silcnet.h
1 /*
2
3   silcnet.h
4  
5   Author: Pekka Riikonen <priikone@silcnet.org>
6  
7   Copyright (C) 1997 - 2001 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; either version 2 of the License, or
12   (at your option) any later version.
13  
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.
18
19 */
20
21 /****h* silcutil/SilcNetAPI
22  *
23  * DESCRIPTION
24  *
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.
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 /****f* silcutil/SilcNetAPI/silc_net_create_server
42  *
43  * SYNOPSIS
44  *
45  *    int silc_net_create_server(int port, char *ip_addr);
46  *
47  * DESCRIPTION
48  *
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.
54  *
55  ***/
56 int silc_net_create_server(int port, const char *ip_addr);
57
58 /****f* silcutil/SilcNetAPI/silc_net_close_server
59  *
60  * SYNOPSIS
61  *
62  *    void silc_net_close_server(int sock);
63  *
64  * DESCRIPTION
65  *
66  *    Closes the server by closing the socket connection.
67  *
68  ***/
69 void silc_net_close_server(int sock);
70
71 /****f* silcutil/SilcNetAPI/silc_net_create_connection
72  *
73  * SYNOPSIS
74  *
75  *    int silc_net_create_connection(const char *local_ip, int port, 
76  *                                   const char *host);
77  *
78  * DESCRIPTION
79  *
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.
85  *
86  ***/
87 int silc_net_create_connection(const char *localhost, int port, 
88                                const char *host);
89
90 /****f* silcutil/SilcNetAPI/silc_net_create_connection_async
91  *
92  * SYNOPSIS
93  *
94  *    int silc_net_create_connection_async(const char *local_ip, int port, 
95  *                                         const char *host);
96  *
97  * DESCRIPTION
98  *
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.
106  *
107  ***/
108 int silc_net_create_connection_async(const char *local_ip, int port, 
109                                      const char *host);
110
111 /****f* silcutil/SilcNetAPI/silc_net_close_connection
112  *
113  * SYNOPSIS
114  *
115  *    void silc_net_close_connection(int sock);
116  *
117  * DESCRIPTION
118  *
119  *    Closes the connection by closing the socket connection.
120  *
121  ***/
122 void silc_net_close_connection(int sock);
123
124 /****f* silcutil/SilcNetAPI/silc_net_accept_connection
125  *
126  * SYNOPSIS
127  *
128  *    int silc_net_accept_connection(int sock);
129  *
130  * DESCRIPTION
131  *
132  *    Accepts a connection from a particular socket.
133  *
134  ***/
135 int silc_net_accept_connection(int sock);
136
137 /****f* silcutil/SilcNetAPI/silc_net_set_socket_nonblock
138  *
139  * SYNOPSIS
140  *
141  *    int silc_net_set_socket_nonblock(int sock);
142  *
143  * DESCRIPTION
144  *
145  *    Sets the socket to non-blocking mode.
146  *
147  ***/
148 int silc_net_set_socket_nonblock(int sock);
149
150 /****f* silcutil/SilcNetAPI/silc_net_set_socket_opt
151  *
152  * SYNOPSIS
153  *
154  *    int silc_net_set_socket_opt(int sock, int level, int option, int on);
155  *
156  * DESCRIPTION
157  *
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
160  *    system specific.
161  *
162  ***/
163 int silc_net_set_socket_opt(int sock, int level, int option, int on);
164
165 /****f* silcutil/SilcNetAPI/silc_net_get_socket_opt
166  *
167  * SYNOPSIS
168  *
169  *    int silc_net_get_socket_opt(int sock, int level, int option, 
170  *                                void *optval, int *opt_len);
171  *
172  * DESCRIPTION
173  *
174  *    Return socket options to the `optval' and `opt_len'.
175  *
176  ***/
177 int silc_net_get_socket_opt(int sock, int level, int option, 
178                             void *optval, int *opt_len);
179
180 /****f* silcutil/SilcNetAPI/silc_net_is_ip4
181  *
182  * SYNOPSIS
183  *
184  *    bool silc_net_is_ip4(const char *addr);
185  *
186  * DESCRIPTION
187  *
188  *    Checks whether IP address sent as argument is valid IPv4 address.
189  *
190  ***/
191 bool silc_net_is_ip4(const char *addr);
192
193 /****f* silcutil/SilcNetAPI/silc_net_is_ip6
194  *
195  * SYNOPSIS
196  *
197  *    bool silc_net_is_ip6(const char *addr);
198  *
199  * DESCRIPTION
200  *
201  *    Checks whether IP address sent as argument is valid IPv6 address.
202  *
203  ***/
204 bool silc_net_is_ip6(const char *addr);
205
206 /****f* silcutil/SilcNetAPI/silc_net_is_ip
207  *
208  * SYNOPSIS
209  *
210  *    bool silc_net_is_ip(const char *addr);
211  *
212  * DESCRIPTION
213  *
214  *    Checks whether IP address sent as argument is valid IP address.
215  *    This supports both IPv4 and IPv6 addresses.
216  *
217  ***/
218 bool silc_net_is_ip(const char *addr);
219
220 /****f* silcutil/SilcNetAPI/silc_net_addr2bin
221  *
222  * SYNOPSIS
223  *
224  *    bool silc_net_addr2bin(const char *addr, void *bin, uint32 bin_len);
225  *
226  * DESCRIPTION
227  *
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.
231  *
232  ***/
233 bool silc_net_addr2bin(const char *addr, void *bin, uint32 bin_len);
234
235 /****f* silcutil/SilcNetAPI/SilcNetResolveCallback
236  *
237  * SYNOPSIS
238  *
239  *    typedef void (*SilcNetResolveCallback)(const char *result, 
240  *                                           void *context);
241  *
242  * DESCRIPTION
243  *
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.
247  *
248  ***/
249 typedef void (*SilcNetResolveCallback)(const char *result, void *context);
250
251 /****f* silcutil/SilcNetAPI/silc_net_gethostbyname
252  *
253  * SYNOPSIS
254  *
255  *    bool silc_net_gethostbyname(const char *name, char *address, 
256  *                                uint32 address_len);
257  *
258  * DESCRIPTION
259  *
260  *    Resolves the IP address of the hostname indicated by the `host'.
261  *    This returns TRUE and the IP address of the host, or FALSE
262  *    if the address could not be resolved.  This is synchronous
263  *    function and will block the calling process.
264  *
265  ***/
266 bool silc_net_gethostbyname(const char *name, char *address, 
267                             uint32 address_len);
268
269 /****f* silcutil/SilcNetAPI/silc_net_gethostbyname_async
270  *
271  * SYNOPSIS
272  *
273  *    void silc_net_gethostbyname_async(const char *name, 
274  *                                      SilcSchedule schedule,
275  *                                      SilcNetResolveCallback completion,
276  *                                      void *context)
277  *
278  * DESCRIPTION
279  *
280  *    Asynchronously resolves the IP address of the hostname indicated
281  *    by the `host'.  This function returns immediately, and the
282  *    `completion' callback will be called after the resolving is
283  *    completed.
284  *
285  ***/
286 void silc_net_gethostbyname_async(const char *name, 
287                                   SilcSchedule schedule,
288                                   SilcNetResolveCallback completion,
289                                   void *context);
290
291 /****f* silcutil/SilcNetAPI/silc_net_gethostbyaddr
292  *
293  * SYNOPSIS
294  *
295  *   bool silc_net_gethostbyaddr(const char *addr, char *name, 
296  *                               uint32 name_len);
297  *
298  * DESCRIPTION
299  *
300  *    Resolves the hostname for the IP address indicated by the `addr'
301  *    This returns TRUE and the resolved hostname, or FALSE on error.
302  *    The `addr' may be either IPv4 or IPv6 address.  This is
303  *    synchronous function and will block the calling process.
304  *
305  ***/
306 bool silc_net_gethostbyaddr(const char *addr, char *name, uint32 name_len);
307
308 /****f* silcutil/SilcNetAPI/silc_net_gethostbyaddr_async
309  *
310  * SYNOPSIS
311  *
312  *    void silc_net_gethostbyaddr_async(const char *addr, 
313  *                                      SilcSchedule schedule,
314  *                                      SilcNetResolveCallback completion,
315  *                                      void *context)
316  *
317  * DESCRIPTION
318  *
319  *    Asynchronously resolves the hostname for the IP address indicated
320  *    by the `addr'.  This function returns immediately, and the
321  *    `completion' callback will be called after the resolving is
322  *    completed.
323  *
324  ***/
325 void silc_net_gethostbyaddr_async(const char *addr, 
326                                   SilcSchedule schedule,
327                                   SilcNetResolveCallback completion,
328                                   void *context);
329
330 /****f* silcutil/SilcNetAPI/silc_net_check_host_by_sock
331  *
332  * SYNOPSIS
333  *
334  *    bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip);
335  *
336  * DESCRIPTION
337  *
338  *    Performs lookups for remote name and IP address. This peforms reverse
339  *    lookup as well to verify that the IP has FQDN.
340  *
341  ***/
342 bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip);
343
344 /****f* silcutil/SilcNetAPI/silc_net_check_local_by_sock
345  *
346  * SYNOPSIS
347  *
348  *    bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip);
349  *
350  * DESCRIPTION
351  *
352  *    Performs lookups for local name and IP address. This peforms reverse
353  *    lookup as well to verify that the IP has FQDN.
354  *
355  ***/
356 bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip);
357
358 /****f* silcutil/SilcNetAPI/silc_net_get_remote_port
359  *
360  * SYNOPSIS
361  *
362  *    uint16 silc_net_get_remote_port(int sock);
363  *
364  * DESCRIPTION
365  *
366  *    Return remote port by socket.
367  *
368  ***/
369 uint16 silc_net_get_remote_port(int sock);
370
371 /****f* silcutil/SilcNetAPI/silc_net_get_local_port
372  *
373  * SYNOPSIS
374  *
375  *    uint16 silc_net_get_local_port(int sock);
376  *
377  * DESCRIPTION
378  *
379  *    Return local port by socket.
380  *
381  ***/
382 uint16 silc_net_get_local_port(int sock);
383
384 /****f* silcutil/SilcNetAPI/silc_net_localhost
385  *
386  * SYNOPSIS
387  *
388  *    char *silc_net_localhost(void);
389  *
390  * DESCRIPTION
391  *
392  *    Return name of localhost.  This will also attempt to resolve
393  *    the real hostname by the local host's IP address.  If unsuccessful
394  *    the first found hostname is returned.
395  *
396  ***/
397 char *silc_net_localhost(void);
398
399 /****f* silcutil/SilcNetAPI/silc_net_localip
400  *
401  * SYNOPSIS
402  *
403  *    char *silc_net_localip(void)
404  *
405  * DESCRIPTION
406  *
407  *    Return IP of localhost.  
408  *
409  ***/
410 char *silc_net_localip(void);
411
412 #ifdef WIN32
413
414 /****f* silcutil/SilcNetAPI/silc_net_win32_init
415  *
416  * SYNOPSIS
417  *
418  *    bool silc_net_win32_init(void);
419  *
420  * DESCRIPTION
421  *
422  *    This is WIN32 system specific function and is used to initialize
423  *    the network.  This must be called by all WIN32 applications.  It
424  *    is usually called at the application's main() or WinMain() before
425  *    calling any other SILC routine.  The application must also call
426  *    the silc_net_win32_uninit when exiting the application.  Returns
427  *    FALSE on error.  The network will not work if this function returns
428  *    FALSE.
429  *
430  ***/
431 bool silc_net_win32_init(void);
432
433 /****f* silcutil/SilcNetAPI/silc_net_win32_uninit
434  *
435  * SYNOPSIS
436  *
437  *    void silc_net_win32_init(void);
438  *
439  * DESCRIPTION
440  *
441  *    This is WIN32 system specific function and is used to uninitialize
442  *    the network.  This must be called by all WIN32 applications.  It
443  *    is usually called when the application is exiting.  After calling
444  *    this function the SILC Net API routines will not work anymore.
445  *
446  ***/
447 void silc_net_win32_uninit(void);
448
449 #endif
450
451 #endif