* initial contribution for 2.0 beta (0.0.2)
[framework/system/sdbd.git] / src / transport_local.c
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 "sdb.h"
27
28 #ifdef __ppc__
29 #define H4(x)   (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24)
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 SDB_HOST
44 /* we keep a list of opened transports. The atransport struct knows to which
45  * local transport it is connected. The list is used to detect when we're
46  * trying to connect twice to a given local transport.
47  */
48 #define  SDB_LOCAL_TRANSPORT_MAX  16
49
50 SDB_MUTEX_DEFINE( local_transports_lock );
51
52 static atransport*  local_transports[ SDB_LOCAL_TRANSPORT_MAX ];
53 #endif /* SDB_HOST */
54
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
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
105 int local_connect(int port) {
106     return local_connect_arbitrary_ports(port-1, port);
107 }
108
109 int local_connect_arbitrary_ports(int console_port, int sdb_port)
110 {
111     char buf[64];
112     int  fd = -1;
113
114 #if SDB_HOST
115     const char *host = getenv("SDBHOST");
116     if (host) {
117         fd = socket_network_client(host, sdb_port, SOCK_STREAM);
118     }
119 #endif
120     if (fd < 0) {
121         fd = socket_loopback_client(sdb_port, SOCK_STREAM);
122     }
123
124     if (fd >= 0) {
125         D("client: connected on remote on fd %d\n", fd);
126         close_on_exec(fd);
127         disable_tcp_nagle(fd);
128         snprintf(buf, sizeof buf, "%s%d", LOCAL_CLIENT_PREFIX, console_port);
129         register_socket_transport(fd, buf, sdb_port, 1);
130         return 0;
131     }
132     return -1;
133 }
134
135
136 static void *client_socket_thread(void *x)
137 {
138 #if SDB_HOST
139 //      for (;;){
140         int  port  = DEFAULT_SDB_LOCAL_TRANSPORT_PORT;
141         int  count = SDB_LOCAL_TRANSPORT_MAX;
142
143         D("transport: client_socket_thread() starting\n");
144
145         /* try to connect to any number of running emulator instances     */
146         /* this is only done when SDB starts up. later, each new emulator */
147         /* will send a message to SDB to indicate that is is starting up  */
148         for ( ; count > 0; count--, port += 10 ) {
149                 (void) local_connect(port);
150         }
151                 
152 //              sdb_sleep_ms(1000);
153 //      }
154 #endif
155     return 0;
156 }
157
158 static void *server_socket_thread(void * arg)
159 {
160     int serverfd, fd;
161     struct sockaddr addr;
162     socklen_t alen;
163     int port = (int)arg;
164
165     D("transport: server_socket_thread() starting\n");
166     serverfd = -1;
167     for(;;) {
168         if(serverfd == -1) {
169             serverfd = socket_inaddr_any_server(port, SOCK_STREAM);
170             if(serverfd < 0) {
171                 D("server: cannot bind socket yet\n");
172                 sdb_sleep_ms(1000);
173                 continue;
174             }
175             close_on_exec(serverfd);
176         }
177
178         alen = sizeof(addr);
179         D("server: trying to get new connection from %d\n", port);
180         fd = sdb_socket_accept(serverfd, &addr, &alen);
181         if(fd >= 0) {
182             D("server: new connection on fd %d\n", fd);
183             close_on_exec(fd);
184             disable_tcp_nagle(fd);
185             register_socket_transport(fd, "host", port, 1);
186         }
187     }
188     D("transport: server_socket_thread() exiting\n");
189     return 0;
190 }
191
192 void local_init(int port)
193 {
194     sdb_thread_t thr;
195     void* (*func)(void *);
196
197     if(HOST) {
198         func = client_socket_thread;
199     } else {
200         func = server_socket_thread;
201     }
202
203     D("transport: local %s init\n", HOST ? "client" : "server");
204
205     if(sdb_thread_create(&thr, func, (void *)port)) {
206         fatal_errno("cannot create local socket %s thread",
207                     HOST ? "client" : "server");
208     }
209 }
210
211 static void remote_kick(atransport *t)
212 {
213     int fd = t->sfd;
214     t->sfd = -1;
215     sdb_shutdown(fd);
216     sdb_close(fd);
217
218 #if SDB_HOST
219     if(HOST) {
220         int  nn;
221         sdb_mutex_lock( &local_transports_lock );
222         for (nn = 0; nn < SDB_LOCAL_TRANSPORT_MAX; nn++) {
223             if (local_transports[nn] == t) {
224                 local_transports[nn] = NULL;
225                 break;
226             }
227         }
228         sdb_mutex_unlock( &local_transports_lock );
229     }
230 #endif
231 }
232
233 static void remote_close(atransport *t)
234 {
235     sdb_close(t->fd);
236 }
237
238
239 #if SDB_HOST
240 /* Only call this function if you already hold local_transports_lock. */
241 atransport* find_emulator_transport_by_sdb_port_locked(int sdb_port)
242 {
243     int i;
244     for (i = 0; i < SDB_LOCAL_TRANSPORT_MAX; i++) {
245         if (local_transports[i] && local_transports[i]->sdb_port == sdb_port) {
246             return local_transports[i];
247         }
248     }
249     return NULL;
250 }
251
252 atransport* find_emulator_transport_by_sdb_port(int sdb_port)
253 {
254     sdb_mutex_lock( &local_transports_lock );
255     atransport* result = find_emulator_transport_by_sdb_port_locked(sdb_port);
256     sdb_mutex_unlock( &local_transports_lock );
257     return result;
258 }
259
260 /* Only call this function if you already hold local_transports_lock. */
261 int get_available_local_transport_index_locked()
262 {
263     int i;
264     for (i = 0; i < SDB_LOCAL_TRANSPORT_MAX; i++) {
265         if (local_transports[i] == NULL) {
266             return i;
267         }
268     }
269     return -1;
270 }
271
272 int get_available_local_transport_index()
273 {
274     sdb_mutex_lock( &local_transports_lock );
275     int result = get_available_local_transport_index_locked();
276     sdb_mutex_unlock( &local_transports_lock );
277     return result;
278 }
279 #endif
280
281 int init_socket_transport(atransport *t, int s, int sdb_port, int local)
282 {
283     int  fail = 0;
284
285     t->kick = remote_kick;
286     t->close = remote_close;
287     t->read_from_remote = remote_read;
288     t->write_to_remote = remote_write;
289     t->sfd = s;
290     t->sync_token = 1;
291     t->connection_state = CS_OFFLINE;
292     t->type = kTransportLocal;
293     t->sdb_port = 0;
294
295 #if SDB_HOST
296     if (HOST && local) {
297         sdb_mutex_lock( &local_transports_lock );
298         {
299             t->sdb_port = sdb_port;
300             atransport* existing_transport =
301                     find_emulator_transport_by_sdb_port_locked(sdb_port);
302             int index = get_available_local_transport_index_locked();
303             if (existing_transport != NULL) {
304                 D("local transport for port %d already registered (%p)?\n",
305                 sdb_port, existing_transport);
306                 fail = -1;
307             } else if (index < 0) {
308                 // Too many emulators.
309                 D("cannot register more emulators. Maximum is %d\n",
310                         SDB_LOCAL_TRANSPORT_MAX);
311                 fail = -1;
312             } else {
313                 local_transports[index] = t;
314             }
315        }
316        sdb_mutex_unlock( &local_transports_lock );
317     }
318 #endif
319     return fail;
320 }