xref: /titanic_41/usr/src/cmd/lms/PortForwardRequest.h (revision 1816cb7076d3ec8a78ef9ac9f895574e13c43645)
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 _PORT_FORWARD_REQUEST_H_
32 #define _PORT_FORWARD_REQUEST_H_
33 
34 #include <string>
35 
36 #if defined(__sun) || defined(_LINUX)
37 #ifndef SOCKET
38 #define SOCKET int
39 #endif
40 #endif
41 
42 typedef int (*IsConnectionPermittedCallback)(void * const param, SOCKET s);
43 
44 class PortForwardRequest
45 {
46 public:
47 	enum PORT_FORWARD_REQUEST_STATUS {
48 		NOT_ACTIVE,
49 		PENDING_REQUEST,
50 		LISTENING
51 	};
52 
53 	PortForwardRequest(std::string bindedAddress, int port,
54 			   SOCKET listeningSocket,
55 			   IsConnectionPermittedCallback cb, bool isLocal) :
56 		_bindedAddress(bindedAddress),
57 		_port(port),
58 		_local(isLocal),
59 		_listeningSocket(listeningSocket),
60 		_cb(cb),
61 		_status(NOT_ACTIVE),
62 		_channelCount(0) {}
63 
64 	const std::string GetBindedAddress() const { return _bindedAddress; }
65 	const unsigned int GetPort() const { return _port; }
66 	SOCKET GetListeningSocket() const { return _listeningSocket; }
67 
68 	int IsConnectionPermitted(void *param, SOCKET s)
69 	{
70 		if (_cb != NULL) {
71 			return _cb(param, s);
72 		} else {
73 			return -1;
74 		}
75 	}
76 
77 	PORT_FORWARD_REQUEST_STATUS GetStatus() { return _status; }
78 	bool IsLocal() { return _local; }
79 	bool SetStatus(PORT_FORWARD_REQUEST_STATUS newStatus) { _status = newStatus; return true; }
80 	unsigned int GetChannelCount() { return _channelCount; }
81 	unsigned int IncreaseChannelCount() { return ++_channelCount; }
82 
83 	unsigned int DecreaseChannelCount()
84 	{
85 		if (_channelCount > 0) {
86 			--_channelCount;
87 		}
88 		return _channelCount;
89 	}
90 
91 
92 private:
93 	const std::string _bindedAddress;
94 	const unsigned int _port;
95 	const bool _local;
96 	const SOCKET _listeningSocket;
97 	const IsConnectionPermittedCallback _cb;
98 	PORT_FORWARD_REQUEST_STATUS _status;
99 	unsigned int _channelCount;
100 };
101 
102 #endif
103