617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan/*******************************************************************************
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * Copyright (C) 2004-2008 Intel Corp. All rights reserved.
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan *
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * Redistribution and use in source and binary forms, with or without
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * modification, are permitted provided that the following conditions are met:
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan *
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * - Redistributions of source code must retain the above copyright notice,
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * this list of conditions and the following disclaimer.
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan *
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * - Redistributions in binary form must reproduce the above copyright notice,
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * this list of conditions and the following disclaimer in the documentation
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * and/or other materials provided with the distribution.
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan *
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * - Neither the name of Intel Corp. nor the names of its
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * contributors may be used to endorse or promote products derived from this
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * software without specific prior written permission.
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan *
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * ARE DISCLAIMED. IN NO EVENT SHALL Intel Corp. OR THE CONTRIBUTORS
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan * POSSIBILITY OF SUCH DAMAGE.
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan *******************************************************************************/
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#ifndef _PROTOCOL_H_
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#define _PROTOCOL_H_
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#include <map>
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#include <vector>
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#include <set>
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#include <string>
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#include "types.h"
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#include "LMS_if.h"
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#include "LMEConnection.h"
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#include "PortForwardRequest.h"
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#include "Channel.h"
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#include "Semaphore.h"
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#if defined(__sun) || defined(_LINUX)
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#define SOCKET int
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#define INVALID_SOCKET (SOCKET)(~0)
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#define SOCKET_ERROR (-1)
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#else
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#include <windows.h>
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#endif // __sun || _LINUX
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#ifdef _REMOTE_SUPPORT
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#include "ConfigConnection.h"
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#endif
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#define FQDN_MAX_SIZE 256
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logantypedef void (*EventLogCallback)(void *param, LPCTSTR message, WORD eventType);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Loganclass Protocol
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan{
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Loganpublic:
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan static const LMEProtocolVersionMessage MIN_PROT_VERSION;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan static const LMEProtocolVersionMessage MAX_PROT_VERSION;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan enum SOCKET_STATUS {
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan ACTIVE = 0,
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan NOT_CREATED,
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan LINGER_ERROR,
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan NOT_BINDED,
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan NOT_EXCLUSIVE_ADDRESS,
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan NOT_LISTENED
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan };
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan Protocol();
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan ~Protocol();
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan bool CreateSockets();
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan void DestroySockets();
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan bool SocketsCreated() { return _sockets_active; }
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan bool IsDeInitialized();
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan bool IsInitialized();
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan bool Init(EventLogCallback cb, void *param);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan int Select();
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan void Deinit();
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan void DeinitFull();
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan bool oldProtocolMode;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Loganprivate:
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan static void _LmeCallback(void *param, void *buffer, unsigned int len, int *status);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan static int _isLocalCallback(void *const param, SOCKET s);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#ifdef _REMOTE_SUPPORT
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan static int _isRemoteCallback(void *const param, SOCKET s);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#endif
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan static char *_getErrMsg(DWORD err);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan bool _checkProtocolFlow(LMEMessage *message);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan unsigned int _getMinMessageLen(LMEMessage *message);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan unsigned int _getMinGlobalMsgLen(LMEGlobalRequestMessage *globalMessage);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan bool _checkMessageAndProtocol(LMEMessage *message, unsigned int len);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan void _closePortForwardRequest(PortForwardRequest *p);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan void _apfGlobalRequest(LMEGlobalRequestMessage *globalMessage, unsigned int len, int *status);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan void _apfTcpForwardRequest(LMETcpForwardRequestMessage *tcpFwdReqMsg, int *status);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan void _apfTcpForwardCancel(LMETcpForwardCancelRequestMessage *tcpFwdCnclMsg);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan void _aptSendUdp(LMEUdpSendToMessage *udpSendToMessage, int *status);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan void _apfProtocolVersion(LMEProtocolVersionMessage *verMsg);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan void _apfChannelOpen(LMEChannelOpenRequestMessage *chOpenMsg, int *status);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan PortForwardRequest *_closeMChannel(Channel *c);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan PortForwardRequest *_apfChannelOFail(LMEChannelOpenReplayFailureMessage *chFailMsg);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan PortForwardRequest *_apfChannelClose(LMEChannelCloseMessage *chClMsg);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan PortForwardRequest *_apfChannelData(LMEChannelDataMessage *chDMsg, int *status);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan void _LmeReceive(void *buffer, unsigned int len, int *status);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan void _LmeReceiveCompat(char *buffer, unsigned int len, int *status);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan void _signalSelect();
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan bool _acceptConnection(SOCKET s, unsigned int port);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan int _rxFromSocket(SOCKET s);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan int _handleFQDNChange(const char *fqdn);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan int _updateIPFQDN(const char *fqdn);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#ifdef _REMOTE_SUPPORT
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan static bool _compareDNSSuffix(std::string AMTDNSSuffix, std::string suffix);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan int _isRemote(SOCKET s) const;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan bool _checkRemoteSupport(bool requestDnsFromAmt = false);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan void _updateEnterpriseAccessStatus(const ATDomainMap &localDNSSuffixes);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#endif
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan ssize_t _send(int s, const void *buf, size_t len, int &senderr);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan bool _checkListen(std::string address, in_port_t port, int &socket);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan int _listenPort(in_port_t port, int &error);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan bool _localListen(in_port_t port);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan Channel *_getSockOpenChannel(SOCKET s);
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan struct Connection {
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan SOCKET s;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan };
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan typedef std::vector<PortForwardRequest *> PortForwardRequestList;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan typedef std::map<unsigned int, PortForwardRequestList> PortMap;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan typedef std::map<unsigned int, Channel *> ChannelMap;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan LMEConnection _lme;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan char *_rxSocketBuffer;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan unsigned int _rxSocketBufferSize;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#ifdef _REMOTE_SUPPORT
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan ConfigConnection _cfg;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#endif
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan SOCKET _serverSignalSocket;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan SOCKET _clientSignalSocket; // Used to notify Select() to check new available channels
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan bool _sockets_active;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan PortMap _openPorts;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan ChannelMap _openChannels;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan Semaphore _portsLock;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan Semaphore _channelsLock;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#ifdef _REMOTE_SUPPORT
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan std::list<std::string> _AMTDNSSuffixes;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan mutable Semaphore _AMTDNSLock;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan bool _remoteAccessEnabled;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan mutable Semaphore _remoteAccessLock;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#endif
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan enum VERSION_HANDSHAKING {
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan NOT_INITIATED,
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan INITIATED,
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan AGREED
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan };
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan enum SERVICE_STATUS {
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan NOT_STARTED,
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan STARTED
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan };
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan VERSION_HANDSHAKING _handshakingStatus;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan SERVICE_STATUS _pfwdService;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan LMEProtocolVersionMessage _AmtProtVersion;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan Semaphore _versionLock;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan char _AMTFQDN[FQDN_MAX_SIZE];
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan EventLogCallback _eventLog;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan void *_eventLogParam;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan bool _deinitReq;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan Semaphore _deinitLock;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan typedef std::set<unsigned int> listenPortSet;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan listenPortSet _listenFailReported;
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan};
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan
617e2443dfc17fe44fd44c0675d6aad2ffc9df42Mark Logan#endif