updates.
[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    unsigned int 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    char *hostname
90    char *ip
91    unsigned short port
92
93        Resolved hostname, IP address and port of the connection who owns
94        this object.
95
96    SilcBuffer inbuf
97    SilcBuffer outbuf
98
99        Incoming and outgoing buffers for the particular socket connection.
100        Incoming data from the socket is put after decryption in to the
101        inbuf buffer and outgoing data after encryption is put to the outbuf
102        buffer.
103
104   SilcSocketConnectionHB hb
105
106        The heartbeat context.  If NULL, heartbeat is not performed.
107
108 */
109 struct SilcSocketConnectionStruct {
110   int sock;
111   SilcSocketType type;
112   void *user_data;
113   SilcProtocol protocol;
114   unsigned int flags;
115
116   char *hostname;
117   char *ip;
118   unsigned short port;
119
120   SilcBuffer inbuf;
121   SilcBuffer outbuf;
122
123   SilcSocketConnectionHB hb;
124 };
125
126 /* Heartbeat context */
127 struct SilcSocketConnectionHB {
128   unsigned long heartbeat;
129   SilcSocketConnectionHBCb hb_callback;
130   void *hb_context;
131   void *timeout_queue;
132   SilcTask hb_task;
133   SilcSocketConnection sock;
134 };
135
136 /* Macros */
137
138 /* Generic manipulation of flags */
139 #define SF_SET(x, f) (x)->flags |= (1L << (f))
140 #define SF_UNSET(x, f) (x)->flags &= ~(1L << (f))
141 #define SF_IS(x, f) (x)->flags & (1L << (f))
142
143 /* Setting/Unsetting flags */
144 #define SILC_SET_OUTBUF_PENDING(x) SF_SET((x), SILC_SF_OUTBUF_PENDING)
145 #define SILC_SET_INBUF_PENDING(x) SF_SET((x), SILC_SF_INBUF_PENDING)
146 #define SILC_SET_DISCONNECTING(x) SF_SET((x), SILC_SF_DISCONNECTING)
147 #define SILC_SET_DISCONNECTED(x) SF_SET((x), SILC_SF_DISCONNECTED)
148 #define SILC_UNSET_OUTBUF_PENDING(x) SF_UNSET((x), SILC_SF_OUTBUF_PENDING)
149 #define SILC_UNSET_INBUF_PENDING(x) SF_UNSET((x), SILC_SF_INBUF_PENDING)
150 #define SILC_UNSET_DISCONNECTING(x) SF_UNSET((x), SILC_SF_DISCONNECTING)
151 #define SILC_UNSET_DISCONNECTED(x) SF_UNSET((x), SILC_SF_DISCONNECTED)
152
153 /* Checking for flags */
154 #define SILC_IS_OUTBUF_PENDING(x) SF_IS((x), SILC_SF_OUTBUF_PENDING)
155 #define SILC_IS_INBUF_PENDING(x) SF_IS((x), SILC_SF_INBUF_PENDING)
156 #define SILC_IS_DISCONNECTING(x) SF_IS((x), SILC_SF_DISCONNECTING)
157 #define SILC_IS_DISCONNECTED(x) SF_IS((x), SILC_SF_DISCONNECTED)
158
159 /* Prototypes */
160 void silc_socket_alloc(int sock, SilcSocketType type, void *user_data,
161                        SilcSocketConnection *new_socket);
162 void silc_socket_free(SilcSocketConnection sock);
163 void silc_socket_set_heartbeat(SilcSocketConnection sock, 
164                                unsigned long heartbeat,
165                                void *hb_context,
166                                SilcSocketConnectionHBCb hb_callback,
167                                void *timeout_queue);
168
169 #endif