Initial revision
[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 /* Socket types. These identifies the socket connection. */
25 typedef enum {
26   SILC_SOCKET_TYPE_UNKNOWN = 0,
27   SILC_SOCKET_TYPE_CLIENT = 1,
28   SILC_SOCKET_TYPE_SERVER = 2,
29   SILC_SOCKET_TYPE_ROUTER = 3
30 } SilcSocketType;
31
32 /* Socket flags */
33 #define SILC_SF_NONE 0
34 #define SILC_SF_INBUF_PENDING 1
35 #define SILC_SF_OUTBUF_PENDING 2
36 #define SILC_SF_DISCONNECTING 3
37 #define SILC_SF_DISCONNECTED 4
38
39 /* 
40    SILC Socket Connection object.
41
42    This object holds information about the connected sockets to the server.
43    This is quite important object since this is referenced by the server all
44    the time when figuring out what the connection is supposed to be doing
45    and to whom we should send a message.
46
47    Following short description of the fields:
48
49    int sock
50
51        The actual connected socket. This is usually saved when accepting
52        new connection to the server.
53
54    SilcSocketType type
55
56        Type of the socket. This identifies the type of the connection. This
57        is mainly used to identify whether the connection is a client or a
58        server connection.
59
60    void *user_data
61
62        This is a pointer to a data that is is saved here at the same
63        time a new connection object is allocated. Usually this is a 
64        back-pointer to some important data for fast referencing. For
65        SILC server this is a pointer to the ID list and for SILC client
66        to object holding active connections (windows).
67
68    SilcProtocol protocol
69
70        Protocol object for the socket. Currently only one protocol can be
71        executing at a time for a particular socket.
72
73    unsigned int flags
74
75        Socket flags that indicate the status of the socket. This can
76        indicate several different status that can affect the use of the
77        socket object.
78
79    SilcBuffer inbuf
80    SilcBuffer outbuf
81
82        Incoming and outgoing buffers for the particular socket connection.
83        Incoming data from the socket is put after decryption in to the
84        inbuf buffer and outgoing data after encryption is put to the outbuf
85        buffer.
86
87 */
88 typedef struct {
89   int sock;
90   SilcSocketType type;
91   void *user_data;
92   SilcProtocol protocol;
93   unsigned int flags;
94
95   char *hostname;
96   char *ip;
97   unsigned short port;
98
99   SilcBuffer inbuf;
100   SilcBuffer outbuf;
101 } SilcSocketConnectionObject;
102
103 typedef SilcSocketConnectionObject *SilcSocketConnection;
104
105 /* Macros */
106
107 /* Generic manipulation of flags */
108 #define SF_SET(x, f) (x)->flags |= (1L << (f))
109 #define SF_UNSET(x, f) (x)->flags &= ~(1L << (f))
110 #define SF_IS(x, f) (x)->flags & (1L << (f))
111
112 /* Setting/Unsetting flags */
113 #define SILC_SET_OUTBUF_PENDING(x) SF_SET((x), SILC_SF_OUTBUF_PENDING)
114 #define SILC_SET_INBUF_PENDING(x) SF_SET((x), SILC_SF_INBUF_PENDING)
115 #define SILC_SET_DISCONNECTING(x) SF_SET((x), SILC_SF_DISCONNECTING)
116 #define SILC_SET_DISCONNECTED(x) SF_SET((x), SILC_SF_DISCONNECTED)
117 #define SILC_UNSET_OUTBUF_PENDING(x) SF_UNSET((x), SILC_SF_OUTBUF_PENDING)
118 #define SILC_UNSET_INBUF_PENDING(x) SF_UNSET((x), SILC_SF_INBUF_PENDING)
119 #define SILC_UNSET_DISCONNECTING(x) SF_UNSET((x), SILC_SF_DISCONNECTING)
120 #define SILC_UNSET_DISCONNECTED(x) SF_UNSET((x), SILC_SF_DISCONNECTED)
121
122 /* Checking for flags */
123 #define SILC_IS_OUTBUF_PENDING(x) SF_IS((x), SILC_SF_OUTBUF_PENDING)
124 #define SILC_IS_INBUF_PENDING(x) SF_IS((x), SILC_SF_INBUF_PENDING)
125 #define SILC_IS_DISCONNECTING(x) SF_IS((x), SILC_SF_DISCONNECTING)
126 #define SILC_IS_DISCONNECTED(x) SF_IS((x), SILC_SF_DISCONNECTED)
127
128 /* Prototypes */
129 void silc_socket_alloc(int sock, SilcSocketType type, void *user_data,
130                        SilcSocketConnection *new_socket);
131 void silc_socket_free(SilcSocketConnection sock);
132
133 #endif