/*
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 .
*/
#include "TcpSocket.h"
#include
#include
#include
#include
#include
#ifdef _WIN32
#else
# include
#endif
#ifdef TRACE_ON
# ifndef TRACE_CLASS
# define TRACE_CLASS(class, func) cout <<"-" <<(class) <<"\t(" < 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
*/