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 /* 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    char *hostname
80    char *ip
81    unsigned short port
82
83        Resolved hostname, IP address and port of the connection who owns
84        this object.
85
86    SilcBuffer inbuf
87    SilcBuffer outbuf
88
89        Incoming and outgoing buffers for the particular socket connection.
90        Incoming data from the socket is put after decryption in to the
91        inbuf buffer and outgoing data after encryption is put to the outbuf
92        buffer.
93
94 */
95 typedef struct {
96   int sock;
97   SilcSocketType type;
98   void *user_data;
99   SilcProtocol protocol;
100   unsigned int flags;
101
102   char *hostname;
103   char *ip;
104   unsigned short port;
105
106   SilcBuffer inbuf;
107   SilcBuffer outbuf;
108 } SilcSocketConnectionObject;
109
110 typedef SilcSocketConnectionObject *SilcSocketConnection;
111
112 /* Macros */
113
114 /* Generic manipulation of flags */
115 #define SF_SET(x, f) (x)->flags |= (1L << (f))
116 #define SF_UNSET(x, f) (x)->flags &= ~(1L << (f))
117 #define SF_IS(x, f) (x)->flags & (1L << (f))
118
119 /* Setting/Unsetting flags */
120 #define SILC_SET_OUTBUF_PENDING(x) SF_SET((x), SILC_SF_OUTBUF_PENDING)
121 #define SILC_SET_INBUF_PENDING(x) SF_SET((x), SILC_SF_INBUF_PENDING)
122 #define SILC_SET_DISCONNECTING(x) SF_SET((x), SILC_SF_DISCONNECTING)
123 #define SILC_SET_DISCONNECTED(x) SF_SET((x), SILC_SF_DISCONNECTED)
124 #define SILC_UNSET_OUTBUF_PENDING(x) SF_UNSET((x), SILC_SF_OUTBUF_PENDING)
125 #define SILC_UNSET_INBUF_PENDING(x) SF_UNSET((x), SILC_SF_INBUF_PENDING)
126 #define SILC_UNSET_DISCONNECTING(x) SF_UNSET((x), SILC_SF_DISCONNECTING)
127 #define SILC_UNSET_DISCONNECTED(x) SF_UNSET((x), SILC_SF_DISCONNECTED)
128
129 /* Checking for flags */
130 #define SILC_IS_OUTBUF_PENDING(x) SF_IS((x), SILC_SF_OUTBUF_PENDING)
131 #define SILC_IS_INBUF_PENDING(x) SF_IS((x), SILC_SF_INBUF_PENDING)
132 #define SILC_IS_DISCONNECTING(x) SF_IS((x), SILC_SF_DISCONNECTING)
133 #define SILC_IS_DISCONNECTED(x) SF_IS((x), SILC_SF_DISCONNECTED)
134
135 /* Prototypes */
136 void silc_socket_alloc(int sock, SilcSocketType type, void *user_data,
137                        SilcSocketConnection *new_socket);
138 void silc_socket_free(SilcSocketConnection sock);
139
140 #endif