xref: /titanic_41/usr/src/cmd/lms/ProtocolCompat.cpp (revision a62774df315360f02521d6470eab7d5080137dad)
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 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34 
35 #if defined(__sun) || defined(_LINUX)
36 #include <arpa/inet.h>
37 #include <netinet/in.h>
38 #else
39 #include <winsock2.h>
40 #endif	// __sun || _LINUX
41 
42 #include <cerrno>
43 #include "Protocol.h"
44 #include "LMS_if_compat.h"
45 #include "Lock.h"
46 #include "ATNetworkTool.h"
47 
48 void Protocol::_LmeReceiveCompat(char *buffer, unsigned int len, int *status)
49 {
50 	int error = 0;
51 
52 	PRINT("[Compat]HECI receive %d bytes (msg type 0x%02x)\n", len, buffer[0]);
53 	*status = 0;
54 
55 	switch (buffer[0]) {
56 	case LMS_MESSAGE_TYPE_OPEN_CONNECTION_EX:
57 		{
58 			SOCKET s_new = INVALID_SOCKET;
59 			LMS_OPEN_CONNECTION_EX_MESSAGE *msg =
60 			    (LMS_OPEN_CONNECTION_EX_MESSAGE *)buffer;
61 
62 			int type;
63 			switch (msg->Protocol) {
64 			case LMS_PROTOCOL_TYPE_UDP_IPV4:
65 				type = SOCK_DGRAM;
66 				break;
67 			case LMS_PROTOCOL_TYPE_TCP_IPV4:
68 			default:
69 				type = SOCK_STREAM;
70 				break;
71 			}
72 
73 			if ((msg->Flags & HOSTNAME_BIT) != 0) {
74 				PRINT("[Compat]Got client connection request %d for host %s, port %d\n",
75 					msg->ConnectionId,
76 					msg->Host,
77 					ntohs(msg->HostPort));
78 
79 				s_new = ATNetworkTool::Connect(
80 					(const char *)msg->Host,
81 					ntohs(msg->HostPort),
82 					error, PF_INET, type);
83 			} else {
84 				PRINT("[Compat]Got client connection request %d for IP %s, port %d\n",
85 					msg->ConnectionId,
86 					inet_ntoa(*((struct in_addr *)msg->Host)),
87 					ntohs(msg->HostPort));
88 
89 				s_new = ATNetworkTool::Connect(
90 					inet_ntoa(*((struct in_addr *)msg->Host)),
91 					ntohs(msg->HostPort),
92 					error, PF_INET, type);
93 			}
94 
95 			if (s_new == INVALID_SOCKET) {
96 				*status = 1;
97 				break;
98 			}
99 
100 			Channel *c = new Channel(NULL, s_new);
101 			c->SetRecipientChannel(msg->ConnectionId);
102 			c->SetStatus(Channel::OPEN);
103 			c->AddBytesTxWindow(1024);
104 			{
105 				Lock l(_channelsLock);
106 				_openChannels[msg->ConnectionId] = c;
107 			}
108 
109 			_signalSelect();
110 		}
111 		break;
112 
113 	case LMS_MESSAGE_TYPE_CLOSE_CONNECTION:
114 		{
115 			LMS_CLOSE_CONNECTION_MESSAGE *msg =
116 			    (LMS_CLOSE_CONNECTION_MESSAGE *)buffer;
117 
118 			PRINT("[Compat]received close connection msg from HECI for connection %d\n", msg->ConnectionId);
119 
120 			Lock l(_channelsLock);
121 
122 			ChannelMap::iterator it = _openChannels.find(msg->ConnectionId);
123 			if (it != _openChannels.end()) {
124 				_closeMChannel(it->second);
125 				_openChannels.erase(it);
126 			}
127 		}
128 		break;
129 
130 	case LMS_MESSAGE_TYPE_SEND_DATA:
131 		{
132 			LMS_SEND_DATA_MESSAGE *msg =
133 			    (LMS_SEND_DATA_MESSAGE *)buffer;
134 
135 			Lock l(_channelsLock);
136 
137 			ChannelMap::iterator it = _openChannels.find(msg->ConnectionId);
138 			if (it != _openChannels.end()) {
139 				PRINT("[Compat]sending %d bytes from HECI connection %d to socket %d\n", ntohs(msg->DataLength), msg->ConnectionId, it->second->GetSocket());
140 				if (-1 == _send(it->second->GetSocket(), (char *)msg->Data, ntohs(msg->DataLength), error)) {
141 					if (EPIPE == error) {
142 						_closeMChannel(it->second);
143 						_openChannels.erase(it);
144 						*status = 1;
145 					}
146 				}
147 			}
148 		}
149 		break;
150 
151 	case LMS_MESSAGE_TYPE_IP_FQDN:
152 		if (_updateIPFQDN((const char *)((LMS_IP_FQDN_MESSAGE *)buffer)->FQDN) != 0) {
153 			ERROR("[Compat]Error: failed to update IP/FQDN info\n");
154 		}
155 		break;
156 
157 	default:
158 		*status = 1;
159 		break;
160 	}
161 }
162 
163