7 static char *rcsid_gdb_conn_c = "$Header$";
33 /************************************************************************/
37 /* GDB - Connection Management Services
39 /* Author: Noah Mendelsohn
40 /* Copyright: 1986 MIT Project Athena
41 /* For copying and distribution information, please see
42 /* the file <mit-copyright.h>.
44 /* Routines used in the creation and maintenance of CONNECTIONS.
45 /* Note: these are closely related to the services provided
46 /* by gdb_trans.c and gdb_trans2.c.
49 /************************************************************************/
51 #include <mit-copyright.h>
55 #include <sys/types.h>
57 #include <sys/socket.h>
58 #include <sys/ioctl.h>
59 #include <netinet/in.h>
65 extern u_short htons(); /* ?? All versions? */
68 CONNECTION gdb_allocate_connection();
70 /************************************************************************/
72 /* start_peer_connection (start_peer_connection)
74 /* Starts a connection to another process which itself will be
75 /* issuing a start_peer_connection to us. Current implementation
76 /* builds at most one stream, with the risk of a hang if
77 /* the attempts to connect cross in the night. This is a bug,
78 /* but this level of support is acceptable for casual debugging
79 /* of applications, and perhaps for some production use in
80 /* controlled settings. I think the only other way to do it
81 /* is to risk building two streams in parallel, possibly tearing
82 /* one down when the duplication is discovered. Seems complicated
85 /************************************************************************/
88 start_peer_connection(id)
89 char *id; /* null terminated string */
91 register CONNECTION con; /* the connection we're */
97 * Try to allocate a connection and fill it in with null values.
103 * In this implementation, we use a single fd for both inbound and
104 * outbound traffic. Try to connect to other side. If that
105 * doesn't work, wait to accept a connection from the other side.
106 * Current implementation of this is synchronous--may be a problem?
107 * Also note timing window bug in the following. If the two peers
108 * are started at just about the same time, the race may not be handled
109 * propoerly. If the connections come up, then verify the level of
110 * protocol being observed on the connections. If incompatible,
111 * then turn off the connection.
114 if(!g_try_connecting(con,id)) {
115 g_try_accepting(con,id);
116 if(con->status == CON_STARTING)
117 g_ver_iprotocol(con);
119 if(con->status == CON_STARTING)
120 g_ver_oprotocol(con);
123 if (con->status == CON_UP) {
125 * We've successfully started the connection, now mark
126 * it for non-blocking I/O. Also, update the high water
127 * mark of fd's controlled by our system.
130 if(ioctl(con->in.fd, FIONBIO, (char *)&nb)== (-1)) {
131 g_stop_with_errno(con);
134 if (con->in.fd +1 > gdb_mfd)
135 gdb_mfd = con->in.fd + 1;
137 * Allocate a buffer, if necessary, and reset buffer pointers
138 * so first request will result in a long read into the buffer
140 g_allocate_connection_buffers(con);
147 /************************************************************************/
151 /* Internal routine to allocate a new connection structure and
152 /* initialize all its fields to logical null values.
154 /************************************************************************/
159 register CONNECTION con;
162 * Try to allocate a connection, fatal error if none available
164 con = gdb_allocate_connection();
166 GDB_GIVEUP("start_peer_connection: Tried to allocate too many connections") /* <==RECOVERABLE */
169 * Give the fields their initial values
177 \f/************************************************************************/
181 /* Sets a connection descriptor to have all null values in
182 /* its fields. This routine does NOT do any of the cleanup
183 /* which is necessary after the connection has really been used.
185 /************************************************************************/
192 * Initialize the connection control data structure.
194 con->id = GDB_CON_ID;
195 con->status = CON_STARTING;
196 con->oob_fcn = NULL; /* out of band signalling */
197 /* is not currently */
199 con->errno = 0; /* system errno gets */
200 /* copied here iff it */
201 /* causes this con to die */
203 * Initialize input half connection to null state before trying
206 con->in.status = OP_NOT_STARTED;
209 con->in.op_q_first = (struct oper_data *)&con->in;
210 con->in.op_q_last = (struct oper_data *)&con->in;
211 con->in.next_byte = NULL;
212 con->in.remaining = 0;
216 * Initialize output half connection to null state before trying
219 con->out.status = OP_NOT_STARTED;
221 con->out.oob_fd = -1;
222 con->out.op_q_first = (struct oper_data *)&con->out;
223 con->out.op_q_last = (struct oper_data *)&con->out;
224 con->out.next_byte = NULL;
225 con->out.remaining = 0;
233 /************************************************************************/
235 /* gdb_allocate_connection
237 /* Return an unused entry in the connection array. Unused entries
238 /* are recognized by being marked as CON_STOPPED.
240 /* Note that gdb_mcons is the number of descriptors which have
241 /* ever been used (i.e. a high water mark), so status fields
242 /* are invalid above that.
244 /************************************************************************/
247 gdb_allocate_connection()
249 register int i; /* index of next one */
253 * First look for one below the high water mark
255 for(i=0; i<gdb_mcons; i++) {
256 if (gdb_cons[i].status == CON_STOPPED)
261 * Allocate one which has never been used, if possible
264 if (i>=GDB_MAX_CONNECTIONS)
265 GDB_GIVEUP("gdb: tried to allocate too many simulataneous connections.\n, See GDB_MAX_CONNECTIONS in gdb.h.") /* <==RECOVERABLE */
267 gdb_mcons++; /* bump the high water mark */
268 gdb_cons[i].status = CON_STOPPED; /* initialize status of the */
270 return &gdb_cons[i]; /* return new highest con */
274 /************************************************************************/
278 /* Try to start a connection to the designated site, filling
279 /* in the appropriate information in the connection descriptor
280 /* if successful. Return TRUE if connection succeeded or if
281 /* error was fatal enough that we shouldn't try accepting. Returns
282 /* FALSE if we should try accepting.
284 /************************************************************************/
288 g_try_connecting(con,id)
292 int peer; /* socket for talking to
294 struct sockaddr_in target; /* build the peer address */
296 struct hostent *peer_host; /* host where peer is */
298 /*----------------------------------------------------------*/
300 /* Make sure connection is marked stopped until we
303 /*----------------------------------------------------------*/
305 con->status = CON_STOPPED;
307 /*----------------------------------------------------------*/
309 /* Find out host where peer is, and validate it. Take
310 /* care of port at the same time.
312 /*----------------------------------------------------------*/
314 bzero((char *)&target, sizeof(target));
315 g_parse_target(id, &peer_host, &target.sin_port);
316 if (peer_host == NULL) {
317 fprintf(gdb_log,"gdb: g_try_connecting... '%s' is not a valid host:server\n",
319 return TRUE; /* so we won't try accepting */
322 /*----------------------------------------------------------*/
326 /*----------------------------------------------------------*/
328 peer = socket(AF_INET, SOCK_STREAM, 0);
330 g_stop_with_errno(con);
331 return TRUE; /* fatal error */
334 /*----------------------------------------------------------*/
336 /* Get information and bind socket using well known
337 /* port (BUG: this restricts us to one pair of peers
338 /* per host pair, as well as being bad practice on
339 /* the network. It will do for debugging.
341 /*----------------------------------------------------------*/
344 bcopy(peer_host->h_addr, (char *)&target.sin_addr, peer_host->h_length);
345 target.sin_family = peer_host->h_addrtype;
347 /*----------------------------------------------------------*/
349 /* Make the connection
351 /*----------------------------------------------------------*/
353 if(connect(peer, (struct sockaddr *)&target, sizeof(target)) < 0) {
354 if (errno == ECONNREFUSED)
355 return FALSE; /* other side not yet */
356 /* up, but no other fatal */
359 gdb_perror("gdb: unexpected error connecting");
360 g_stop_with_errno(con);
365 /*----------------------------------------------------------*/
367 /* The connection has been made, fill in the connection
368 /* control data structure.
370 /*----------------------------------------------------------*/
374 con->status = CON_STARTING;
380 /************************************************************************/
384 /* For a given server or peer i.d., figure out the host and the
385 /* port. Arguments are:
387 /* string i.d. of the server, which is
388 /* in one of two forms:
390 /* host:servicename (where service name must not begin
393 /* host:#portnumber (where portnumber is the actual
394 /* number of the port to be used)
396 /* (actually, a 3rd form, with no port number supplied,
397 /* will use a default GDB_PORT, but this is unsafe
398 /* and it will be disabled in production versions
399 /* of the gdb system.)
401 /* **hostent: returned to indicate host to be used. Null
402 /* if host could not be found
404 /* *port pointer to an integer where the port number will
405 /* be put. We return the port number in network
408 /************************************************************************/
411 g_parse_target(id, host, port)
413 struct hostent **host;
416 char buffer[256]; /* longest host name */
417 register char *ip, *bp; /* for copying name */
418 struct servent *serv; /* returned from */
419 /* get service by name */
421 /*----------------------------------------------------------*/
423 /* copy the host name part only to local buffer
425 /*----------------------------------------------------------*/
430 while (*ip != '\0' && *ip != ':')
434 /*----------------------------------------------------------*/
436 /* Look up the host name, return if bad.
438 /*----------------------------------------------------------*/
440 *host = gethostbyname(buffer);
445 /*----------------------------------------------------------*/
447 /* Set up the port address
449 /*----------------------------------------------------------*/
463 * port number supplied explictly
466 if (*ip < '0' || *ip>'9') {
470 *port = htons((u_short)atoi(ip));
473 * service identified by name
475 serv = getservbyname(ip, "tcp");
480 *port = serv->s_port;
484 /************************************************************************/
488 /* Try to accept a connection to the designated site, filling
489 /* in the appropriate information in the connection descriptor
492 /************************************************************************/
495 g_try_accepting(con,id)
499 int slisten; /* socket on which
500 we listen for connections */
502 int peer; /* socket for talking to
505 struct sockaddr_in self, from;
506 int retries = GDB_BIND_RETRY_COUNT;
507 int onoff = 1; /* used as argument to */
510 struct hostent *peer_host; /* host where peer is */
512 /*----------------------------------------------------------*/
514 /* Make sure connection is marked stopped until we
517 /*----------------------------------------------------------*/
519 con->status = CON_STOPPED;
521 /*----------------------------------------------------------*/
523 /* Create a socket on which to listen. Tell it that
524 /* it's OK to re-use the port address, which may still
525 /* appear busy if connections are taking awhile to go
528 /*----------------------------------------------------------*/
530 slisten = socket(AF_INET, SOCK_STREAM, 0);
532 gdb_perror("g_try_accepting: error creating listen socket");
533 g_stop_with_errno(con);
536 if(setsockopt(slisten, SOL_SOCKET, SO_REUSEADDR, (char *)0, 0)<0)
537 /* that didn't work, try 4.3 */
538 if(setsockopt(slisten, SOL_SOCKET, SO_REUSEADDR,
539 (char *)&onoff, sizeof(int)) <0)
540 GDB_GIVEUP("g_try_accepting: could not set SO_REUSEADDR");
542 /*----------------------------------------------------------*/
544 /* Find out host where peer is, and validate it. Take
545 /* care of port at the same time. This is redundant
546 /* given that g_try_connecting is always called first.
548 /*----------------------------------------------------------*/
550 bzero((char *)&self, sizeof(self));
551 g_parse_target(id, &peer_host, &self.sin_port);
552 if (peer_host == NULL) {
553 GDB_GIVEUP("gdb_try_accepting: bad port not caught by try connecting")
556 /*----------------------------------------------------------*/
558 /* Bind the socket to ourselves, using the well known
559 /* port (See bug note in g_try_connecting.
561 /* This code should really go in initialization, I think.
563 /*----------------------------------------------------------*/
565 while (bind(slisten,(struct sockaddr *)&self,sizeof(self)) < 0) {
566 if (errno == EADDRINUSE && retries--) {
567 fprintf(gdb_log,"gdb: port address in use, will retry %d more time(s)\n",retries+1);
568 sleep(GDB_BIND_RETRY_INTERVAL);
571 gdb_perror("gdb: error binding listen socket");
572 g_stop_with_errno(con);
573 (void) close(slisten);
578 /*----------------------------------------------------------*/
580 /* Listen for connections.
582 /*----------------------------------------------------------*/
584 (void) listen (slisten, 5); /* does not block, just */
585 /* sets the maximum backlog */
586 /* of pending non-accepted */
588 fromlen = sizeof(from);
589 peer = accept(slisten, &from, &fromlen);
591 g_stop_with_errno(con);
592 gdb_perror("gdb_try_accepting: error accepting connection");
593 (void) close(slisten);
597 (void) close (slisten); /* we're not using the */
598 /* listening socket */
599 /* anymore, only the */
600 /* connection to the peer */
602 /*----------------------------------------------------------*/
604 /* The connection has been made, fill in the connection
605 /* control data structure.
607 /*----------------------------------------------------------*/
611 con->status = CON_STARTING;
614 /************************************************************************/
618 /* Called when an outbound connection is started to verify
619 /* the version of the protocol being observed.
621 /************************************************************************/
627 #ifdef VERIFY_PROTOCOL
628 char ver = GDB_PROTOCOL_VERSION;
632 int onoff = 0; /* for ioctl to turn off */
635 * Because the connection was accepted on a non-blocking
636 * listening socket, the connection itself may be non-blocking.
637 * We can't tolerate that here. It will be reset later.
639 if (ioctl(con->in.fd, FIONBIO, (char *)&onoff) < 0) {
640 g_stop_with_errno(con);
641 gdb_perror("Can't turn off FIONBIO in g_ver_iprotocol");
645 while (write(con->out.fd, &ver, 1) < 0) {
646 g_stop_with_errno(con);
651 len = read(con->in.fd, &theirs, 1);
653 g_stop_with_errno(con);
659 con->status = CON_UP;
661 con->status = CON_STOPPED;
662 #else !VERIFY_PROTOCOL
663 con->status = CON_UP;
664 #endif !VERIFY_PROTOCOL
667 /************************************************************************/
671 /* Called when an inbound connection is started to verify
672 /* the version of the protocol being observed.
674 /************************************************************************/
680 #ifdef VERIFY_PROTOCOL
681 char ver = GDB_PROTOCOL_VERSION;
685 int onoff = 0; /* for ioctl to turn off */
688 * Because the connection was accepted on a non-blocking
689 * listening socket, the connection itself may be non-blocking.
690 * We can't tolerate that here. It will be reset later.
692 if (ioctl(con->in.fd, FIONBIO, (char *)&onoff) < 0) {
693 g_stop_with_errno(con);
694 gdb_perror("Can't turn off FIONBIO in g_ver_iprotocol");
699 len = read(con->in.fd, &theirs, 1);
701 g_stop_with_errno(con);
707 con->status = CON_UP;
709 con->status = CON_STOPPED;
711 while (write(con->out.fd, &ver, 1) < 0) {
712 g_stop_with_errno(con);
715 #else !VERIFY_PROTOCOL
716 con->status = CON_UP;
721 /************************************************************************/
723 /* sever_connection (sever_connection)
725 /* Unconditionally, but cleanly, terminates a connection. All
726 /* pending operations on the connection are cancelled, and the
727 /* file descriptor for the connection is closed. This routine
728 /* should be called directly from applications wishing to shut
729 /* down a connection. No transmissions are attempted
730 /* by this routine. Returns NULL, in the hope that applications
731 /* will assign this to their old CONNECTION variable.
733 /************************************************************************/
736 sever_connection(con)
741 GDB_CHECK_CON(con, "sever_connection")
742 if (con->status == CON_UP || con->status == CON_STARTING)
743 g_stop_connection(con);
744 if (con->status != CON_STOPPED)
745 gdb_de_allocate_connection(con);
750 /************************************************************************/
754 /* This connection is stopping because of a problem on a syscall.
755 /* We record the errno in the connection descriptor for inspection
756 /* by the application, then stop the connection.
758 /************************************************************************/
762 g_stop_with_errno(con)
766 g_stop_connection(con);
770 /************************************************************************/
774 /* Unconditionally, but cleanly, terminates a connection. All
775 /* pending operations on the connection are cancelled, and the
776 /* file descriptor for the connection is closed. This routine is
777 /* for internal use. Applications call sever_connection, which
778 /* also de_allocates the descriptor. No transmissions are attempted
781 /************************************************************************/
784 g_stop_connection(con)
788 * Shutdown activity on the two half connections.
790 g_cleanup_half_connection(&(con->in));
791 g_cleanup_half_connection(&(con->out));
794 * Remove the file descriptor from the select bit maps
796 if (!(con->in.flags & HCON_UNUSED) && con->in.fd >= 0)
797 FD_CLR(con->in.fd, &gdb_crfds);
798 if (!(con->out.flags & HCON_UNUSED) && con->out.fd >= 0)
799 FD_CLR(con->out.fd, &gdb_cwfds);
801 * Close the file descriptor. Note, this presumes that in fact
802 * 1) in is never the unused half and
803 * 2) when the connection is bi-directional, in and out share an
804 * fd. We could do with a more elaborate scheme to control
805 * this in the future.
807 (void) close(con->in.fd);
810 * Mark the connection as stopping. We can't reclaim the
811 * descriptor until the application does a sever, or else there
812 * would be a risk of re-allocating it out from under the application.
815 con->status = CON_STOPPING;
821 /************************************************************************/
823 /* gdb_de_allocate_connection
825 /* Return a connection whose file descriptors have been closed
828 /************************************************************************/
831 gdb_de_allocate_connection(con)
836 con->status = CON_STOPPED;
838 i = gdb_mcons-1; /* start at last one used */
841 * Reset gdb_mcons to be the number of connections in use
843 while (i>=0 && gdb_cons[i].status == CON_STOPPED)
849 /************************************************************************/
851 /* g_cleanup_half_conection
853 /* Terminate all pending operations on the supplied half
854 /* connection. Note that the algorithm used here presumes
855 /* that cancel_operation will de-queue the operation descriptor,
856 /* therefore we have to be careful here about when we look at
859 /************************************************************************/
862 g_cleanup_half_connection(hcon)
863 HALF_CONNECTION hcon;
865 OPERATION current, next;
867 current = hcon->op_q_first;
870 * Loop through all operations in the queue canceling them.
871 * Make sure to pick up pointer to 'next' before the current
872 * one is canceled, as cancelling may invalidate the pointer.
875 while (current != (OPERATION)hcon) {
876 next = current->next;
877 (void) cancel_operation(current);
882 /************************************************************************/
884 /* create_listening_connection (create_listening_connection)
886 /* Starts a special type of connection which is used to listen
887 /* for incoming connection requests. The inbound half-connection
888 /* is the only one used for this special kind of connection.
890 /* It is the user's responsibility to insure that only appropriate
891 /* types of operation are queued on a connection of this sort. In
892 /* general, these connections are intended for internal use by
893 /* GDB, and they are not intended to be visible to servers or
896 /* The id supplied should be in one of two forms. If just a
897 /* string is supplied then it is presumed to be the name of
898 /* a registered tcp service. If the name begins with a #, then
899 /* the rest is interpreted as the integer port number to be used.
901 /* In future implementations, the id may have more structure, which
902 /* is why we define it as a string.
904 /************************************************************************/
907 create_listening_connection(id)
910 register CONNECTION con; /* the connection we're */
913 register int slisten; /* socket on which
914 we listen for connections */
916 struct sockaddr_in self;
917 int retries = GDB_BIND_RETRY_COUNT;
918 int onoff = 1; /* used as argument to */
920 struct servent *serv;
925 * Try to allocate a connection and fill it in with null values.
931 * Try to create a socket for listening
933 con->in.fd = socket(AF_INET, SOCK_STREAM, 0);
934 slisten = con->in.fd; /* easier and faster than */
935 /* using con->in.fd all the */
938 gdb_perror("create_listening_connection: error creating listen socket");
939 (void) g_stop_with_errno(con);
943 * Set options so the listening address can be re-used (this
944 * has its dangers, but otherwise we can't restart our servers
945 * for long periods after they crash because of connections which
946 * take a long to time clean up and hold ports in use.)
950 if(setsockopt(slisten, SOL_SOCKET, SO_REUSEADDR, (char *)0,0)<0)
951 /* that didn't work, try 4.3 */
952 if(setsockopt(slisten, SOL_SOCKET, SO_REUSEADDR,
953 (char *)&onoff, sizeof(int)) <0)
954 GDB_GIVEUP("create_listening_connection: could not set SO_REUSEADDR")
957 * Make the listening socket non-blocking so we won't have to do
958 * selects before polling it (change made by Bill Sommerfeld - wesommer)
960 if (ioctl(slisten, FIONBIO, (char *)&onoff) < 0) { /*<==FIX,,,add comment */
961 g_stop_with_errno(con);
962 gdb_perror("ioctl for listening socket");
965 /*----------------------------------------------------------*/
967 /* Bind the socket to ourselves, using port derived from
968 /* the supplied id string.
970 /*----------------------------------------------------------*/
972 bzero((char *)&self, sizeof(self));
974 * Determine our port number
977 self.sin_port = htons((u_short)atoi(id+1));
979 serv = getservbyname(id, "tcp");
981 fprintf(gdb_log,"gdb create_listening_connection: cannot become service named %s\n",id);
982 return NULL; /* BUG: causes connetion */
983 /* descriptor leakage. Should */
984 /* return an error code in */
985 /* the connection descriptor*/
987 self.sin_port = serv->s_port;
991 * Try and re-try the bind until it works or until retry count
994 while (bind(slisten,(struct sockaddr *)&self,sizeof(self)) < 0) {
995 if (errno == EADDRINUSE && retries--) {
996 fprintf(gdb_log,"gdb create_listening_connection: port address in use, will retry %d more time(s)\n",retries+1);
997 sleep(GDB_BIND_RETRY_INTERVAL);
1000 gdb_perror("gdb create_listening_connection: error binding listen socket");
1001 g_stop_with_errno(con);
1006 /*----------------------------------------------------------*/
1008 /* Listen for connections.
1010 /*----------------------------------------------------------*/
1012 (void) listen (slisten, 5); /* does not block, just */
1013 /* sets the maximum backlog */
1014 /* of pending non-accepted */
1017 con->in.flags |= HCON_LISTEN;
1018 con->out.flags |= HCON_UNUSED;
1019 con->status = CON_UP;
1020 if (con->in.fd +1 > gdb_mfd)
1021 gdb_mfd = con->in.fd + 1;
1025 /************************************************************************/
1027 /* g_allocate_connection_buffers
1029 /* Create a buffer which can be used to receive large
1030 /* chunks of data from the socket. This is currently done only
1031 /* on the inbound half connection. Also, the buffers are not freed
1032 /* once allocated, even if the connection descriptor is re-used.
1034 /************************************************************************/
1037 g_allocate_connection_buffers(con)
1040 HALF_CONNECTION inbound = &(con->in);
1043 * See if there is already one allocated, if not, allocate one.
1045 if (inbound->stream_buffer == (char *)NULL) {
1046 inbound->stream_buffer =
1047 db_alloc(inbound->stream_buffer_length);
1051 * In any case, make sure that it is effectively empty
1053 inbound -> stream_buffer_next = inbound -> stream_buffer;
1054 inbound -> stream_buffer_remaining = 0;