• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2011-2014, Intel Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors
16  * may be used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <asio.hpp>
32 
33 #include <iostream>
34 #include <string>
35 #include <cstring>
36 #include <stdlib.h>
37 #include "RequestMessage.h"
38 #include "AnswerMessage.h"
39 #include "Socket.h"
40 #include "convert.hpp"
41 
42 using namespace std;
43 
sendAndDisplayCommand(asio::generic::stream_protocol::socket & socket,CRequestMessage & requestMessage)44 bool sendAndDisplayCommand(asio::generic::stream_protocol::socket &socket,
45                            CRequestMessage &requestMessage)
46 {
47     string strError;
48 
49     if (requestMessage.serialize(Socket(socket), true, strError) != CRequestMessage::success) {
50 
51         cerr << "Unable to send command to target: " << strError << endl;
52         return false;
53     }
54 
55     ///// Get answer
56     CAnswerMessage answerMessage;
57     if (answerMessage.serialize(Socket(socket), false, strError) != CRequestMessage::success) {
58 
59         cerr << "Unable to received answer from target: " << strError << endl;
60         return false;
61     }
62 
63     // Success?
64     if (!answerMessage.success()) {
65 
66         // Display error answer
67         cerr << answerMessage.getAnswer() << endl;
68         return false;
69     }
70 
71     // Display success answer
72     cout << answerMessage.getAnswer() << endl;
73 
74     return true;
75 }
76 
usage(const std::string & command,const std::string & error)77 int usage(const std::string &command, const std::string &error)
78 {
79     if (not error.empty()) {
80         cerr << error << endl;
81     }
82     cerr << "Usage: " << endl;
83     cerr << "Send a single command:" << endl;
84     cerr << "\t" << command
85          << " <hostname port|tcp://[host]:port|unix://path> <command> [argument[s]]" << endl;
86 
87     return 1;
88 }
89 
main(int argc,char * argv[])90 int main(int argc, char *argv[])
91 {
92     int commandPos;
93 
94     // Enough args?
95     if (argc < 3) {
96         return usage(argv[0], "Missing arguments");
97     }
98     asio::io_service io_service;
99     asio::generic::stream_protocol::socket connectionSocket(io_service);
100 
101     bool isInet = false;
102     string port;
103     string host;
104     try {
105         // backward compatibility: tcp port only refered by its value
106         uint16_t testConverter;
107         if (convertTo({argv[2]}, testConverter)) {
108             isInet = true;
109             port = argv[2];
110             host = argv[1];
111             if (argc <= 3) {
112                 return usage(argv[0], "Missing arguments");
113             }
114             commandPos = 3;
115         } else {
116             commandPos = 2;
117             string endPortArg{argv[1]};
118             std::string protocol;
119 
120             const std::string tcpProtocol{"tcp"};
121             const std::string unixProtocol{"unix"};
122             const std::vector<std::string> supportedProtocols{tcpProtocol, unixProtocol};
123             const std::string protocolDelimiter{"://"};
124 
125             size_t protocolDelPos = endPortArg.find(protocolDelimiter);
126             if (protocolDelPos == std::string::npos) {
127                 return usage(argv[0], "Invalid endpoint " + endPortArg);
128             }
129             protocol = endPortArg.substr(0, protocolDelPos);
130 
131             if (std::find(begin(supportedProtocols), end(supportedProtocols), protocol) ==
132                 end(supportedProtocols)) {
133                 return usage(argv[0], "Invalid endpoint " + endPortArg);
134             }
135             isInet = (endPortArg.find(tcpProtocol) != std::string::npos);
136             if (isInet) {
137                 size_t portDelPos = endPortArg.rfind(':');
138                 if (portDelPos == std::string::npos) {
139                     return usage(argv[0], "Invalid endpoint " + endPortArg);
140                 }
141                 host = endPortArg.substr(protocolDelPos + protocolDelimiter.size(),
142                                          portDelPos - (protocolDelPos + protocolDelimiter.size()));
143                 port = endPortArg.substr(portDelPos + 1);
144             } else {
145                 port = endPortArg.substr(protocolDelPos + protocolDelimiter.size());
146             }
147         }
148         if (isInet) {
149             asio::ip::tcp::resolver resolver(io_service);
150             asio::ip::tcp::socket tcpSocket(io_service);
151 
152             asio::connect(tcpSocket, resolver.resolve(asio::ip::tcp::resolver::query(host, port)));
153             connectionSocket = std::move(tcpSocket);
154         } else {
155             asio::generic::stream_protocol::socket socket(io_service);
156             asio::generic::stream_protocol::endpoint endpoint =
157                 asio::local::stream_protocol::endpoint(port);
158             socket.connect(endpoint);
159             connectionSocket = std::move(socket);
160         }
161 
162     } catch (const asio::system_error &e) {
163         string endpoint;
164 
165         if (isInet) {
166             endpoint = string("tcp://") + host + ":" + port;
167         } else { /* other supported protocols */
168             endpoint = argv[1];
169         }
170         cerr << "Connection to '" << endpoint << "' failed: " << e.what() << endl;
171         return 1;
172     }
173 
174     // Create command message
175     CRequestMessage requestMessage(argv[commandPos]);
176 
177     // Add arguments
178     for (int arg = commandPos + 1; arg < argc; arg++) {
179 
180         requestMessage.addArgument(argv[arg]);
181     }
182 
183     if (!sendAndDisplayCommand(connectionSocket, requestMessage)) {
184         return 1;
185     }
186 
187     // Program status
188     return 0;
189 }
190