1 
2 /*-
3  *
4  * New BSD License 2006
5  *
6  * Copyright (c) 2006, Jorgen Lundman
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are
11  * met:
12  *
13  * 1 Redistributions of source code must retain the above copyright
14  *   notice, this list of conditions and the following disclaimer.
15  * 2 Redistributions in binary form must reproduce the above
16  *   copyright notice, this list of conditions and the following
17  *   disclaimer in the documentation and/or other materials provided
18  *   with the distribution.
19  * 3 Neither the name of the stuff nor the names of its contributors
20  *   may be used to endorse or promote products derived from this
21  *   software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  */
36 
37 // $Id: connections.h,v 1.24 2006/06/30 01:22:58 lundman Exp $
38 // Temporary connection space holder
39 // Jorgen Lundman November 5th, 1999
40 
41 #include "lion_types.h"
42 
43 
44 #ifndef CONNECTIONS_H
45 #define CONNECTIONS_H
46 
47 #ifdef WIN32
48 #define WINDOWS_LEAN_AND_MEAN
49 #include <winsock2.h>
50 #endif
51 #include <time.h>
52 
53 #ifdef WITH_SSL
54 #include <openssl/blowfish.h>
55 #endif
56 
57 //#include "lion.h"
58 
59 
60 
61 /* Defines   */
62 
63 #undef ST_CONNECTED
64 
65 enum connection_state {
66 	ST_NONE=0,
67 	ST_PENDING,
68 	ST_CONNECTED,
69 	ST_LISTEN,
70 	ST_BUFFERFULL,
71 	ST_READBUFFERFULL,
72 	ST_WANTRETURN,
73 	ST_RESUMEREAD,
74 #ifdef WITH_SSL
75 	ST_SSL_NEG,
76 #endif
77 	ST_DISCONNECT
78 };
79 
80 enum net_want {
81 	NET_WANT_NONE = 0,
82 	NET_WANT_READ,
83 	NET_WANT_WRITE,
84 	NET_WANT_CLOSE    // SECRET INTERNAL DELAYED CLOSE PROCESS.
85 };
86 
87 
88 enum lion_udp_flag {
89 	LION_UDP_UNBOUND = 0,
90 	LION_UDP_BOUND = 1
91 }; // 1, 2 , 4, 8, 16
92 
93 
94 
95 
96 //
97 // Just the default buffer size. You can set it in your aplication using
98 // lion_buffersize() preferably before init.
99 // Not on *BSD systems, if you do network traffic, you get very bad
100 // performance if the buffersize > MTU (generally 1500)
101 //
102 #define BUFFER_SIZE_DEFAULT 1400
103 
104 
105 // Size of the printf buffers. It is not common to printf this large.
106 #define STATIC_BUFFER_SIZE 8192
107 
108 
109 
110 extern unsigned int buffer_size;
111 THREAD_SAFE extern FILE *trace_file;
112 
113 
114 
115 /* Variables */
116 
117 struct connection_struct {
118 
119 	int type;
120 
121 	/* Data fields */
122 	int status;
123 	int socket;
124 
125 	int binary;
126 
127 	int disable_read; // Is read-fd to be disabled? dont add to readfd
128 	int soft_closed; // net_close was called, but we need to flush output
129 
130 	int inbuffer;  // Bytes currently held in the input buffer
131 	char *buffer;  // the input buffer
132 	int inbuffer_size; // current allocation size
133 
134 	int outbuffer; // Bytes currently held in the output buffer
135 	char *obuffer; // the output buffer
136 	int outbuffer_size;
137 
138 #ifdef WITH_SSL
139 
140 	int use_ssl;      // SSL is enabled and active
141 	int want_ssl;     // SSL is requested, attempt it when connected
142 	int auth_type;     // SSL is requested, attempt it when connected
143 	void *ctx;        // SSL context
144 	void *data_ctx;   // SSL data context
145 
146 	// File encryption
147 	BF_KEY bf_key;     // BF_KEY
148 	unsigned char bf_ivec[8];
149 	int bf_num;
150 
151 #endif
152 
153 	enum net_want want_mode;
154 
155 	void *user_data; // optional user data
156 
157 	int rate_in;     // maximum input rate, in KB/s
158 	int rate_out;    // maximum input rate, in KB/s
159 	time_t time_start;  // Last time stamp
160 	bytes_t bytes_in;
161 	bytes_t bytes_out;
162 
163 	time_t group_last;
164 
165 	//	int group_bytes_in;
166 	//	int group_bytes_out;
167 
168 	// When we are in rate_out capping, and we have posted the event, we
169 	// need to remember this, so we can post the available event when it
170 	// is within limits again.
171 	int rate_out_capped;
172 
173 	// Groups
174 	// FIXME!! Multigroup support!
175 	int group;
176 
177 
178 	// PIPES
179 	int pid;           // type_pipe needs to remember pid.
180 	int return_code;   // and return code, if known.
181 
182 	int error_code;    // We keep the error code if we delay event
183 
184 #ifdef WIN32
185 	HANDLE mutex;
186 	int file_socket;  // Only used in Win32 as we thread to read files.
187 	HANDLE thread;
188 	HANDLE thread2;
189 	int (*start_address)(void *);
190 #endif
191 
192 	// UDP
193 	unsigned long host;
194 	unsigned int port;   // we avoid the whole short int issue.
195 
196 	unsigned int udp_flags; // So we know if it is bound
197 	unsigned long udp_host; // need to store, so we dont over write bound
198 	unsigned int udp_port;  // hosts when we receive data.
199 
200 	// Is this node being traced?
201 	unsigned int trace;
202 
203 
204 	/* User defined Event Handler */
205 	//lion_handler_t event_handler;
206 	unsigned int in_event;
207 	int (*event_handler)(struct connection_struct *,
208 						 void *, int, int, char *);
209 
210 	/* Internal fields */
211 	struct connection_struct *next;
212 
213 
214 };
215 
216 typedef struct connection_struct connection_t;
217 
218 
219 
220 /* Functions */
221 
222 connection_t *connections_new(void);
223 void          connections_free(connection_t *);
224 connection_t *connections_find( int (*)(connection_t *, void *, void *),
225 								void *, void *);
226 void          connections_setuser(connection_t *, char *, char *);
227 void		  connections_dupe(connection_t *dst, connection_t *src);
228 void          connections_cycle( void );
229 
230 
231 #endif
232 
233 
234 
235 
236 
237 
238