updates. New data types.
[silc.git] / lib / silccore / silcsockconn.h
1 /*
2
3   silcsockconn.h
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2000 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 #ifndef SILCSOCKCONN_H
22 #define SILCSOCKCONN_H
23
24 /* Forward declarations */
25 typedef struct SilcSocketConnectionStruct *SilcSocketConnection;
26 typedef struct SilcSocketConnectionHB *SilcSocketConnectionHB;
27
28 /* Socket types. These identifies the socket connection. */
29 typedef enum {
30   SILC_SOCKET_TYPE_UNKNOWN = 0,
31   SILC_SOCKET_TYPE_CLIENT = 1,
32   SILC_SOCKET_TYPE_SERVER = 2,
33   SILC_SOCKET_TYPE_ROUTER = 3
34 } SilcSocketType;
35
36 /* Socket flags */
37 #define SILC_SF_NONE 0
38 #define SILC_SF_INBUF_PENDING 1
39 #define SILC_SF_OUTBUF_PENDING 2
40 #define SILC_SF_DISCONNECTING 3
41 #define SILC_SF_DISCONNECTED 4
42
43 /* Heartbeat callback function. This is the function in the application
44    that this library will call when it is time to send the keepalive
45    packet SILC_PACKET_HEARTBEAT. */
46 typedef void (*SilcSocketConnectionHBCb)(SilcSocketConnection sock,
47                                          void *context);
48
49 /* 
50    SILC Socket Connection object.
51
52    This object holds information about the connected sockets to the server.
53    This is quite important object since this is referenced by the server all
54    the time when figuring out what the connection is supposed to be doing
55    and to whom we should send a message.
56
57    Following short description of the fields:
58
59    int sock
60
61        The actual connected socket. This is usually saved when accepting
62        new connection to the server.
63
64    SilcSocketType type
65
66        Type of the socket. This identifies the type of the connection. This
67        is mainly used to identify whether the connection is a client or a
68        server connection.
69
70    void *user_data
71
72        This is a pointer to a data that is is saved here at the same
73        time a new connection object is allocated. Usually this is a 
74        back-pointer to some important data for fast referencing. For
75        SILC server this is a pointer to the ID list and for SILC client
76        to object holding active connections (windows).
77
78    SilcProtocol protocol
79
80        Protocol object for the socket. Currently only one protocol can be
81        executing at a time for a particular socket.
82
83    uint32 flags
84
85        Socket flags that indicate the status of the socket. This can
86        indicate several different status that can affect the use of the
87        socket object.
88
89    int users
90
91        Reference counter. When allocated it is set to one (1) and it won't
92        be freed until it hits zero (0).
93
94    char *hostname
95    char *ip
96    uint16 port
97
98        Resolved hostname, IP address and port of the connection who owns
99        this object.
100
101    SilcBuffer inbuf
102    SilcBuffer outbuf
103
104        Incoming and outgoing buffers for the particular socket connection.
105        Incoming data from the socket is put after decryption in to the
106        inbuf buffer and outgoing data after encryption is put to the outbuf
107        buffer.
108
109   SilcSocketConnectionHB hb
110
111        The heartbeat context.  If NULL, heartbeat is not performed.
112
113 */
114 struct SilcSocketConnectionStruct {
115   int sock;
116   SilcSocketType type;
117   void *user_data;
118   SilcProtocol protocol;
119   uint32 flags;
120   int users;
121
122   char *hostname;
123   char *ip;
124   uint16 port;
125
126   SilcBuffer inbuf;
127   SilcBuffer outbuf;
128
129   SilcSocketConnectionHB hb;
130 };
131
132 /* Heartbeat context */
133 struct SilcSocketConnectionHB {
134   uint32 heartbeat;
135   SilcSocketConnectionHBCb hb_callback;
136   void *hb_context;
137   void *timeout_queue;
138   SilcTask hb_task;
139   SilcSocketConnection sock;
140 };
141
142 /* Macros */
143
144 /* Generic manipulation of flags */
145 #define SF_SET(x, f) (x)->flags |= (1L << (f))
146 #define SF_UNSET(x, f) (x)->flags &= ~(1L << (f))
147 #define SF_IS(x, f) ((x)->flags & (1L << (f)))
148
149 /* Setting/Unsetting flags */
150 #define SILC_SET_OUTBUF_PENDING(x) SF_SET((x), SILC_SF_OUTBUF_PENDING)
151 #define SILC_SET_INBUF_PENDING(x) SF_SET((x), SILC_SF_INBUF_PENDING)
152 #define SILC_SET_DISCONNECTING(x) SF_SET((x), SILC_SF_DISCONNECTING)
153 #define SILC_SET_DISCONNECTED(x) SF_SET((x), SILC_SF_DISCONNECTED)
154 #define SILC_UNSET_OUTBUF_PENDING(x) SF_UNSET((x), SILC_SF_OUTBUF_PENDING)
155 #define SILC_UNSET_INBUF_PENDING(x) SF_UNSET((x), SILC_SF_INBUF_PENDING)
156 #define SILC_UNSET_DISCONNECTING(x) SF_UNSET((x), SILC_SF_DISCONNECTING)
157 #define SILC_UNSET_DISCONNECTED(x) SF_UNSET((x), SILC_SF_DISCONNECTED)
158
159 /* Checking for flags */
160 #define SILC_IS_OUTBUF_PENDING(x) SF_IS((x), SILC_SF_OUTBUF_PENDING)
161 #define SILC_IS_INBUF_PENDING(x) SF_IS((x), SILC_SF_INBUF_PENDING)
162 #define SILC_IS_DISCONNECTING(x) SF_IS((x), SILC_SF_DISCONNECTING)
163 #define SILC_IS_DISCONNECTED(x) SF_IS((x), SILC_SF_DISCONNECTED)
164
165 /* Prototypes */
166 void silc_socket_alloc(int sock, SilcSocketType type, void *user_data,
167                        SilcSocketConnection *new_socket);
168 void silc_socket_free(SilcSocketConnection sock);
169 SilcSocketConnection silc_socket_dup(SilcSocketConnection sock);
170 void silc_socket_set_heartbeat(SilcSocketConnection sock, 
171                                uint32 heartbeat,
172                                void *hb_context,
173                                SilcSocketConnectionHBCb hb_callback,
174                                void *timeout_queue);
175
176 #endif