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