2009-03-04 04:32:55 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-03-19 23:21:08 +01:00
|
|
|
#define TRACE_TAG TRACE_SOCKETS
|
|
|
|
|
|
|
|
#include "sysdeps.h"
|
|
|
|
|
2015-02-25 00:51:19 +01:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-02-25 00:51:19 +01:00
|
|
|
#include <unistd.h>
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-02-25 00:51:19 +01:00
|
|
|
#if !ADB_HOST
|
|
|
|
#include "cutils/properties.h"
|
|
|
|
#endif
|
2015-03-19 23:21:08 +01:00
|
|
|
|
|
|
|
#include "adb.h"
|
|
|
|
#include "adb_io.h"
|
2015-02-25 00:51:19 +01:00
|
|
|
#include "transport.h"
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
ADB_MUTEX_DEFINE( socket_list_lock );
|
|
|
|
|
|
|
|
static void local_socket_close_locked(asocket *s);
|
|
|
|
|
|
|
|
static unsigned local_socket_next_id = 1;
|
|
|
|
|
|
|
|
static asocket local_socket_list = {
|
|
|
|
.next = &local_socket_list,
|
|
|
|
.prev = &local_socket_list,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* the the list of currently closing local sockets.
|
|
|
|
** these have no peer anymore, but still packets to
|
|
|
|
** write to their fd.
|
|
|
|
*/
|
|
|
|
static asocket local_socket_closing_list = {
|
|
|
|
.next = &local_socket_closing_list,
|
|
|
|
.prev = &local_socket_closing_list,
|
|
|
|
};
|
|
|
|
|
2013-12-13 14:09:44 +01:00
|
|
|
// Parse the global list of sockets to find one with id |local_id|.
|
|
|
|
// If |peer_id| is not 0, also check that it is connected to a peer
|
|
|
|
// with id |peer_id|. Returns an asocket handle on success, NULL on failure.
|
|
|
|
asocket *find_local_socket(unsigned local_id, unsigned peer_id)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
|
|
|
asocket *s;
|
|
|
|
asocket *result = NULL;
|
|
|
|
|
|
|
|
adb_mutex_lock(&socket_list_lock);
|
2010-06-12 16:40:20 +02:00
|
|
|
for (s = local_socket_list.next; s != &local_socket_list; s = s->next) {
|
2013-12-13 14:09:44 +01:00
|
|
|
if (s->id != local_id)
|
|
|
|
continue;
|
|
|
|
if (peer_id == 0 || (s->peer && s->peer->id == peer_id)) {
|
2010-06-12 16:40:20 +02:00
|
|
|
result = s;
|
|
|
|
}
|
2013-12-13 14:09:44 +01:00
|
|
|
break;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
adb_mutex_unlock(&socket_list_lock);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
insert_local_socket(asocket* s, asocket* list)
|
|
|
|
{
|
|
|
|
s->next = list;
|
|
|
|
s->prev = s->next->prev;
|
|
|
|
s->prev->next = s;
|
|
|
|
s->next->prev = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void install_local_socket(asocket *s)
|
|
|
|
{
|
|
|
|
adb_mutex_lock(&socket_list_lock);
|
|
|
|
|
|
|
|
s->id = local_socket_next_id++;
|
2013-12-13 14:09:44 +01:00
|
|
|
|
|
|
|
// Socket ids should never be 0.
|
|
|
|
if (local_socket_next_id == 0)
|
|
|
|
local_socket_next_id = 1;
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
insert_local_socket(s, &local_socket_list);
|
|
|
|
|
|
|
|
adb_mutex_unlock(&socket_list_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove_socket(asocket *s)
|
|
|
|
{
|
|
|
|
// socket_list_lock should already be held
|
|
|
|
if (s->prev && s->next)
|
|
|
|
{
|
|
|
|
s->prev->next = s->next;
|
|
|
|
s->next->prev = s->prev;
|
|
|
|
s->next = 0;
|
|
|
|
s->prev = 0;
|
|
|
|
s->id = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void close_all_sockets(atransport *t)
|
|
|
|
{
|
|
|
|
asocket *s;
|
|
|
|
|
|
|
|
/* this is a little gross, but since s->close() *will* modify
|
|
|
|
** the list out from under you, your options are limited.
|
|
|
|
*/
|
|
|
|
adb_mutex_lock(&socket_list_lock);
|
|
|
|
restart:
|
|
|
|
for(s = local_socket_list.next; s != &local_socket_list; s = s->next){
|
|
|
|
if(s->transport == t || (s->peer && s->peer->transport == t)) {
|
|
|
|
local_socket_close_locked(s);
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
adb_mutex_unlock(&socket_list_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int local_socket_enqueue(asocket *s, apacket *p)
|
|
|
|
{
|
2015-09-03 02:44:28 +02:00
|
|
|
D("LS(%d): enqueue %d", s->id, p->len);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
p->ptr = p->data;
|
|
|
|
|
|
|
|
/* if there is already data queue'd, we will receive
|
|
|
|
** events when it's time to write. just add this to
|
|
|
|
** the tail
|
|
|
|
*/
|
|
|
|
if(s->pkt_first) {
|
|
|
|
goto enqueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* write as much as we can, until we
|
|
|
|
** would block or there is an error/eof
|
|
|
|
*/
|
|
|
|
while(p->len > 0) {
|
|
|
|
int r = adb_write(s->fd, p->ptr, p->len);
|
|
|
|
if(r > 0) {
|
|
|
|
p->len -= r;
|
|
|
|
p->ptr += r;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if((r == 0) || (errno != EAGAIN)) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D( "LS(%d): not ready, errno=%d: %s", s->id, errno, strerror(errno) );
|
2009-03-04 04:32:55 +01:00
|
|
|
s->close(s);
|
|
|
|
return 1; /* not ready (error) */
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(p->len == 0) {
|
|
|
|
put_apacket(p);
|
|
|
|
return 0; /* ready for more data */
|
|
|
|
}
|
|
|
|
|
|
|
|
enqueue:
|
|
|
|
p->next = 0;
|
|
|
|
if(s->pkt_first) {
|
|
|
|
s->pkt_last->next = p;
|
|
|
|
} else {
|
|
|
|
s->pkt_first = p;
|
|
|
|
}
|
|
|
|
s->pkt_last = p;
|
|
|
|
|
|
|
|
/* make sure we are notified when we can drain the queue */
|
|
|
|
fdevent_add(&s->fde, FDE_WRITE);
|
|
|
|
|
|
|
|
return 1; /* not ready (backlog) */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void local_socket_ready(asocket *s)
|
|
|
|
{
|
2015-02-18 12:53:37 +01:00
|
|
|
/* far side is ready for data, pay attention to
|
|
|
|
readable events */
|
2009-03-04 04:32:55 +01:00
|
|
|
fdevent_add(&s->fde, FDE_READ);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void local_socket_close(asocket *s)
|
|
|
|
{
|
|
|
|
adb_mutex_lock(&socket_list_lock);
|
|
|
|
local_socket_close_locked(s);
|
|
|
|
adb_mutex_unlock(&socket_list_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
// be sure to hold the socket list lock when calling this
|
|
|
|
static void local_socket_destroy(asocket *s)
|
|
|
|
{
|
|
|
|
apacket *p, *n;
|
2012-03-16 22:50:07 +01:00
|
|
|
int exit_on_close = s->exit_on_close;
|
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("LS(%d): destroying fde.fd=%d", s->id, s->fde.fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
/* IMPORTANT: the remove closes the fd
|
|
|
|
** that belongs to this socket
|
|
|
|
*/
|
|
|
|
fdevent_remove(&s->fde);
|
|
|
|
|
|
|
|
/* dispose of any unwritten data */
|
|
|
|
for(p = s->pkt_first; p; p = n) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("LS(%d): discarding %d bytes", s->id, p->len);
|
2009-03-04 04:32:55 +01:00
|
|
|
n = p->next;
|
|
|
|
put_apacket(p);
|
|
|
|
}
|
|
|
|
remove_socket(s);
|
|
|
|
free(s);
|
2012-03-16 22:50:07 +01:00
|
|
|
|
|
|
|
if (exit_on_close) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("local_socket_destroy: exiting");
|
2012-03-16 22:50:07 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void local_socket_close_locked(asocket *s)
|
|
|
|
{
|
2015-09-03 02:44:28 +02:00
|
|
|
D("entered local_socket_close_locked. LS(%d) fd=%d", s->id, s->fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
if(s->peer) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("LS(%d): closing peer. peer->id=%d peer->fd=%d",
|
2011-03-16 23:57:42 +01:00
|
|
|
s->id, s->peer->id, s->peer->fd);
|
2013-12-13 14:09:44 +01:00
|
|
|
/* Note: it's important to call shutdown before disconnecting from
|
|
|
|
* the peer, this ensures that remote sockets can still get the id
|
|
|
|
* of the local socket they're connected to, to send a CLOSE()
|
|
|
|
* protocol event. */
|
|
|
|
if (s->peer->shutdown)
|
|
|
|
s->peer->shutdown(s->peer);
|
2009-03-04 04:32:55 +01:00
|
|
|
s->peer->peer = 0;
|
|
|
|
// tweak to avoid deadlock
|
2011-05-13 20:24:55 +02:00
|
|
|
if (s->peer->close == local_socket_close) {
|
2009-03-04 04:32:55 +01:00
|
|
|
local_socket_close_locked(s->peer);
|
2011-05-13 20:24:55 +02:00
|
|
|
} else {
|
2009-03-04 04:32:55 +01:00
|
|
|
s->peer->close(s->peer);
|
2011-05-13 20:24:55 +02:00
|
|
|
}
|
|
|
|
s->peer = 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If we are already closing, or if there are no
|
|
|
|
** pending packets, destroy immediately
|
|
|
|
*/
|
|
|
|
if (s->closing || s->pkt_first == NULL) {
|
|
|
|
int id = s->id;
|
|
|
|
local_socket_destroy(s);
|
2015-09-03 02:44:28 +02:00
|
|
|
D("LS(%d): closed", id);
|
2009-03-04 04:32:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* otherwise, put on the closing list
|
|
|
|
*/
|
2015-09-03 02:44:28 +02:00
|
|
|
D("LS(%d): closing", s->id);
|
2009-03-04 04:32:55 +01:00
|
|
|
s->closing = 1;
|
|
|
|
fdevent_del(&s->fde, FDE_READ);
|
|
|
|
remove_socket(s);
|
2015-09-03 02:44:28 +02:00
|
|
|
D("LS(%d): put on socket_closing_list fd=%d", s->id, s->fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
insert_local_socket(s, &local_socket_closing_list);
|
|
|
|
}
|
|
|
|
|
2015-02-26 02:51:28 +01:00
|
|
|
static void local_socket_event_func(int fd, unsigned ev, void* _s)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2015-02-26 02:51:28 +01:00
|
|
|
asocket* s = reinterpret_cast<asocket*>(_s);
|
2015-09-03 02:44:28 +02:00
|
|
|
D("LS(%d): event_func(fd=%d(==%d), ev=%04x)", s->id, s->fd, fd, ev);
|
2011-03-16 23:57:42 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
/* put the FDE_WRITE processing before the FDE_READ
|
|
|
|
** in order to simplify the code.
|
|
|
|
*/
|
2015-02-26 02:51:28 +01:00
|
|
|
if (ev & FDE_WRITE) {
|
|
|
|
apacket* p;
|
|
|
|
while ((p = s->pkt_first) != nullptr) {
|
|
|
|
while (p->len > 0) {
|
2009-03-04 04:32:55 +01:00
|
|
|
int r = adb_write(fd, p->ptr, p->len);
|
2015-02-26 02:51:28 +01:00
|
|
|
if (r == -1) {
|
2009-03-04 04:32:55 +01:00
|
|
|
/* returning here is ok because FDE_READ will
|
|
|
|
** be processed in the next iteration loop
|
|
|
|
*/
|
2015-02-26 02:51:28 +01:00
|
|
|
if (errno == EAGAIN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (r > 0) {
|
|
|
|
p->ptr += r;
|
|
|
|
p->len -= r;
|
|
|
|
continue;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
2015-02-26 02:51:28 +01:00
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D(" closing after write because r=%d and errno is %d", r, errno);
|
2009-03-04 04:32:55 +01:00
|
|
|
s->close(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-26 02:51:28 +01:00
|
|
|
if (p->len == 0) {
|
2009-03-04 04:32:55 +01:00
|
|
|
s->pkt_first = p->next;
|
2015-02-26 02:51:28 +01:00
|
|
|
if (s->pkt_first == 0) {
|
|
|
|
s->pkt_last = 0;
|
|
|
|
}
|
2009-03-04 04:32:55 +01:00
|
|
|
put_apacket(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-26 02:51:28 +01:00
|
|
|
/* if we sent the last packet of a closing socket,
|
|
|
|
** we can now destroy it.
|
|
|
|
*/
|
2009-03-04 04:32:55 +01:00
|
|
|
if (s->closing) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D(" closing because 'closing' is set after write");
|
2009-03-04 04:32:55 +01:00
|
|
|
s->close(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-26 02:51:28 +01:00
|
|
|
/* no more packets queued, so we can ignore
|
|
|
|
** writable events again and tell our peer
|
|
|
|
** to resume writing
|
|
|
|
*/
|
2009-03-04 04:32:55 +01:00
|
|
|
fdevent_del(&s->fde, FDE_WRITE);
|
|
|
|
s->peer->ready(s->peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-26 02:51:28 +01:00
|
|
|
if (ev & FDE_READ) {
|
2009-03-04 04:32:55 +01:00
|
|
|
apacket *p = get_apacket();
|
|
|
|
unsigned char *x = p->data;
|
2015-07-13 20:12:28 +02:00
|
|
|
const size_t max_payload = s->get_max_payload();
|
|
|
|
size_t avail = max_payload;
|
|
|
|
int r = 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
int is_eof = 0;
|
|
|
|
|
2015-02-26 02:51:28 +01:00
|
|
|
while (avail > 0) {
|
2009-03-04 04:32:55 +01:00
|
|
|
r = adb_read(fd, x, avail);
|
2015-09-03 02:44:28 +02:00
|
|
|
D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu",
|
2015-02-26 02:51:28 +01:00
|
|
|
s->id, s->fd, r, r < 0 ? errno : 0, avail);
|
|
|
|
if (r == -1) {
|
|
|
|
if (errno == EAGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (r > 0) {
|
2009-03-04 04:32:55 +01:00
|
|
|
avail -= r;
|
|
|
|
x += r;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-02-26 02:51:28 +01:00
|
|
|
/* r = 0 or unhandled error */
|
2009-03-04 04:32:55 +01:00
|
|
|
is_eof = 1;
|
|
|
|
break;
|
|
|
|
}
|
2015-09-03 02:44:28 +02:00
|
|
|
D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d",
|
2011-03-16 23:57:42 +01:00
|
|
|
s->id, s->fd, r, is_eof, s->fde.force_eof);
|
2015-07-13 20:12:28 +02:00
|
|
|
if ((avail == max_payload) || (s->peer == 0)) {
|
2009-03-04 04:32:55 +01:00
|
|
|
put_apacket(p);
|
|
|
|
} else {
|
2015-07-13 20:12:28 +02:00
|
|
|
p->len = max_payload - avail;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-08-26 21:27:40 +02:00
|
|
|
// s->peer->enqueue() may call s->close() and free s,
|
|
|
|
// so save variables for debug printing below.
|
|
|
|
unsigned saved_id = s->id;
|
|
|
|
int saved_fd = s->fd;
|
2009-03-04 04:32:55 +01:00
|
|
|
r = s->peer->enqueue(s->peer, p);
|
2015-09-03 02:44:28 +02:00
|
|
|
D("LS(%u): fd=%d post peer->enqueue(). r=%d", saved_id, saved_fd, r);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-02-26 02:51:28 +01:00
|
|
|
if (r < 0) {
|
2009-03-04 04:32:55 +01:00
|
|
|
/* error return means they closed us as a side-effect
|
|
|
|
** and we must return immediately.
|
|
|
|
**
|
|
|
|
** note that if we still have buffered packets, the
|
|
|
|
** socket will be placed on the closing socket list.
|
|
|
|
** this handler function will be called again
|
|
|
|
** to process FDE_WRITE events.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-26 02:51:28 +01:00
|
|
|
if (r > 0) {
|
2009-03-04 04:32:55 +01:00
|
|
|
/* if the remote cannot accept further events,
|
|
|
|
** we disable notification of READs. They'll
|
|
|
|
** be enabled again when we get a call to ready()
|
|
|
|
*/
|
|
|
|
fdevent_del(&s->fde, FDE_READ);
|
|
|
|
}
|
|
|
|
}
|
2011-04-13 07:01:58 +02:00
|
|
|
/* Don't allow a forced eof if data is still there */
|
2015-02-26 02:51:28 +01:00
|
|
|
if ((s->fde.force_eof && !r) || is_eof) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D(" closing because is_eof=%d r=%d s->fde.force_eof=%d",
|
2015-02-26 02:51:28 +01:00
|
|
|
is_eof, r, s->fde.force_eof);
|
2009-03-04 04:32:55 +01:00
|
|
|
s->close(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-26 02:51:28 +01:00
|
|
|
if (ev & FDE_ERROR){
|
2009-03-04 04:32:55 +01:00
|
|
|
/* this should be caught be the next read or write
|
|
|
|
** catching it here means we may skip the last few
|
|
|
|
** bytes of readable data.
|
|
|
|
*/
|
2015-09-03 02:44:28 +02:00
|
|
|
D("LS(%d): FDE_ERROR (fd=%d)", s->id, s->fd);
|
2011-03-16 23:57:42 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
asocket *create_local_socket(int fd)
|
|
|
|
{
|
2015-02-26 02:51:28 +01:00
|
|
|
asocket *s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket)));
|
2010-06-11 01:48:19 +02:00
|
|
|
if (s == NULL) fatal("cannot allocate socket");
|
2009-03-04 04:32:55 +01:00
|
|
|
s->fd = fd;
|
|
|
|
s->enqueue = local_socket_enqueue;
|
|
|
|
s->ready = local_socket_ready;
|
2013-12-13 14:09:44 +01:00
|
|
|
s->shutdown = NULL;
|
2009-03-04 04:32:55 +01:00
|
|
|
s->close = local_socket_close;
|
2011-03-16 23:57:42 +01:00
|
|
|
install_local_socket(s);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
fdevent_install(&s->fde, fd, local_socket_event_func, s);
|
2015-09-03 02:44:28 +02:00
|
|
|
D("LS(%d): created (fd=%d)", s->id, s->fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
asocket *create_local_service_socket(const char *name)
|
|
|
|
{
|
|
|
|
#if !ADB_HOST
|
|
|
|
if (!strcmp(name,"jdwp")) {
|
|
|
|
return create_jdwp_service_socket();
|
|
|
|
}
|
|
|
|
if (!strcmp(name,"track-jdwp")) {
|
|
|
|
return create_jdwp_tracker_service_socket();
|
|
|
|
}
|
|
|
|
#endif
|
2014-10-06 19:57:20 +02:00
|
|
|
int fd = service_to_fd(name);
|
2009-03-04 04:32:55 +01:00
|
|
|
if(fd < 0) return 0;
|
|
|
|
|
2014-10-06 19:57:20 +02:00
|
|
|
asocket* s = create_local_socket(fd);
|
2015-09-03 02:44:28 +02:00
|
|
|
D("LS(%d): bound to '%s' via %d", s->id, name, fd);
|
2012-03-16 22:50:07 +01:00
|
|
|
|
2012-03-30 22:19:11 +02:00
|
|
|
#if !ADB_HOST
|
2014-10-06 19:57:20 +02:00
|
|
|
char debug[PROPERTY_VALUE_MAX];
|
2013-05-24 23:40:15 +02:00
|
|
|
if (!strncmp(name, "root:", 5))
|
|
|
|
property_get("ro.debuggable", debug, "");
|
|
|
|
|
2014-10-06 19:57:20 +02:00
|
|
|
if ((!strncmp(name, "root:", 5) && getuid() != 0 && strcmp(debug, "1") == 0)
|
|
|
|
|| (!strncmp(name, "unroot:", 7) && getuid() == 0)
|
2012-06-12 21:12:18 +02:00
|
|
|
|| !strncmp(name, "usb:", 4)
|
|
|
|
|| !strncmp(name, "tcpip:", 6)) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("LS(%d): enabling exit_on_close", s->id);
|
2012-03-16 22:50:07 +01:00
|
|
|
s->exit_on_close = 1;
|
|
|
|
}
|
2012-03-30 22:19:11 +02:00
|
|
|
#endif
|
2012-03-16 22:50:07 +01:00
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ADB_HOST
|
|
|
|
static asocket *create_host_service_socket(const char *name, const char* serial)
|
|
|
|
{
|
|
|
|
asocket *s;
|
|
|
|
|
|
|
|
s = host_service_to_socket(name, serial);
|
|
|
|
|
|
|
|
if (s != NULL) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("LS(%d) bound to '%s'", s->id, name);
|
2009-03-04 04:32:55 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
#endif /* ADB_HOST */
|
|
|
|
|
|
|
|
static int remote_socket_enqueue(asocket *s, apacket *p)
|
|
|
|
{
|
2015-09-03 02:44:28 +02:00
|
|
|
D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d",
|
2011-03-16 23:57:42 +01:00
|
|
|
s->id, s->fd, s->peer->fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
p->msg.command = A_WRTE;
|
|
|
|
p->msg.arg0 = s->peer->id;
|
|
|
|
p->msg.arg1 = s->id;
|
|
|
|
p->msg.data_length = p->len;
|
|
|
|
send_packet(p, s->transport);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remote_socket_ready(asocket *s)
|
|
|
|
{
|
2015-09-03 02:44:28 +02:00
|
|
|
D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d",
|
2011-03-16 23:57:42 +01:00
|
|
|
s->id, s->fd, s->peer->fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
apacket *p = get_apacket();
|
|
|
|
p->msg.command = A_OKAY;
|
|
|
|
p->msg.arg0 = s->peer->id;
|
|
|
|
p->msg.arg1 = s->id;
|
|
|
|
send_packet(p, s->transport);
|
|
|
|
}
|
|
|
|
|
2013-12-13 14:09:44 +01:00
|
|
|
static void remote_socket_shutdown(asocket *s)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2015-09-03 02:44:28 +02:00
|
|
|
D("entered remote_socket_shutdown RS(%d) CLOSE fd=%d peer->fd=%d",
|
2011-03-16 23:57:42 +01:00
|
|
|
s->id, s->fd, s->peer?s->peer->fd:-1);
|
2009-03-04 04:32:55 +01:00
|
|
|
apacket *p = get_apacket();
|
|
|
|
p->msg.command = A_CLSE;
|
|
|
|
if(s->peer) {
|
|
|
|
p->msg.arg0 = s->peer->id;
|
2013-12-13 14:09:44 +01:00
|
|
|
}
|
|
|
|
p->msg.arg1 = s->id;
|
|
|
|
send_packet(p, s->transport);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remote_socket_close(asocket *s)
|
|
|
|
{
|
|
|
|
if (s->peer) {
|
2009-03-04 04:32:55 +01:00
|
|
|
s->peer->peer = 0;
|
2015-09-03 02:44:28 +02:00
|
|
|
D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d",
|
2011-03-16 23:57:42 +01:00
|
|
|
s->id, s->peer->id, s->peer->fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
s->peer->close(s->peer);
|
|
|
|
}
|
2015-09-03 02:44:28 +02:00
|
|
|
D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d",
|
2013-12-13 14:09:44 +01:00
|
|
|
s->id, s->fd, s->peer?s->peer->fd:-1);
|
2015-09-03 02:44:28 +02:00
|
|
|
D("RS(%d): closed", s->id);
|
2009-03-04 04:32:55 +01:00
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
2015-08-28 03:50:04 +02:00
|
|
|
// Create a remote socket to exchange packets with a remote service through transport
|
|
|
|
// |t|. Where |id| is the socket id of the corresponding service on the other
|
|
|
|
// side of the transport (it is allocated by the remote side and _cannot_ be 0).
|
|
|
|
// Returns a new non-NULL asocket handle.
|
2009-03-04 04:32:55 +01:00
|
|
|
asocket *create_remote_socket(unsigned id, atransport *t)
|
|
|
|
{
|
2013-12-13 14:09:44 +01:00
|
|
|
if (id == 0) fatal("invalid remote socket id (0)");
|
2015-08-28 03:50:04 +02:00
|
|
|
asocket* s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket)));
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2010-06-11 01:48:19 +02:00
|
|
|
if (s == NULL) fatal("cannot allocate socket");
|
2009-03-04 04:32:55 +01:00
|
|
|
s->id = id;
|
|
|
|
s->enqueue = remote_socket_enqueue;
|
|
|
|
s->ready = remote_socket_ready;
|
2013-12-13 14:09:44 +01:00
|
|
|
s->shutdown = remote_socket_shutdown;
|
2009-03-04 04:32:55 +01:00
|
|
|
s->close = remote_socket_close;
|
|
|
|
s->transport = t;
|
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("RS(%d): created", s->id);
|
2009-03-04 04:32:55 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void connect_to_remote(asocket *s, const char *destination)
|
|
|
|
{
|
2015-09-03 02:44:28 +02:00
|
|
|
D("Connect_to_remote call RS(%d) fd=%d", s->id, s->fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
apacket *p = get_apacket();
|
2015-07-13 20:12:28 +02:00
|
|
|
size_t len = strlen(destination) + 1;
|
2009-03-04 04:32:55 +01:00
|
|
|
|
2015-07-13 20:12:28 +02:00
|
|
|
if(len > (s->get_max_payload()-1)) {
|
2009-03-04 04:32:55 +01:00
|
|
|
fatal("destination oversized");
|
|
|
|
}
|
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("LS(%d): connect('%s')", s->id, destination);
|
2009-03-04 04:32:55 +01:00
|
|
|
p->msg.command = A_OPEN;
|
|
|
|
p->msg.arg0 = s->id;
|
|
|
|
p->msg.data_length = len;
|
|
|
|
strcpy((char*) p->data, destination);
|
|
|
|
send_packet(p, s->transport);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* this is used by magic sockets to rig local sockets to
|
|
|
|
send the go-ahead message when they connect */
|
|
|
|
static void local_socket_ready_notify(asocket *s)
|
|
|
|
{
|
|
|
|
s->ready = local_socket_ready;
|
2013-12-13 14:09:44 +01:00
|
|
|
s->shutdown = NULL;
|
2009-03-04 04:32:55 +01:00
|
|
|
s->close = local_socket_close;
|
2015-05-01 02:32:03 +02:00
|
|
|
SendOkay(s->fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
s->ready(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is used by magic sockets to rig local sockets to
|
|
|
|
send the failure message if they are closed before
|
|
|
|
connected (to avoid closing them without a status message) */
|
|
|
|
static void local_socket_close_notify(asocket *s)
|
|
|
|
{
|
|
|
|
s->ready = local_socket_ready;
|
2013-12-13 14:09:44 +01:00
|
|
|
s->shutdown = NULL;
|
2009-03-04 04:32:55 +01:00
|
|
|
s->close = local_socket_close;
|
2015-05-01 02:32:03 +02:00
|
|
|
SendFail(s->fd, "closed");
|
2009-03-04 04:32:55 +01:00
|
|
|
s->close(s);
|
|
|
|
}
|
|
|
|
|
2015-05-01 02:32:03 +02:00
|
|
|
static unsigned unhex(unsigned char *s, int len)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
|
|
|
unsigned n = 0, c;
|
|
|
|
|
|
|
|
while(len-- > 0) {
|
|
|
|
switch((c = *s++)) {
|
|
|
|
case '0': case '1': case '2':
|
|
|
|
case '3': case '4': case '5':
|
|
|
|
case '6': case '7': case '8':
|
|
|
|
case '9':
|
|
|
|
c -= '0';
|
|
|
|
break;
|
|
|
|
case 'a': case 'b': case 'c':
|
|
|
|
case 'd': case 'e': case 'f':
|
|
|
|
c = c - 'a' + 10;
|
|
|
|
break;
|
|
|
|
case 'A': case 'B': case 'C':
|
|
|
|
case 'D': case 'E': case 'F':
|
|
|
|
c = c - 'A' + 10;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0xffffffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = (n << 4) | c;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2015-05-01 02:32:03 +02:00
|
|
|
#if ADB_HOST
|
|
|
|
|
adb: Generalizing -s to take qualifiers.
Prior to this change, -s could take either a serial number or a
device path (e.g. "-s 01498B1F02015015" or "-s usb:1-4.2"). This
change extends -s to also allow product, model or device names
(e.g. "-s product:mysid"). These new qualifiers will only be
available on devices that are running an adb daemon that provides
properties in the connect message per Change-Id:
I09200decde4facb8fc9b4056fdae910155f2bcb9
The product, model and device are derived from the
ro.product.name, ro.product.model and ro.product.device
properties respectively. They are prefixed with "product:",
"model:" or "device:" as appropriate. In addition, any
non-alphanumerics in the model are changed to underscores.
If the -s parameter matches multiple devices, the result will be
the same as when multiple devices are connected but no -d, -e or
-s option is specified. In general, this means the user will get
"error: more than one device". However for get-state,
get-devpath and get-serialno, they will get "unknown".
The format of "devices -l" was changed to list all of the
qualifiers that are available. The following example output
(with the last digits of the serial numbers replaced with X's) is
with a Galaxy Prime with an older adb daemon and another Galaxy
Prime and Galaxy S both with the enhanced adb daemons:
List of devices attached
016B75D60A0060XX device usb:2-5 product:mysid model:Galaxy_Nexus device:toro
3731B535FAC200XX device usb:1-4.2 product:soju model:Nexus_S device:crespo
01498B1F020150XX device usb:1-4.1
Note that the serial number and state are now column oriented
instead of tab delimited. After the serial number and state, all
qualifiers are listed with each preceded by a space. The output
of the original devices command (without -l) is unchanged.
Change-Id: Iceeb2789874effc25a630d514a375d6f1889dc56
Signed-off-by: Scott Anderson <saa@android.com>
2012-05-31 03:11:27 +02:00
|
|
|
#define PREFIX(str) { str, sizeof(str) - 1 }
|
|
|
|
static const struct prefix_struct {
|
|
|
|
const char *str;
|
|
|
|
const size_t len;
|
|
|
|
} prefixes[] = {
|
|
|
|
PREFIX("usb:"),
|
|
|
|
PREFIX("product:"),
|
|
|
|
PREFIX("model:"),
|
|
|
|
PREFIX("device:"),
|
|
|
|
};
|
|
|
|
static const int num_prefixes = (sizeof(prefixes) / sizeof(prefixes[0]));
|
|
|
|
|
2011-03-16 09:43:56 +01:00
|
|
|
/* skip_host_serial return the position in a string
|
|
|
|
skipping over the 'serial' parameter in the ADB protocol,
|
|
|
|
where parameter string may be a host:port string containing
|
|
|
|
the protocol delimiter (colon). */
|
2015-05-01 02:32:03 +02:00
|
|
|
static char *skip_host_serial(char *service) {
|
2011-03-16 09:43:56 +01:00
|
|
|
char *first_colon, *serial_end;
|
adb: Generalizing -s to take qualifiers.
Prior to this change, -s could take either a serial number or a
device path (e.g. "-s 01498B1F02015015" or "-s usb:1-4.2"). This
change extends -s to also allow product, model or device names
(e.g. "-s product:mysid"). These new qualifiers will only be
available on devices that are running an adb daemon that provides
properties in the connect message per Change-Id:
I09200decde4facb8fc9b4056fdae910155f2bcb9
The product, model and device are derived from the
ro.product.name, ro.product.model and ro.product.device
properties respectively. They are prefixed with "product:",
"model:" or "device:" as appropriate. In addition, any
non-alphanumerics in the model are changed to underscores.
If the -s parameter matches multiple devices, the result will be
the same as when multiple devices are connected but no -d, -e or
-s option is specified. In general, this means the user will get
"error: more than one device". However for get-state,
get-devpath and get-serialno, they will get "unknown".
The format of "devices -l" was changed to list all of the
qualifiers that are available. The following example output
(with the last digits of the serial numbers replaced with X's) is
with a Galaxy Prime with an older adb daemon and another Galaxy
Prime and Galaxy S both with the enhanced adb daemons:
List of devices attached
016B75D60A0060XX device usb:2-5 product:mysid model:Galaxy_Nexus device:toro
3731B535FAC200XX device usb:1-4.2 product:soju model:Nexus_S device:crespo
01498B1F020150XX device usb:1-4.1
Note that the serial number and state are now column oriented
instead of tab delimited. After the serial number and state, all
qualifiers are listed with each preceded by a space. The output
of the original devices command (without -l) is unchanged.
Change-Id: Iceeb2789874effc25a630d514a375d6f1889dc56
Signed-off-by: Scott Anderson <saa@android.com>
2012-05-31 03:11:27 +02:00
|
|
|
int i;
|
2011-03-16 09:43:56 +01:00
|
|
|
|
adb: Generalizing -s to take qualifiers.
Prior to this change, -s could take either a serial number or a
device path (e.g. "-s 01498B1F02015015" or "-s usb:1-4.2"). This
change extends -s to also allow product, model or device names
(e.g. "-s product:mysid"). These new qualifiers will only be
available on devices that are running an adb daemon that provides
properties in the connect message per Change-Id:
I09200decde4facb8fc9b4056fdae910155f2bcb9
The product, model and device are derived from the
ro.product.name, ro.product.model and ro.product.device
properties respectively. They are prefixed with "product:",
"model:" or "device:" as appropriate. In addition, any
non-alphanumerics in the model are changed to underscores.
If the -s parameter matches multiple devices, the result will be
the same as when multiple devices are connected but no -d, -e or
-s option is specified. In general, this means the user will get
"error: more than one device". However for get-state,
get-devpath and get-serialno, they will get "unknown".
The format of "devices -l" was changed to list all of the
qualifiers that are available. The following example output
(with the last digits of the serial numbers replaced with X's) is
with a Galaxy Prime with an older adb daemon and another Galaxy
Prime and Galaxy S both with the enhanced adb daemons:
List of devices attached
016B75D60A0060XX device usb:2-5 product:mysid model:Galaxy_Nexus device:toro
3731B535FAC200XX device usb:1-4.2 product:soju model:Nexus_S device:crespo
01498B1F020150XX device usb:1-4.1
Note that the serial number and state are now column oriented
instead of tab delimited. After the serial number and state, all
qualifiers are listed with each preceded by a space. The output
of the original devices command (without -l) is unchanged.
Change-Id: Iceeb2789874effc25a630d514a375d6f1889dc56
Signed-off-by: Scott Anderson <saa@android.com>
2012-05-31 03:11:27 +02:00
|
|
|
for (i = 0; i < num_prefixes; i++) {
|
|
|
|
if (!strncmp(service, prefixes[i].str, prefixes[i].len))
|
|
|
|
return strchr(service + prefixes[i].len, ':');
|
2012-05-31 21:04:23 +02:00
|
|
|
}
|
|
|
|
|
2011-03-16 09:43:56 +01:00
|
|
|
first_colon = strchr(service, ':');
|
|
|
|
if (!first_colon) {
|
|
|
|
/* No colon in service string. */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
serial_end = first_colon;
|
|
|
|
if (isdigit(serial_end[1])) {
|
|
|
|
serial_end++;
|
|
|
|
while ((*serial_end) && isdigit(*serial_end)) {
|
|
|
|
serial_end++;
|
|
|
|
}
|
|
|
|
if ((*serial_end) != ':') {
|
|
|
|
// Something other than numbers was found, reset the end.
|
|
|
|
serial_end = first_colon;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return serial_end;
|
|
|
|
}
|
|
|
|
|
2015-05-01 02:32:03 +02:00
|
|
|
#endif // ADB_HOST
|
|
|
|
|
2009-03-04 04:32:55 +01:00
|
|
|
static int smart_socket_enqueue(asocket *s, apacket *p)
|
|
|
|
{
|
|
|
|
unsigned len;
|
|
|
|
#if ADB_HOST
|
|
|
|
char *service = NULL;
|
|
|
|
char* serial = NULL;
|
2015-05-05 22:10:43 +02:00
|
|
|
TransportType type = kTransportAny;
|
2009-03-04 04:32:55 +01:00
|
|
|
#endif
|
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("SS(%d): enqueue %d", s->id, p->len);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
if(s->pkt_first == 0) {
|
|
|
|
s->pkt_first = p;
|
|
|
|
s->pkt_last = p;
|
|
|
|
} else {
|
2015-07-13 20:12:28 +02:00
|
|
|
if((s->pkt_first->len + p->len) > s->get_max_payload()) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("SS(%d): overflow", s->id);
|
2009-03-04 04:32:55 +01:00
|
|
|
put_apacket(p);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(s->pkt_first->data + s->pkt_first->len,
|
|
|
|
p->data, p->len);
|
|
|
|
s->pkt_first->len += p->len;
|
|
|
|
put_apacket(p);
|
|
|
|
|
|
|
|
p = s->pkt_first;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* don't bother if we can't decode the length */
|
|
|
|
if(p->len < 4) return 0;
|
|
|
|
|
|
|
|
len = unhex(p->data, 4);
|
|
|
|
if((len < 1) || (len > 1024)) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("SS(%d): bad size (%d)", s->id, len);
|
2009-03-04 04:32:55 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("SS(%d): len is %d", s->id, len );
|
2009-03-04 04:32:55 +01:00
|
|
|
/* can't do anything until we have the full header */
|
|
|
|
if((len + 4) > p->len) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D("SS(%d): waiting for %d more bytes", s->id, len+4 - p->len);
|
2009-03-04 04:32:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->data[len + 4] = 0;
|
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("SS(%d): '%s'", s->id, (char*) (p->data + 4));
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
#if ADB_HOST
|
|
|
|
service = (char *)p->data + 4;
|
|
|
|
if(!strncmp(service, "host-serial:", strlen("host-serial:"))) {
|
|
|
|
char* serial_end;
|
|
|
|
service += strlen("host-serial:");
|
|
|
|
|
2011-03-16 09:43:56 +01:00
|
|
|
// serial number should follow "host:" and could be a host:port string.
|
|
|
|
serial_end = skip_host_serial(service);
|
2009-03-04 04:32:55 +01:00
|
|
|
if (serial_end) {
|
|
|
|
*serial_end = 0; // terminate string
|
|
|
|
serial = service;
|
|
|
|
service = serial_end + 1;
|
|
|
|
}
|
|
|
|
} else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) {
|
2015-05-05 22:10:43 +02:00
|
|
|
type = kTransportUsb;
|
2009-03-04 04:32:55 +01:00
|
|
|
service += strlen("host-usb:");
|
|
|
|
} else if (!strncmp(service, "host-local:", strlen("host-local:"))) {
|
2015-05-05 22:10:43 +02:00
|
|
|
type = kTransportLocal;
|
2009-03-04 04:32:55 +01:00
|
|
|
service += strlen("host-local:");
|
|
|
|
} else if (!strncmp(service, "host:", strlen("host:"))) {
|
2015-05-05 22:10:43 +02:00
|
|
|
type = kTransportAny;
|
2009-03-04 04:32:55 +01:00
|
|
|
service += strlen("host:");
|
|
|
|
} else {
|
|
|
|
service = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (service) {
|
|
|
|
asocket *s2;
|
|
|
|
|
|
|
|
/* some requests are handled immediately -- in that
|
|
|
|
** case the handle_host_request() routine has sent
|
|
|
|
** the OKAY or FAIL message and all we have to do
|
|
|
|
** is clean up.
|
|
|
|
*/
|
2015-05-05 22:10:43 +02:00
|
|
|
if(handle_host_request(service, type, serial, s->peer->fd, s) == 0) {
|
2009-03-04 04:32:55 +01:00
|
|
|
/* XXX fail message? */
|
2015-09-03 02:44:28 +02:00
|
|
|
D( "SS(%d): handled host service '%s'", s->id, service );
|
2009-03-04 04:32:55 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (!strncmp(service, "transport", strlen("transport"))) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D( "SS(%d): okay transport", s->id );
|
2009-03-04 04:32:55 +01:00
|
|
|
p->len = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* try to find a local service with this name.
|
|
|
|
** if no such service exists, we'll fail out
|
|
|
|
** and tear down here.
|
|
|
|
*/
|
|
|
|
s2 = create_host_service_socket(service, serial);
|
|
|
|
if(s2 == 0) {
|
2015-09-03 02:44:28 +02:00
|
|
|
D( "SS(%d): couldn't create host service '%s'", s->id, service );
|
2015-05-01 02:32:03 +02:00
|
|
|
SendFail(s->peer->fd, "unknown host service");
|
2009-03-04 04:32:55 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we've connected to a local host service,
|
|
|
|
** so we make our peer back into a regular
|
|
|
|
** local socket and bind it to the new local
|
|
|
|
** service socket, acknowledge the successful
|
|
|
|
** connection, and close this smart socket now
|
|
|
|
** that its work is done.
|
|
|
|
*/
|
2015-05-01 02:32:03 +02:00
|
|
|
SendOkay(s->peer->fd);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
s->peer->ready = local_socket_ready;
|
2013-12-13 14:09:44 +01:00
|
|
|
s->peer->shutdown = NULL;
|
2009-03-04 04:32:55 +01:00
|
|
|
s->peer->close = local_socket_close;
|
|
|
|
s->peer->peer = s2;
|
|
|
|
s2->peer = s->peer;
|
|
|
|
s->peer = 0;
|
2015-09-03 02:44:28 +02:00
|
|
|
D( "SS(%d): okay", s->id );
|
2009-03-04 04:32:55 +01:00
|
|
|
s->close(s);
|
|
|
|
|
|
|
|
/* initial state is "ready" */
|
|
|
|
s2->ready(s2);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else /* !ADB_HOST */
|
|
|
|
if (s->transport == NULL) {
|
2015-04-17 07:54:44 +02:00
|
|
|
std::string error_msg = "unknown failure";
|
2015-05-19 01:43:57 +02:00
|
|
|
s->transport =
|
|
|
|
acquire_one_transport(kCsAny, kTransportAny, NULL, &error_msg);
|
2009-03-04 04:32:55 +01:00
|
|
|
|
|
|
|
if (s->transport == NULL) {
|
2015-05-01 02:32:03 +02:00
|
|
|
SendFail(s->peer->fd, error_msg);
|
2009-03-04 04:32:55 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-05-19 01:43:57 +02:00
|
|
|
if(!(s->transport) || (s->transport->connection_state == kCsOffline)) {
|
2009-03-04 04:32:55 +01:00
|
|
|
/* if there's no remote we fail the connection
|
|
|
|
** right here and terminate it
|
|
|
|
*/
|
2015-05-01 02:32:03 +02:00
|
|
|
SendFail(s->peer->fd, "device offline (x)");
|
2009-03-04 04:32:55 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* instrument our peer to pass the success or fail
|
|
|
|
** message back once it connects or closes, then
|
|
|
|
** detach from it, request the connection, and
|
|
|
|
** tear down
|
|
|
|
*/
|
|
|
|
s->peer->ready = local_socket_ready_notify;
|
2013-12-13 14:09:44 +01:00
|
|
|
s->peer->shutdown = NULL;
|
2009-03-04 04:32:55 +01:00
|
|
|
s->peer->close = local_socket_close_notify;
|
|
|
|
s->peer->peer = 0;
|
|
|
|
/* give him our transport and upref it */
|
|
|
|
s->peer->transport = s->transport;
|
|
|
|
|
|
|
|
connect_to_remote(s->peer, (char*) (p->data + 4));
|
|
|
|
s->peer = 0;
|
|
|
|
s->close(s);
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
/* we're going to close our peer as a side-effect, so
|
|
|
|
** return -1 to signal that state to the local socket
|
|
|
|
** who is enqueueing against us
|
|
|
|
*/
|
|
|
|
s->close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void smart_socket_ready(asocket *s)
|
|
|
|
{
|
2015-09-03 02:44:28 +02:00
|
|
|
D("SS(%d): ready", s->id);
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void smart_socket_close(asocket *s)
|
|
|
|
{
|
2015-09-03 02:44:28 +02:00
|
|
|
D("SS(%d): closed", s->id);
|
2009-03-04 04:32:55 +01:00
|
|
|
if(s->pkt_first){
|
|
|
|
put_apacket(s->pkt_first);
|
|
|
|
}
|
|
|
|
if(s->peer) {
|
|
|
|
s->peer->peer = 0;
|
|
|
|
s->peer->close(s->peer);
|
2011-05-13 20:24:55 +02:00
|
|
|
s->peer = 0;
|
2009-03-04 04:32:55 +01:00
|
|
|
}
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
2013-02-21 00:04:53 +01:00
|
|
|
static asocket *create_smart_socket(void)
|
2009-03-04 04:32:55 +01:00
|
|
|
{
|
2015-09-03 02:44:28 +02:00
|
|
|
D("Creating smart socket");
|
2015-02-26 02:51:28 +01:00
|
|
|
asocket *s = reinterpret_cast<asocket*>(calloc(1, sizeof(asocket)));
|
2010-06-11 01:48:19 +02:00
|
|
|
if (s == NULL) fatal("cannot allocate socket");
|
2009-03-04 04:32:55 +01:00
|
|
|
s->enqueue = smart_socket_enqueue;
|
|
|
|
s->ready = smart_socket_ready;
|
2013-12-13 14:09:44 +01:00
|
|
|
s->shutdown = NULL;
|
2009-03-04 04:32:55 +01:00
|
|
|
s->close = smart_socket_close;
|
|
|
|
|
2015-09-03 02:44:28 +02:00
|
|
|
D("SS(%d)", s->id);
|
2009-03-04 04:32:55 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void connect_to_smartsocket(asocket *s)
|
|
|
|
{
|
2015-09-03 02:44:28 +02:00
|
|
|
D("Connecting to smart socket");
|
2013-02-21 00:04:53 +01:00
|
|
|
asocket *ss = create_smart_socket();
|
2009-03-04 04:32:55 +01:00
|
|
|
s->peer = ss;
|
|
|
|
ss->peer = s;
|
|
|
|
s->ready(s);
|
|
|
|
}
|
2015-07-13 20:12:28 +02:00
|
|
|
|
|
|
|
size_t asocket::get_max_payload() const {
|
|
|
|
size_t max_payload = MAX_PAYLOAD;
|
|
|
|
if (transport) {
|
|
|
|
max_payload = std::min(max_payload, transport->get_max_payload());
|
|
|
|
}
|
|
|
|
if (peer && peer->transport) {
|
|
|
|
max_payload = std::min(max_payload, peer->transport->get_max_payload());
|
|
|
|
}
|
|
|
|
return max_payload;
|
|
|
|
}
|