updates.
[silc.git] / lib / silcutil / silcnet.h
1 /****h* silcutil/silcnet.h
2  *
3  * NAME
4  *
5  * silcnet.h
6  *
7  * COPYRIGHT
8  *
9  * Author: Pekka Riikonen <priikone@silcnet.org>
10  *
11  * Copyright (C) 1997 - 2001 Pekka Riikonen
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
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, 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(int port, 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.
82  *
83  ***/
84 int silc_net_create_connection(int port, char *host);
85
86 /****f* silcutil/SilcNetAPI/silc_net_create_connection_async
87  *
88  * SYNOPSIS
89  *
90  *    int silc_net_create_connection_async(int port, char *host);
91  *
92  * DESCRIPTION
93  *
94  *    Creates a connection (TCP/IP) to a remote host. Returns the connection
95  *    socket or -1 on error. This creates non-blocking socket hence the
96  *    connection returns directly. To get the result of the connect() one
97  *    must select() the socket and read the result after it's ready.
98  *
99  ***/
100 int silc_net_create_connection_async(int port, char *host);
101
102 /****f* silcutil/SilcNetAPI/silc_net_close_connection
103  *
104  * SYNOPSIS
105  *
106  *    void silc_net_close_connection(int sock);
107  *
108  * DESCRIPTION
109  *
110  *    Closes the connection by closing the socket connection.
111  *
112  ***/
113 void silc_net_close_connection(int sock);
114
115 /****f* silcutil/SilcNetAPI/silc_net_accept_connection
116  *
117  * SYNOPSIS
118  *
119  *    int silc_net_accept_connection(int sock);
120  *
121  * DESCRIPTION
122  *
123  *    Accepts a connection from a particular socket.
124  *
125  ***/
126 int silc_net_accept_connection(int sock);
127
128 /****f* silcutil/SilcNetAPI/silc_net_set_socket_nonblock
129  *
130  * SYNOPSIS
131  *
132  *    int silc_net_set_socket_nonblock(int sock);
133  *
134  * DESCRIPTION
135  *
136  *    Sets the socket to non-blocking mode.
137  *
138  ***/
139 int silc_net_set_socket_nonblock(int sock);
140
141 /****f* silcutil/SilcNetAPI/silc_net_set_socket_opt
142  *
143  * SYNOPSIS
144  *
145  *    int silc_net_set_socket_opt(int sock, int level, int option, int on);
146  *
147  * DESCRIPTION
148  *
149  *    Sets a option for a socket.  This function can be used to set
150  *    various options for the socket.  Some of the options might be
151  *    system specific.
152  *
153  ***/
154 int silc_net_set_socket_opt(int sock, int level, int option, int on);
155
156 /****f* silcutil/SilcNetAPI/silc_net_get_socket_opt
157  *
158  * SYNOPSIS
159  *
160  *    int silc_net_get_socket_opt(int sock, int level, int option, 
161  *                                void *optval, int *opt_len);
162  *
163  * DESCRIPTION
164  *
165  *    Return socket options to the `optval' and `opt_len'.
166  *
167  ***/
168 int silc_net_get_socket_opt(int sock, int level, int option, 
169                             void *optval, int *opt_len);
170
171 /****f* silcutil/SilcNetAPI/silc_net_is_ip
172  *
173  * SYNOPSIS
174  *
175  *    bool silc_net_is_ip(const char *addr);
176  *
177  * DESCRIPTION
178  *
179  *    Checks whether IP address sent as argument is valid IP address.
180  *
181  ***/
182 bool silc_net_is_ip(const char *addr);
183
184 /****f* silcutil/SilcNetAPI/silc_net_addr2bin
185  *
186  * SYNOPSIS
187  *
188  *    bool silc_net_addr2bin(const char *addr, unsigned char *bin,
189  *                           uint32 bin_len);
190  *
191  * DESCRIPTION
192  *
193  *    Converts the IP number string from numbers-and-dots notation to
194  *    binary form.
195  *
196  ***/
197 bool silc_net_addr2bin(const char *addr, unsigned char *bin,
198                        uint32 bin_len);
199
200 /****f* silcutil/SilcNetAPI/silc_net_check_host_by_sock
201  *
202  * SYNOPSIS
203  *
204  *    bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip);
205  *
206  * DESCRIPTION
207  *
208  *    Performs lookups for remote name and IP address. This peforms reverse
209  *    lookup as well to verify that the IP has FQDN.
210  *
211  ***/
212 bool silc_net_check_host_by_sock(int sock, char **hostname, char **ip);
213
214 /****f* silcutil/SilcNetAPI/silc_net_check_local_by_sock
215  *
216  * SYNOPSIS
217  *
218  *    bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip);
219  *
220  * DESCRIPTION
221  *
222  *    Performs lookups for local name and IP address. This peforms reverse
223  *    lookup as well to verify that the IP has FQDN.
224  *
225  ***/
226 bool silc_net_check_local_by_sock(int sock, char **hostname, char **ip);
227
228 /****f* silcutil/SilcNetAPI/silc_net_get_remote_port
229  *
230  * SYNOPSIS
231  *
232  *    uint16 silc_net_get_remote_port(int sock);
233  *
234  * DESCRIPTION
235  *
236  *    Return remote port by socket.
237  *
238  ***/
239 uint16 silc_net_get_remote_port(int sock);
240
241 /****f* silcutil/SilcNetAPI/silc_net_get_local_port
242  *
243  * SYNOPSIS
244  *
245  *    uint16 silc_net_get_local_port(int sock);
246  *
247  * DESCRIPTION
248  *
249  *    Return local port by socket.
250  *
251  ***/
252 uint16 silc_net_get_local_port(int sock);
253
254 /****f* silcutil/SilcNetAPI/silc_net_localhost
255  *
256  * SYNOPSIS
257  *
258  *    char *silc_net_localhost();
259  *
260  * DESCRIPTION
261  *
262  *    Return name of localhost.  This will also attempt to resolve
263  *    the real hostname by the local host's IP address.  If unsuccessful
264  *    the first found hostname is returned.
265  *
266  ***/
267 char *silc_net_localhost();
268
269 #ifdef WIN32
270
271 /****f* silcutil/SilcNetAPI/silc_net_win32_init
272  *
273  * SYNOPSIS
274  *
275  *    bool silc_net_win32_init(void);
276  *
277  * DESCRIPTION
278  *
279  *    This is WIN32 system specific function and is used to initialize
280  *    the network.  This must be called by all WIN32 applications.  It
281  *    is usually called at the application's main() or WinMain() before
282  *    calling any other SILC routine.  The application must also call
283  *    the silc_net_win32_uninit when exiting the application.  Returns
284  *    FALSE on error.  The network will not work if this function returns
285  *    FALSE.
286  *
287  ***/
288 bool silc_net_win32_init(void);
289
290 /****f* silcutil/SilcNetAPI/silc_net_win32_uninit
291  *
292  * SYNOPSIS
293  *
294  *    void silc_net_win32_init(void);
295  *
296  * DESCRIPTION
297  *
298  *    This is WIN32 system specific function and is used to uninitialize
299  *    the network.  This must be called by all WIN32 applications.  It
300  *    is usually called when the application is exiting.  After calling
301  *    this function the SILC Net API routines will not work anymore.
302  *
303  ***/
304 void silc_net_win32_uninit(void);
305
306 #endif
307
308 #endif