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
41 using namespace std;
42
sendAndDisplayCommand(asio::ip::tcp::socket & socket,CRequestMessage & requestMessage)43 bool sendAndDisplayCommand(asio::ip::tcp::socket &socket, CRequestMessage &requestMessage)
44 {
45 string strError;
46
47 if (requestMessage.serialize(Socket(socket), true, strError) != CRequestMessage::success) {
48
49 cerr << "Unable to send command to target: " << strError << endl;
50 return false;
51 }
52
53 ///// Get answer
54 CAnswerMessage answerMessage;
55 if (answerMessage.serialize(Socket(socket), false, strError) != CRequestMessage::success) {
56
57 cerr << "Unable to received answer from target: " << strError << endl;
58 return false;
59 }
60
61 // Success?
62 if (!answerMessage.success()) {
63
64 // Display error answer
65 cerr << answerMessage.getAnswer() << endl;
66 return false;
67 }
68
69 // Display success answer
70 cout << answerMessage.getAnswer() << endl;
71
72 return true;
73 }
74
75 // hostname port command [argument[s]]
76 // or
77 // hostname port < commands
main(int argc,char * argv[])78 int main(int argc, char *argv[])
79 {
80 // Enough args?
81 if (argc < 4) {
82
83 cerr << "Missing arguments" << endl;
84 cerr << "Usage: " << endl;
85 cerr << "Send a single command:" << endl;
86 cerr << "\t" << argv[0] << " hostname port command [argument[s]]" << endl;
87
88 return 1;
89 }
90 using asio::ip::tcp;
91 asio::io_service io_service;
92 tcp::resolver resolver(io_service);
93
94 tcp::socket connectionSocket(io_service);
95
96 string host{argv[1]};
97 string port{argv[2]};
98 try {
99 asio::connect(connectionSocket, resolver.resolve(tcp::resolver::query(host, port)));
100 } catch (const asio::system_error &e) {
101 cerr << "Connection to '" << host << ":" << port << "' failed: " << e.what() << endl;
102 return 1;
103 }
104
105 // Create command message
106 CRequestMessage requestMessage(argv[3]);
107
108 // Add arguments
109 for (int arg = 4; arg < argc; arg++) {
110
111 requestMessage.addArgument(argv[arg]);
112 }
113
114 if (!sendAndDisplayCommand(connectionSocket, requestMessage)) {
115 return 1;
116 }
117
118 // Program status
119 return 0;
120 }
121