summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosh Gao2018-07-30 20:49:03 -0500
committerJosh Gao2018-07-31 17:36:55 -0500
commit190624301755d366ccc464b2223eeeec5c7ccd32 (patch)
treec4b7724b174d8fe1540624309c55fb7978b11ad3
parent4c87c1743efecaf992c7d0340041458f78fc9294 (diff)
downloadplatform-system-core-190624301755d366ccc464b2223eeeec5c7ccd32.tar.gz
platform-system-core-190624301755d366ccc464b2223eeeec5c7ccd32.tar.xz
platform-system-core-190624301755d366ccc464b2223eeeec5c7ccd32.zip
adb: move list-forward, kill-forward back into handle_forward_request.
The daemon-side reverse functions depended on handle_forward_request: move them back instead of duplicating the logic we had in handle_host_request. Accomplish what we originally wanted to do in this change by changing the transport argument of handle_forward_request to a std::function that acquires a transport, either via acquire_one_transport or immediately returning a value that we already have. As a side effect, fix a bug where we would emit spurious errors for host service requests. Bug: http://b/112009742 Test: echo "001chost:connect:127.0.0.1:5555" | nc localhost 5037 Test: python test_device.py Test: python test_adb.py Change-Id: Iccc555575df6dbd7de10382854c4ea2c6f4beeaa
-rw-r--r--adb/adb.cpp84
-rw-r--r--adb/adb.h5
-rw-r--r--adb/daemon/services.cpp2
3 files changed, 53 insertions, 38 deletions
diff --git a/adb/adb.cpp b/adb/adb.cpp
index 19300f660..38c6f62c9 100644
--- a/adb/adb.cpp
+++ b/adb/adb.cpp
@@ -920,13 +920,45 @@ int launch_server(const std::string& socket_spec) {
920} 920}
921#endif /* ADB_HOST */ 921#endif /* ADB_HOST */
922 922
923bool handle_forward_request(const char* service, atransport* transport, int reply_fd) {
924 return handle_forward_request(service, [transport](std::string*) { return transport; },
925 reply_fd);
926}
927
923// Try to handle a network forwarding request. 928// Try to handle a network forwarding request.
924// This returns 1 on success, 0 on failure, and -1 to indicate this is not 929bool handle_forward_request(const char* service,
925// a forwarding-related request. 930 std::function<atransport*(std::string* error)> transport_acquirer,
926int handle_forward_request(const char* service, atransport* transport, int reply_fd) { 931 int reply_fd) {
932 if (!strcmp(service, "list-forward")) {
933 // Create the list of forward redirections.
934 std::string listeners = format_listeners();
935#if ADB_HOST
936 SendOkay(reply_fd);
937#endif
938 SendProtocolString(reply_fd, listeners);
939 return true;
940 }
941
942 if (!strcmp(service, "killforward-all")) {
943 remove_all_listeners();
944#if ADB_HOST
945 /* On the host: 1st OKAY is connect, 2nd OKAY is status */
946 SendOkay(reply_fd);
947#endif
948 SendOkay(reply_fd);
949 return true;
950 }
951
927 if (!strncmp(service, "forward:", 8) || !strncmp(service, "killforward:", 12)) { 952 if (!strncmp(service, "forward:", 8) || !strncmp(service, "killforward:", 12)) {
928 // killforward:local 953 // killforward:local
929 // forward:(norebind:)?local;remote 954 // forward:(norebind:)?local;remote
955 std::string error;
956 atransport* transport = transport_acquirer(&error);
957 if (!transport) {
958 SendFail(reply_fd, error);
959 return true;
960 }
961
930 bool kill_forward = false; 962 bool kill_forward = false;
931 bool no_rebind = false; 963 bool no_rebind = false;
932 if (android::base::StartsWith(service, "killforward:")) { 964 if (android::base::StartsWith(service, "killforward:")) {
@@ -946,17 +978,16 @@ int handle_forward_request(const char* service, atransport* transport, int reply
946 // Check killforward: parameter format: '<local>' 978 // Check killforward: parameter format: '<local>'
947 if (pieces.size() != 1 || pieces[0].empty()) { 979 if (pieces.size() != 1 || pieces[0].empty()) {
948 SendFail(reply_fd, android::base::StringPrintf("bad killforward: %s", service)); 980 SendFail(reply_fd, android::base::StringPrintf("bad killforward: %s", service));
949 return 1; 981 return true;
950 } 982 }
951 } else { 983 } else {
952 // Check forward: parameter format: '<local>;<remote>' 984 // Check forward: parameter format: '<local>;<remote>'
953 if (pieces.size() != 2 || pieces[0].empty() || pieces[1].empty() || pieces[1][0] == '*') { 985 if (pieces.size() != 2 || pieces[0].empty() || pieces[1].empty() || pieces[1][0] == '*') {
954 SendFail(reply_fd, android::base::StringPrintf("bad forward: %s", service)); 986 SendFail(reply_fd, android::base::StringPrintf("bad forward: %s", service));
955 return 1; 987 return true;
956 } 988 }
957 } 989 }
958 990
959 std::string error;
960 InstallStatus r; 991 InstallStatus r;
961 int resolved_tcp_port = 0; 992 int resolved_tcp_port = 0;
962 if (kill_forward) { 993 if (kill_forward) {
@@ -977,7 +1008,7 @@ int handle_forward_request(const char* service, atransport* transport, int reply
977 SendProtocolString(reply_fd, android::base::StringPrintf("%d", resolved_tcp_port)); 1008 SendProtocolString(reply_fd, android::base::StringPrintf("%d", resolved_tcp_port));
978 } 1009 }
979 1010
980 return 1; 1011 return true;
981 } 1012 }
982 1013
983 std::string message; 1014 std::string message;
@@ -996,9 +1027,10 @@ int handle_forward_request(const char* service, atransport* transport, int reply
996 break; 1027 break;
997 } 1028 }
998 SendFail(reply_fd, message); 1029 SendFail(reply_fd, message);
999 return 1; 1030 return true;
1000 } 1031 }
1001 return 0; 1032
1033 return false;
1002} 1034}
1003 1035
1004#if ADB_HOST 1036#if ADB_HOST
@@ -1186,35 +1218,15 @@ int handle_host_request(const char* service, TransportType type, const char* ser
1186 return SendOkay(reply_fd, response); 1218 return SendOkay(reply_fd, response);
1187 } 1219 }
1188 1220
1189 if (!strcmp(service, "list-forward")) { 1221 if (handle_forward_request(service,
1190 // Create the list of forward redirections. 1222 [=](std::string* error) {
1191 std::string listeners = format_listeners(); 1223 return acquire_one_transport(type, serial, transport_id, nullptr,
1192#if ADB_HOST 1224 error);
1193 SendOkay(reply_fd); 1225 },
1194#endif 1226 reply_fd)) {
1195 return SendProtocolString(reply_fd, listeners); 1227 return 0;
1196 }
1197
1198 if (!strcmp(service, "killforward-all")) {
1199 remove_all_listeners();
1200#if ADB_HOST
1201 /* On the host: 1st OKAY is connect, 2nd OKAY is status */
1202 SendOkay(reply_fd);
1203#endif
1204 SendOkay(reply_fd);
1205 return 1;
1206 }
1207
1208 std::string error;
1209 atransport* t = acquire_one_transport(type, serial, transport_id, nullptr, &error);
1210 if (!t) {
1211 SendFail(reply_fd, error);
1212 return 1;
1213 } 1228 }
1214 1229
1215 int ret = handle_forward_request(service, t, reply_fd);
1216 if (ret >= 0)
1217 return ret - 1;
1218 return -1; 1230 return -1;
1219} 1231}
1220 1232
diff --git a/adb/adb.h b/adb/adb.h
index 13ca4d7e0..e6af780c4 100644
--- a/adb/adb.h
+++ b/adb/adb.h
@@ -158,7 +158,10 @@ asocket* create_jdwp_tracker_service_socket();
158unique_fd create_jdwp_connection_fd(int jdwp_pid); 158unique_fd create_jdwp_connection_fd(int jdwp_pid);
159#endif 159#endif
160 160
161int handle_forward_request(const char* service, atransport* transport, int reply_fd); 161bool handle_forward_request(const char* service, atransport* transport, int reply_fd);
162bool handle_forward_request(const char* service,
163 std::function<atransport*(std::string* error)> transport_acquirer,
164 int reply_fd);
162 165
163/* packet allocator */ 166/* packet allocator */
164apacket* get_apacket(void); 167apacket* get_apacket(void);
diff --git a/adb/daemon/services.cpp b/adb/daemon/services.cpp
index 25024b05a..1f59d6446 100644
--- a/adb/daemon/services.cpp
+++ b/adb/daemon/services.cpp
@@ -157,7 +157,7 @@ unique_fd reverse_service(const char* command, atransport* transport) {
157 return unique_fd{}; 157 return unique_fd{};
158 } 158 }
159 VLOG(SERVICES) << "service socketpair: " << s[0] << ", " << s[1]; 159 VLOG(SERVICES) << "service socketpair: " << s[0] << ", " << s[1];
160 if (handle_forward_request(command, transport, s[1]) < 0) { 160 if (!handle_forward_request(command, transport, s[1])) {
161 SendFail(s[1], "not a reverse forwarding command"); 161 SendFail(s[1], "not a reverse forwarding command");
162 } 162 }
163 adb_close(s[1]); 163 adb_close(s[1]);