sipxportlib  Version 3.3
OsSocket.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2006 SIPfoundry Inc.
3 // Licensed by SIPfoundry under the LGPL license.
4 //
5 // Copyright (C) 2004-2006 Pingtel Corp. All rights reserved.
6 // Licensed to SIPfoundry under a Contributor Agreement.
7 //
8 // $$
10 
11 
12 #ifndef _OsSocket_h_
13 #define _OsSocket_h_
14 
15 // SYSTEM INCLUDES
16 #if defined(__pingtel_on_posix__)
17 // To get "struct sockaddr_in".
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #endif
21 #include <stdarg.h>
22 
23 // APPLICATION INCLUDES
24 #include "os/OsDefs.h"
26 #include "utl/UtlString.h"
27 #include "os/OsBSem.h"
28 #include "os/OsAtomics.h"
29 
30 
31 // DEFINES
32 #define MAX_IP_ADDRESSES 32
33 #define MAX_ADAPTER_NAME_LENGTH 256
34 
35 //: constant indentifier indicating the maximum number of IP addresses on this host.
36 #define OS_INVALID_SOCKET_DESCRIPTOR (-1)
37 
38 #if defined(_WIN32)
40 # include "os/wnt/WindowsAdapterInfo.h"
41 # define OsSocketGetERRNO() (WSAGetLastError())
42 # define OS_INVALID_INET_ADDRESS INADDR_NONE // 0xffffffff
43 
44 #elif defined(_VXWORKS)
45 # include <os/Vxw/OsAdapterInfoVxw.h>
46 # define OsSocketGetERRNO() (errno)
47 # define OS_INVALID_INET_ADDRESS 0xffffffff
48 
49 #elif defined(__pingtel_on_posix__)
50 # include "os/linux/AdapterInfo.h"
51 # define OsSocketGetERRNO() (errno)
52 # define OS_INVALID_INET_ADDRESS 0xffffffff
53 
54 #else
55 # error Unsupported target platform.
56 #endif
57 
58 // MACROS
59 // EXTERNAL FUNCTIONS
60 
61 //used to access BindAddress from "C" code
62 extern "C" unsigned long osSocketGetDefaultBindAddress();
63 
64 // EXTERNAL VARIABLES
65 // CONSTANTS
66 // STRUCTS
67 
68 // TYPEDEFS
69 // ENUMS
70 // FORWARD DECLARATIONS
71 
72 //: Abstract Socket class
73 // This class encapsulates the Berkley socket in an object oriented,
74 // platform independent way. The intention is also to be independent of
75 // the protocol over IP as much as is possible. This should enable
76 // generic message transport with minimal knowledge of the underlying
77 // protocol.
78 
80 {
81 /* //////////////////////////// PUBLIC //////////////////////////////////// */
82 public:
83 
85 
87  virtual UtlContainableType getContainableType() const;
92  typedef enum
93  {
94  UNKNOWN = -1,
95  TCP = 0,
96  UDP = 1,
97  MULTICAST = 2,
99  CUSTOM = 4
101  //: Protocol Types
102 
103 /* ============================ CREATORS ================================== */
104  OsSocket();
105  //:Default constructor
106 
107  virtual
108  ~OsSocket();
109  //:Destructor
110 
111 /* ============================ MANIPULATORS ============================== */
112 
113  static UtlBoolean socketInit();
114  static unsigned long initDefaultAdapterID(UtlString &adapter_id);
115 
116 
117  virtual int write(const char* buffer,
118  int bufferLength,
119  const char* ipAddress,
120  int port)
121  {
122  // default implementation ignores ipAddress and port
123  return write(buffer, bufferLength);
124  }
125 
126  virtual int write(const char* buffer, int bufferLength);
127  //:Blocking write to the socket
128  // Write the characters in the given buffer to the socket.
129  // This method will block until all of the bytes are written.
135 
136  virtual int write(const char* buffer, int bufferLength, long waitMilliseconds);
137  //:Non-blocking or limited blocking write to socket
138  // Same as blocking version except that this write will block
139  // for no more than the specified length of time.
141 
142  virtual int read(char* buffer, int bufferLength);
143  //:Blocking read from the socket
144  // Read bytes into the buffer from the socket up to a maximum of
145  // bufferLength bytes. This method will block until there is
146  // something to read from the socket.
150 
151  virtual int read(char* buffer, int bufferLength,
152  UtlString* ipAddress, int* port);
153  //:Blocking read from the socket
154  // Read bytes into the buffer from the socket up to a maximum of
155  // bufferLength bytes. This method will block until there is
156  // something to read from the socket.
162 
163  virtual int read(char* buffer, int bufferLength,
164  struct in_addr* ipAddress, int* port);
165  //:Blocking read from the socket
166  // Read bytes into the buffer from the socket up to a maximum of
167  // bufferLength bytes. This method will block until there is
168  // something to read from the socket.
174 
175  virtual int read(char* buffer, int bufferLength, long waitMilliseconds);
176  //: Non-blocking or limited blocking read from socket
177  // Same as blocking version except that this read will block
178  // for no more than the specified length of time.
180 
181  virtual void close();
182  //: Closes the socket
183 
184  virtual void makeNonblocking();
185  virtual void makeBlocking();
186  //: Make the connect and all subsequent operations blocking
187  // By default the sockets are blocking.
188 
189  static void setDefaultBindAddress(const unsigned long bind_address);
190  //set the default ipaddress the phone should bind to
191 
192 /* ============================ ACCESSORS ================================= */
193 
194  virtual OsSocket::IpProtocolSocketType getIpProtocol() const = 0;
195  //:Return the protocol type of this socket
196 
199 
200  virtual UtlBoolean reconnect() = 0;
201  //:Set up the connection again, assuming the connection failed
202 
203  virtual int getSocketDescriptor() const;
204  //:Return the socket descriptor
205  // Warning: Use of this method risks the creation of platform-dependent
206  // code.
207 
208 
209  static void getDomainName(UtlString &domain_name);
210  //gets static member m_DomainName
211 
212  static unsigned long getDefaultBindAddress();
213  // get default ip address in network byte order
214 
215  static void getHostName(UtlString* hostName);
216  //:Get this host's name
217  // Gets the host name independent of a socket.
218 
219  static void getHostIp(UtlString* hostAddress);
220  //:Get this host's IP address
221 
222 // virtual void getLocalHostName(UtlString* localHostName) const;
223  //:Return this host's name
224  // Returns a string containing the name of the host on which this socket
225  // resides. This may be the local name, a fully qualified domain name or
226  // anything in between. This name may vary on the same host if it is
227  // multi-homed, depending upon which NIC the Socket is associated with.
228 
229  virtual void getLocalHostIp(UtlString* localHostAddress) const;
230  //:Return this host's ip address
231  // Returns the ip address for this host on which this socket is communicating
232  // On multi-homed machines, this is the address to the NIC over which the
233  // socket communicates. The format is of the form: xx.x.xxx.x
234 
235  virtual const UtlString& getLocalIp() const;
236  //:Return this socket's Local Ip Address
237 
238  virtual void setLocalIp(const UtlString& localIp) { mLocalIp = localIp; }
239 
240  virtual int getLocalHostPort() const;
241  //:Return the local port number
242  // Returns the port to which this socket is bound on this host.
243 
244  virtual void getRemoteHostName(UtlString* remoteHostName) const;
245  //:Return remote host name
246  // Returns a string containing the name of the host on which the socket
247  // on the other end of this socket is bound. This may be the local
248  // name, a fully qualified domain name or anything in between.
249 
250 
251  virtual void getRemoteHostIp(struct in_addr* remoteHostAddress,
252  int* remotePort = NULL);
253  //:Return remote host ip address
254  // Returns the ip address for the host on which the socket on the
255  // other end of this socket is bound.
256 
257  virtual void getRemoteHostIp(UtlString* remoteHostAddress,
258  int* remotePort = NULL);
259  //:Return remote host ip address
260  // Returns the ip address for the host on which the socket on the
261  // other end of this socket is bound. The format is of the form:
262  // xx.x.xxx.x
263 
264  virtual int getRemoteHostPort() const;
265  //:Return the remote port number
266  // Returns the port to which the socket on the other end of this socket
267  // is bound.
268 
269 /* ============================ INQUIRY =================================== */
270 
271  virtual UtlBoolean isOk() const;
272  //:Returns TRUE if this socket's descriptor is not the invalid descriptor
273 
274  virtual UtlBoolean isConnected() const;
275  //:Returns TRUE if this socket is connected
276 
277  virtual UtlBoolean isReadyToReadEx(long waitMilliseconds, UtlBoolean &rSocketError) const;
278  //:Poll if there are bytes to read
279  // Returns TRUE if socket is read to read.
280  // Returns FALSE if wait expires or socket error.
281  // rSocketError returns TRUE is socket error occurred.
282 
283  virtual UtlBoolean isReadyToRead(long waitMilliseconds = 0) const;
284  //:Poll if there are bytes to read
285  // Returns TRUE if socket is ready to read.
286  // Returns FALSE if wait expires or socket error.
287 
288  virtual UtlBoolean isReadyToWrite(long waitMilliseconds = 0) const;
289  //:Poll if socket is able to write without blocking
290 
291  static UtlBoolean isIp4Address(const char* address);
292  //:Is the address a dotted IP4 address
293  // (i.e., nnn.nnn.nnn.nnn where 0 <= nnn <= 255)
294 
295  static UtlBoolean isMcastAddr(const char* ipAddress);
296  //:Is the given dotted IP4 address a multicast address
297 
298  static UtlBoolean isLocalHost(const char* hostAddress);
299  //:Is the given host name this host
300 
301  static UtlBoolean isSameHost(const char* host1, const char* host2);
302  //:Are these two host names/addresses equivalent
303 
304  static UtlBoolean getHostIpByName(const char* hostName, UtlString* hostAddress);
305  //:Look up IP address for host name
306 
307  static void inet_ntoa_pt(struct in_addr input_address, UtlString& output_address);
308  //:Convert in_addr input_address to dot ip address to avoid memory leak
309 
311  //:Returns TRUE if the given IpProtocolSocketType is a framed message protocol
312  // (that is, every read returns exactly one message), and so the Content-Length
313  // header may be omitted.
314 
315 /* //////////////////////////// PROTECTED ///////////////////////////////// */
316 protected:
317  static const UtlContainableType TYPE;
318  static OsBSem mInitializeSem;
320 
328 
329 /* //////////////////////////// PRIVATE /////////////////////////////////// */
330 private:
331  static unsigned long m_DefaultBindAddress;
332  //default ip address the phone should bind to. May be IPADDR_ANY
333 
334  static UtlString m_DomainName;
335  //domain name for host machine
336 
337  OsSocket& operator=(const OsSocket& rhs);
338  //:Disable assignment operator
339 
340  OsSocket(const OsSocket& rOsSocket);
341  //:Disable copy constructor
342 
343  static UtlBoolean hasDefaultDnsDomain();
344  //:Returns TRUE if this host has a default DNS domain
345 };
346 
347 /* ============================ INLINE METHODS ============================ */
348 
349 #endif // _OsSocket_h_
static UtlBoolean socketInit()
Definition: OsSocket.cpp:790
static unsigned long getDefaultBindAddress()
Definition: OsSocket.cpp:900
Definition: OsSocket.h:79
Definition: OsSocket.h:99
unsigned long osSocketGetDefaultBindAddress()
Definition: OsSocket.cpp:123
IpProtocolSocketType
Definition: OsSocket.h:92
static UtlBoolean isFramed(IpProtocolSocketType type)
Definition: OsSocket.cpp:1300
static const char * ipProtocolString(OsSocket::IpProtocolSocketType)
return the string representation of the SocketProtocolType
Definition: OsSocket.cpp:735
virtual ~OsSocket()
Definition: OsSocket.cpp:150
virtual int getRemoteHostPort() const
Definition: OsSocket.cpp:1060
virtual int getLocalHostPort() const
Definition: OsSocket.cpp:1148
UtlBoolean mIsConnected
Definition: OsSocket.h:327
UtlString mLocalIp
Definition: OsSocket.h:323
virtual const UtlString & getLocalIp() const
Definition: OsSocket.cpp:1276
virtual int write(const char *buffer, int bufferLength, const char *ipAddress, int port)
Definition: OsSocket.h:117
virtual UtlContainableType getContainableType() const
Determine whether or not the values in a containable are comparable.
Definition: OsSocket.cpp:1324
virtual void getRemoteHostName(UtlString *remoteHostName) const
Definition: OsSocket.cpp:1054
Definition: OsSocket.h:98
virtual void close()
param: waitMilliseconds - The maximum number of milliseconds to block. This may be set to zero in whi...
Definition: OsSocket.cpp:692
static void getHostIp(UtlString *hostAddress)
Definition: OsSocket.cpp:945
virtual void setLocalIp(const UtlString &localIp)
Definition: OsSocket.h:238
virtual UtlBoolean reconnect()=0
Definition: OsSocket.h:96
virtual UtlBoolean isReadyToWrite(long waitMilliseconds=0) const
Definition: OsSocket.cpp:581
virtual void makeBlocking()
Definition: OsSocket.cpp:776
static void setDefaultBindAddress(const unsigned long bind_address)
Definition: OsSocket.cpp:893
virtual UtlBoolean isOk() const
Definition: OsSocket.cpp:1155
static unsigned long initDefaultAdapterID(UtlString &adapter_id)
Definition: OsSocket.cpp:830
#define NULL
Definition: UtlDefs.h:29
virtual int read(char *buffer, int bufferLength)
param: waitMilliseconds - The maximum number of milliseconds to block. This may be set to zero...
Definition: OsSocket.cpp:235
const char *const UtlContainableType
Definition: UtlDefs.h:70
OsAtomicLight< int > OsAtomicLightInt
Definition: OsAtomics.h:353
Definition: OsSocket.h:97
static UtlBoolean isSameHost(const char *host1, const char *host2)
Definition: OsSocket.cpp:1233
static UtlBoolean isLocalHost(const char *hostAddress)
Definition: OsSocket.cpp:1165
OsSocket()
Definition: OsSocket.cpp:131
static UtlBoolean getHostIpByName(const char *hostName, UtlString *hostAddress)
Definition: OsSocket.cpp:1065
static void inet_ntoa_pt(struct in_addr input_address, UtlString &output_address)
Definition: OsSocket.cpp:1282
virtual UtlBoolean isReadyToRead(long waitMilliseconds=0) const
Definition: OsSocket.cpp:574
Definition: UtlString.h:48
virtual void makeNonblocking()
Definition: OsSocket.cpp:762
static UtlBoolean isIp4Address(const char *address)
Definition: OsSocket.cpp:1189
int localHostPort
Definition: OsSocket.h:321
static UtlBoolean socketInitialized
Definition: OsSocket.h:84
Definition: UtlContainableAtomic.h:39
Definition: OsSocket.h:94
static const UtlContainableType TYPE
Class type used for runtime checking.
Definition: OsSocket.h:317
virtual OsSocket::IpProtocolSocketType getIpProtocol() const =0
virtual int getSocketDescriptor() const
Definition: OsSocket.cpp:888
int UtlBoolean
Definition: UtlDefs.h:41
virtual void getRemoteHostIp(struct in_addr *remoteHostAddress, int *remotePort=NULL)
Definition: OsSocket.cpp:1010
Definition: OsSocket.h:95
OsAtomicLightInt socketDescriptor
Definition: OsSocket.h:319
static void getHostName(UtlString *hostName)
Definition: OsSocket.cpp:936
int remoteHostPort
Definition: OsSocket.h:322
virtual UtlBoolean isConnected() const
Definition: OsSocket.cpp:1160
virtual UtlBoolean isReadyToReadEx(long waitMilliseconds, UtlBoolean &rSocketError) const
Definition: OsSocket.cpp:406
static OsBSem mInitializeSem
Definition: OsSocket.h:318
static void getDomainName(UtlString &domain_name)
Definition: OsSocket.cpp:907
UtlString remoteHostName
Definition: OsSocket.h:325
virtual void getLocalHostIp(UtlString *localHostAddress) const
Definition: OsSocket.cpp:1141
static UtlBoolean isMcastAddr(const char *ipAddress)
Definition: OsSocket.cpp:1221
UtlString localHostName
Definition: OsSocket.h:324
UtlString mRemoteIpAddress
Definition: OsSocket.h:326