xref: /titanic_41/usr/src/cmd/lms/Protocol.h (revision 617e2443dfc17fe44fd44c0675d6aad2ffc9df42)
1 /*******************************************************************************
2  * Copyright (C) 2004-2008 Intel Corp. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *  - Redistributions of source code must retain the above copyright notice,
8  *    this list of conditions and the following disclaimer.
9  *
10  *  - Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  *
14  *  - Neither the name of Intel Corp. nor the names of its
15  *    contributors may be used to endorse or promote products derived from this
16  *    software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL Intel Corp. OR THE CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *******************************************************************************/
30 
31 #ifndef _PROTOCOL_H_
32 #define _PROTOCOL_H_
33 
34 #include <map>
35 #include <vector>
36 #include <set>
37 #include <string>
38 #include "types.h"
39 #include "LMS_if.h"
40 #include "LMEConnection.h"
41 #include "PortForwardRequest.h"
42 #include "Channel.h"
43 #include "Semaphore.h"
44 
45 #if defined(__sun) || defined(_LINUX)
46 
47 #define SOCKET int
48 #define INVALID_SOCKET  (SOCKET)(~0)
49 #define SOCKET_ERROR            (-1)
50 #else
51 #include <windows.h>
52 #endif	// __sun || _LINUX
53 
54 #ifdef _REMOTE_SUPPORT
55 #include "ConfigConnection.h"
56 #endif
57 
58 #define FQDN_MAX_SIZE 256
59 
60 typedef void (*EventLogCallback)(void *param, LPCTSTR message, WORD eventType);
61 
62 class Protocol
63 {
64 public:
65 
66 	static const LMEProtocolVersionMessage MIN_PROT_VERSION;
67 	static const LMEProtocolVersionMessage MAX_PROT_VERSION;
68 
69 	enum SOCKET_STATUS {
70 		ACTIVE = 0,
71 		NOT_CREATED,
72 		LINGER_ERROR,
73 		NOT_BINDED,
74 		NOT_EXCLUSIVE_ADDRESS,
75 		NOT_LISTENED
76 	};
77 
78 	Protocol();
79 	~Protocol();
80 
81 	bool CreateSockets();
82 	void DestroySockets();
SocketsCreated()83 	bool SocketsCreated() { return _sockets_active; }
84 	bool IsDeInitialized();
85 	bool IsInitialized();
86 	bool Init(EventLogCallback cb, void *param);
87 	int Select();
88 	void Deinit();
89 	void DeinitFull();
90 
91 	bool oldProtocolMode;
92 
93 private:
94 	static void _LmeCallback(void *param, void *buffer, unsigned int len, int *status);
95 	static int _isLocalCallback(void *const param, SOCKET s);
96 #ifdef _REMOTE_SUPPORT
97 	static int _isRemoteCallback(void *const param, SOCKET s);
98 #endif
99 	static char *_getErrMsg(DWORD err);
100 
101 	bool _checkProtocolFlow(LMEMessage *message);
102 	unsigned int _getMinMessageLen(LMEMessage *message);
103 	unsigned int _getMinGlobalMsgLen(LMEGlobalRequestMessage *globalMessage);
104 	bool _checkMessageAndProtocol(LMEMessage *message, unsigned int len);
105 	void _closePortForwardRequest(PortForwardRequest *p);
106 	void _apfGlobalRequest(LMEGlobalRequestMessage *globalMessage, unsigned int len, int *status);
107 	void _apfTcpForwardRequest(LMETcpForwardRequestMessage *tcpFwdReqMsg, int *status);
108 	void _apfTcpForwardCancel(LMETcpForwardCancelRequestMessage *tcpFwdCnclMsg);
109 	void _aptSendUdp(LMEUdpSendToMessage *udpSendToMessage, int *status);
110 	void _apfProtocolVersion(LMEProtocolVersionMessage *verMsg);
111 	void _apfChannelOpen(LMEChannelOpenRequestMessage *chOpenMsg, int *status);
112 	PortForwardRequest *_closeMChannel(Channel *c);
113 	PortForwardRequest *_apfChannelOFail(LMEChannelOpenReplayFailureMessage *chFailMsg);
114 	PortForwardRequest *_apfChannelClose(LMEChannelCloseMessage *chClMsg);
115 	PortForwardRequest *_apfChannelData(LMEChannelDataMessage *chDMsg, int *status);
116 	void _LmeReceive(void *buffer, unsigned int len, int *status);
117 	void _LmeReceiveCompat(char *buffer, unsigned int len, int *status);
118 	void _signalSelect();
119 	bool _acceptConnection(SOCKET s, unsigned int port);
120 	int _rxFromSocket(SOCKET s);
121 	int _handleFQDNChange(const char *fqdn);
122 	int _updateIPFQDN(const char *fqdn);
123 #ifdef _REMOTE_SUPPORT
124 	static bool _compareDNSSuffix(std::string AMTDNSSuffix, std::string suffix);
125 	int _isRemote(SOCKET s) const;
126 	bool _checkRemoteSupport(bool requestDnsFromAmt = false);
127 	void _updateEnterpriseAccessStatus(const ATDomainMap &localDNSSuffixes);
128 #endif
129 	ssize_t _send(int s, const void *buf, size_t len, int &senderr);
130 	bool _checkListen(std::string address, in_port_t port, int &socket);
131 	int _listenPort(in_port_t port, int &error);
132 	bool _localListen(in_port_t port);
133 	Channel *_getSockOpenChannel(SOCKET s);
134 
135 
136 	struct Connection {
137 		SOCKET s;
138 	};
139 
140 	typedef std::vector<PortForwardRequest *> PortForwardRequestList;
141 	typedef std::map<unsigned int, PortForwardRequestList> PortMap;
142 	typedef std::map<unsigned int, Channel *> ChannelMap;
143 
144 	LMEConnection _lme;
145 	char *_rxSocketBuffer;
146 	unsigned int _rxSocketBufferSize;
147 #ifdef _REMOTE_SUPPORT
148 	ConfigConnection _cfg;
149 #endif
150 	SOCKET _serverSignalSocket;
151 	SOCKET _clientSignalSocket; // Used to notify Select() to check new available channels
152 	bool _sockets_active;
153 	PortMap _openPorts;
154 	ChannelMap _openChannels;
155 	Semaphore _portsLock;
156 	Semaphore _channelsLock;
157 #ifdef _REMOTE_SUPPORT
158 	std::list<std::string> _AMTDNSSuffixes;
159 	mutable Semaphore _AMTDNSLock;
160 	bool _remoteAccessEnabled;
161 	mutable Semaphore _remoteAccessLock;
162 #endif
163 
164 	enum VERSION_HANDSHAKING {
165 		NOT_INITIATED,
166 		INITIATED,
167 		AGREED
168 	};
169 
170 	enum SERVICE_STATUS {
171 		NOT_STARTED,
172 		STARTED
173 	};
174 
175 	VERSION_HANDSHAKING _handshakingStatus;
176 	SERVICE_STATUS _pfwdService;
177 	LMEProtocolVersionMessage _AmtProtVersion;
178 	Semaphore _versionLock;
179 
180 	char _AMTFQDN[FQDN_MAX_SIZE];
181 	EventLogCallback _eventLog;
182 	void *_eventLogParam;
183 
184 	bool _deinitReq;
185 	Semaphore _deinitLock;
186 
187 	typedef std::set<unsigned int> listenPortSet;
188 	listenPortSet _listenFailReported;
189 };
190 
191 #endif
192