1*617e2443SMark Logan /*******************************************************************************
2*617e2443SMark Logan * Copyright (C) 2004-2008 Intel Corp. All rights reserved.
3*617e2443SMark Logan *
4*617e2443SMark Logan * Redistribution and use in source and binary forms, with or without
5*617e2443SMark Logan * modification, are permitted provided that the following conditions are met:
6*617e2443SMark Logan *
7*617e2443SMark Logan * - Redistributions of source code must retain the above copyright notice,
8*617e2443SMark Logan * this list of conditions and the following disclaimer.
9*617e2443SMark Logan *
10*617e2443SMark Logan * - Redistributions in binary form must reproduce the above copyright notice,
11*617e2443SMark Logan * this list of conditions and the following disclaimer in the documentation
12*617e2443SMark Logan * and/or other materials provided with the distribution.
13*617e2443SMark Logan *
14*617e2443SMark Logan * - Neither the name of Intel Corp. nor the names of its
15*617e2443SMark Logan * contributors may be used to endorse or promote products derived from this
16*617e2443SMark Logan * software without specific prior written permission.
17*617e2443SMark Logan *
18*617e2443SMark Logan * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
19*617e2443SMark Logan * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*617e2443SMark Logan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*617e2443SMark Logan * ARE DISCLAIMED. IN NO EVENT SHALL Intel Corp. OR THE CONTRIBUTORS
22*617e2443SMark Logan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*617e2443SMark Logan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*617e2443SMark Logan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*617e2443SMark Logan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*617e2443SMark Logan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*617e2443SMark Logan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*617e2443SMark Logan * POSSIBILITY OF SUCH DAMAGE.
29*617e2443SMark Logan *******************************************************************************/
30*617e2443SMark Logan
31*617e2443SMark Logan #ifdef HAVE_CONFIG_H
32*617e2443SMark Logan #include "config.h"
33*617e2443SMark Logan #endif
34*617e2443SMark Logan
35*617e2443SMark Logan #if defined(__sun) || defined(_LINUX)
36*617e2443SMark Logan #include <arpa/inet.h>
37*617e2443SMark Logan #include <netinet/in.h>
38*617e2443SMark Logan #else
39*617e2443SMark Logan #include <winsock2.h>
40*617e2443SMark Logan #endif // __sun || _LINUX
41*617e2443SMark Logan
42*617e2443SMark Logan #include <cerrno>
43*617e2443SMark Logan #include "Protocol.h"
44*617e2443SMark Logan #include "LMS_if_compat.h"
45*617e2443SMark Logan #include "Lock.h"
46*617e2443SMark Logan #include "ATNetworkTool.h"
47*617e2443SMark Logan
_LmeReceiveCompat(char * buffer,unsigned int len,int * status)48*617e2443SMark Logan void Protocol::_LmeReceiveCompat(char *buffer, unsigned int len, int *status)
49*617e2443SMark Logan {
50*617e2443SMark Logan int error = 0;
51*617e2443SMark Logan
52*617e2443SMark Logan PRINT("[Compat]HECI receive %d bytes (msg type 0x%02x)\n", len, buffer[0]);
53*617e2443SMark Logan *status = 0;
54*617e2443SMark Logan
55*617e2443SMark Logan switch (buffer[0]) {
56*617e2443SMark Logan case LMS_MESSAGE_TYPE_OPEN_CONNECTION_EX:
57*617e2443SMark Logan {
58*617e2443SMark Logan SOCKET s_new = INVALID_SOCKET;
59*617e2443SMark Logan LMS_OPEN_CONNECTION_EX_MESSAGE *msg =
60*617e2443SMark Logan (LMS_OPEN_CONNECTION_EX_MESSAGE *)buffer;
61*617e2443SMark Logan
62*617e2443SMark Logan int type;
63*617e2443SMark Logan switch (msg->Protocol) {
64*617e2443SMark Logan case LMS_PROTOCOL_TYPE_UDP_IPV4:
65*617e2443SMark Logan type = SOCK_DGRAM;
66*617e2443SMark Logan break;
67*617e2443SMark Logan case LMS_PROTOCOL_TYPE_TCP_IPV4:
68*617e2443SMark Logan default:
69*617e2443SMark Logan type = SOCK_STREAM;
70*617e2443SMark Logan break;
71*617e2443SMark Logan }
72*617e2443SMark Logan
73*617e2443SMark Logan if ((msg->Flags & HOSTNAME_BIT) != 0) {
74*617e2443SMark Logan PRINT("[Compat]Got client connection request %d for host %s, port %d\n",
75*617e2443SMark Logan msg->ConnectionId,
76*617e2443SMark Logan msg->Host,
77*617e2443SMark Logan ntohs(msg->HostPort));
78*617e2443SMark Logan
79*617e2443SMark Logan s_new = ATNetworkTool::Connect(
80*617e2443SMark Logan (const char *)msg->Host,
81*617e2443SMark Logan ntohs(msg->HostPort),
82*617e2443SMark Logan error, PF_INET, type);
83*617e2443SMark Logan } else {
84*617e2443SMark Logan PRINT("[Compat]Got client connection request %d for IP %s, port %d\n",
85*617e2443SMark Logan msg->ConnectionId,
86*617e2443SMark Logan inet_ntoa(*((struct in_addr *)msg->Host)),
87*617e2443SMark Logan ntohs(msg->HostPort));
88*617e2443SMark Logan
89*617e2443SMark Logan s_new = ATNetworkTool::Connect(
90*617e2443SMark Logan inet_ntoa(*((struct in_addr *)msg->Host)),
91*617e2443SMark Logan ntohs(msg->HostPort),
92*617e2443SMark Logan error, PF_INET, type);
93*617e2443SMark Logan }
94*617e2443SMark Logan
95*617e2443SMark Logan if (s_new == INVALID_SOCKET) {
96*617e2443SMark Logan *status = 1;
97*617e2443SMark Logan break;
98*617e2443SMark Logan }
99*617e2443SMark Logan
100*617e2443SMark Logan Channel *c = new Channel(NULL, s_new);
101*617e2443SMark Logan c->SetRecipientChannel(msg->ConnectionId);
102*617e2443SMark Logan c->SetStatus(Channel::OPEN);
103*617e2443SMark Logan c->AddBytesTxWindow(1024);
104*617e2443SMark Logan {
105*617e2443SMark Logan Lock l(_channelsLock);
106*617e2443SMark Logan _openChannels[msg->ConnectionId] = c;
107*617e2443SMark Logan }
108*617e2443SMark Logan
109*617e2443SMark Logan _signalSelect();
110*617e2443SMark Logan }
111*617e2443SMark Logan break;
112*617e2443SMark Logan
113*617e2443SMark Logan case LMS_MESSAGE_TYPE_CLOSE_CONNECTION:
114*617e2443SMark Logan {
115*617e2443SMark Logan LMS_CLOSE_CONNECTION_MESSAGE *msg =
116*617e2443SMark Logan (LMS_CLOSE_CONNECTION_MESSAGE *)buffer;
117*617e2443SMark Logan
118*617e2443SMark Logan PRINT("[Compat]received close connection msg from HECI for connection %d\n", msg->ConnectionId);
119*617e2443SMark Logan
120*617e2443SMark Logan Lock l(_channelsLock);
121*617e2443SMark Logan
122*617e2443SMark Logan ChannelMap::iterator it = _openChannels.find(msg->ConnectionId);
123*617e2443SMark Logan if (it != _openChannels.end()) {
124*617e2443SMark Logan _closeMChannel(it->second);
125*617e2443SMark Logan _openChannels.erase(it);
126*617e2443SMark Logan }
127*617e2443SMark Logan }
128*617e2443SMark Logan break;
129*617e2443SMark Logan
130*617e2443SMark Logan case LMS_MESSAGE_TYPE_SEND_DATA:
131*617e2443SMark Logan {
132*617e2443SMark Logan LMS_SEND_DATA_MESSAGE *msg =
133*617e2443SMark Logan (LMS_SEND_DATA_MESSAGE *)buffer;
134*617e2443SMark Logan
135*617e2443SMark Logan Lock l(_channelsLock);
136*617e2443SMark Logan
137*617e2443SMark Logan ChannelMap::iterator it = _openChannels.find(msg->ConnectionId);
138*617e2443SMark Logan if (it != _openChannels.end()) {
139*617e2443SMark Logan PRINT("[Compat]sending %d bytes from HECI connection %d to socket %d\n", ntohs(msg->DataLength), msg->ConnectionId, it->second->GetSocket());
140*617e2443SMark Logan if (-1 == _send(it->second->GetSocket(), (char *)msg->Data, ntohs(msg->DataLength), error)) {
141*617e2443SMark Logan if (EPIPE == error) {
142*617e2443SMark Logan _closeMChannel(it->second);
143*617e2443SMark Logan _openChannels.erase(it);
144*617e2443SMark Logan *status = 1;
145*617e2443SMark Logan }
146*617e2443SMark Logan }
147*617e2443SMark Logan }
148*617e2443SMark Logan }
149*617e2443SMark Logan break;
150*617e2443SMark Logan
151*617e2443SMark Logan case LMS_MESSAGE_TYPE_IP_FQDN:
152*617e2443SMark Logan if (_updateIPFQDN((const char *)((LMS_IP_FQDN_MESSAGE *)buffer)->FQDN) != 0) {
153*617e2443SMark Logan ERROR("[Compat]Error: failed to update IP/FQDN info\n");
154*617e2443SMark Logan }
155*617e2443SMark Logan break;
156*617e2443SMark Logan
157*617e2443SMark Logan default:
158*617e2443SMark Logan *status = 1;
159*617e2443SMark Logan break;
160*617e2443SMark Logan }
161*617e2443SMark Logan }
162*617e2443SMark Logan
163