PEP 55 Update license on source files to current license text and date
[tpot/pegasus/.git] / src / Pegasus / Common / pegasus_socket.h
1 //%2003////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001, 2002  BMC Software, Hewlett-Packard Development
4 // Company, L. P., IBM Corp., The Open Group, Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L. P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 // 
15 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
16 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
17 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
18 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
19 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24 //==============================================================================
25 //
26 // Author: Mike Day (mdday@us.ibm.com)
27 //
28 // Modified By:
29 //
30 //%/////////////////////////////////////////////////////////////////////////////
31
32
33 #ifndef peg_socket_class_heirarchy_h
34 #define peg_socket_class_heirarchy_h
35
36 #include <Pegasus/Common/Config.h>
37 #include <Pegasus/Common/IPC.h>
38 #include <Pegasus/Common/TLS.h>
39 #ifdef PEGASUS_OS_TYPE_WINDOWS
40 #include <windows.h>
41 # ifndef _WINSOCKAPI_
42 #   include <winsock2.h>
43 # endif
44 #else
45 # include <cctype>
46 #ifndef PEGASUS_OS_OS400
47 //#   include <unistd.h>
48 #else
49 #   include <Pegasus/Common/OS400ConvertChar.h>
50 #   include <unistd.cleinc>
51 #endif
52 #ifdef PEGASUS_OS_ZOS
53 #   include <string.h>  // added by rk for memcpy
54 #endif
55 # include <cstdlib>
56 # include <errno.h>
57 # include <fcntl.h>
58 # include <netdb.h>
59 # include <netinet/in.h>
60 # include <arpa/inet.h>
61 # include <sys/socket.h>
62 #endif
63 #ifdef PEGASUS_OS_ZOS
64 #   include <Pegasus/Common/SocketzOS_inline.h>
65 #endif
66 #include <Pegasus/Common/Linkage.h>
67
68 PEGASUS_NAMESPACE_BEGIN
69
70 #if defined(PEGASUS_PLATFORM_ZOS_ZSERIES_IBM)
71 typedef size_t PEGASUS_SOCKLEN_SIZE;
72
73 #elif defined(PEGASUS_PLATFORM_AIX_RS_IBMCXX) || defined(PEGASUS_PLATFORM_LINUX_GENERIC_GNU) || (defined(PEGASUS_OS_SOLARIS) && !defined(SUNOS_5_6))
74 typedef socklen_t PEGASUS_SOCKLEN_SIZE;
75 #else
76 typedef int PEGASUS_SOCKLEN_SIZE;
77 #endif
78
79
80
81
82 //  <<< Thu Jul  3 13:50:29 2003 mdd >>> pep_88
83 /*****************************************************************
84  *
85  *  The socket support in pegasus is schizophrenic. Some code uses 
86  *  an Sint32 (fd) as a socket, while other code uses a pointer to an 
87  *  MP_Socket, which is kind of a container for either an Sint32 socket 
88  *  or an SSL socket. 
89  *
90  *  Then there is also the local socket. (AF_UNIX). 
91  *
92  *  What we need to make all of this coherent is a general-purpose
93  *  socket class that uses polymorphism to provide a good sockets
94  *  interface.
95  *  Because of what we are planning for the pep_88 connection management
96  *  code this general-purpose socket class should be reference counted.
97  *
98  *****************************************************************/ 
99
100 class abstract_socket;
101 class socket_factory;
102
103
104 class PEGASUS_COMMON_LINKAGE pegasus_socket 
105 {
106
107 public:
108   
109   pegasus_socket(void);
110   pegasus_socket(socket_factory *);
111   pegasus_socket(abstract_socket *);
112   pegasus_socket(const pegasus_socket& s);
113   ~pegasus_socket(void);
114       
115   pegasus_socket& operator=(const pegasus_socket& s);
116   Boolean operator==(const pegasus_socket& s);
117   
118   operator Sint32() const;
119
120   int socket(int type, int style, int protocol, void *ssl_context = 0);
121                 
122   Sint32 read(void* ptr, Uint32 size);
123   Sint32 write(const void* ptr, Uint32 size);
124   int close(void);
125   int enableBlocking(void);
126   int disableBlocking(void);
127
128   int getsockname (struct sockaddr *addr, PEGASUS_SOCKLEN_SIZE *length_ptr);
129   int bind (struct sockaddr *addr, PEGASUS_SOCKLEN_SIZE length);
130      
131   // change size_t to size_t for ZOS and windows
132   pegasus_socket accept(struct sockaddr *addr, PEGASUS_SOCKLEN_SIZE *length_ptr);
133   int connect (struct sockaddr *addr, PEGASUS_SOCKLEN_SIZE length);
134   int shutdown(int how);
135   int listen(int q);
136   int getpeername (struct sockaddr *addr, PEGASUS_SOCKLEN_SIZE *length_ptr);
137   int send (void *buffer, PEGASUS_SOCKLEN_SIZE size, int flags);
138   int recv (void *buffer, PEGASUS_SOCKLEN_SIZE size, int flags);
139   int sendto(void *buffer, PEGASUS_SOCKLEN_SIZE size, int flags, struct sockaddr *addr, PEGASUS_SOCKLEN_SIZE length);
140   int recvfrom(void *buffer, PEGASUS_SOCKLEN_SIZE size, int flags, struct sockaddr *addr, PEGASUS_SOCKLEN_SIZE *length_ptr);
141   int setsockopt (int level, int optname, void *optval, PEGASUS_SOCKLEN_SIZE optlen);
142   int getsockopt (int level, int optname, void *optval, PEGASUS_SOCKLEN_SIZE *optlen_ptr);
143
144
145   Boolean incompleteReadOccurred(Sint32 retCode);
146   Boolean is_secure(void);
147     void set_close_on_exec(void);
148       
149   const char* get_err_string(void);
150   
151 private:
152
153   abstract_socket * _rep;
154
155 };
156
157
158 class PEGASUS_COMMON_LINKAGE socket_factory 
159 {
160 public:
161   socket_factory(void)
162   {
163   }
164       
165   virtual ~socket_factory(void)
166   {
167   }
168       
169   virtual abstract_socket *make_socket(void) = 0;
170 #ifdef PEGASUS_HAS_SSL
171   virtual abstract_socket *make_socket(SSLContext *) = 0;
172 #endif
173 };
174
175
176
177 /**
178  *  factory class for creating the bsd socket object 
179  **/
180 class PEGASUS_COMMON_LINKAGE bsd_socket_factory : public socket_factory
181 {
182 public:
183   bsd_socket_factory(void);
184   ~bsd_socket_factory(void);
185   abstract_socket *make_socket(void);
186 #ifdef PEGASUS_HAS_SSL
187   abstract_socket *make_socket(SSLContext* );
188 #endif
189 };
190
191
192
193 #ifdef PEGASUS_HAS_SSL
194
195 class PEGASUS_COMMON_LINKAGE ssl_socket_factory : public socket_factory
196 {
197
198 public:
199   ssl_socket_factory(void);
200   ~ssl_socket_factory(void);
201   abstract_socket* make_socket(void);
202   abstract_socket* make_socket(SSLContext* );
203 };
204
205
206
207 #endif 
208
209
210 # ifdef PEGASUS_LOCAL_DOMAIN_SOCKET
211 // << Thu Aug 14 15:01:30 2003 mdd >> domain sockets work 
212 class PEGASUS_COMMON_LINKAGE unix_socket_factory : public socket_factory
213 {
214 public:
215   unix_socket_factory(void);
216   ~unix_socket_factory(void);
217   abstract_socket* make_socket(void);
218 #ifdef PEGASUS_HAS_SSL
219   abstract_socket *make_socket(SSLContext*);  // Do not use
220 #endif
221 };
222
223
224 #endif
225
226 PEGASUS_NAMESPACE_END
227
228 #endif /* peg_socket_class_heirarchy_h */