updates.
[silc.git] / lib / silcutil / silcsockconn.h
1 /*
2  
3   silcsockconn.h
4  
5   Author: Pekka Riikonen <priikone@silnet.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/SilcSocketConnectionAPI
22  *
23  * DESCRIPTION
24  *
25  * Implementation of the Socket Connection object. The SilcSocketConnection
26  * is used by all applications to represent a socket based connection
27  * to the network. The Socket Connection object handles inbound and outbound
28  * data buffers, can perform keepalive actions for the connection and
29  * supports connection based protocols as well.
30  *
31  ***/
32
33 #ifndef SILCSOCKCONN_H
34 #define SILCSOCKCONN_H
35
36 /****s* silcutil/SilcSocketConnectionAPI/SilcSocketConnection
37  *
38  * NAME
39  * 
40  *    typedef struct SilcSocketConnectionStruct *SilcSocketConnection;
41  *
42  * DESCRIPTION
43  *
44  *    This context is forward declaration for the SilcSocketConnectionStruct.
45  *    This is allocated by the silc_socket_alloc and freed by the
46  *    silc_socket_free function. The silc_socket_dup can be used to
47  *    increase the reference counter of the context. The data is freed
48  *    by the silc_socket_free function only after the reference counter
49  *    hits zero.
50  *
51  ***/
52 typedef struct SilcSocketConnectionStruct *SilcSocketConnection;
53
54 /****s* silcutil/SilcSocketConnectionAPI/SilcSocketConnectionHB
55  *
56  * NAME
57  * 
58  *    typedef struct SilcSocketConnectionHB *SilcSocketConnectionHB;
59  *
60  * DESCRIPTION
61  *
62  *    This context is the heartbeat context for the SilcSockeConnection.
63  *    It is meant to hold the keepalive information for the connection.
64  *    This is allocated internally and freed internally by the 
65  *    interface routines.
66  *
67  ***/
68 typedef struct SilcSocketConnectionHBStruct *SilcSocketConnectionHB;
69
70 /****d* silcutil/SilcSocketConnectionAPI/SilcSocketType
71  *
72  * NAME
73  * 
74  *    typedef enum { ... } SilcSocketType;
75  *
76  * DESCRIPTION
77  *
78  *    Socket types. These identifies the socket connection. There
79  *    are four different types; unknown, client, server and router.
80  *    Unknown connections are connections that hasn't advanced long
81  *    enough so that we might know which type of connection it is.
82  *    It is the applications responsibility to update the type 
83  *    information when it becomes available.
84  *
85  * SOURCE
86  */
87 typedef enum {
88   SILC_SOCKET_TYPE_UNKNOWN = 0,
89   SILC_SOCKET_TYPE_CLIENT = 1,
90   SILC_SOCKET_TYPE_SERVER = 2,
91   SILC_SOCKET_TYPE_ROUTER = 3
92 } SilcSocketType;
93 /***/
94
95 /* Socket flags */
96 #define SILC_SF_NONE             0
97 #define SILC_SF_INBUF_PENDING    1 /* data in inbound buffer */
98 #define SILC_SF_OUTBUF_PENDING   2 /* data in outbound buffer */
99 #define SILC_SF_DISCONNECTING    3 /* socket disconnecting */
100 #define SILC_SF_DISCONNECTED     4 /* socket disconnected */
101 #define SILC_SF_HOST_LOOKUP      5 /* performing host lookup for socket */
102 #define SILC_SF_DISABLED         6 /* socket connection is disabled,
103                                       no data is sent or received. */
104
105 /****s* silcutil/SilcSocketConnectionAPI/SilcSocketConnectionStruct
106  *
107  * NAME
108  * 
109  *    struct SilcSocketConnectionStruct { ... };
110  *
111  * DESCRIPTION
112  *
113  *    This object holds information about the connected sockets to the server.
114  *    This is quite important object since this is referenced by the server all
115  *    the time when figuring out what the connection is supposed to be doing
116  *    and to whom we should send a message. This structure is the structure
117  *    for the SilcSocketConnection forward declaration.
118  *
119  *    Following short description of the fields:
120  *
121  *    int sock
122  *
123  *      The actual connected socket. This is usually saved when accepting
124  *      new connection to the server.
125  *
126  *    SilcSocketType type
127  *
128  *      Type of the socket. This identifies the type of the connection. This
129  *      is mainly used to identify whether the connection is a client or a
130  *      server connection.
131  *
132  *    void *user_data
133  *
134  *      This is a pointer to a data that is is saved here at the same
135  *      time a new connection object is allocated. Usually this is a 
136  *      back-pointer to some important data for fast referencing. For
137  *      SILC server this is a pointer to the ID list and for SILC client
138  *      to object holding active connections (windows).
139  *
140  *    SilcProtocol protocol
141  *
142  *      Protocol object for the socket. Currently only one protocol can be
143  *      executing at a time for a particular socket.
144  *
145  *    uint32 flags
146  *
147  *      Socket flags that indicate the status of the socket. This can
148  *      indicate several different status that can affect the use of the
149  *      socket object.
150  *
151  *    int users
152  *
153  *      Reference counter. When allocated it is set to one (1) and it won't
154  *      be freed until it hits zero (0).
155  *
156  *    char *hostname
157  *    char *ip
158  *    uint16 port
159  *
160  *      Resolved hostname, IP address and port of the connection who owns
161  *      this object.
162  *
163  *    SilcBuffer inbuf
164  *    SilcBuffer outbuf
165  *
166  *      Incoming and outgoing buffers for the particular socket connection.
167  *      Incoming data from the socket is put after decryption in to the
168  *      inbuf buffer and outgoing data after encryption is put to the outbuf
169  *      buffer.
170  *
171  *    SilcSocketConnectionHB hb
172  *
173  *      The heartbeat context.  If NULL, heartbeat is not performed.
174  *
175  ***/
176 struct SilcSocketConnectionStruct {
177   int sock;
178   SilcSocketType type;
179   void *user_data;
180   SilcProtocol protocol;
181   uint32 flags;
182   int users;
183
184   char *hostname;
185   char *ip;
186   uint16 port;
187
188   SilcBuffer inbuf;
189   SilcBuffer outbuf;
190
191   SilcSocketConnectionHB hb;
192 };
193
194 /* Macros */
195
196 /* Amount of bytes to be read from the socket connection at once. */
197 #define SILC_SOCKET_READ_SIZE 16384
198
199 /* Default socket buffer size. */
200 #define SILC_SOCKET_BUF_SIZE 1024
201
202 /* Generic manipulation of flags */
203 #define SF_SET(x, f) (x)->flags |= (1L << (f))
204 #define SF_UNSET(x, f) (x)->flags &= ~(1L << (f))
205 #define SF_IS(x, f) ((x)->flags & (1L << (f)))
206
207 /* Setting/Unsetting flags */
208 #define SILC_SET_OUTBUF_PENDING(x) SF_SET((x), SILC_SF_OUTBUF_PENDING)
209 #define SILC_SET_INBUF_PENDING(x) SF_SET((x), SILC_SF_INBUF_PENDING)
210 #define SILC_SET_DISCONNECTING(x) SF_SET((x), SILC_SF_DISCONNECTING)
211 #define SILC_SET_DISCONNECTED(x) SF_SET((x), SILC_SF_DISCONNECTED)
212 #define SILC_SET_HOST_LOOKUP(x) SF_SET((x), SILC_SF_HOST_LOOKUP)
213 #define SILC_SET_DISABLED(x) SF_SET((x), SILC_SF_HOST_LOOKUP)
214 #define SILC_UNSET_OUTBUF_PENDING(x) SF_UNSET((x), SILC_SF_OUTBUF_PENDING)
215 #define SILC_UNSET_INBUF_PENDING(x) SF_UNSET((x), SILC_SF_INBUF_PENDING)
216 #define SILC_UNSET_DISCONNECTING(x) SF_UNSET((x), SILC_SF_DISCONNECTING)
217 #define SILC_UNSET_DISCONNECTED(x) SF_UNSET((x), SILC_SF_DISCONNECTED)
218 #define SILC_UNSET_HOST_LOOKUP(x) SF_UNSET((x), SILC_SF_HOST_LOOKUP)
219 #define SILC_UNSET_DISABLED(x) SF_UNSET((x), SILC_SF_DISABLED)
220
221 /* Checking for flags */
222 #define SILC_IS_OUTBUF_PENDING(x) SF_IS((x), SILC_SF_OUTBUF_PENDING)
223 #define SILC_IS_INBUF_PENDING(x) SF_IS((x), SILC_SF_INBUF_PENDING)
224 #define SILC_IS_DISCONNECTING(x) SF_IS((x), SILC_SF_DISCONNECTING)
225 #define SILC_IS_DISCONNECTED(x) SF_IS((x), SILC_SF_DISCONNECTED)
226 #define SILC_IS_HOST_LOOKUP(x) SF_IS((x), SILC_SF_HOST_LOOKUP)
227 #define SILC_IS_DISABLED(x) SF_IS((x), SILC_SF_DISABLED)
228
229 /* Prototypes */
230
231 /****f* silcutil/SilcSocketConnectionAPI/silc_socket_alloc
232  *
233  * SYNOPSIS
234  *
235  *    void silc_socket_alloc(int sock, SilcSocketType type, void *user_data,
236  *                           SilcSocketConnection *new_socket);
237  *
238  * DESCRIPTION
239  *
240  *    Allocates a new socket connection object. The allocated object is 
241  *    returned to the new_socket argument. The `sock' is the socket
242  *    for the connection, the `type' the initial type of the connection and
243  *    the `user_data' a application specific pointer.
244  *
245  ***/
246 void silc_socket_alloc(int sock, SilcSocketType type, void *user_data,
247                        SilcSocketConnection *new_socket);
248
249 /****f* silcutil/SilcSocketConnectionAPI/silc_socket_free
250  *
251  * SYNOPSIS
252  *
253  *    void silc_socket_free(SilcSocketConnection sock);
254  *
255  * DESCRIPTION
256  *
257  *    Frees the socket connection context. This frees it only if the
258  *    reference counter of the socket is zero, otherwise it decreases the
259  *    reference counter.
260  *
261  ***/
262 void silc_socket_free(SilcSocketConnection sock);
263
264 /****f* silcutil/SilcSocketConnectionAPI/silc_socket_dup
265  *
266  * SYNOPSIS
267  *
268  *    SilcSocketConnection silc_socket_dup(SilcSocketConnection sock);
269  *
270  * DESCRIPTION
271  *
272  *    Duplicates the socket context. This actually does not duplicate
273  *    any data, instead this increases the reference counter of the
274  *    context. The reference counter is decreased by calling the
275  *    silc_socket_free function and it frees the data when the counter
276  *    hits zero.
277  *
278  ***/
279 SilcSocketConnection silc_socket_dup(SilcSocketConnection sock);
280
281 /****f* silcutil/SilcSocketConnectionAPI/silc_socket_read
282  *
283  * SYNOPSIS
284  *
285  *    int silc_socket_read(SilcSocketConnection sock);
286  *
287  * DESCRIPTION
288  *
289  *    Reads data from the socket connection into the incoming data buffer.
290  *    It reads as much as possible from the socket connection. This returns
291  *    amount of bytes read or -1 on error or -2 on case where all of the
292  *    data could not be read at once. Implementation of this function
293  *    may be platform specific.
294  *
295  ***/
296 int silc_socket_read(SilcSocketConnection sock);
297
298 /****f* silcutil/SilcSocketConnectionAPI/silc_socket_write
299  *
300  * SYNOPSIS
301  *
302  *    int silc_socket_read(SilcSocketConnection sock);
303  *
304  * DESCRIPTION
305  *
306  *    Writes data from the outgoing buffer to the socket connection. If the
307  *    data cannot be written at once, it must be written at later time. 
308  *    The data is written from the data section of the buffer, not from head
309  *    or tail section. This automatically pulls the data section towards end
310  *    after writing the data. Implementation of this function may be
311  *    platform specific.
312  *
313  ***/
314 int silc_socket_write(SilcSocketConnection sock);
315
316 /****f* silcutil/SilcSocketConnectionAPI/SilcSocketConnectionHBCb
317  *
318  * SYNOPSIS
319  *
320  *    typedef void (*SilcSocketConnectionHBCb)(SilcSocketConnection sock,
321  *                                             void *context);
322  *
323  * DESCRIPTION
324  *
325  *    Heartbeat callback function. This is the function in the application
326  *    that this library will call when it is time to send the keepalive
327  *    packet SILC_PACKET_HEARTBEAT.
328  *
329  ***/
330 typedef void (*SilcSocketConnectionHBCb)(SilcSocketConnection sock,
331                                          void *context);
332
333 /****f* silcutil/SilcSocketConnectionAPI/silc_socket_set_heartbeat
334  *
335  * SYNOPSIS
336  *
337  *    void silc_socket_set_heartbeat(SilcSocketConnection sock, 
338  *                                   uint32 heartbeat,
339  *                                   void *hb_context,
340  *                                   SilcSocketConnectionHBCb hb_callback,
341  *                                   SilcSchedule schedule);
342  *
343  * DESCRIPTION
344  *
345  *    Sets the heartbeat timeout and prepares the socket for performing
346  *    heartbeat in `heartbeat' intervals (seconds). The `hb_context' is
347  *    allocated by the application and will be sent as argument to the
348  *    `hb_callback' function that is called when the `heartbeat' timeout
349  *    expires.  The callback `hb_context' won't be touched by the library
350  *    but will be freed automatically when calling silc_socket_free.  The
351  *    `schedule' is the application's scheduler.
352  *
353  ***/
354 void silc_socket_set_heartbeat(SilcSocketConnection sock, 
355                                uint32 heartbeat,
356                                void *hb_context,
357                                SilcSocketConnectionHBCb hb_callback,
358                                SilcSchedule schedule);
359
360 /****f* silcutil/SilcSocketConnectionAPI/SilcSocketHostLookupCb
361  *
362  * SYNOPSIS
363  *
364  *    typedef void (*SilcSocketHostLookupCb)(SilcSocketConnection sock,
365  *                                           void *context);
366  *
367  * DESCRIPTION
368  *
369  *    Asynchronous host lookup callback function that will be called
370  *    when the lookup is performed.
371  *
372  ***/
373 typedef void (*SilcSocketHostLookupCb)(SilcSocketConnection sock,
374                                        void *context);
375
376 /****f* silcutil/SilcSocketConnectionAPI/silc_socket_host_lookup
377  *
378  * SYNOPSIS
379  *
380  *    void silc_socket_host_lookup(SilcSocketConnection sock,
381  *                                 bool port_lookup,
382  *                                 SilcSocketHostLookupCb callback,
383  *                                 void *context,
384  *                                 SilcSchedule schedule);
385  *
386  * DESCRIPTION
387  *
388  *    Performs asynchronous host name and IP address lookups for the
389  *    specified socket connection. This may be called when the socket
390  *    connection is created and the full IP address and fully qualified
391  *    domain name information is desired. The `callback' with `context'
392  *    will be called after the lookup is performed. The `schedule'
393  *    is the application's scheduler which the lookup routine needs. 
394  *    If the socket connection is freed during the lookup the library
395  *    will automatically cancel the lookup and the `callback' will not be
396  *    called.
397  *
398  *    If `port_lookup' is TRUE then the remote port of the socket 
399  *    connection is resolved. After the information is resolved they
400  *    are accessible using sock->ip and sock->hostname pointers. Note
401  *    that if the both IP and FQDN could not be resolved the sock->hostname
402  *    includes the IP address of the remote host. The resolved port is 
403  *    available in sock->port.
404  *
405  ***/
406 void silc_socket_host_lookup(SilcSocketConnection sock,
407                              bool port_lookup,
408                              SilcSocketHostLookupCb callback,
409                              void *context,
410                              SilcSchedule schedule);
411
412 #endif