1 //####COPYRIGHTBEGIN####
3 // ----------------------------------------------------------------------------
4 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
6 // This program is part of the eCos host tools.
8 // This program is free software; you can redistribute it and/or modify it
9 // under the terms of the GNU General Public License as published by the Free
10 // Software Foundation; either version 2 of the License, or (at your option)
13 // This program is distributed in the hope that it will be useful, but WITHOUT
14 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 // You should have received a copy of the GNU General Public License along with
19 // this program; if not, write to the Free Software Foundation, Inc.,
20 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 // ----------------------------------------------------------------------------
24 //####COPYRIGHTEND####
25 //=================================================================
31 //=================================================================
32 //=================================================================
33 //#####DESCRIPTIONBEGIN####
38 // Description: This class abstracts tcp/ip sockets for use in the testing infrastructure
41 //####DESCRIPTIONEND####
42 //=================================================================
43 // This class is a host-independent interface to a TCP/IP socket
44 // There are two flavours of socket - server and client.
45 // Server sockets listen (accept) on a socket number. Client sockets connect to a host:port.
46 // The class can be used thus:
48 // CeCosSocket sock; // no-argument ctor
49 // if(-1!=sock.Listen(6000)){
54 // if(sock.Connect(_T("ginga"),5000)){
57 // In each of the above cases the socket is closed automatically by the dtor.
59 // Alternatively, the ctor can be used directly:
61 // CeCosSocket sock(6000));
64 // CeCosSocket sock(_T("ginga"),5000));
67 //=================================================================
69 #include "Collections.h"
71 #ifndef _SOCKETUTILS_H
72 #define _SOCKETUTILS_H
78 static const String GetHostByName(LPCTSTR pszHost);
80 // These functions must be called before any other operation is carried out:
84 typedef bool (CALLBACK FilterFunc)(void *&,unsigned int &,CeCosSerial&,CeCosSocket &,void *);
86 // A function that causes an operation to stop - i.e. it when it returns true the operation is aborted.
87 typedef bool (CALLBACK StopFunc)(void *);
89 enum {NOTIMEOUT=0x7fffffff-1,DEFAULTTIMEOUT=-2}; // No explicit timeout specified
91 // Listen and this form of constructor used to act as server
92 static int Listen(int nTcpPort);
93 CeCosSocket (); // Caller promises to call Accept() or Connect() later
95 // Accept-like ctor (act as server)
96 CeCosSocket (int sock /*result of previous call of Listen*/, bool *pbStop=0);
97 // Connect-like ctor (act as client)
98 CeCosSocket (LPCTSTR pszHostPort,Duration dTimeout=NOTIMEOUT);
100 bool Accept(int sock /*result of previous call of Listen*/, bool *pbStop=0);
101 // This form of constructor used to act as client
102 bool Connect(LPCTSTR pszHostPort,Duration dTimeout=NOTIMEOUT);
105 int Client() const { return m_nClient; }
106 static String ClientName(int nClient);
108 int Sock() const { return m_nSock; }
110 // Set the default timeout for all operations
111 void SetTimeout (Duration dTimeout) { m_nDefaultTimeout=dTimeout; }
113 // Use to test success after opening with the ctor:
114 bool Ok() { return -1!=m_nSock; }
116 // Close the given socket
117 bool Close () { return CloseSocket(m_nSock); }
119 // Return last error on this socket
120 int SocketError() { return m_nErr; }
122 // Return last socket error, translated to a string
123 String SocketErrString();
124 static String SocketErrString(int nErr);
126 // Read and write functions
128 // Untyped: these versions allow the operation to be aborted either by timeout or by the "stop func" returning true.
129 bool send(const void *pData,unsigned int nLength,LPCTSTR pszMsg=_T(""),int dTimeout=DEFAULTTIMEOUT,StopFunc *pFunc=0,void *pParam=0){
130 return sendrecv(true,pData,nLength,pszMsg,dTimeout,pFunc,pParam);
132 bool recv(const void *pData,unsigned int nLength,LPCTSTR pszMsg=_T(""),int dTimeout=DEFAULTTIMEOUT,StopFunc *pFunc=0,void *pParam=0){
133 return sendrecv(false,pData,nLength,pszMsg,dTimeout,pFunc,pParam);
136 // Read/write an integer (this can be used between machines of different endianness)
137 bool recvInteger (int &n,LPCTSTR pszMsg=_T(""),Duration dTimeout=DEFAULTTIMEOUT);
138 bool sendInteger (int n,LPCTSTR pszMsg=_T(""),Duration dTimeout=DEFAULTTIMEOUT);
140 // Read/write a string
141 bool recvString (String &str,LPCTSTR pszMsg=_T(""),Duration dTimeout=DEFAULTTIMEOUT);
142 bool sendString (const String &str,LPCTSTR pszMsg=_T(""),Duration dTimeout=DEFAULTTIMEOUT);
144 static bool CloseSocket (int &sock);
145 bool Peek (unsigned int &nAvail);
147 // Miscellaneous helper functions:
149 // Combine string and integer to the form host:port:
150 static String HostPort(LPCTSTR pszHost,int nPort);
151 // Decompose (opposite of the above):
152 static bool ParseHostPort (LPCTSTR pszHostPort, String &pszHost, int &nPort);
153 // Just check for legality:
154 static bool IsLegalHostPort (LPCTSTR pszHostPort);
155 // Are these two hosts really the same?
156 static bool SameHost (LPCTSTR host1,LPCTSTR host2);
157 // Set up a connection between a serial port and a socket. Traffic is simply passed between them.
158 static bool ConnectSocketToSerial (int nListenSock,LPCTSTR pszPort, int nBaud,FilterFunc *pSerialToSocketFilterFunc=0,void *pSerialParam=0,FilterFunc *pSocketToSerialFilterFunc=0,void *pSocketParam=0,bool *pbStop=0);
159 static bool ConnectSocketToSerial (CeCosSocket &socket,CeCosSerial &serial,FilterFunc *pSerialToSocketFilterFunc=0,void *pSerialParam=0, FilterFunc *pSocketToSerialFilterFunc=0,void *pSocketParam=0,bool *pbStop=0);
161 static LPCTSTR MyHostName();
162 static LPCTSTR MySimpleHostName();
164 // Set up a connection between two sockets. Traffic is simply passed between them.
165 bool ConnectSocketToSocket (CeCosSocket &o,FilterFunc *pSocketToSocketFilterFunc1,FilterFunc *pSocketToSocketFilterFunc2,void *pParam,bool *pbStop);
167 enum SSReadResult {SS_SOCKET_ERROR=-1,SS_SOCKET_READ=1,SS_SERIAL_ERROR=-2,SS_SERIAL_READ=2,SS_STOPPED=0};
171 // Blocking read on one or other of the data sources:
172 // Result: -1 - socket error occurred
173 // 1 - data read from socket
174 // -2 - serial error occurred
175 // 2 - data read from serial
177 static SSReadResult SSRead (CeCosSerial &serial,CeCosSocket &socket,void *pBuf,unsigned int nSize,unsigned int &nRead,bool *pbStop);
179 Duration m_nDefaultTimeout;
180 Duration TimeoutDuration (Duration dTimeout);
181 // Set appropriate socket options (most importantly, non-blocking mode)
182 bool SetSocketOptions ();
188 m_nErr=WSAGetLastError();
193 bool sendrecv(bool bSend,const void *pData,unsigned int nLength,LPCTSTR pszMsg=_T(""),int dTimeout=DEFAULTTIMEOUT,StopFunc *pFunc=0,void *pParam=0);