]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/lib/ZAsyncLocate.c
undo merge disaster
[1ts-debian.git] / zephyr / lib / ZAsyncLocate.c
1 /* This file is part of the Project Athena Zephyr Notification System.
2  * It contains source for asynchronous location functions.
3  *
4  *      Created by:     Marc Horowitz
5  *
6  *      $Id$
7  *
8  *      Copyright (c) 1990,1991 by the Massachusetts Institute of Technology.
9  *      For copying and distribution information, see the file
10  *      "mit-copyright.h". 
11  */
12
13 #include <internal.h>
14
15 #ifndef lint
16 static const char rcsid_ZAsyncLocate_c[] = "$Id$";
17 #endif
18
19 Code_t ZRequestLocations(user, zald, kind, auth)
20      char *user;
21      register ZAsyncLocateData_t *zald;
22      ZNotice_Kind_t kind;       /* UNSAFE, UNACKED, or ACKED */
23      Z_AuthProc auth;
24 {
25     int retval;
26     ZNotice_t notice;
27
28     if (ZGetFD() < 0)
29         if ((retval = ZOpenPort((u_short *)0)) != ZERR_NONE)
30             return (retval);
31
32     (void) memset((char *)&notice, 0, sizeof(notice));
33     notice.z_kind = kind;
34     notice.z_port = __Zephyr_port;
35     notice.z_class = LOCATE_CLASS;
36     notice.z_class_inst = user;
37     notice.z_opcode = LOCATE_LOCATE;
38     notice.z_sender = 0;
39     notice.z_recipient = "";
40     notice.z_default_format = "";
41     notice.z_message_len = 0;
42
43     if ((retval = ZSendNotice(&notice, auth)) != ZERR_NONE)
44         return(retval);
45
46     if ((zald->user = (char *) malloc(strlen(user)+1)) == NULL) {
47         return(ENOMEM);
48     }
49     if ((zald->version = (char *) malloc(strlen(notice.z_version)+1)) == NULL) {
50         free(zald->user);
51         return(ENOMEM);
52     }
53     zald->uid = notice.z_multiuid;
54     strcpy(zald->user,user);
55     strcpy(zald->version,notice.z_version);
56
57     return(ZERR_NONE);
58 }
59
60 Code_t ZParseLocations(notice,zald,nlocs,user)
61      register ZNotice_t *notice;
62      register ZAsyncLocateData_t *zald;
63      int *nlocs;
64      char **user;
65 {
66     char *ptr, *end;
67     int i;
68
69     ZFlushLocations();    /* This never fails (this function is part of the
70                              library, so it is allowed to know this). */
71
72     /* non-matching protocol version numbers means the
73        server is probably an older version--must punt */
74
75     if (zald && strcmp(notice->z_version, zald->version))
76       return(ZERR_VERS);
77
78     if (notice->z_kind == SERVNAK)
79       return (ZERR_SERVNAK);
80
81     /* flag ACKs as special */
82     if (notice->z_kind == SERVACK &&
83         !strcmp(notice->z_opcode, LOCATE_LOCATE)) {
84         *nlocs = -1;
85         return(ZERR_NONE);
86     }
87
88     if (notice->z_kind != ACKED)
89         return (ZERR_INTERNAL);
90
91     end = notice->z_message+notice->z_message_len;
92
93     __locate_num = 0;
94         
95     for (ptr=notice->z_message;ptr<end;ptr++)
96       if (!*ptr)
97         __locate_num++;
98
99     __locate_num /= 3;
100
101     if (__locate_num)
102       {
103       __locate_list = (ZLocations_t *)malloc((unsigned)__locate_num*
104                                              sizeof(ZLocations_t));
105       if (!__locate_list)
106         return (ENOMEM);
107     } else {
108       __locate_list = 0;
109     }
110
111     for (ptr=notice->z_message, i=0; i<__locate_num; i++) {
112        unsigned int len;
113
114        len = strlen (ptr) + 1;
115        __locate_list[i].host = (char *) malloc(len);
116        if (!__locate_list[i].host)
117           return (ENOMEM);
118        (void) strcpy(__locate_list[i].host, ptr);
119        ptr += len;
120
121        len = strlen (ptr) + 1;
122        __locate_list[i].time = (char *) malloc(len);
123        if (!__locate_list[i].time)
124           return (ENOMEM);
125        (void) strcpy(__locate_list[i].time, ptr);
126        ptr += len;
127
128        len = strlen (ptr) + 1;
129        __locate_list[i].tty = (char *) malloc(len);
130        if (!__locate_list[i].tty)
131           return (ENOMEM);
132        (void) strcpy(__locate_list[i].tty, ptr);
133        ptr += len;
134     }
135
136     __locate_next = 0;
137     *nlocs = __locate_num;
138     if (user) {
139         if (zald) {
140             if ((*user = (char *) malloc(strlen(zald->user)+1)) == NULL)
141                 return(ENOMEM);
142             strcpy(*user,zald->user);
143         } else {
144             if ((*user = (char *) malloc(strlen(notice->z_class_inst)+1)) == NULL)
145                 return(ENOMEM);
146             strcpy(*user,notice->z_class_inst);
147         }
148     }
149     return (ZERR_NONE);
150 }
151
152 int ZCompareALDPred(notice, zald)
153      ZNotice_t *notice;
154      void *zald;
155 {
156     return(ZCompareUID(&(notice->z_multiuid),
157                        &(((ZAsyncLocateData_t *) zald)->uid)));
158 }
159
160 void ZFreeALD(zald)
161      register ZAsyncLocateData_t *zald;
162 {
163    if (!zald) return;
164
165    if (zald->user) free(zald->user);
166    if (zald->version) free(zald->version);
167    (void) memset(zald, 0, sizeof(*zald));
168 }