File: streams.h

package info (click to toggle)
xevil 2.02r2-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,888 kB
  • sloc: cpp: 47,789; makefile: 189; csh: 4
file content (289 lines) | stat: -rw-r--r-- 6,660 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* 
 * XEvil(TM) Copyright (C) 1994,2000 Steve Hardt and Michael Judge
 * http://www.xevil.com
 * satan@xevil.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program, the file "gpl.txt"; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA, or visit http://www.gnu.org.
 */

// streams.h

#ifndef STREAMS_H
#define STREAMS_H

#if X11
	#ifndef NO_PRAGMAS
	#pragma interface
	#endif
#endif

#include "utils.h"

#define UDP_STREAM_HEADER_LEN 12    // <XETP::versionStr:8> + <checksum:4>
#define UDP_STREAM_BODY_LEN 5000 //2044  should make this some sort of growable array
#define UDP_STREAM_BUFFER_LEN (UDP_STREAM_HEADER_LEN + UDP_STREAM_BODY_LEN)

typedef u_int Checksum;

class GenericStream {
public:
  enum {TCP,UDP};

  GenericStream(Boolean ownSock) {ownSocket = ownSock;}
  /* EFFECTS: ownSock means whether the stream owns the socket or not, i.e
     whether it will take responsibility for closing it when done. */

  virtual ~GenericStream();

  virtual int get_protocol() = 0;

  virtual Boolean alive() = 0;

  static Boolean get_buggy() {return buggy;}
  static void set_buggy(Boolean val) {buggy = val;}
  /* EFFECTS:  Simulate a bad network connection. */
  
  
#ifndef PROTECTED_IS_PUBLIC
protected:
#endif
  Boolean ownSocket;
  
  static Boolean buggy;

  Checksum compute_checksum(u_char *data,int len);
};



class InStream : public GenericStream {
public:
  InStream(Boolean ownSock) : GenericStream(ownSock) {}
  
  virtual Boolean read(void *buf,int size) = 0;
  /* EFFECTS: Read size number of bytes from buf.  Return True if
     successful. */

  virtual u_char read_char() = 0;

  virtual int read_signed_char() = 0;

  virtual u_short read_short() = 0;

  virtual int read_signed_short() = 0;

  virtual u_int read_int() = 0;

  virtual float read_float() = 0;
};
typedef InStream *InStreamP;



class OutStream : public GenericStream {
public:
  OutStream(Boolean ownSock) : GenericStream(ownSock) {}

  virtual Boolean write(const void *buf,int size) = 0;
  /* EFFECTS: Write size number of bytes into buf.  Return True if
     successful. */

  virtual void write_char(u_char) = 0;
  
  virtual void write_signed_char(int) = 0;

  virtual void write_short(u_short) = 0;

  virtual void write_signed_short(int) = 0;

  virtual void write_int(u_int) = 0;

  virtual void write_float(float) = 0;
};
typedef OutStream *OutStreamP;



// Non-buffering stream to read from TCP socket.
class NetInStream : public InStream {
public:
  NetInStream(CMN_SOCKET sock,Boolean ownSocket);

  virtual ~NetInStream();
  /* NOTE: Closes the socket */

  virtual int get_protocol();

  virtual Boolean alive();
  
  virtual Boolean read(void *buf,int size);

  virtual u_char read_char();

  virtual int read_signed_char();

  virtual u_short read_short();

  virtual int read_signed_short();

  virtual u_int read_int();

  virtual float read_float();

  static int get_bytes_in() {return bytesIn;}
  static void reset_counter() {bytesIn = 0;}
  /* NOTE: Simple statistics. */


#ifndef PROTECTED_IS_PUBLIC
protected:
#endif
  Boolean isAlive;
  CMN_SOCKET sock;

  static int bytesIn;
};



// Non-buffering stream to write to TCP socket.
class NetOutStream : public OutStream {
public:
  NetOutStream(CMN_SOCKET sock,Boolean ownSocket);

  virtual ~NetOutStream();
  /* NOTE: Closes the socket */
  
  virtual int get_protocol();

  virtual Boolean alive();
  
  virtual Boolean write(const void *buf,int size);

  virtual void write_char(u_char);

  virtual void write_signed_char(int);

  virtual void write_short(u_short);

  virtual void write_signed_short(int);

  virtual void write_int(u_int);

  virtual void write_float(float);

  static int get_bytes_out() {return bytesOut;}
  static void reset_counter() {bytesOut = 0;}
  /* NOTE: Simple statistics. */


#ifndef PROTECTED_IS_PUBLIC
protected:
#endif
  Boolean isAlive;
  CMN_SOCKET sock;

  static int bytesOut;
};



class UDPInStream : public NetInStream {
public:
  UDPInStream(CMN_SOCKET sock,Boolean ownSocket);

  virtual ~UDPInStream();

  virtual int get_protocol();

  virtual int prepare_packet(CMN_SOCKADDR_IN *address);
  /* MODIFIES: address */
  /* EFFECTS: Set the address of the packet coming in, return the length of
     the data.  Return -1 if failure. */

  virtual void done_packet();

  virtual Boolean read(void *buf,int size);

  int bytes_remaining() {return bufLen - bufPtr;}
  /* EFFECTS: Number of bytes that can still be read from this UDP packet. */

  void revive() {isAlive = True;}
  /* EFFECTS: Bring this stream back to life. */

  static int get_bytes_in() {return bytesIn;}
  static void reset_counter() {bytesIn = 0;}
  /* NOTE: Simple statistics. */


private:
  u_char buffer[UDP_STREAM_BUFFER_LEN];
  int bufPtr; // Current location reading in buffer.
  int bufLen;

  static int bytesIn;
};
typedef UDPInStream *UDPInStreamP;



class UDPOutStream : public NetOutStream {
public:
  UDPOutStream(CMN_SOCKET sock,CMN_SOCKADDR_IN *,Boolean ownSocket);
  /* NOTE: Shares the sockaddr_in struct that is passed in. */

  virtual ~UDPOutStream();

  virtual int get_protocol();

  virtual void prepare_packet(int);

  virtual void done_packet();

  virtual Boolean write(const void *buf,int size);

  void flush();

  static float get_average_length_out() 
  { return (float)totalBytesOut / (float)packetCount;}
  static int get_bytes_out() {return bytesOut;}
  static void reset_counter() {bytesOut = 0;}
  /* NOTE: Simple statistics. */


private:
  int buggy_tests();
  /* EFFECTS: Possibly modify data to run tests.  Return the number of
     times to send the data in buffer. */

  void create_header();
  /* MODIFIES: header */
  /* EFFECTS: Compute checksum of data in body and put in header. */

  CMN_SOCKADDR_IN *address;
  u_char buffer[UDP_STREAM_BUFFER_LEN];
  int bufPtr; // Current location writing to buffer.
  int bufLen; // length of the packet to be written

  static int bytesOut;

  // For average packet length
  static int totalBytesOut;
  static int packetCount;
};
typedef UDPOutStream *UDPOutStreamP;

#endif