1 /*
2  * Copyright (C) 2022 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 #include "io_vsock.h"
17 
18 #include <android-base/file.h>
19 #include <android-base/logging.h>
20 #include <android-base/result.h>
21 #include <android-base/unique_fd.h>
22 #include <linux/vm_sockets.h>
23 #include <sys/socket.h>
24 
25 using namespace android::base;
26 
27 namespace io_vsock {
init_vsock_server(unsigned int port)28 Result<int> init_vsock_server(unsigned int port) {
29     int server_fd(TEMP_FAILURE_RETRY(socket(AF_VSOCK, SOCK_STREAM | SOCK_CLOEXEC, 0)));
30     if (server_fd < 0) {
31         return Error() << "VM:cannot create socket";
32     }
33     struct sockaddr_vm server_sa = (struct sockaddr_vm){
34             .svm_family = AF_VSOCK,
35             .svm_port = port,
36             .svm_cid = VMADDR_CID_ANY,
37     };
38     LOG(INFO) << "VM:Connecting on port " << port << "...";
39     int ret = TEMP_FAILURE_RETRY(bind(server_fd, (struct sockaddr *)&server_sa, sizeof(server_sa)));
40     if (ret < 0) {
41         return Error() << "VM:cannot bind an address with the socket";
42     }
43     ret = TEMP_FAILURE_RETRY(listen(server_fd, /*backlog=*/1));
44     if (ret < 0) {
45         return Error() << "VM:cannot listen to port";
46     }
47     LOG(INFO) << "Server now listening";
48     return server_fd;
49 }
50 
run_vsock_server_and_receive_data(int server_fd,int num_bytes_to_receive)51 Result<void> run_vsock_server_and_receive_data(int server_fd, int num_bytes_to_receive) {
52     LOG(INFO) << "Accepting connection...";
53     struct sockaddr_vm client_sa;
54     socklen_t client_sa_len = sizeof(client_sa);
55     unique_fd client_fd(TEMP_FAILURE_RETRY(
56             accept4(server_fd, (struct sockaddr *)&client_sa, &client_sa_len, SOCK_CLOEXEC)));
57     if (client_fd < 0) {
58         return Error() << "Cannot retrieve connect requests";
59     }
60     LOG(INFO) << "VM:Connection from CID " << client_sa.svm_cid << " on port "
61               << client_sa.svm_port;
62 
63     ssize_t total = 0;
64     char buf[4096];
65     for (;;) {
66         ssize_t n = TEMP_FAILURE_RETRY(read(client_fd.get(), buf, sizeof(buf)));
67         if (n < 0) {
68             return Error() << "Cannot get data from the host.";
69         }
70         if (n == 0) {
71             break;
72         }
73         total += n;
74     }
75 
76     if (total != num_bytes_to_receive) {
77         return Error() << "Received data length(" << total << ") is not equal to "
78                        << num_bytes_to_receive;
79     }
80     LOG(INFO) << "VM:Finished reading data.";
81     return {};
82 }
83 } // namespace io_vsock
84