]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/lib/ZParseNot.c
7950e9641df6b76b8db8ab2ffc14bbb50100bc95
[1ts-debian.git] / zephyr / lib / ZParseNot.c
1 /* This file is part of the Project Athena Zephyr Notification System.
2  * It contains source for the ZParseNotice function.
3  *
4  *      Created by:     Robert French
5  *
6  *      $Id$
7  *
8  *      Copyright (c) 1987,1991 by the Massachusetts Institute of Technology.
9  *      For copying and distribution information, see the file
10  *      "mit-copyright.h". 
11  */
12
13 #ifndef lint
14 static char rcsid_ZParseNotice_c[] =
15     "$Zephyr: /mit/zephyr/src/lib/RCS/ZParseNotice.c,v 1.22 91/03/29 03:34:46 raeburn Exp $";
16 #endif
17
18 #include <internal.h>
19
20 /* Skip to the next NUL-terminated field in the packet. */
21 static char *next_field(ptr, end)
22     char *ptr, *end;
23 {
24     while (ptr < end && *ptr != '\0')
25         ptr++;
26     if (ptr < end)
27         ptr++;
28     return (ptr);
29 }
30
31 Code_t ZParseNotice(buffer, len, notice)
32     char *buffer;
33     int len;
34     ZNotice_t *notice;
35 {
36     char *ptr, *end;
37     unsigned long temp;
38     int maj, numfields, i;
39
40 #ifdef __LINE__
41     int lineno;
42     /* Note: This definition of BAD eliminates lint and compiler
43      * complains about the "while (0)", but require that the macro not
44      * be used as the "then" part of an "if" statement that also has
45      * an "else" clause.
46      */
47 #define BAD_PACKET      {lineno=__LINE__;goto badpkt;}
48     /* This one gets lint/compiler complaints.  */
49 /*#define BAD   do{lineno=__LINE__;goto badpkt;}while(0)*/
50 #else
51 #define BAD_PACKET      goto badpkt
52 #endif
53
54     (void) memset((char *)notice, 0, sizeof(ZNotice_t));
55         
56     ptr = buffer;
57     end = buffer+len;
58
59     notice->z_packet = buffer;
60     
61     notice->z_version = ptr;
62     if (strncmp(ptr, ZVERSIONHDR, sizeof(ZVERSIONHDR) - 1))
63         return (ZERR_VERS);
64     ptr += sizeof(ZVERSIONHDR) - 1;
65     if (!*ptr) {
66 #ifdef Z_DEBUG
67         Z_debug ("ZParseNotice: null version string");
68 #endif
69         return ZERR_BADPKT;
70     }
71     maj = atoi(ptr);
72     if (maj != ZVERSIONMAJOR)
73         return (ZERR_VERS);
74     ptr = next_field(ptr, end);
75
76     if (ZReadAscii32(ptr, end-ptr, &temp) == ZERR_BADFIELD)
77         BAD_PACKET;
78     numfields = temp;
79     ptr = next_field(ptr, end);
80
81     /*XXX 3 */
82     numfields -= 2; /* numfields, version, and checksum */
83     if (numfields < 0) {
84 #ifdef __LINE__
85         lineno = __LINE__;
86       badpkt:
87 #ifdef Z_DEBUG
88         Z_debug ("ZParseNotice: bad packet from %s/%d (line %d)",
89                  inet_ntoa (notice->z_uid.zuid_addr.s_addr),
90                  notice->z_port, lineno);
91 #endif
92 #else
93     badpkt:
94 #ifdef Z_DEBUG
95         Z_debug ("ZParseNotice: bad packet from %s/%d",
96                  inet_ntoa (notice->z_uid.zuid_addr.s_addr),
97                  notice->z_port);
98 #endif
99 #endif
100         return ZERR_BADPKT;
101     }
102
103     if (numfields && ptr < end) {
104         if (ZReadAscii32(ptr, end-ptr, &temp) == ZERR_BADFIELD)
105             BAD_PACKET;
106         notice->z_kind = (ZNotice_Kind_t)temp;
107         numfields--;
108         ptr = next_field(ptr, end);
109     }
110     else
111         BAD_PACKET;
112         
113     if (numfields && ptr < end) {
114         if (ZReadAscii(ptr, end-ptr, (unsigned char *)&notice->z_uid,
115                        sizeof(ZUnique_Id_t)) == ZERR_BADFIELD)
116             BAD_PACKET;
117         notice->z_time.tv_sec = ntohl((u_long) notice->z_uid.tv.tv_sec);
118         notice->z_time.tv_usec = ntohl((u_long) notice->z_uid.tv.tv_usec);
119         numfields--;
120         ptr = next_field(ptr, end);
121     }
122     else
123         BAD_PACKET;
124         
125     if (numfields && ptr < end) {
126         if (ZReadAscii16(ptr, end-ptr, &notice->z_port) == ZERR_BADFIELD)
127             BAD_PACKET;
128         notice->z_port = htons(notice->z_port);
129         numfields--;
130         ptr = next_field(ptr, end);
131     }
132     else
133         BAD_PACKET;
134
135     if (numfields && ptr < end) {
136         if (ZReadAscii32(ptr, end-ptr, &temp) == ZERR_BADFIELD)
137             BAD_PACKET;
138         notice->z_auth = temp;
139         numfields--;
140         ptr = next_field(ptr, end);
141     }
142     else
143         BAD_PACKET;
144     notice->z_checked_auth = ZAUTH_UNSET;
145         
146     if (numfields && ptr < end) {
147         if (ZReadAscii32(ptr, end-ptr, &temp) == ZERR_BADFIELD)
148             BAD_PACKET;
149         notice->z_authent_len = temp;
150         numfields--;
151         ptr = next_field(ptr, end);
152     }
153     else
154         BAD_PACKET;
155
156     if (numfields && ptr < end) {
157         notice->z_ascii_authent = ptr;
158         numfields--;
159         ptr = next_field(ptr, end);
160     }
161     else
162         BAD_PACKET;
163
164     if (numfields && ptr < end) {
165         notice->z_class = ptr;
166         numfields--;
167         ptr = next_field(ptr, end);
168     }
169     else
170         notice->z_class = "";
171         
172     if (numfields && ptr < end) {
173         notice->z_class_inst = ptr;
174         numfields--;
175         ptr = next_field(ptr, end);
176     }
177     else
178         notice->z_class_inst = "";
179
180     if (numfields && ptr < end) {
181         notice->z_opcode = ptr;
182         numfields--;
183         ptr = next_field(ptr, end);
184     }
185     else
186         notice->z_opcode = "";
187
188     if (numfields && ptr < end) {
189         notice->z_sender = ptr;
190         numfields--;
191         ptr = next_field(ptr, end);
192     }
193     else
194         notice->z_sender = "";
195
196     if (numfields && ptr < end) {
197         notice->z_recipient = ptr;
198         numfields--;
199         ptr = next_field(ptr, end);
200     }
201     else
202         notice->z_recipient = "";
203
204     if (numfields && ptr < end) {
205         notice->z_default_format = ptr;
206         numfields--;
207         ptr = next_field(ptr, end);
208     }
209     else
210         notice->z_default_format = "";
211         
212     if (ZReadAscii32(ptr, end-ptr, &temp) == ZERR_BADFIELD)
213         BAD_PACKET;
214     notice->z_checksum = temp;
215     numfields--;
216     ptr = next_field(ptr, end);
217
218     if (numfields && ptr < end) {
219         notice->z_multinotice = ptr;
220         numfields--;
221         ptr = next_field(ptr, end);
222     }
223     else
224         notice->z_multinotice = "";
225
226     if (numfields && ptr < end) {
227         if (ZReadAscii(ptr, end-ptr, (unsigned char *)&notice->z_multiuid,
228                        sizeof(ZUnique_Id_t)) == ZERR_BADFIELD)
229             BAD_PACKET;
230         notice->z_time.tv_sec = ntohl((u_long) notice->z_multiuid.tv.tv_sec);
231         notice->z_time.tv_usec = ntohl((u_long) notice->z_multiuid.tv.tv_usec);
232         numfields--;
233         ptr = next_field(ptr, end);
234     }
235     else
236         notice->z_multiuid = notice->z_uid;
237
238     for (i=0;ptr < end && i<Z_MAXOTHERFIELDS && numfields;i++,numfields--) {
239         notice->z_other_fields[i] = ptr;
240         ptr = next_field(ptr, end);
241     }
242     notice->z_num_other_fields = i;
243     
244     for (i=0;ptr < end && numfields;numfields--)
245         ptr = next_field(ptr, end);
246
247     if (numfields || *(ptr - 1) != '\0')
248         BAD_PACKET;
249
250     notice->z_message = (caddr_t) ptr;
251     notice->z_message_len = len-(ptr-buffer);
252
253     return (ZERR_NONE);
254 }