1 /*
2  * Copyright 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "VtsDriverCommUtil"
18 
19 #include "VtsDriverCommUtil.h"
20 
21 #include <errno.h>
22 #include <netdb.h>
23 #include <netinet/in.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <sstream>
27 
28 #include <android-base/logging.h>
29 
30 #include "test/vts/proto/VtsDriverControlMessage.pb.h"
31 
32 using namespace std;
33 
34 #define MAX_HEADER_BUFFER_SIZE 128
35 
36 namespace android {
37 namespace vts {
38 
Connect(const string & socket_name)39 bool VtsDriverCommUtil::Connect(const string& socket_name) {
40   struct sockaddr_un serv_addr;
41   struct hostent* server;
42 
43   LOG(DEBUG) << "Connect socket: " << socket_name;
44   sockfd_ = socket(PF_UNIX, SOCK_STREAM, 0);
45   if (sockfd_ < 0) {
46     LOG(ERROR) << "ERROR opening socket.";
47     return false;
48   }
49 
50   server = gethostbyname("127.0.0.1");
51   if (server == NULL) {
52     LOG(ERROR) << "ERROR can't resolve the host name, 127.0.0.1";
53     return false;
54   }
55 
56   bzero((char*)&serv_addr, sizeof(serv_addr));
57   serv_addr.sun_family = AF_UNIX;
58   strcpy(serv_addr.sun_path, socket_name.c_str());
59 
60   if (connect(sockfd_, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
61     LOG(ERROR) << "ERROR connecting to " << socket_name << " errno = " << errno
62                << " " << strerror(errno);
63     sockfd_ = -1;
64     return false;
65   }
66   return true;
67 }
68 
Close()69 int VtsDriverCommUtil::Close() {
70   int result = 0;
71   if (sockfd_ != -1) {
72     result = close(sockfd_);
73     if (result != 0) {
74       LOG(ERROR) << "ERROR closing socket (errno = " << errno << ")";
75     }
76 
77     sockfd_ = -1;
78   }
79 
80   return result;
81 }
82 
VtsSocketSendBytes(const string & message)83 bool VtsDriverCommUtil::VtsSocketSendBytes(const string& message) {
84   if (sockfd_ == -1) {
85     LOG(ERROR) << "ERROR sockfd not set.";
86     return false;
87   }
88   stringstream header;
89   header << message.length() << "\n";
90   LOG(DEBUG) << "[agent->driver] len = " << message.length();
91   int n = write(sockfd_, header.str().c_str(), header.str().length());
92   if (n < 0) {
93     LOG(ERROR) << " ERROR writing to socket.";
94     return false;
95   }
96 
97   int bytes_sent = 0;
98   int msg_len = message.length();
99   while (bytes_sent < msg_len) {
100     n = write(sockfd_, &message.c_str()[bytes_sent], msg_len - bytes_sent);
101     if (n <= 0) {
102       LOG(ERROR) << "ERROR writing to socket.";
103       return false;
104     }
105     bytes_sent += n;
106   }
107   return true;
108 }
109 
VtsSocketRecvBytes()110 string VtsDriverCommUtil::VtsSocketRecvBytes() {
111   if (sockfd_ == -1) {
112     LOG(ERROR) << "ERROR sockfd not set.";
113     return string();
114   }
115 
116   int header_index = 0;
117   char header_buffer[MAX_HEADER_BUFFER_SIZE];
118 
119   for (header_index = 0; header_index < MAX_HEADER_BUFFER_SIZE;
120        header_index++) {
121     int ret = read(sockfd_, &header_buffer[header_index], 1);
122     if (ret != 1) {
123       int errno_save = errno;
124       LOG(DEBUG) << "ERROR reading the length ret = " << ret
125                  << " sockfd = " << sockfd_ << " "
126                  << " errno = " << errno_save << " " << strerror(errno_save);
127       return string();
128     }
129     if (header_buffer[header_index] == '\n' ||
130         header_buffer[header_index] == '\r') {
131       header_buffer[header_index] = '\0';
132       break;
133     }
134   }
135 
136   int msg_len = atoi(header_buffer);
137   char* msg = (char*)malloc(msg_len + 1);
138   if (!msg) {
139     LOG(ERROR) << "ERROR malloc failed.";
140     return string();
141   }
142 
143   int bytes_read = 0;
144   while (bytes_read < msg_len) {
145     int result = read(sockfd_, &msg[bytes_read], msg_len - bytes_read);
146     if (result <= 0) {
147       LOG(ERROR) << "ERROR read failed.";
148       return string();
149     }
150     bytes_read += result;
151   }
152   msg[msg_len] = '\0';
153   return string(msg, msg_len);
154 }
155 
VtsSocketSendMessage(const google::protobuf::Message & message)156 bool VtsDriverCommUtil::VtsSocketSendMessage(
157     const google::protobuf::Message& message) {
158   if (sockfd_ == -1) {
159     LOG(ERROR) << "ERROR sockfd not set.";
160     return false;
161   }
162 
163   string message_string;
164   if (!message.SerializeToString(&message_string)) {
165     LOG(ERROR) << "ERROR can't serialize the message to a string.";
166     return false;
167   }
168   return VtsSocketSendBytes(message_string);
169 }
170 
VtsSocketRecvMessage(google::protobuf::Message * message)171 bool VtsDriverCommUtil::VtsSocketRecvMessage(
172     google::protobuf::Message* message) {
173   if (sockfd_ == -1) {
174     LOG(ERROR) << "ERROR sockfd not set.";
175     return false;
176   }
177 
178   string message_string = VtsSocketRecvBytes();
179   if (message_string.length() == 0) {
180     LOG(DEBUG) << "ERROR message string zero length.";
181     return false;
182   }
183 
184   return message->ParseFromString(message_string);
185 }
186 
187 }  // namespace vts
188 }  // namespace android
189