10 #include <apps/tcpip/socket.h> 11 #include <sys/simulation/simulation_controller.h> 20 #include <sys/types.h> 21 #include <sys/socket.h> 22 #include <netinet/in.h> 23 #include <netinet/tcp.h> 24 #include <arpa/inet.h> 37 #define vsnprintf _vsnprintf 54 extern "C" void init_tcpip( shawn::SimulationController& sc )
65 bool Socket::init_windows_sockets_ =
true;
66 bool Socket::windows_sockets_initialized_ =
false;
67 int Socket::instance_count_ = 0;
104 if( init_windows_sockets_ && !windows_sockets_initialized_ )
107 if( WSAStartup(MAKEWORD(1, 1), &wsaData) != 0 )
109 windows_sockets_initialized_ =
true;
137 && init_windows_sockets_ && instance_count_ == 0 )
139 windows_sockets_initialized_ =
false;
150 int e = WSAGetLastError();
151 std::string msg = GetWinsockErrorString( e );
153 std::string msg = strerror( errno );
175 FD_SET( (
unsigned int)sock, &fds );
181 int r = select( sock+1, &fds, NULL, NULL, &tv);
186 if( FD_ISSET( sock, &fds ) )
195 atoaddr( std::string address,
struct in_addr& addr)
197 struct hostent* host;
198 struct in_addr saddr;
201 saddr.s_addr = inet_addr(address.c_str());
202 if (saddr.s_addr != static_cast<unsigned int>(-1))
208 host = gethostbyname(address.c_str());
210 addr = *((
struct in_addr*)host->h_addr_list[0]);
227 struct sockaddr_in client_addr;
229 int addrlen =
sizeof(client_addr);
231 socklen_t addrlen =
sizeof(client_addr);
236 struct sockaddr_in self;
239 server_socket_ =
static_cast<int>(socket( AF_INET, SOCK_STREAM, 0 ));
240 if( server_socket_ < 0 )
251 setsockopt(server_socket_, SOL_SOCKET, SO_REUSEADDR, &reuseaddr,
sizeof(reuseaddr));
256 memset(&
self, 0,
sizeof(
self));
257 self.sin_family = AF_INET;
258 self.sin_port = htons((
unsigned short)
port_);
259 self.sin_addr.s_addr = htonl(INADDR_ANY);
262 if ( bind(server_socket_, (
struct sockaddr*)&
self,
sizeof(
self)) != 0 )
267 if ( listen(server_socket_, 10) == -1 )
279 setsockopt(
socket_, IPPROTO_TCP, TCP_NODELAY, (
const char*)&x,
sizeof(x));
295 if (ioctlsocket(
server_socket_, FIONBIO, &NonBlock) == SOCKET_ERROR)
296 BailOnSocketError(
"tcpip::Socket::set_blocking() Unable to initialize non blocking I/O");
322 memset( (
char*)&address, 0,
sizeof(address) );
323 address.sin_family = AF_INET;
324 address.sin_port = htons((
unsigned short)
port_);
325 address.sin_addr.s_addr = addr.s_addr;
327 socket_ =
static_cast<int>(socket( PF_INET, SOCK_STREAM, 0 ));
331 if( ::
connect(
socket_, (sockaddr
const*)&address,
sizeof(address) ) < 0 )
337 setsockopt(
socket_, IPPROTO_TCP, TCP_NODELAY, (
const char*)&x,
sizeof(x));
363 send(
const std::vector<unsigned char> &buffer)
371 size_t numbytes = buffer.size();
372 unsigned char const *bufPtr = &buffer[0];
373 while( numbytes > 0 )
376 int bytesSent =
::send(
socket_, (
const char*)bufPtr, static_cast<int>(numbytes), 0 );
383 numbytes -= bytesSent;
397 int length =
static_cast<int>(b.size());
404 std::vector<unsigned char> msg;
405 msg.insert(msg.end(), length_storage.
begin(), length_storage.
end());
406 msg.insert(msg.end(), b.begin(), b.end());
418 const int bytesReceived = recv(
socket_, (
char*)buffer, static_cast<int>(len), 0 );
420 const int bytesReceived =
static_cast<int>(recv(
socket_, buffer, len, 0 ));
422 if( bytesReceived == 0 )
423 throw SocketException(
"tcpip::Socket::recvAndCheck @ recv: peer shutdown" );
424 if( bytesReceived < 0 )
427 return static_cast<size_t>(bytesReceived);
440 len -= bytesReceived;
441 buffer += bytesReceived;
454 std::cerr << label <<
" " << buffer.size() <<
" bytes via tcpip::Socket: [";
456 const std::vector<unsigned char>::const_iterator end = buffer.end();
457 for (std::vector<unsigned char>::const_iterator it = buffer.begin(); end != it; ++it)
458 std::cerr <<
" " << static_cast<int>(*it) <<
" ";
459 std::cerr <<
"]" << std::endl;
465 std::vector<unsigned char>
470 std::vector<unsigned char> buffer;
478 buffer.resize(bufSize);
479 const size_t bytesReceived =
recvAndCheck(&buffer[0], bufSize);
481 buffer.resize(bytesReceived);
499 std::vector<unsigned char> buffer(
lengthLen);
504 const int totalLen = length_storage.
readInt();
508 buffer.resize(totalLen);
515 msg.writePacket(&buffer[lengthLen], totalLen - lengthLen);
546 GetWinsockErrorString(
int err)
552 case 0:
return "No error";
553 case WSAEINTR:
return "Interrupted system call";
554 case WSAEBADF:
return "Bad file number";
555 case WSAEACCES:
return "Permission denied";
556 case WSAEFAULT:
return "Bad address";
557 case WSAEINVAL:
return "Invalid argument";
558 case WSAEMFILE:
return "Too many open sockets";
559 case WSAEWOULDBLOCK:
return "Operation would block";
560 case WSAEINPROGRESS:
return "Operation now in progress";
561 case WSAEALREADY:
return "Operation already in progress";
562 case WSAENOTSOCK:
return "Socket operation on non-socket";
563 case WSAEDESTADDRREQ:
return "Destination address required";
564 case WSAEMSGSIZE:
return "Message too long";
565 case WSAEPROTOTYPE:
return "Protocol wrong type for socket";
566 case WSAENOPROTOOPT:
return "Bad protocol option";
567 case WSAEPROTONOSUPPORT:
return "Protocol not supported";
568 case WSAESOCKTNOSUPPORT:
return "Socket type not supported";
569 case WSAEOPNOTSUPP:
return "Operation not supported on socket";
570 case WSAEPFNOSUPPORT:
return "Protocol family not supported";
571 case WSAEAFNOSUPPORT:
return "Address family not supported";
572 case WSAEADDRINUSE:
return "Address already in use";
573 case WSAEADDRNOTAVAIL:
return "Can't assign requested address";
574 case WSAENETDOWN:
return "Network is down";
575 case WSAENETUNREACH:
return "Network is unreachable";
576 case WSAENETRESET:
return "Net Socket reset";
577 case WSAECONNABORTED:
return "Software caused tcpip::Socket abort";
578 case WSAECONNRESET:
return "Socket reset by peer";
579 case WSAENOBUFS:
return "No buffer space available";
580 case WSAEISCONN:
return "Socket is already connected";
581 case WSAENOTCONN:
return "Socket is not connected";
582 case WSAESHUTDOWN:
return "Can't send after socket shutdown";
583 case WSAETOOMANYREFS:
return "Too many references, can't splice";
584 case WSAETIMEDOUT:
return "Socket timed out";
585 case WSAECONNREFUSED:
return "Socket refused";
586 case WSAELOOP:
return "Too many levels of symbolic links";
587 case WSAENAMETOOLONG:
return "File name too long";
588 case WSAEHOSTDOWN:
return "Host is down";
589 case WSAEHOSTUNREACH:
return "No route to host";
590 case WSAENOTEMPTY:
return "Directory not empty";
591 case WSAEPROCLIM:
return "Too many processes";
592 case WSAEUSERS:
return "Too many users";
593 case WSAEDQUOT:
return "Disc quota exceeded";
594 case WSAESTALE:
return "Stale NFS file handle";
595 case WSAEREMOTE:
return "Too many levels of remote in path";
596 case WSASYSNOTREADY:
return "Network system is unavailable";
597 case WSAVERNOTSUPPORTED:
return "Winsock version out of range";
598 case WSANOTINITIALISED:
return "WSAStartup not yet called";
599 case WSAEDISCON:
return "Graceful shutdown in progress";
600 case WSAHOST_NOT_FOUND:
return "Host not found";
601 case WSANO_DATA:
return "No host data of that type was found";
611 #endif // BUILD_TCPIP
bool receiveExact(Storage &)
Receive a complete TraCI message from Socket::socket_.
void printBufferOnVerbose(const std::vector< unsigned char > buffer, const std::string &label) const
Print label and buffer to stderr if Socket::verbose_ is set.
std::vector< unsigned char > receive(int bufSize=2048)
Receive up to bufSize available bytes from Socket::socket_.
void accept()
Wait for a incoming connection to port_.
static const int lengthLen
Length of the message length part of a TraCI message.
Socket(std::string host, int port)
Constructor that prepare to connect to host:port.
virtual void writeInt(int)
void connect()
Connects to host_:port_.
bool datawaiting(int sock) const
bool atoaddr(std::string, struct in_addr &addr)
StorageType::const_iterator end() const
void BailOnSocketError(std::string) const
size_t recvAndCheck(unsigned char *const buffer, std::size_t len) const
Receive up to len available bytes from Socket::socket_.
void send(const std::vector< unsigned char > &buffer)
void sendExact(const Storage &)
void receiveComplete(unsigned char *const buffer, std::size_t len) const
Receive len bytes from Socket::socket_.
StorageType::const_iterator begin() const
bool has_client_connection() const