]>
Commit | Line | Data |
---|---|---|
5580185e | 1 | /* |
2 | * $Source$ | |
3 | * $Header$ | |
4 | */ | |
5 | ||
6 | #ifndef lint | |
7 | static char *rcsid_gdb_conn_c = "$Header$"; | |
8 | #endif lint | |
9 | ||
10 | ||
11 | ||
12 | ||
13 | ||
14 | ||
15 | ||
16 | ||
17 | ||
18 | ||
19 | ||
20 | ||
21 | ||
22 | ||
23 | ||
24 | ||
25 | ||
26 | ||
27 | ||
28 | ||
29 | ||
30 | ||
31 | ||
32 | ||
33 | /************************************************************************/ | |
34 | /* | |
35 | /* gdb_conn.c | |
36 | /* | |
37 | /* GDB - Connection Management Services | |
38 | /* | |
39 | /* Author: Noah Mendelsohn | |
40 | /* Copyright: 1986 MIT Project Athena | |
41 | /* | |
42 | /* Routines used in the creation and maintenance of CONNECTIONS. | |
43 | /* Note: these are closely related to the services provided | |
44 | /* by gdb_trans.c and gdb_trans2.c. | |
45 | /* | |
46 | /* | |
47 | /************************************************************************/ | |
48 | ||
49 | #include <stdio.h> | |
50 | #include <strings.h> | |
51 | #include "gdb.h" | |
52 | #include <sys/types.h> | |
53 | #include <sys/uio.h> | |
54 | #include <sys/socket.h> | |
55 | #include <sys/ioctl.h> | |
56 | #include <netinet/in.h> | |
57 | #include <netdb.h> | |
58 | #include <errno.h> | |
59 | ||
60 | extern int errno; | |
61 | #ifdef vax | |
62 | extern u_short htons(); /* ?? All versions? */ | |
63 | #endif vax | |
64 | ||
65 | CONNECTION gdb_allocate_connection(); | |
66 | ||
67 | /************************************************************************/ | |
68 | /* | |
69 | /* start_peer_connection (start_peer_connection) | |
70 | /* | |
71 | /* Starts a connection to another process which itself will be | |
72 | /* issuing a start_peer_connection to us. Current implementation | |
73 | /* builds at most one stream, with the risk of a hang if | |
74 | /* the attempts to connect cross in the night. This is a bug, | |
75 | /* but this level of support is acceptable for casual debugging | |
76 | /* of applications, and perhaps for some production use in | |
77 | /* controlled settings. I think the only other way to do it | |
78 | /* is to risk building two streams in parallel, possibly tearing | |
79 | /* one down when the duplication is discovered. Seems complicated | |
80 | /* and messy. | |
81 | /* | |
82 | /************************************************************************/ | |
83 | ||
84 | CONNECTION | |
85 | start_peer_connection(id) | |
86 | char *id; /* null terminated string */ | |
87 | { | |
88 | register CONNECTION con; /* the connection we're */ | |
89 | /* creating */ | |
90 | ||
91 | GDB_INIT_CHECK | |
92 | ||
93 | /* | |
94 | * Try to allocate a connection and fill it in with null values. | |
95 | */ | |
96 | ||
97 | con = g_make_con(); | |
98 | ||
99 | /* | |
100 | * In this implementation, we use a single fd for both inbound and | |
101 | * outbound traffic. Try to connect to other side. If that | |
102 | * doesn't work, wait to accept a connection from the other side. | |
103 | * Current implementation of this is synchronous--may be a problem? | |
104 | * Also note timing window bug in the following. If the two peers | |
105 | * are started at just about the same time, the race may not be handled | |
106 | * propoerly. If the connections come up, then verify the level of | |
107 | * protocol being observed on the connections. If incompatible, | |
108 | * then turn off the connection. | |
109 | */ | |
110 | ||
111 | if(!g_try_connecting(con,id)) { | |
112 | g_try_accepting(con,id); | |
113 | if(con->status == CON_STARTING) | |
114 | g_ver_iprotocol(con); | |
115 | } else | |
116 | if(con->status == CON_STARTING) | |
117 | g_ver_oprotocol(con); | |
118 | ||
119 | ||
120 | if (con->status == CON_UP) { | |
121 | /* | |
122 | * We've successfully started the connection, now mark | |
123 | * it for non-blocking I/O. Also, update the high water | |
124 | * mark of fd's controlled by our system. | |
125 | */ | |
126 | int nb = 1; | |
127 | if(ioctl(con->in.fd, FIONBIO, (char *)&nb)== (-1)) { | |
128 | g_stop_with_errno(con); | |
129 | return con; | |
130 | } | |
131 | if (con->in.fd +1 > gdb_mfd) | |
132 | gdb_mfd = con->in.fd + 1; | |
133 | /* | |
134 | * Allocate a buffer, if necessary, and reset buffer pointers | |
135 | * so first request will result in a long read into the buffer | |
136 | */ | |
137 | g_allocate_connection_buffers(con); | |
138 | ||
139 | return con; | |
140 | } else | |
141 | return NULL; | |
142 | } | |
143 | \f | |
144 | /************************************************************************/ | |
145 | /* | |
146 | /* g_make_con | |
147 | /* | |
148 | /* Internal routine to allocate a new connection structure and | |
149 | /* initialize all its fields to logical null values. | |
150 | /* | |
151 | /************************************************************************/ | |
152 | ||
153 | CONNECTION | |
154 | g_make_con() | |
155 | { | |
156 | register CONNECTION con; | |
157 | ||
158 | /* | |
159 | * Try to allocate a connection, fatal error if none available | |
160 | */ | |
161 | con = gdb_allocate_connection(); | |
162 | if (con == NULL) | |
163 | GDB_GIVEUP("start_peer_connection: Tried to allocate too many connections") /* <==RECOVERABLE */ | |
164 | ||
165 | /* | |
166 | * Give the fields their initial values | |
167 | */ | |
168 | g_null_con(con); | |
169 | ||
170 | return con; | |
171 | ||
172 | } | |
173 | ||
174 | \f/************************************************************************/ | |
175 | /* | |
176 | /* g_null_con | |
177 | /* | |
178 | /* Sets a connection descriptor to have all null values in | |
179 | /* its fields. This routine does NOT do any of the cleanup | |
180 | /* which is necessary after the connection has really been used. | |
181 | /* | |
182 | /************************************************************************/ | |
183 | ||
184 | int | |
185 | g_null_con(con) | |
186 | CONNECTION con; | |
187 | { | |
188 | /* | |
189 | * Initialize the connection control data structure. | |
190 | */ | |
191 | con->id = GDB_CON_ID; | |
192 | con->status = CON_STARTING; | |
193 | con->oob_fcn = NULL; /* out of band signalling */ | |
194 | /* is not currently */ | |
195 | /* implemented */ | |
196 | con->errno = 0; /* system errno gets */ | |
197 | /* copied here iff it */ | |
198 | /* causes this con to die */ | |
199 | /* | |
200 | * Initialize input half connection to null state before trying | |
201 | * to bring it up. | |
202 | */ | |
203 | con->in.status = OP_NOT_STARTED; | |
204 | con->in.fd = -1; | |
205 | con->in.oob_fd = -1; | |
206 | con->in.op_q_first = (struct oper_data *)&con->in; | |
207 | con->in.op_q_last = (struct oper_data *)&con->in; | |
208 | con->in.next_byte = NULL; | |
209 | con->in.remaining = 0; | |
210 | con->in.flags = 0; | |
211 | ||
212 | /* | |
213 | * Initialize output half connection to null state before trying | |
214 | * to bring it up. | |
215 | */ | |
216 | con->out.status = OP_NOT_STARTED; | |
217 | con->out.fd = -1; | |
218 | con->out.oob_fd = -1; | |
219 | con->out.op_q_first = (struct oper_data *)&con->out; | |
220 | con->out.op_q_last = (struct oper_data *)&con->out; | |
221 | con->out.next_byte = NULL; | |
222 | con->out.remaining = 0; | |
223 | con->out.flags = 0; | |
224 | ||
225 | return; | |
226 | ||
227 | } | |
228 | ||
229 | \f | |
230 | /************************************************************************/ | |
231 | /* | |
232 | /* gdb_allocate_connection | |
233 | /* | |
234 | /* Return an unused entry in the connection array. Unused entries | |
235 | /* are recognized by being marked as CON_STOPPED. | |
236 | /* | |
237 | /* Note that gdb_mcons is the number of descriptors which have | |
238 | /* ever been used (i.e. a high water mark), so status fields | |
239 | /* are invalid above that. | |
240 | /* | |
241 | /************************************************************************/ | |
242 | ||
243 | CONNECTION | |
244 | gdb_allocate_connection() | |
245 | { | |
246 | register int i; /* index of next one */ | |
247 | /* to check */ | |
248 | ||
249 | /* | |
250 | * First look for one below the high water mark | |
251 | */ | |
252 | for(i=0; i<gdb_mcons; i++) { | |
253 | if (gdb_cons[i].status == CON_STOPPED) | |
254 | return &gdb_cons[i]; | |
255 | } | |
256 | ||
257 | /* | |
258 | * Allocate one which has never been used, if possible | |
259 | */ | |
260 | ||
261 | if (i>=GDB_MAX_CONNECTIONS) | |
262 | GDB_GIVEUP("gdb: tried to allocate too many simulataneous connections.\n, See GDB_MAX_CONNECTIONS in gdb.h.") /* <==RECOVERABLE */ | |
263 | ||
264 | gdb_mcons++; /* bump the high water mark */ | |
265 | gdb_cons[i].status = CON_STOPPED; /* initialize status of the */ | |
266 | /* new connection */ | |
267 | return &gdb_cons[i]; /* return new highest con */ | |
268 | /* ever used*/ | |
269 | } | |
270 | \f | |
271 | /************************************************************************/ | |
272 | /* | |
273 | /* g_try_connecting | |
274 | /* | |
275 | /* Try to start a connection to the designated site, filling | |
276 | /* in the appropriate information in the connection descriptor | |
277 | /* if successful. Return TRUE if connection succeeded or if | |
278 | /* error was fatal enough that we shouldn't try accepting. Returns | |
279 | /* FALSE if we should try accepting. | |
280 | /* | |
281 | /************************************************************************/ | |
282 | ||
283 | int | |
284 | ||
285 | g_try_connecting(con,id) | |
286 | CONNECTION con; | |
287 | char *id; | |
288 | { | |
289 | int peer; /* socket for talking to | |
290 | peer */ | |
291 | struct sockaddr_in target; /* build the peer address */ | |
292 | /* here */ | |
293 | struct hostent *peer_host; /* host where peer is */ | |
294 | ||
295 | /*----------------------------------------------------------*/ | |
296 | /* | |
297 | /* Make sure connection is marked stopped until we | |
298 | /* get it going. | |
299 | /* | |
300 | /*----------------------------------------------------------*/ | |
301 | ||
302 | con->status = CON_STOPPED; | |
303 | ||
304 | /*----------------------------------------------------------*/ | |
305 | /* | |
306 | /* Find out host where peer is, and validate it. Take | |
307 | /* care of port at the same time. | |
308 | /* | |
309 | /*----------------------------------------------------------*/ | |
310 | ||
311 | bzero((char *)&target, sizeof(target)); | |
312 | g_parse_target(id, &peer_host, &target.sin_port); | |
313 | if (peer_host == NULL) { | |
314 | fprintf(gdb_log,"gdb: g_try_connecting... '%s' is not a valid host:server\n", | |
315 | id); | |
316 | return TRUE; /* so we won't try accepting */ | |
317 | } | |
318 | ||
319 | /*----------------------------------------------------------*/ | |
320 | /* | |
321 | /* Create a socket | |
322 | /* | |
323 | /*----------------------------------------------------------*/ | |
324 | ||
325 | peer = socket(AF_INET, SOCK_STREAM, 0); | |
326 | if (peer < 0) { | |
327 | g_stop_with_errno(con); | |
328 | return TRUE; /* fatal error */ | |
329 | } | |
330 | ||
331 | /*----------------------------------------------------------*/ | |
332 | /* | |
333 | /* Get information and bind socket using well known | |
334 | /* port (BUG: this restricts us to one pair of peers | |
335 | /* per host pair, as well as being bad practice on | |
336 | /* the network. It will do for debugging. | |
337 | /* | |
338 | /*----------------------------------------------------------*/ | |
339 | ||
340 | ||
341 | bcopy(peer_host->h_addr, (char *)&target.sin_addr, peer_host->h_length); | |
342 | target.sin_family = peer_host->h_addrtype; | |
343 | ||
344 | /*----------------------------------------------------------*/ | |
345 | /* | |
346 | /* Make the connection | |
347 | /* | |
348 | /*----------------------------------------------------------*/ | |
349 | ||
350 | if(connect(peer, (struct sockaddr *)&target, sizeof(target)) < 0) { | |
351 | if (errno == ECONNREFUSED) | |
352 | return FALSE; /* other side not yet */ | |
353 | /* up, but no other fatal */ | |
354 | /* errors*/ | |
355 | else { | |
356 | gdb_perror("gdb: unexpected error connecting"); | |
357 | g_stop_with_errno(con); | |
358 | return TRUE; | |
359 | } | |
360 | } | |
361 | ||
362 | /*----------------------------------------------------------*/ | |
363 | /* | |
364 | /* The connection has been made, fill in the connection | |
365 | /* control data structure. | |
366 | /* | |
367 | /*----------------------------------------------------------*/ | |
368 | ||
369 | con->in.fd = peer; | |
370 | con->out.fd = peer; | |
371 | con->status = CON_STARTING; | |
372 | ||
373 | return TRUE; | |
374 | ||
375 | } | |
376 | \f | |
377 | /************************************************************************/ | |
378 | /* | |
379 | /* g_parse_target | |
380 | /* | |
381 | /* For a given server or peer i.d., figure out the host and the | |
382 | /* port. Arguments are: | |
383 | /* | |
384 | /* string i.d. of the server, which is | |
385 | /* in one of two forms: | |
386 | /* | |
387 | /* host:servicename (where service name must not begin | |
388 | /* with #) | |
389 | /* | |
390 | /* host:#portnumber (where portnumber is the actual | |
391 | /* number of the port to be used) | |
392 | /* | |
393 | /* (actually, a 3rd form, with no port number supplied, | |
394 | /* will use a default GDB_PORT, but this is unsafe | |
395 | /* and it will be disabled in production versions | |
396 | /* of the gdb system.) | |
397 | /* | |
398 | /* **hostent: returned to indicate host to be used. Null | |
399 | /* if host could not be found | |
400 | /* | |
401 | /* *port pointer to an integer where the port number will | |
402 | /* be put. We return the port number in network | |
403 | /* byte order. | |
404 | /* | |
405 | /************************************************************************/ | |
406 | ||
407 | int | |
408 | g_parse_target(id, host, port) | |
409 | char *id; | |
410 | struct hostent **host; | |
411 | u_short *port; | |
412 | { | |
413 | char buffer[256]; /* longest host name */ | |
414 | register char *ip, *bp; /* for copying name */ | |
415 | struct servent *serv; /* returned from */ | |
416 | /* get service by name */ | |
417 | ||
418 | /*----------------------------------------------------------*/ | |
419 | /* | |
420 | /* copy the host name part only to local buffer | |
421 | /* | |
422 | /*----------------------------------------------------------*/ | |
423 | ||
424 | ip = id; | |
425 | bp = buffer; | |
426 | ||
427 | while (*ip != '\0' && *ip != ':') | |
428 | *bp++ = *ip++; | |
429 | *bp = '\0'; | |
430 | ||
431 | /*----------------------------------------------------------*/ | |
432 | /* | |
433 | /* Look up the host name, return if bad. | |
434 | /* | |
435 | /*----------------------------------------------------------*/ | |
436 | ||
437 | *host = gethostbyname(buffer); | |
438 | ||
439 | if (*host == NULL) | |
440 | return; | |
441 | ||
442 | /*----------------------------------------------------------*/ | |
443 | /* | |
444 | /* Set up the port address | |
445 | /* | |
446 | /*----------------------------------------------------------*/ | |
447 | ||
448 | if (*ip++ != ':') { | |
449 | *port = GDB_PORT; | |
450 | return; | |
451 | } | |
452 | ||
453 | if (*ip == '\0') { | |
454 | *host = NULL; | |
455 | return; | |
456 | } | |
457 | ||
458 | if (*ip == '#') { | |
459 | /* | |
460 | * port number supplied explictly | |
461 | */ | |
462 | ip++; | |
463 | if (*ip < '0' || *ip>'9') { | |
464 | *host = NULL; | |
465 | return; | |
466 | } | |
467 | *port = htons((u_short)atoi(ip)); | |
468 | } else { | |
469 | /* | |
470 | * service identified by name | |
471 | */ | |
472 | serv = getservbyname(ip, "tcp"); | |
473 | if (serv == NULL) { | |
474 | *host = NULL; | |
475 | return; | |
476 | } | |
477 | *port = serv->s_port; | |
478 | } | |
479 | } | |
480 | \f | |
481 | /************************************************************************/ | |
482 | /* | |
483 | /* g_try_accepting | |
484 | /* | |
485 | /* Try to accept a connection to the designated site, filling | |
486 | /* in the appropriate information in the connection descriptor | |
487 | /* if successful. | |
488 | /* | |
489 | /************************************************************************/ | |
490 | ||
491 | int | |
492 | g_try_accepting(con,id) | |
493 | CONNECTION con; | |
494 | char *id; | |
495 | { | |
496 | int slisten; /* socket on which | |
497 | we listen for connections */ | |
498 | ||
499 | int peer; /* socket for talking to | |
500 | peer */ | |
501 | int fromlen; | |
502 | struct sockaddr_in self, from; | |
503 | int retries = GDB_BIND_RETRY_COUNT; | |
504 | int onoff = 1; /* used as argument to */ | |
505 | /* setsockopt */ | |
506 | ||
507 | struct hostent *peer_host; /* host where peer is */ | |
508 | ||
509 | /*----------------------------------------------------------*/ | |
510 | /* | |
511 | /* Make sure connection is marked stopped until we | |
512 | /* get it going. | |
513 | /* | |
514 | /*----------------------------------------------------------*/ | |
515 | ||
516 | con->status = CON_STOPPED; | |
517 | ||
518 | /*----------------------------------------------------------*/ | |
519 | /* | |
520 | /* Create a socket on which to listen. Tell it that | |
521 | /* it's OK to re-use the port address, which may still | |
522 | /* appear busy if connections are taking awhile to go | |
523 | /* away. | |
524 | /* | |
525 | /*----------------------------------------------------------*/ | |
526 | ||
527 | slisten = socket(AF_INET, SOCK_STREAM, 0); | |
528 | if (slisten < 0) { | |
529 | gdb_perror("g_try_accepting: error creating listen socket"); | |
530 | g_stop_with_errno(con); | |
531 | } | |
532 | /* Try 4.2 method */ | |
533 | if(setsockopt(slisten, SOL_SOCKET, SO_REUSEADDR, (char *)0, 0)<0) | |
534 | /* that didn't work, try 4.3 */ | |
535 | if(setsockopt(slisten, SOL_SOCKET, SO_REUSEADDR, | |
536 | (char *)&onoff, sizeof(int)) <0) | |
537 | GDB_GIVEUP("g_try_accepting: could not set SO_REUSEADDR"); | |
538 | ||
539 | /*----------------------------------------------------------*/ | |
540 | /* | |
541 | /* Find out host where peer is, and validate it. Take | |
542 | /* care of port at the same time. This is redundant | |
543 | /* given that g_try_connecting is always called first. | |
544 | /* | |
545 | /*----------------------------------------------------------*/ | |
546 | ||
547 | bzero((char *)&self, sizeof(self)); | |
548 | g_parse_target(id, &peer_host, &self.sin_port); | |
549 | if (peer_host == NULL) { | |
550 | GDB_GIVEUP("gdb_try_accepting: bad port not caught by try connecting") | |
551 | } | |
552 | ||
553 | /*----------------------------------------------------------*/ | |
554 | /* | |
555 | /* Bind the socket to ourselves, using the well known | |
556 | /* port (See bug note in g_try_connecting. | |
557 | /* | |
558 | /* This code should really go in initialization, I think. | |
559 | /* | |
560 | /*----------------------------------------------------------*/ | |
561 | ||
562 | while (bind(slisten,(struct sockaddr *)&self,sizeof(self)) < 0) { | |
563 | if (errno == EADDRINUSE && retries--) { | |
564 | fprintf(gdb_log,"gdb: port address in use, will retry %d more time(s)\n",retries+1); | |
565 | sleep(GDB_BIND_RETRY_INTERVAL); | |
566 | continue; | |
567 | } else { | |
568 | gdb_perror("gdb: error binding listen socket"); | |
569 | g_stop_with_errno(con); | |
570 | (void) close(slisten); | |
571 | return; | |
572 | } | |
573 | } | |
574 | ||
575 | /*----------------------------------------------------------*/ | |
576 | /* | |
577 | /* Listen for connections. | |
578 | /* | |
579 | /*----------------------------------------------------------*/ | |
580 | ||
581 | (void) listen (slisten, 5); /* does not block, just */ | |
582 | /* sets the maximum backlog */ | |
583 | /* of pending non-accepted */ | |
584 | /* cons.*/ | |
585 | fromlen = sizeof(from); | |
586 | peer = accept(slisten, &from, &fromlen); | |
587 | if (peer < 0) { | |
588 | g_stop_with_errno(con); | |
589 | gdb_perror("gdb_try_accepting: error accepting connection"); | |
590 | (void) close(slisten); | |
591 | return; | |
592 | } | |
593 | ||
594 | (void) close (slisten); /* we're not using the */ | |
595 | /* listening socket */ | |
596 | /* anymore, only the */ | |
597 | /* connection to the peer */ | |
598 | ||
599 | /*----------------------------------------------------------*/ | |
600 | /* | |
601 | /* The connection has been made, fill in the connection | |
602 | /* control data structure. | |
603 | /* | |
604 | /*----------------------------------------------------------*/ | |
605 | ||
606 | con->in.fd = peer; | |
607 | con->out.fd = peer; | |
608 | con->status = CON_STARTING; | |
609 | } | |
610 | \f | |
611 | /************************************************************************/ | |
612 | /* | |
613 | /* g_ver_oprotocol | |
614 | /* | |
615 | /* Called when an outbound connection is started to verify | |
616 | /* the version of the protocol being observed. | |
617 | /* | |
618 | /************************************************************************/ | |
619 | ||
620 | int | |
621 | g_ver_oprotocol(con) | |
622 | CONNECTION con; | |
623 | { | |
624 | #ifdef VERIFY_PROTOCOL | |
625 | char ver = GDB_PROTOCOL_VERSION; | |
626 | char theirs; | |
627 | int len; | |
628 | ||
629 | int onoff = 0; /* for ioctl to turn off */ | |
630 | ||
631 | /* | |
632 | * Because the connection was accepted on a non-blocking | |
633 | * listening socket, the connection itself may be non-blocking. | |
634 | * We can't tolerate that here. It will be reset later. | |
635 | */ | |
636 | if (ioctl(con->in.fd, FIONBIO, (char *)&onoff) < 0) { | |
637 | g_stop_with_errno(con); | |
638 | gdb_perror("Can't turn off FIONBIO in g_ver_iprotocol"); | |
639 | return; | |
640 | } | |
641 | ||
642 | while (write(con->out.fd, &ver, 1) < 0) { | |
643 | g_stop_with_errno(con); | |
644 | return; | |
645 | } | |
646 | ||
647 | do { | |
648 | len = read(con->in.fd, &theirs, 1); | |
649 | if (len == (-1)) { | |
650 | g_stop_with_errno(con); | |
651 | return; | |
652 | } | |
653 | } while (len !=1); | |
654 | ||
655 | if (theirs == ver) | |
656 | con->status = CON_UP; | |
657 | else | |
658 | con->status = CON_STOPPED; | |
659 | #else !VERIFY_PROTOCOL | |
660 | con->status = CON_UP; | |
661 | #endif !VERIFY_PROTOCOL | |
662 | } | |
663 | ||
664 | /************************************************************************/ | |
665 | /* | |
666 | /* g_ver_iprotocol | |
667 | /* | |
668 | /* Called when an inbound connection is started to verify | |
669 | /* the version of the protocol being observed. | |
670 | /* | |
671 | /************************************************************************/ | |
672 | ||
673 | int | |
674 | g_ver_iprotocol(con) | |
675 | CONNECTION con; | |
676 | { | |
677 | #ifdef VERIFY_PROTOCOL | |
678 | char ver = GDB_PROTOCOL_VERSION; | |
679 | char theirs; | |
680 | int len; | |
681 | int old_nbio; | |
682 | int onoff = 0; /* for ioctl to turn off */ | |
683 | ||
684 | /* | |
685 | * Because the connection was accepted on a non-blocking | |
686 | * listening socket, the connection itself may be non-blocking. | |
687 | * We can't tolerate that here. It will be reset later. | |
688 | */ | |
689 | if (ioctl(con->in.fd, FIONBIO, (char *)&onoff) < 0) { | |
690 | g_stop_with_errno(con); | |
691 | gdb_perror("Can't turn off FIONBIO in g_ver_iprotocol"); | |
692 | return; | |
693 | } | |
694 | ||
695 | do { | |
696 | len = read(con->in.fd, &theirs, 1); | |
697 | if (len == (-1)) { | |
698 | g_stop_with_errno(con); | |
699 | return; | |
700 | } | |
701 | } while (len !=1) ; | |
702 | ||
703 | if (theirs == ver) | |
704 | con->status = CON_UP; | |
705 | else | |
706 | con->status = CON_STOPPED; | |
707 | ||
708 | while (write(con->out.fd, &ver, 1) < 0) { | |
709 | g_stop_with_errno(con); | |
710 | return; | |
711 | } | |
712 | #else !VERIFY_PROTOCOL | |
713 | con->status = CON_UP; | |
714 | #endif | |
715 | } | |
716 | ||
717 | \f | |
718 | /************************************************************************/ | |
719 | /* | |
720 | /* sever_connection (sever_connection) | |
721 | /* | |
722 | /* Unconditionally, but cleanly, terminates a connection. All | |
723 | /* pending operations on the connection are cancelled, and the | |
724 | /* file descriptor for the connection is closed. This routine | |
725 | /* should be called directly from applications wishing to shut | |
726 | /* down a connection. No transmissions are attempted | |
727 | /* by this routine. Returns NULL, in the hope that applications | |
728 | /* will assign this to their old CONNECTION variable. | |
729 | /* | |
730 | /************************************************************************/ | |
731 | ||
732 | CONNECTION | |
733 | sever_connection(con) | |
734 | CONNECTION con; | |
735 | { | |
736 | if (con == NULL) | |
737 | return NULL; | |
738 | GDB_CHECK_CON(con, "sever_connection") | |
739 | if (con->status == CON_UP || con->status == CON_STARTING) | |
740 | g_stop_connection(con); | |
741 | if (con->status != CON_STOPPED) | |
742 | gdb_de_allocate_connection(con); | |
743 | ||
744 | return NULL; | |
745 | } | |
746 | ||
747 | /************************************************************************/ | |
748 | /* | |
749 | /* g_stop_with_errno | |
750 | /* | |
751 | /* This connection is stopping because of a problem on a syscall. | |
752 | /* We record the errno in the connection descriptor for inspection | |
753 | /* by the application, then stop the connection. | |
754 | /* | |
755 | /************************************************************************/ | |
756 | ||
757 | ||
758 | int | |
759 | g_stop_with_errno(con) | |
760 | CONNECTION con; | |
761 | { | |
762 | con->errno = errno; | |
763 | g_stop_connection(con); | |
764 | ||
765 | } | |
766 | ||
767 | /************************************************************************/ | |
768 | /* | |
769 | /* g_stop_connection | |
770 | /* | |
771 | /* Unconditionally, but cleanly, terminates a connection. All | |
772 | /* pending operations on the connection are cancelled, and the | |
773 | /* file descriptor for the connection is closed. This routine is | |
774 | /* for internal use. Applications call sever_connection, which | |
775 | /* also de_allocates the descriptor. No transmissions are attempted | |
776 | /* by this routine. | |
777 | /* | |
778 | /************************************************************************/ | |
779 | ||
780 | int | |
781 | g_stop_connection(con) | |
782 | CONNECTION con; | |
783 | { | |
784 | /* | |
785 | * Shutdown activity on the two half connections. | |
786 | */ | |
787 | g_cleanup_half_connection(&(con->in)); | |
788 | g_cleanup_half_connection(&(con->out)); | |
789 | ||
790 | /* | |
791 | * Remove the file descriptor from the select bit maps | |
792 | */ | |
793 | if (!(con->in.flags & HCON_UNUSED)) | |
794 | FD_CLR(con->in.fd, &gdb_crfds); | |
795 | if (!(con->out.flags & HCON_UNUSED)) | |
796 | FD_CLR(con->out.fd, &gdb_cwfds); | |
797 | /* | |
798 | * Close the file descriptor. Note, this presumes that in fact | |
799 | * 1) in is never the unused half and | |
800 | * 2) when the connection is bi-directional, in and out share an | |
801 | * fd. We could do with a more elaborate scheme to control | |
802 | * this in the future. | |
803 | */ | |
804 | (void) close(con->in.fd); | |
805 | ||
806 | /* | |
807 | * Mark the connection as stopping. We can't reclaim the | |
808 | * descriptor until the application does a sever, or else there | |
809 | * would be a risk of re-allocating it out from under the application. | |
810 | */ | |
811 | ||
812 | con->status = CON_STOPPING; | |
813 | ||
814 | return; | |
815 | } | |
816 | ||
817 | \f | |
818 | /************************************************************************/ | |
819 | /* | |
820 | /* gdb_de_allocate_connection | |
821 | /* | |
822 | /* Return a connection whose file descriptors have been closed | |
823 | /* to the pool. | |
824 | /* | |
825 | /************************************************************************/ | |
826 | ||
827 | int | |
828 | gdb_de_allocate_connection(con) | |
829 | CONNECTION con; | |
830 | { | |
831 | register int i; | |
832 | ||
833 | con->status = CON_STOPPED; | |
834 | ||
835 | i = gdb_mcons-1; /* start at last one used */ | |
836 | ||
837 | /* | |
838 | * Reset gdb_mcons to be the number of connections in use | |
839 | */ | |
840 | while (i>=0 && gdb_cons[i].status == CON_STOPPED) | |
841 | i--; | |
842 | ||
843 | gdb_mcons = i + 1; | |
844 | } | |
845 | \f | |
846 | /************************************************************************/ | |
847 | /* | |
848 | /* g_cleanup_half_conection | |
849 | /* | |
850 | /* Terminate all pending operations on the supplied half | |
851 | /* connection. Note that the algorithm used here presumes | |
852 | /* that cancel_operation will de-queue the operation descriptor, | |
853 | /* therefore we have to be careful here about when we look at | |
854 | /* chain pointers. | |
855 | /* | |
856 | /************************************************************************/ | |
857 | ||
858 | int | |
859 | g_cleanup_half_connection(hcon) | |
860 | HALF_CONNECTION hcon; | |
861 | { | |
862 | OPERATION current, next; | |
863 | ||
864 | current = hcon->op_q_first; | |
865 | ||
866 | /* | |
867 | * Loop through all operations in the queue canceling them. | |
868 | * Make sure to pick up pointer to 'next' before the current | |
869 | * one is canceled, as cancelling may invalidate the pointer. | |
870 | */ | |
871 | ||
872 | while (current != (OPERATION)hcon) { | |
873 | next = current->next; | |
874 | (void) cancel_operation(current); | |
875 | current = next; | |
876 | } | |
877 | } | |
878 | \f | |
879 | /************************************************************************/ | |
880 | /* | |
881 | /* create_listening_connection (create_listening_connection) | |
882 | /* | |
883 | /* Starts a special type of connection which is used to listen | |
884 | /* for incoming connection requests. The inbound half-connection | |
885 | /* is the only one used for this special kind of connection. | |
886 | /* | |
887 | /* It is the user's responsibility to insure that only appropriate | |
888 | /* types of operation are queued on a connection of this sort. In | |
889 | /* general, these connections are intended for internal use by | |
890 | /* GDB, and they are not intended to be visible to servers or | |
891 | /* clients directly. | |
892 | /* | |
893 | /* The id supplied should be in one of two forms. If just a | |
894 | /* string is supplied then it is presumed to be the name of | |
895 | /* a registered tcp service. If the name begins with a #, then | |
896 | /* the rest is interpreted as the integer port number to be used. | |
897 | /* | |
898 | /* In future implementations, the id may have more structure, which | |
899 | /* is why we define it as a string. | |
900 | /* | |
901 | /************************************************************************/ | |
902 | ||
903 | CONNECTION | |
904 | create_listening_connection(id) | |
905 | char *id; | |
906 | { | |
907 | register CONNECTION con; /* the connection we're */ | |
908 | /* creating */ | |
909 | ||
910 | register int slisten; /* socket on which | |
911 | we listen for connections */ | |
912 | ||
913 | struct sockaddr_in self; | |
914 | int retries = GDB_BIND_RETRY_COUNT; | |
915 | int onoff = 1; /* used as argument to */ | |
916 | /* setsockopt */ | |
917 | struct servent *serv; | |
918 | ||
919 | GDB_INIT_CHECK | |
920 | ||
921 | /* | |
922 | * Try to allocate a connection and fill it in with null values. | |
923 | */ | |
924 | ||
925 | con = g_make_con(); | |
926 | ||
927 | /* | |
928 | * Try to create a socket for listening | |
929 | */ | |
930 | con->in.fd = socket(AF_INET, SOCK_STREAM, 0); | |
931 | slisten = con->in.fd; /* easier and faster than */ | |
932 | /* using con->in.fd all the */ | |
933 | /* time*/ | |
934 | if (slisten < 0 ) { | |
935 | gdb_perror("create_listening_connection: error creating listen socket"); | |
936 | (void) g_stop_with_errno(con); | |
937 | return con; | |
938 | } | |
939 | /* | |
940 | * Set options so the listening address can be re-used (this | |
941 | * has its dangers, but otherwise we can't restart our servers | |
942 | * for long periods after they crash because of connections which | |
943 | * take a long to time clean up and hold ports in use.) | |
944 | */ | |
945 | ||
946 | /* Try 4.2 method */ | |
947 | if(setsockopt(slisten, SOL_SOCKET, SO_REUSEADDR, (char *)0,0)<0) | |
948 | /* that didn't work, try 4.3 */ | |
949 | if(setsockopt(slisten, SOL_SOCKET, SO_REUSEADDR, | |
950 | (char *)&onoff, sizeof(int)) <0) | |
951 | GDB_GIVEUP("create_listening_connection: could not set SO_REUSEADDR") | |
952 | ; | |
953 | /* | |
954 | * Make the listening socket non-blocking so we won't have to do | |
955 | * selects before polling it (change made by Bill Sommerfeld - wesommer) | |
956 | */ | |
957 | if (ioctl(slisten, FIONBIO, (char *)&onoff) < 0) { /*<==FIX,,,add comment */ | |
958 | g_stop_with_errno(con); | |
959 | gdb_perror("ioctl for listening socket"); | |
960 | return con; | |
961 | } | |
962 | /*----------------------------------------------------------*/ | |
963 | /* | |
964 | /* Bind the socket to ourselves, using port derived from | |
965 | /* the supplied id string. | |
966 | /* | |
967 | /*----------------------------------------------------------*/ | |
968 | ||
969 | bzero((char *)&self, sizeof(self)); | |
970 | /* | |
971 | * Determine our port number | |
972 | */ | |
973 | if (*id == '#') | |
974 | self.sin_port = htons((u_short)atoi(id+1)); | |
975 | else { | |
976 | serv = getservbyname(id, "tcp"); | |
977 | if (serv == NULL) { | |
978 | fprintf(gdb_log,"gdb create_listening_connection: cannot become service named %s\n",id); | |
979 | return NULL; /* BUG: causes connetion */ | |
980 | /* descriptor leakage. Should */ | |
981 | /* return an error code in */ | |
982 | /* the connection descriptor*/ | |
983 | } | |
984 | self.sin_port = serv->s_port; | |
985 | ||
986 | } | |
987 | /* | |
988 | * Try and re-try the bind until it works or until retry count | |
989 | * is exhausted. | |
990 | */ | |
991 | while (bind(slisten,(struct sockaddr *)&self,sizeof(self)) < 0) { | |
992 | if (errno == EADDRINUSE && retries--) { | |
993 | fprintf(gdb_log,"gdb create_listening_connection: port address in use, will retry %d more time(s)\n",retries+1); | |
994 | sleep(GDB_BIND_RETRY_INTERVAL); | |
995 | continue; | |
996 | } else { | |
997 | gdb_perror("gdb create_listening_connection: error binding listen socket"); | |
998 | g_stop_with_errno(con); | |
999 | return con; | |
1000 | } | |
1001 | } | |
1002 | ||
1003 | /*----------------------------------------------------------*/ | |
1004 | /* | |
1005 | /* Listen for connections. | |
1006 | /* | |
1007 | /*----------------------------------------------------------*/ | |
1008 | ||
1009 | (void) listen (slisten, 5); /* does not block, just */ | |
1010 | /* sets the maximum backlog */ | |
1011 | /* of pending non-accepted */ | |
1012 | /* cons.*/ | |
1013 | ||
1014 | con->in.flags |= HCON_LISTEN; | |
1015 | con->out.flags |= HCON_UNUSED; | |
1016 | con->status = CON_UP; | |
1017 | if (con->in.fd +1 > gdb_mfd) | |
1018 | gdb_mfd = con->in.fd + 1; | |
1019 | return con; | |
1020 | } | |
1021 | \f | |
1022 | /************************************************************************/ | |
1023 | /* | |
1024 | /* g_allocate_connection_buffers | |
1025 | /* | |
1026 | /* Create a buffer which can be used to receive large | |
1027 | /* chunks of data from the socket. This is currently done only | |
1028 | /* on the inbound half connection. Also, the buffers are not freed | |
1029 | /* once allocated, even if the connection descriptor is re-used. | |
1030 | /* | |
1031 | /************************************************************************/ | |
1032 | ||
1033 | int | |
1034 | g_allocate_connection_buffers(con) | |
1035 | CONNECTION con; | |
1036 | { | |
1037 | HALF_CONNECTION inbound = &(con->in); | |
1038 | ||
1039 | /* | |
1040 | * See if there is already one allocated, if not, allocate one. | |
1041 | */ | |
1042 | if (inbound->stream_buffer == (char *)NULL) { | |
1043 | inbound->stream_buffer = | |
1044 | db_alloc(inbound->stream_buffer_length); | |
1045 | } | |
1046 | ||
1047 | /* | |
1048 | * In any case, make sure that it is effectively empty | |
1049 | */ | |
1050 | inbound -> stream_buffer_next = inbound -> stream_buffer; | |
1051 | inbound -> stream_buffer_remaining = 0; | |
1052 | } |