Home | History | Annotate | Line # | Download | only in xdm
      1 /*
      2  *
      3 Copyright 1989, 1998  The Open Group
      4 
      5 Permission to use, copy, modify, distribute, and sell this software and its
      6 documentation for any purpose is hereby granted without fee, provided that
      7 the above copyright notice appear in all copies and that both that
      8 copyright notice and this permission notice appear in supporting
      9 documentation.
     10 
     11 The above copyright notice and this permission notice shall be included in
     12 all copies or substantial portions of the Software.
     13 
     14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
     17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
     18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
     19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     20 
     21 Except as contained in this notice, the name of The Open Group shall not be
     22 used in advertising or otherwise to promote the sale, use or other dealings
     23 in this Software without prior written authorization from The Open Group.
     24  *
     25  * Author:  Keith Packard, MIT X Consortium
     26  */
     27 
     28 /*
     29  * protodpy.c
     30  *
     31  * manage a collection of proto-displays.  These are displays for
     32  * which sessionID's have been generated, but no session has been
     33  * started.
     34  */
     35 
     36 #include "dm.h"
     37 #include "dm_error.h"
     38 #include "dm_socket.h"
     39 
     40 #ifdef XDMCP
     41 
     42 # include <sys/types.h>
     43 # include <time.h>
     44 # define Time_t time_t
     45 
     46 static struct protoDisplay	*protoDisplays;
     47 
     48 # ifdef DEBUG
     49 static void
     50 PrintProtoDisplay (struct protoDisplay *pdpy)
     51 {
     52     Debug ("ProtoDisplay %p\n", pdpy);
     53     Debug ("\taddress: %s", pdpy->address);
     54     //PrintSockAddr (pdpy->address, pdpy->addrlen);
     55     Debug ("\tdate %lu (%lu from now)\n", pdpy->date, time(0) - pdpy->date);
     56     Debug ("\tdisplay Number %d\n", pdpy->displayNumber);
     57     Debug ("\tsessionID %lu\n", (unsigned long)pdpy->sessionID);
     58 }
     59 # endif
     60 
     61 struct protoDisplay *
     62 FindProtoDisplay (
     63     XdmcpNetaddr    address,
     64     int		    addrlen,
     65     CARD16	    displayNumber)
     66 {
     67     struct protoDisplay	*pdpy;
     68 
     69     Debug ("FindProtoDisplay\n");
     70     for (pdpy = protoDisplays; pdpy; pdpy=pdpy->next)
     71     {
     72 	if (pdpy->displayNumber == displayNumber &&
     73 	    addressEqual (address, addrlen, pdpy->address, pdpy->addrlen))
     74 	{
     75 	    return pdpy;
     76 	}
     77     }
     78     return (struct protoDisplay *) 0;
     79 }
     80 
     81 static void
     82 TimeoutProtoDisplays (Time_t now)
     83 {
     84     struct protoDisplay	*pdpy, *next;
     85 
     86     for (pdpy = protoDisplays; pdpy; pdpy = next)
     87     {
     88 	next = pdpy->next;
     89 	if (pdpy->date < now - PROTO_TIMEOUT)
     90 	    DisposeProtoDisplay (pdpy);
     91     }
     92 }
     93 
     94 struct protoDisplay *
     95 NewProtoDisplay (
     96     XdmcpNetaddr    address,
     97     int		    addrlen,
     98     CARD16	    displayNumber,
     99     CARD16	    connectionType,
    100     ARRAY8Ptr	    connectionAddress,
    101     CARD32	    sessionID)
    102 {
    103     struct protoDisplay	*pdpy;
    104     Time_t date;
    105 
    106     Debug ("NewProtoDisplay\n");
    107     time (&date);
    108     TimeoutProtoDisplays (date);
    109     pdpy = malloc (sizeof *pdpy);
    110     if (!pdpy)
    111 	return NULL;
    112     pdpy->address = malloc (addrlen);
    113     if (!pdpy->address)
    114     {
    115 	free (pdpy);
    116 	return NULL;
    117     }
    118     pdpy->addrlen = addrlen;
    119     memcpy(pdpy->address, address, addrlen);
    120     pdpy->displayNumber = displayNumber;
    121     pdpy->connectionType = connectionType;
    122     pdpy->date = date;
    123     if (!XdmcpCopyARRAY8 (connectionAddress, &pdpy->connectionAddress))
    124     {
    125 	free (pdpy->address);
    126 	free (pdpy);
    127 	return NULL;
    128     }
    129     pdpy->sessionID = sessionID;
    130     pdpy->fileAuthorization = (Xauth *) NULL;
    131     pdpy->xdmcpAuthorization = (Xauth *) NULL;
    132     pdpy->next = protoDisplays;
    133     protoDisplays = pdpy;
    134     return pdpy;
    135 }
    136 
    137 void
    138 DisposeProtoDisplay (struct protoDisplay *pdpy)
    139 {
    140     struct protoDisplay	*p, *prev;
    141 
    142     prev = NULL;
    143     for (p = protoDisplays; p; p=p->next)
    144     {
    145 	if (p == pdpy)
    146 	    break;
    147 	prev = p;
    148     }
    149     if (!p)
    150 	return;
    151     if (prev)
    152 	prev->next = pdpy->next;
    153     else
    154 	protoDisplays = pdpy->next;
    155     bzero(&pdpy->key, sizeof(pdpy->key));
    156     if (pdpy->fileAuthorization)
    157 	XauDisposeAuth (pdpy->fileAuthorization);
    158     if (pdpy->xdmcpAuthorization)
    159 	XauDisposeAuth (pdpy->xdmcpAuthorization);
    160     XdmcpDisposeARRAY8 (&pdpy->connectionAddress);
    161     free (pdpy->address);
    162     free (pdpy);
    163 }
    164 
    165 #endif /* XDMCP */
    166