aboutsummaryrefslogtreecommitdiffstats
path: root/src/TcpSocket.cpp
blob: 75b320f0cf593be2377aeeb787c8fd2a555b0bbf (plain)
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
   Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Her Majesty the Queen in
   Right of Canada (Communications Research Center Canada)
   */
/*
   This file is part of ODR-DabMux.

   ODR-DabMux 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 3 of the
   License, or (at your option) any later version.

   ODR-DabMux 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 ODR-DabMux.  If not, see <http://www.gnu.org/licenses/>.
   */

#include "TcpSocket.h"
#include <iostream>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>

#ifdef _WIN32
#else
#   include <unistd.h>
#endif

#ifdef TRACE_ON
# ifndef TRACE_CLASS
#  define TRACE_CLASS(class, func) cout <<"-" <<(class) <<"\t(" <<this <<")::" <<(func) <<endl
#  define TRACE_STATIC(class, func) cout <<"-" <<(class) <<"\t(static)::" <<(func) <<endl
# endif
#else
# ifndef TRACE_CLASS
#  define TRACE_CLASS(class, func)
#  define TRACE_STATIC(class, func)
# endif
#endif


/// Must be call once before doing any operation on sockets
int TcpSocket::init()
{
#ifdef _WIN32
  WSADATA wsaData;
  WORD wVersionRequested = wVersionRequested = MAKEWORD( 2, 2 );
  
  int res = WSAStartup( wVersionRequested, &wsaData );
  if (res) {
    setInetError("Can't initialize winsock");
    return -1;
  }
#endif
  return 0;
}


/// Must be call once before leaving application
int TcpSocket::clean()
{
#ifdef _WIN32
  int res = WSACleanup();
  if (res) {
    setInetError("Can't initialize winsock");
    return -1;
  }
#endif
  return 0;
}


/**
 *  Two step constructor. Create must be called prior to use this
 *  socket.
 */
TcpSocket::TcpSocket() :
  listenSocket(INVALID_SOCKET)
{
  TRACE_CLASS("TcpSocket", "TcpSocket()");
}


/**
 *  One step constructor.
 *  @param port The port number on which the socket will be bind
 *  @param name The IP address on which the socket will be bind.
 *              It is used to bind the socket on a specific interface if
 *              the computer have many NICs.
 */
TcpSocket::TcpSocket(int port, char *name) :
  listenSocket(INVALID_SOCKET)
{
  TRACE_CLASS("TcpSocket", "TcpSocket(int, char*)");
  create(port, name);
}


/**
 *  Close the underlying socket.
 *  @return 0  if ok
 *          -1 if error
 */
int TcpSocket::close()
{
    TRACE_CLASS("TcpSocket", "close()");
    if (listenSocket != INVALID_SOCKET) {
        int res = closesocket(listenSocket);
        if (res != 0) {
            setInetError("Can't close socket");
            return -1;
        }
        listenSocket = INVALID_SOCKET;
    }
    return 0;    
}


/**
 *  Two step initializer. This function must be called after the constructor
 *  without argument as been called.
 *  @param port The port number on which the socket will be bind
 *  @param name The IP address on which the socket will be bind.
 *              It is used to bind the socket on a specific interface if
 *              the computer have many NICs.
 *  @return 0  if ok
 *          -1 if error
 */
int TcpSocket::create(int port, char *name)
{
  TRACE_CLASS("TcpSocket", "create(int, char*)");
  if (listenSocket != INVALID_SOCKET)
    closesocket(listenSocket);
  address.setAddress(name);
  address.setPort(port);
  if ((listenSocket = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
    setInetError("Can't create socket");
    return -1;
  }
  reuseopt_t reuse = 1;
  if (setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))
      == SOCKET_ERROR) {
    setInetError("Can't reuse address");
    return -1;
  }
  if (bind(listenSocket, address.getAddress(), sizeof(sockaddr_in)) == SOCKET_ERROR) {
    setInetError("Can't bind socket");
    closesocket(listenSocket);
    listenSocket = INVALID_SOCKET;
    return -1;
  }
  return 0;
}


/// Destructor
TcpSocket::~TcpSocket() {
  TRACE_CLASS("TcpSocket", "~TcpSocket()");
  close();
}


/**
 *  Receive an telnet packet, i.e a TCP stream ending with carriage return.
 *  @param data The buffer that will receive data.
 *  @param size The buffer size.
 *  @return > 0 if ok, -1 (SOCKET_ERROR) if error
 */
int TcpSocket::telnetRead(void* data, int size)
{
  TRACE_CLASS("TcpSocket", "read(void*, size)");
  int ret;
  
  printf("selectCall\n");
  
  printf("readread 1\n");  
  char *line=GetLine(listenSocket);
  ret=strlen(line);
  printf("readread 2\n");  
  if (ret <= size)
  {
	strcpy((char*)data, line);	
  }
  else
  {
//	size_t n = size;
	strcpy((char*)data, line);
	ret = size;
  }
  printf("TELNET READ returned %d\n", ret);
  return ret;  
}

/**
 *  Receive a TCP stream.
 *  @param data The buffer that will receive data.
 *  @param size The buffer size.
 *  @return > 0 if ok, -1 (SOCKET_ERROR) if error
 */
int TcpSocket::read(void* data, int size)
{
  TRACE_CLASS("TcpSocket", "read(void*, size)");
 
  int ret = recv(listenSocket, (char*)data, size, 0);
  if (ret == SOCKET_ERROR) {
    setInetError("Can't receive TCP packet");
    return -1;
  }
  return ret;  
}


#define MAX 512
char* TcpSocket::GetLine(int fd)
{
  static char line[MAX];
  static char netread[MAX] = "";
  int n, len;
  char *p;

  len = strlen(netread);

  /* look for \r\n in netread buffer */
  p = strstr(netread, "\r\n");
  if (p == NULL) {  
    /* fill buff - no \r\n found */
    //n = ::read(fd, (void*)(netread+len), (size_t)(MAX-len));
    n = recv(fd, (netread+len), MAX-len, 0);
    if (n == SOCKET_ERROR) {
      setInetError("Can't receive TCP packet");
      return NULL;
    }
    len += n;
    netread[len] = '\0';
    if (n>0)
	    return GetLine(fd);
  }
  if (p!=NULL)
  {
	  *p = '\0';
	  strcpy(line, netread);
	  /* copy rest of buf down */
	  memmove(netread, p+2, strlen(p+2)+1);  
  }
  return line;
}


/**
 *  Send an TCP packet.
 *  @param data The buffer taht will be sent.
 *  @param size Number of bytes to send.
 *  return 0 if ok, -1 if error
 */
int TcpSocket::write(const void* data, int size)
{
#ifdef DUMP
  TRACE_CLASS("TcpSocket", "write(const void*, int)");
#endif

  // ignore BROKENPIPE signal (we handle it instead)
//  void* old_sigpipe = signal ( SIGPIPE, SIG_IGN );
  // try to send data
  int ret = send(listenSocket, (const char*)data, size, 0 /*MSG_NOSIGNAL*/	);
  // restore the BROKENPIPE handling
//  signal ( SIGPIPE,  (__sighandler_t)old_sigpipe );
  if (ret == SOCKET_ERROR) {
    setInetError("Can't send TCP packet");
    return -1;
  }
  return ret;
}


int TcpSocket::setDestination(InetAddress &addr)
{
  address = addr;
  int ret = connect(listenSocket, addr.getAddress(), sizeof(*addr.getAddress()));
  // A etre verifier: code de retour differend entre Linux et Windows
  return ret;
}


void TcpSocket::setSocket(SOCKET socket, InetAddress &addr)
{
  if (listenSocket != INVALID_SOCKET)
    closesocket(listenSocket);
  listenSocket = socket;
  address = addr;
}


InetAddress TcpSocket::getAddress()
{
  return address;
}


int TcpSocket::PeekCount()
{
	int count;
	char tempBuffer[3];
	int size=3;
	
	count = recv(listenSocket, tempBuffer, size, MSG_PEEK);
	if (count == -1)
	{
		printf("ERROR WHEN PEEKING SOCKET\n");
	}		
	return count;
}

/*
WSAEINTR
WSAEBADF
WSAEACCES
WSAEFAULT
WSAEINVAL
WSAEMFILE
WSAEWOULDBLOCK
WSAEINPROGRESS
WSAEALREADY
WSAENOTSOCK
WSAEDESTADDRREQ
WSAEMSGSIZE
WSAEPROTOTYPE
WSAENOPROTOOPT
WSAEPROTONOSUPPORT
WSAESOCKTNOSUPPORT
WSAEOPNOTSUPP
WSAEPFNOSUPPORT
WSAEAFNOSUPPORT
WSAEADDRINUSE
WSAEADDRNOTAVAIL
WSAENETDOWN
WSAENETUNREACH
WSAENETRESET
WSAECONNABORTED
WSAECONNRESET
WSAENOBUFS
WSAEISCONN
WSAENOTCONN
WSAESHUTDOWN
WSAETOOMANYREFS
WSAETIMEDOUT
WSAECONNREFUSED
WSAELOOP
WSAENAMETOOLONG
WSAEHOSTDOWN
WSAEHOSTUNREACH
WSAENOTEMPTY
WSAEPROCLIM
WSAEUSERS
WSAEDQUOT
WSAESTALE
WSAEREMOTE
WSAEDISCON
WSASYSNOTREADY
WSAVERNOTSUPPORTED
WSANOTINITIALISED
*/