Add max allowed size to TC_Read and TC_Talk
[tedtools.git] / connection.h
1 /*
2  * Copyright (c) 2004 Teodor Sigaev <teodor@sigaev.ru>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *        notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *        notice, this list of conditions and the following disclaimer in the
12  *        documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *        may be used to endorse or promote products derived from this software
15  *        without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #ifndef __CONNECTION_H__
31 #define __CONNECTION_H__
32
33 #include <sys/types.h>
34 #include <sys/time.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38
39
40
41 #define CS_OK           0
42 #define CS_INPROCESS    1
43 #define CS_CONNECTED    2
44 #define CS_READ         3
45 #define CS_SEND         4
46 #define CS_WAIT         5
47 #define CS_ERROR        6
48 #define CS_FINISHSEND   7
49 #define CS_FINISHREAD   8
50 #define CS_TIMEOUT      9
51 #define CS_CLOSED       10      
52 #define CS_NOTINITED    11
53 #define CS_AGAIN        12
54 #define CS_FINISH       13
55
56
57 #define READ_INCRIMENT_BUFSIZ 1024
58
59 typedef struct {
60         /* I/O buffer */
61         u_int32_t  len;
62         char    *buf;
63         char    *ptr;
64
65         /* internal fields */
66         int     fd;
67         u_int32_t
68                 readyio:1,  
69                 state:29;
70         struct sockaddr_in serv_addr;
71
72         /* external link */
73         void*           data;
74 } TC_Connection;
75
76 #define TCCONNHDRSZ   ( sizeof(TC_Connection) - sizeof(void*) )
77
78 TC_Connection *TC_fillConnection( TC_Connection *cs, char *name, u_int32_t port );
79 TC_Connection* TC_AcceptTcp(TC_Connection *cs);
80 u_int32_t TC_ClientInitConnection(TC_Connection *cs, char *name, u_int32_t port);
81 u_int32_t TC_ServerInitConnect( TC_Connection *cs );
82 u_int32_t TC_ServerConnect( TC_Connection *cs, int timeout );
83 u_int32_t TC_Send( TC_Connection *cs );
84 u_int32_t TC_Read( TC_Connection *cs, size_t maxsize );
85 u_int32_t TC_Talk( TC_Connection *cs, size_t maxsize );
86 void TC_FreeConnection( TC_Connection *cs );
87 int TC_ReadyIO( TC_Connection **cs, int number, int timeout );
88
89 typedef struct {
90         u_int32_t       len;
91         u_int32_t       type;
92         char            data[1];
93 } TCMsg;
94
95 #define TCMSGHDRSZ      (2*sizeof(u_int32_t))
96
97
98 /* udp */
99 typedef struct {
100         char    *host;
101         u_int32_t       port;
102         TCMsg   *msg;
103
104         /* private members */
105         int             sockfd;
106         struct sockaddr_in host_addr;
107 } Msg; 
108
109 /* 
110  * Udp server loop:
111  * TC_Connection  conn,*connptr;
112  * conn.fd = TC_AcceptUdp("127.0.0.1", 5432);
113  * conn.state = CS_READ;
114  * connptr = &conn;
115  * while(1) {
116  *      if ( TC_ReadyIO( &connptr, 1, 100 ) ) {
117  *              Msg m; 
118  *              if ( TC_getMsg(conn.fd, &m) == CS_OK ) {
119  *                      //do something
120  *              } 
121  *      }
122  * }
123  * close(conn.fd);
124  *
125  * Udp client send:
126  * Msg msg;
127  * msg.host        = "127.0.0.1";
128  * msg.port        = 5432;
129  * msg.sockfd =-1;
130  * msg.msg = GOTFILLEDPMSG();
131  * if ( TC_sendMsg(&msg)!=CS_OK ) {
132  *        //Very bad
133  * }
134  * msg.msg = GOTFILLEDPMSG();
135  * if ( TC_sendMsg(&msg)!=CS_OK ) {
136  *        //Very bad
137  * }
138  * TC_closefd(&msg);
139  */ 
140
141 int TC_AcceptUdp(char *host, int port);
142 u_int32_t TC_getMsg( int sockfd, Msg *msg );
143 u_int32_t TC_sendMsg( Msg *msg );
144 void      TC_closefd( Msg *msg );
145
146 typedef struct {
147         u_int32_t       len;
148         u_int32_t       number;
149         TC_Connection   **conn;
150 } PoolConnection;
151
152 void TC_addConnection(PoolConnection *pool, TC_Connection *c);
153 void TC_deleteConnectionByN(PoolConnection *pool, int n);
154 void TC_deleteConnectionByC(PoolConnection *pool, TC_Connection *c);
155
156 #endif
157