blob: 699ce27e5a321622c8a5bdf65cb2dec8e4487810 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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 <unistd.h>
20#include <errno.h>
21#include <string.h>
22#include <ctype.h>
23
24#include "sysdeps.h"
25
26#define TRACE_TAG TRACE_SOCKETS
27#include "adb.h"
28
29ADB_MUTEX_DEFINE( socket_list_lock );
30
31static void local_socket_close_locked(asocket *s);
32
33int sendfailmsg(int fd, const char *reason)
34{
35 char buf[9];
36 int len;
37 len = strlen(reason);
38 if(len > 0xffff) len = 0xffff;
39 snprintf(buf, sizeof buf, "FAIL%04x", len);
40 if(writex(fd, buf, 8)) return -1;
41 return writex(fd, reason, len);
42}
43
44//extern int online;
45
46static unsigned local_socket_next_id = 1;
47
48static asocket local_socket_list = {
49 .next = &local_socket_list,
50 .prev = &local_socket_list,
51};
52
53/* the the list of currently closing local sockets.
54** these have no peer anymore, but still packets to
55** write to their fd.
56*/
57static asocket local_socket_closing_list = {
58 .next = &local_socket_closing_list,
59 .prev = &local_socket_closing_list,
60};
61
62asocket *find_local_socket(unsigned id)
63{
64 asocket *s;
65 asocket *result = NULL;
66
67 adb_mutex_lock(&socket_list_lock);
André Goddard Rosa81828292010-06-12 11:40:20 -030068 for (s = local_socket_list.next; s != &local_socket_list; s = s->next) {
69 if (s->id == id) {
70 result = s;
71 break;
72 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073 }
74 adb_mutex_unlock(&socket_list_lock);
75
76 return result;
77}
78
79static void
80insert_local_socket(asocket* s, asocket* list)
81{
82 s->next = list;
83 s->prev = s->next->prev;
84 s->prev->next = s;
85 s->next->prev = s;
86}
87
88
89void install_local_socket(asocket *s)
90{
91 adb_mutex_lock(&socket_list_lock);
92
93 s->id = local_socket_next_id++;
94 insert_local_socket(s, &local_socket_list);
95
96 adb_mutex_unlock(&socket_list_lock);
97}
98
99void remove_socket(asocket *s)
100{
101 // socket_list_lock should already be held
102 if (s->prev && s->next)
103 {
104 s->prev->next = s->next;
105 s->next->prev = s->prev;
106 s->next = 0;
107 s->prev = 0;
108 s->id = 0;
109 }
110}
111
112void close_all_sockets(atransport *t)
113{
114 asocket *s;
115
116 /* this is a little gross, but since s->close() *will* modify
117 ** the list out from under you, your options are limited.
118 */
119 adb_mutex_lock(&socket_list_lock);
120restart:
121 for(s = local_socket_list.next; s != &local_socket_list; s = s->next){
122 if(s->transport == t || (s->peer && s->peer->transport == t)) {
123 local_socket_close_locked(s);
124 goto restart;
125 }
126 }
127 adb_mutex_unlock(&socket_list_lock);
128}
129
130static int local_socket_enqueue(asocket *s, apacket *p)
131{
132 D("LS(%d): enqueue %d\n", s->id, p->len);
133
134 p->ptr = p->data;
135
136 /* if there is already data queue'd, we will receive
137 ** events when it's time to write. just add this to
138 ** the tail
139 */
140 if(s->pkt_first) {
141 goto enqueue;
142 }
143
144 /* write as much as we can, until we
145 ** would block or there is an error/eof
146 */
147 while(p->len > 0) {
148 int r = adb_write(s->fd, p->ptr, p->len);
149 if(r > 0) {
150 p->len -= r;
151 p->ptr += r;
152 continue;
153 }
154 if((r == 0) || (errno != EAGAIN)) {
155 D( "LS(%d): not ready, errno=%d: %s\n", s->id, errno, strerror(errno) );
156 s->close(s);
157 return 1; /* not ready (error) */
158 } else {
159 break;
160 }
161 }
162
163 if(p->len == 0) {
164 put_apacket(p);
165 return 0; /* ready for more data */
166 }
167
168enqueue:
169 p->next = 0;
170 if(s->pkt_first) {
171 s->pkt_last->next = p;
172 } else {
173 s->pkt_first = p;
174 }
175 s->pkt_last = p;
176
177 /* make sure we are notified when we can drain the queue */
178 fdevent_add(&s->fde, FDE_WRITE);
179
180 return 1; /* not ready (backlog) */
181}
182
183static void local_socket_ready(asocket *s)
184{
185 /* far side is ready for data, pay attention to
186 readable events */
187 fdevent_add(&s->fde, FDE_READ);
188// D("LS(%d): ready()\n", s->id);
189}
190
191static void local_socket_close(asocket *s)
192{
193 adb_mutex_lock(&socket_list_lock);
194 local_socket_close_locked(s);
195 adb_mutex_unlock(&socket_list_lock);
196}
197
198// be sure to hold the socket list lock when calling this
199static void local_socket_destroy(asocket *s)
200{
201 apacket *p, *n;
202
203 /* IMPORTANT: the remove closes the fd
204 ** that belongs to this socket
205 */
206 fdevent_remove(&s->fde);
207
208 /* dispose of any unwritten data */
209 for(p = s->pkt_first; p; p = n) {
210 D("LS(%d): discarding %d bytes\n", s->id, p->len);
211 n = p->next;
212 put_apacket(p);
213 }
214 remove_socket(s);
215 free(s);
216}
217
218
219static void local_socket_close_locked(asocket *s)
220{
221 if(s->peer) {
222 s->peer->peer = 0;
223 // tweak to avoid deadlock
224 if (s->peer->close == local_socket_close)
225 local_socket_close_locked(s->peer);
226 else
227 s->peer->close(s->peer);
228 }
229
230 /* If we are already closing, or if there are no
231 ** pending packets, destroy immediately
232 */
233 if (s->closing || s->pkt_first == NULL) {
234 int id = s->id;
235 local_socket_destroy(s);
236 D("LS(%d): closed\n", id);
237 return;
238 }
239
240 /* otherwise, put on the closing list
241 */
242 D("LS(%d): closing\n", s->id);
243 s->closing = 1;
244 fdevent_del(&s->fde, FDE_READ);
245 remove_socket(s);
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800246 D("LS(%d): put on socket_closing_list fd=%d\n", s->id, s->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247 insert_local_socket(s, &local_socket_closing_list);
248}
249
250static void local_socket_event_func(int fd, unsigned ev, void *_s)
251{
252 asocket *s = _s;
253
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800254 D("LS(%d): event_func(fd=%d, ev=%04x)\n", s->id, s->fd, ev);
255
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800256 /* put the FDE_WRITE processing before the FDE_READ
257 ** in order to simplify the code.
258 */
259 if(ev & FDE_WRITE){
260 apacket *p;
261
262 while((p = s->pkt_first) != 0) {
263 while(p->len > 0) {
264 int r = adb_write(fd, p->ptr, p->len);
265 if(r > 0) {
266 p->ptr += r;
267 p->len -= r;
268 continue;
269 }
270 if(r < 0) {
271 /* returning here is ok because FDE_READ will
272 ** be processed in the next iteration loop
273 */
274 if(errno == EAGAIN) return;
275 if(errno == EINTR) continue;
276 }
277 s->close(s);
278 return;
279 }
280
281 if(p->len == 0) {
282 s->pkt_first = p->next;
283 if(s->pkt_first == 0) s->pkt_last = 0;
284 put_apacket(p);
285 }
286 }
287
288 /* if we sent the last packet of a closing socket,
289 ** we can now destroy it.
290 */
291 if (s->closing) {
292 s->close(s);
293 return;
294 }
295
296 /* no more packets queued, so we can ignore
297 ** writable events again and tell our peer
298 ** to resume writing
299 */
300 fdevent_del(&s->fde, FDE_WRITE);
301 s->peer->ready(s->peer);
302 }
303
304
305 if(ev & FDE_READ){
306 apacket *p = get_apacket();
307 unsigned char *x = p->data;
308 size_t avail = MAX_PAYLOAD;
309 int r;
310 int is_eof = 0;
311
312 while(avail > 0) {
313 r = adb_read(fd, x, avail);
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800314 D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%d\n", s->id, s->fd, r, r<0?errno:0, avail);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800315 if(r > 0) {
316 avail -= r;
317 x += r;
318 continue;
319 }
320 if(r < 0) {
321 if(errno == EAGAIN) break;
322 if(errno == EINTR) continue;
323 }
324
325 /* r = 0 or unhandled error */
326 is_eof = 1;
327 break;
328 }
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800329 D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d\n", s->id, s->fd, r, is_eof);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330
331 if((avail == MAX_PAYLOAD) || (s->peer == 0)) {
332 put_apacket(p);
333 } else {
334 p->len = MAX_PAYLOAD - avail;
335
336 r = s->peer->enqueue(s->peer, p);
337
338 if(r < 0) {
339 /* error return means they closed us as a side-effect
340 ** and we must return immediately.
341 **
342 ** note that if we still have buffered packets, the
343 ** socket will be placed on the closing socket list.
344 ** this handler function will be called again
345 ** to process FDE_WRITE events.
346 */
347 return;
348 }
349
350 if(r > 0) {
351 /* if the remote cannot accept further events,
352 ** we disable notification of READs. They'll
353 ** be enabled again when we get a call to ready()
354 */
355 fdevent_del(&s->fde, FDE_READ);
356 }
357 }
358
359 if(is_eof) {
360 s->close(s);
361 }
362 }
363
364 if(ev & FDE_ERROR){
365 /* this should be caught be the next read or write
366 ** catching it here means we may skip the last few
367 ** bytes of readable data.
368 */
369// s->close(s);
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800370 D("LS(%d): FDE_ERROR (fd=%d)\n", s->id, s->fd);
371
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372 return;
373 }
374}
375
376asocket *create_local_socket(int fd)
377{
378 asocket *s = calloc(1, sizeof(asocket));
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300379 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800380 s->fd = fd;
381 s->enqueue = local_socket_enqueue;
382 s->ready = local_socket_ready;
383 s->close = local_socket_close;
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800384 install_local_socket(s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385
386 fdevent_install(&s->fde, fd, local_socket_event_func, s);
387/* fdevent_add(&s->fde, FDE_ERROR); */
388 //fprintf(stderr, "Created local socket in create_local_socket \n");
389 D("LS(%d): created (fd=%d)\n", s->id, s->fd);
390 return s;
391}
392
393asocket *create_local_service_socket(const char *name)
394{
395 asocket *s;
396 int fd;
397
398#if !ADB_HOST
399 if (!strcmp(name,"jdwp")) {
400 return create_jdwp_service_socket();
401 }
402 if (!strcmp(name,"track-jdwp")) {
403 return create_jdwp_tracker_service_socket();
404 }
405#endif
406 fd = service_to_fd(name);
407 if(fd < 0) return 0;
408
409 s = create_local_socket(fd);
JP Abgrall69c5c4c2011-02-18 14:16:59 -0800410 D("LS(%d): bound to '%s' via %d\n", s->id, name, fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800411 return s;
412}
413
414#if ADB_HOST
415static asocket *create_host_service_socket(const char *name, const char* serial)
416{
417 asocket *s;
418
419 s = host_service_to_socket(name, serial);
420
421 if (s != NULL) {
422 D("LS(%d) bound to '%s'\n", s->id, name);
423 return s;
424 }
425
426 return s;
427}
428#endif /* ADB_HOST */
429
430/* a Remote socket is used to send/receive data to/from a given transport object
431** it needs to be closed when the transport is forcibly destroyed by the user
432*/
433typedef struct aremotesocket {
434 asocket socket;
435 adisconnect disconnect;
436} aremotesocket;
437
438static int remote_socket_enqueue(asocket *s, apacket *p)
439{
440 D("Calling remote_socket_enqueue\n");
441 p->msg.command = A_WRTE;
442 p->msg.arg0 = s->peer->id;
443 p->msg.arg1 = s->id;
444 p->msg.data_length = p->len;
445 send_packet(p, s->transport);
446 return 1;
447}
448
449static void remote_socket_ready(asocket *s)
450{
451 D("Calling remote_socket_ready\n");
452 apacket *p = get_apacket();
453 p->msg.command = A_OKAY;
454 p->msg.arg0 = s->peer->id;
455 p->msg.arg1 = s->id;
456 send_packet(p, s->transport);
457}
458
459static void remote_socket_close(asocket *s)
460{
461 D("Calling remote_socket_close\n");
462 apacket *p = get_apacket();
463 p->msg.command = A_CLSE;
464 if(s->peer) {
465 p->msg.arg0 = s->peer->id;
466 s->peer->peer = 0;
467 s->peer->close(s->peer);
468 }
469 p->msg.arg1 = s->id;
470 send_packet(p, s->transport);
471 D("RS(%d): closed\n", s->id);
472 remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect );
473 free(s);
474}
475
476static void remote_socket_disconnect(void* _s, atransport* t)
477{
478 asocket* s = _s;
479 asocket* peer = s->peer;
480
481 D("remote_socket_disconnect RS(%d)\n", s->id);
482 if (peer) {
483 peer->peer = NULL;
484 peer->close(peer);
485 }
486 remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect );
487 free(s);
488}
489
490asocket *create_remote_socket(unsigned id, atransport *t)
491{
492 asocket *s = calloc(1, sizeof(aremotesocket));
493 adisconnect* dis = &((aremotesocket*)s)->disconnect;
494
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300495 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800496 s->id = id;
497 s->enqueue = remote_socket_enqueue;
498 s->ready = remote_socket_ready;
499 s->close = remote_socket_close;
500 s->transport = t;
501
502 dis->func = remote_socket_disconnect;
503 dis->opaque = s;
504 add_transport_disconnect( t, dis );
505 D("RS(%d): created\n", s->id);
506 return s;
507}
508
509void connect_to_remote(asocket *s, const char *destination)
510{
511 D("Connect_to_remote call \n");
512 apacket *p = get_apacket();
513 int len = strlen(destination) + 1;
514
515 if(len > (MAX_PAYLOAD-1)) {
516 fatal("destination oversized");
517 }
518
519 D("LS(%d): connect('%s')\n", s->id, destination);
520 p->msg.command = A_OPEN;
521 p->msg.arg0 = s->id;
522 p->msg.data_length = len;
523 strcpy((char*) p->data, destination);
524 send_packet(p, s->transport);
525}
526
527
528/* this is used by magic sockets to rig local sockets to
529 send the go-ahead message when they connect */
530static void local_socket_ready_notify(asocket *s)
531{
532 s->ready = local_socket_ready;
533 s->close = local_socket_close;
534 adb_write(s->fd, "OKAY", 4);
535 s->ready(s);
536}
537
538/* this is used by magic sockets to rig local sockets to
539 send the failure message if they are closed before
540 connected (to avoid closing them without a status message) */
541static void local_socket_close_notify(asocket *s)
542{
543 s->ready = local_socket_ready;
544 s->close = local_socket_close;
545 sendfailmsg(s->fd, "closed");
546 s->close(s);
547}
548
549unsigned unhex(unsigned char *s, int len)
550{
551 unsigned n = 0, c;
552
553 while(len-- > 0) {
554 switch((c = *s++)) {
555 case '0': case '1': case '2':
556 case '3': case '4': case '5':
557 case '6': case '7': case '8':
558 case '9':
559 c -= '0';
560 break;
561 case 'a': case 'b': case 'c':
562 case 'd': case 'e': case 'f':
563 c = c - 'a' + 10;
564 break;
565 case 'A': case 'B': case 'C':
566 case 'D': case 'E': case 'F':
567 c = c - 'A' + 10;
568 break;
569 default:
570 return 0xffffffff;
571 }
572
573 n = (n << 4) | c;
574 }
575
576 return n;
577}
578
579static int smart_socket_enqueue(asocket *s, apacket *p)
580{
581 unsigned len;
582#if ADB_HOST
583 char *service = NULL;
584 char* serial = NULL;
585 transport_type ttype = kTransportAny;
586#endif
587
588 D("SS(%d): enqueue %d\n", s->id, p->len);
589
590 if(s->pkt_first == 0) {
591 s->pkt_first = p;
592 s->pkt_last = p;
593 } else {
594 if((s->pkt_first->len + p->len) > MAX_PAYLOAD) {
595 D("SS(%d): overflow\n", s->id);
596 put_apacket(p);
597 goto fail;
598 }
599
600 memcpy(s->pkt_first->data + s->pkt_first->len,
601 p->data, p->len);
602 s->pkt_first->len += p->len;
603 put_apacket(p);
604
605 p = s->pkt_first;
606 }
607
608 /* don't bother if we can't decode the length */
609 if(p->len < 4) return 0;
610
611 len = unhex(p->data, 4);
612 if((len < 1) || (len > 1024)) {
613 D("SS(%d): bad size (%d)\n", s->id, len);
614 goto fail;
615 }
616
617 D("SS(%d): len is %d\n", s->id, len );
618 /* can't do anything until we have the full header */
619 if((len + 4) > p->len) {
620 D("SS(%d): waiting for %d more bytes\n", s->id, len+4 - p->len);
621 return 0;
622 }
623
624 p->data[len + 4] = 0;
625
626 D("SS(%d): '%s'\n", s->id, (char*) (p->data + 4));
627
628#if ADB_HOST
629 service = (char *)p->data + 4;
630 if(!strncmp(service, "host-serial:", strlen("host-serial:"))) {
631 char* serial_end;
632 service += strlen("host-serial:");
633
634 // serial number should follow "host:"
635 serial_end = strchr(service, ':');
636 if (serial_end) {
637 *serial_end = 0; // terminate string
638 serial = service;
639 service = serial_end + 1;
640 }
641 } else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) {
642 ttype = kTransportUsb;
643 service += strlen("host-usb:");
644 } else if (!strncmp(service, "host-local:", strlen("host-local:"))) {
645 ttype = kTransportLocal;
646 service += strlen("host-local:");
647 } else if (!strncmp(service, "host:", strlen("host:"))) {
648 ttype = kTransportAny;
649 service += strlen("host:");
650 } else {
651 service = NULL;
652 }
653
654 if (service) {
655 asocket *s2;
656
657 /* some requests are handled immediately -- in that
658 ** case the handle_host_request() routine has sent
659 ** the OKAY or FAIL message and all we have to do
660 ** is clean up.
661 */
662 if(handle_host_request(service, ttype, serial, s->peer->fd, s) == 0) {
663 /* XXX fail message? */
664 D( "SS(%d): handled host service '%s'\n", s->id, service );
665 goto fail;
666 }
667 if (!strncmp(service, "transport", strlen("transport"))) {
668 D( "SS(%d): okay transport\n", s->id );
669 p->len = 0;
670 return 0;
671 }
672
673 /* try to find a local service with this name.
674 ** if no such service exists, we'll fail out
675 ** and tear down here.
676 */
677 s2 = create_host_service_socket(service, serial);
678 if(s2 == 0) {
679 D( "SS(%d): couldn't create host service '%s'\n", s->id, service );
680 sendfailmsg(s->peer->fd, "unknown host service");
681 goto fail;
682 }
683
684 /* we've connected to a local host service,
685 ** so we make our peer back into a regular
686 ** local socket and bind it to the new local
687 ** service socket, acknowledge the successful
688 ** connection, and close this smart socket now
689 ** that its work is done.
690 */
691 adb_write(s->peer->fd, "OKAY", 4);
692
693 s->peer->ready = local_socket_ready;
694 s->peer->close = local_socket_close;
695 s->peer->peer = s2;
696 s2->peer = s->peer;
697 s->peer = 0;
698 D( "SS(%d): okay\n", s->id );
699 s->close(s);
700
701 /* initial state is "ready" */
702 s2->ready(s2);
703 return 0;
704 }
705#else /* !ADB_HOST */
706 if (s->transport == NULL) {
707 char* error_string = "unknown failure";
708 s->transport = acquire_one_transport (CS_ANY,
709 kTransportAny, NULL, &error_string);
710
711 if (s->transport == NULL) {
712 sendfailmsg(s->peer->fd, error_string);
713 goto fail;
714 }
715 }
716#endif
717
718 if(!(s->transport) || (s->transport->connection_state == CS_OFFLINE)) {
719 /* if there's no remote we fail the connection
720 ** right here and terminate it
721 */
722 sendfailmsg(s->peer->fd, "device offline (x)");
723 goto fail;
724 }
725
726
727 /* instrument our peer to pass the success or fail
728 ** message back once it connects or closes, then
729 ** detach from it, request the connection, and
730 ** tear down
731 */
732 s->peer->ready = local_socket_ready_notify;
733 s->peer->close = local_socket_close_notify;
734 s->peer->peer = 0;
735 /* give him our transport and upref it */
736 s->peer->transport = s->transport;
737
738 connect_to_remote(s->peer, (char*) (p->data + 4));
739 s->peer = 0;
740 s->close(s);
741 return 1;
742
743fail:
744 /* we're going to close our peer as a side-effect, so
745 ** return -1 to signal that state to the local socket
746 ** who is enqueueing against us
747 */
748 s->close(s);
749 return -1;
750}
751
752static void smart_socket_ready(asocket *s)
753{
754 D("SS(%d): ready\n", s->id);
755}
756
757static void smart_socket_close(asocket *s)
758{
759 D("SS(%d): closed\n", s->id);
760 if(s->pkt_first){
761 put_apacket(s->pkt_first);
762 }
763 if(s->peer) {
764 s->peer->peer = 0;
765 s->peer->close(s->peer);
766 }
767 free(s);
768}
769
770asocket *create_smart_socket(void (*action_cb)(asocket *s, const char *act))
771{
772 D("Creating smart socket \n");
773 asocket *s = calloc(1, sizeof(asocket));
André Goddard Rosac419e2a2010-06-10 20:48:19 -0300774 if (s == NULL) fatal("cannot allocate socket");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800775 s->enqueue = smart_socket_enqueue;
776 s->ready = smart_socket_ready;
777 s->close = smart_socket_close;
778 s->extra = action_cb;
779
780 D("SS(%d): created %p\n", s->id, action_cb);
781 return s;
782}
783
784void smart_socket_action(asocket *s, const char *act)
785{
786
787}
788
789void connect_to_smartsocket(asocket *s)
790{
791 D("Connecting to smart socket \n");
792 asocket *ss = create_smart_socket(smart_socket_action);
793 s->peer = ss;
794 ss->peer = s;
795 s->ready(s);
796}