• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 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 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <errno.h>
21 
22 #include "sysdeps.h"
23 #include <sys/types.h>
24 
25 #define  TRACE_TAG  TRACE_TRANSPORT
26 #include "adb.h"
27 
28 #ifdef __ppc__
29 #define H4(x)	(((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24)
fix_endians(apacket * p)30 static inline void fix_endians(apacket *p)
31 {
32     p->msg.command     = H4(p->msg.command);
33     p->msg.arg0        = H4(p->msg.arg0);
34     p->msg.arg1        = H4(p->msg.arg1);
35     p->msg.data_length = H4(p->msg.data_length);
36     p->msg.data_check  = H4(p->msg.data_check);
37     p->msg.magic       = H4(p->msg.magic);
38 }
39 #else
40 #define fix_endians(p) do {} while (0)
41 #endif
42 
43 #if ADB_HOST
44 /* we keep a list of opened transports, transport 0 is bound to 5555,
45  * transport 1 to 5557, .. transport n to 5555 + n*2. the list is used
46  * to detect when we're trying to connect twice to a given local transport
47  */
48 #define  ADB_LOCAL_TRANSPORT_MAX  16
49 
50 ADB_MUTEX_DEFINE( local_transports_lock );
51 
52 static atransport*  local_transports[ ADB_LOCAL_TRANSPORT_MAX ];
53 #endif /* ADB_HOST */
54 
remote_read(apacket * p,atransport * t)55 static int remote_read(apacket *p, atransport *t)
56 {
57     if(readx(t->sfd, &p->msg, sizeof(amessage))){
58         D("remote local: read terminated (message)\n");
59         return -1;
60     }
61 
62     fix_endians(p);
63 
64 #if 0 && defined __ppc__
65     D("read remote packet: %04x arg0=%0x arg1=%0x data_length=%0x data_check=%0x magic=%0x\n",
66       p->msg.command, p->msg.arg0, p->msg.arg1, p->msg.data_length, p->msg.data_check, p->msg.magic);
67 #endif
68     if(check_header(p)) {
69         D("bad header: terminated (data)\n");
70         return -1;
71     }
72 
73     if(readx(t->sfd, p->data, p->msg.data_length)){
74         D("remote local: terminated (data)\n");
75         return -1;
76     }
77 
78     if(check_data(p)) {
79         D("bad data: terminated (data)\n");
80         return -1;
81     }
82 
83     return 0;
84 }
85 
remote_write(apacket * p,atransport * t)86 static int remote_write(apacket *p, atransport *t)
87 {
88     int   length = p->msg.data_length;
89 
90     fix_endians(p);
91 
92 #if 0 && defined __ppc__
93     D("write remote packet: %04x arg0=%0x arg1=%0x data_length=%0x data_check=%0x magic=%0x\n",
94       p->msg.command, p->msg.arg0, p->msg.arg1, p->msg.data_length, p->msg.data_check, p->msg.magic);
95 #endif
96     if(writex(t->sfd, &p->msg, sizeof(amessage) + length)) {
97         D("remote local: write terminated\n");
98         return -1;
99     }
100 
101     return 0;
102 }
103 
104 
local_connect(int port)105 int  local_connect(int  port)
106 {
107     char buf[64];
108     int  fd = -1;
109 
110 #if ADB_HOST
111     const char *host = getenv("ADBHOST");
112     if (host) {
113         fd = socket_network_client(host, port, SOCK_STREAM);
114     }
115 #endif
116     if (fd < 0) {
117         fd = socket_loopback_client(port, SOCK_STREAM);
118     }
119 
120     if (fd >= 0) {
121         D("client: connected on remote on fd %d\n", fd);
122         close_on_exec(fd);
123         disable_tcp_nagle(fd);
124         snprintf(buf, sizeof buf, "%s%d", LOCAL_CLIENT_PREFIX, port - 1);
125         register_socket_transport(fd, buf, port, 1);
126         return 0;
127     }
128     return -1;
129 }
130 
131 
client_socket_thread(void * x)132 static void *client_socket_thread(void *x)
133 {
134 #if ADB_HOST
135     int  port  = ADB_LOCAL_TRANSPORT_PORT;
136     int  count = ADB_LOCAL_TRANSPORT_MAX;
137 
138     D("transport: client_socket_thread() starting\n");
139 
140     /* try to connect to any number of running emulator instances     */
141     /* this is only done when ADB starts up. later, each new emulator */
142     /* will send a message to ADB to indicate that is is starting up  */
143     for ( ; count > 0; count--, port += 2 ) {
144         (void) local_connect(port);
145     }
146 #endif
147     return 0;
148 }
149 
server_socket_thread(void * arg)150 static void *server_socket_thread(void * arg)
151 {
152     int serverfd, fd;
153     struct sockaddr addr;
154     socklen_t alen;
155     int port = (int)arg;
156 
157     D("transport: server_socket_thread() starting\n");
158     serverfd = -1;
159     for(;;) {
160         if(serverfd == -1) {
161             serverfd = socket_inaddr_any_server(port, SOCK_STREAM);
162             if(serverfd < 0) {
163                 D("server: cannot bind socket yet\n");
164                 adb_sleep_ms(1000);
165                 continue;
166             }
167             close_on_exec(serverfd);
168         }
169 
170         alen = sizeof(addr);
171         D("server: trying to get new connection from %d\n", port);
172         fd = adb_socket_accept(serverfd, &addr, &alen);
173         if(fd >= 0) {
174             D("server: new connection on fd %d\n", fd);
175             close_on_exec(fd);
176             disable_tcp_nagle(fd);
177             register_socket_transport(fd, "host", port, 1);
178         }
179     }
180     D("transport: server_socket_thread() exiting\n");
181     return 0;
182 }
183 
local_init(int port)184 void local_init(int port)
185 {
186     adb_thread_t thr;
187     void* (*func)(void *);
188 
189     if(HOST) {
190         func = client_socket_thread;
191     } else {
192         func = server_socket_thread;
193     }
194 
195     D("transport: local %s init\n", HOST ? "client" : "server");
196 
197     if(adb_thread_create(&thr, func, (void *)port)) {
198         fatal_errno("cannot create local socket %s thread",
199                     HOST ? "client" : "server");
200     }
201 }
202 
remote_kick(atransport * t)203 static void remote_kick(atransport *t)
204 {
205     int fd = t->sfd;
206     t->sfd = -1;
207     adb_shutdown(fd);
208     adb_close(fd);
209 
210 #if ADB_HOST
211     if(HOST) {
212         int  nn;
213         adb_mutex_lock( &local_transports_lock );
214         for (nn = 0; nn < ADB_LOCAL_TRANSPORT_MAX; nn++) {
215             if (local_transports[nn] == t) {
216                 local_transports[nn] = NULL;
217                 break;
218             }
219         }
220         adb_mutex_unlock( &local_transports_lock );
221     }
222 #endif
223 }
224 
remote_close(atransport * t)225 static void remote_close(atransport *t)
226 {
227     adb_close(t->fd);
228 }
229 
init_socket_transport(atransport * t,int s,int port,int local)230 int init_socket_transport(atransport *t, int s, int port, int local)
231 {
232     int  fail = 0;
233 
234     t->kick = remote_kick;
235     t->close = remote_close;
236     t->read_from_remote = remote_read;
237     t->write_to_remote = remote_write;
238     t->sfd = s;
239     t->sync_token = 1;
240     t->connection_state = CS_OFFLINE;
241     t->type = kTransportLocal;
242 
243 #if ADB_HOST
244     if (HOST && local) {
245         adb_mutex_lock( &local_transports_lock );
246         {
247             int  index = (port - ADB_LOCAL_TRANSPORT_PORT)/2;
248 
249             if (!(port & 1) || index < 0 || index >= ADB_LOCAL_TRANSPORT_MAX) {
250                 D("bad local transport port number: %d\n", port);
251                 fail = -1;
252             }
253             else if (local_transports[index] != NULL) {
254                 D("local transport for port %d already registered (%p)?\n",
255                 port, local_transports[index]);
256                 fail = -1;
257             }
258             else
259                 local_transports[index] = t;
260         }
261         adb_mutex_unlock( &local_transports_lock );
262     }
263 #endif
264     return fail;
265 }
266