2 Copyright 2004-2005 Chris Tallon
3 Copyright 2003-2004 University Of Bradford
5 This file is part of VOMP.
7 VOMP is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 VOMP is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with VOMP; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 readTimeoutEnabled = 1;
39 if (connected) cleanup();
47 printf("TCP has closed socket\n");
51 void TCP::disableReadTimeout()
53 readTimeoutEnabled = 0;
56 int TCP::connectTo(char* host, unsigned short port)
58 sock = socket(PF_INET, SOCK_STREAM, 0);
59 if (sock == -1) return 0;
61 struct sockaddr_in dest_addr;
62 dest_addr.sin_family = AF_INET;
63 dest_addr.sin_port = htons(port);
65 if (!inet_aton(host, &dest_addr.sin_addr))
71 memset(&(dest_addr.sin_zero), '\0', 8);
73 int success = connect(sock, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr));
84 void TCP::setNonBlocking()
86 int oldflags = fcntl(sock, F_GETFL, 0);
87 oldflags |= O_NONBLOCK;
88 fcntl(sock, F_SETFL, oldflags);
91 int TCP::setSoKeepTime(int timeOut)
97 s1 = setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &option, sizeof(option));
98 printf("SO_KEEPALIVE = %i\n", s1);
101 s2 = setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, &option, sizeof(option));
102 printf("TCP_KEEPIDLE = %i\n", s2);
104 s3 = setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, &option, sizeof(option));
105 printf("TCP_KEEPINTVL = %i\n", s3);
108 s4 = setsockopt(sock, SOL_TCP, TCP_KEEPCNT, &option, sizeof(option));
109 printf("TCP_KEEPCNT = %i\n", s4);
111 if (s1 || s2 || s3 || s4) return 0;
115 void TCP::assignSocket(int tsocket)
121 int TCP::isConnected()
126 UCHAR* TCP::receivePacket()
128 if (!connected) return NULL;
133 success = readData((UCHAR*)&packetLength, sizeof(int));
140 packetLength = ntohl(packetLength);
142 if (packetLength > 200000) return NULL;
143 UCHAR* buffer = (UCHAR*) malloc(packetLength);
145 success = readData(buffer, packetLength);
153 dump((unsigned char*)buffer, packetLength);
155 dataLength = packetLength;
159 int TCP::getDataLength()
164 int TCP::readData(UCHAR* buffer, int totalBytes)
166 if (!connected) return 0;
173 struct timeval timeout;
174 struct timeval* passToSelect;
176 if (readTimeoutEnabled) passToSelect = &timeout;
177 else passToSelect = NULL;
182 FD_SET(sock, &readSet);
185 success = select(sock + 1, &readSet, NULL, NULL, passToSelect);
189 printf("TCP: error or timeout\n");
190 return 0; // error, or timeout
193 thisRead = read(sock, &buffer[bytesRead], totalBytes - bytesRead);
196 // if read returns 0 then connection is closed
197 // in non-blocking mode if read is called with no data available, it returns -1
198 // and sets errno to EGAGAIN. but we use select so it wouldn't do that anyway.
202 bytesRead += thisRead;
204 printf("Bytes read now: %u\n", bytesRead);
205 if (bytesRead == totalBytes)
211 if (++readTries == 100)
214 printf("too many reads\n");
221 int TCP::sendPacket(UCHAR* buf, size_t count)
223 if (!connected) return 0;
225 unsigned int bytesWritten = 0;
230 struct timeval timeout;
235 FD_SET(sock, &writeSet);
238 success = select(sock + 1, NULL, &writeSet, NULL, &timeout);
242 printf("TCP: error or timeout\n");
243 return 0; // error, or timeout
246 thisWrite = write(sock, &buf[bytesWritten], count - bytesWritten);
249 // if write returns 0 then connection is closed ?
250 // in non-blocking mode if read is called with no data available, it returns -1
251 // and sets errno to EGAGAIN. but we use select so it wouldn't do that anyway.
253 printf("Detected connection closed\n");
256 bytesWritten += thisWrite;
258 printf("Bytes written now: %u\n", bytesWritten);
259 if (bytesWritten == count)
265 if (++writeTries == 100)
268 printf("too many writes\n");
278 void TCP::dump(unsigned char* data, USHORT size)
280 printf("Size = %u\n", size);
287 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",
288 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
289 data[c+8], data[c+9], data[c+10], data[c+11], data[c+12], data[c+13], data[c+14], data[c+15],
290 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
291 dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]), dcc(data[c+12]), dcc(data[c+13]), dcc(data[c+14]), dcc(data[c+15]));
299 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",
300 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
301 data[c+8], data[c+9], data[c+10], data[c+11], data[c+12], data[c+13], data[c+14],
302 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
303 dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]), dcc(data[c+12]), dcc(data[c+13]), dcc(data[c+14]));
307 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",
308 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
309 data[c+8], data[c+9], data[c+10], data[c+11], data[c+12], data[c+13],
310 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
311 dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]), dcc(data[c+12]), dcc(data[c+13]));
315 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c%c%c%c\n",
316 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
317 data[c+8], data[c+9], data[c+10], data[c+11], data[c+12],
318 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
319 dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]), dcc(data[c+12]));
323 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c%c%c\n",
324 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
325 data[c+8], data[c+9], data[c+10], data[c+11],
326 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
327 dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]), dcc(data[c+11]));
331 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c%c\n",
332 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
333 data[c+8], data[c+9], data[c+10],
334 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
335 dcc(data[c+8]), dcc(data[c+9]), dcc(data[c+10]));
339 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c%c\n",
340 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
341 data[c+8], data[c+9],
342 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
343 dcc(data[c+8]), dcc(data[c+9]));
347 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c%c\n",
348 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
350 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]),
355 printf(" %02X %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c%c\n",
356 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6], data[c+7],
357 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]), dcc(data[c+7]));
361 printf(" %02X %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c%c\n",
362 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5], data[c+6],
363 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]), dcc(data[c+6]));
367 printf(" %02X %02X %02X %02X %02X %02X %c%c%c%c%c%c\n",
368 data[c], data[c+1], data[c+2], data[c+3], data[c+4], data[c+5],
369 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]), dcc(data[c+5]));
373 printf(" %02X %02X %02X %02X %02X %c%c%c%c%c\n",
374 data[c], data[c+1], data[c+2], data[c+3], data[c+4],
375 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]), dcc(data[c+4]));
379 printf(" %02X %02X %02X %02X %c%c%c%c\n",
380 data[c], data[c+1], data[c+2], data[c+3],
381 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]), dcc(data[c+3]));
385 printf(" %02X %02X %02X %c%c%c\n",
386 data[c], data[c+1], data[c+2],
387 dcc(data[c]), dcc(data[c+1]), dcc(data[c+2]));
391 printf(" %02X %02X %c%c\n",
393 dcc(data[c]), dcc(data[c+1]));
407 unsigned char TCP::dcc(UCHAR c)
409 if (isspace(c)) return ' ';
410 if (isprint(c)) return c;