2086
|
1 /* This file is part of the Project Athena Zephyr Notification System.
|
|
2 * It contains source for the ZSubscribeTo, ZUnsubscribeTo, and
|
|
3 * ZCancelSubscriptions functions.
|
|
4 *
|
|
5 * Created by: Robert French
|
|
6 *
|
|
7 * $Source$
|
|
8 * $Author: warmenhoven $
|
|
9 *
|
|
10 * Copyright (c) 1987,1988 by the Massachusetts Institute of Technology.
|
|
11 * For copying and distribution information, see the file
|
|
12 * "mit-copyright.h".
|
|
13 */
|
|
14 /* $Header$ */
|
|
15
|
|
16 #include <internal.h>
|
|
17
|
|
18 #ifndef lint
|
|
19 static const char rcsid_ZSubscriptions_c[] = "$Id: ZSubs.c 2096 2001-07-31 01:00:39Z warmenhoven $";
|
|
20 #endif
|
|
21
|
|
22 static Code_t Z_Subscriptions __P((register ZSubscription_t *sublist,
|
|
23 int nitems, unsigned int port,
|
|
24 char *opcode, int authit));
|
|
25 static Code_t subscr_sendoff __P((ZNotice_t *notice, char **lyst, int num,
|
|
26 int authit));
|
|
27
|
|
28 Code_t ZSubscribeTo(sublist, nitems, port)
|
|
29 ZSubscription_t *sublist;
|
|
30 int nitems;
|
|
31 unsigned int port;
|
|
32 {
|
|
33 return (Z_Subscriptions(sublist, nitems, port, CLIENT_SUBSCRIBE, 1));
|
|
34 }
|
|
35
|
|
36 Code_t ZSubscribeToSansDefaults(sublist, nitems, port)
|
|
37 ZSubscription_t *sublist;
|
|
38 int nitems;
|
|
39 unsigned int port;
|
|
40 {
|
|
41 return (Z_Subscriptions(sublist, nitems, port, CLIENT_SUBSCRIBE_NODEFS,
|
|
42 1));
|
|
43 }
|
|
44
|
|
45 Code_t ZUnsubscribeTo(sublist, nitems, port)
|
|
46 ZSubscription_t *sublist;
|
|
47 int nitems;
|
|
48 unsigned int port;
|
|
49 {
|
|
50 return (Z_Subscriptions(sublist, nitems, port, CLIENT_UNSUBSCRIBE, 1));
|
|
51 }
|
|
52
|
|
53 Code_t ZCancelSubscriptions(port)
|
|
54 unsigned int port;
|
|
55 {
|
|
56 return (Z_Subscriptions((ZSubscription_t *)0, 0, port,
|
|
57 CLIENT_CANCELSUB, 0));
|
|
58 }
|
|
59
|
|
60 /*
|
|
61 * This routine must do its own fragmentation. Subscriptions must
|
|
62 * not be broken across packet boundaries, or else the server will
|
|
63 * mis-interpret them.
|
|
64 */
|
|
65
|
|
66 static Code_t
|
|
67 Z_Subscriptions(sublist, nitems, port, opcode, authit)
|
|
68 register ZSubscription_t *sublist;
|
|
69 int nitems;
|
|
70 unsigned int port;
|
|
71 char *opcode;
|
|
72 int authit;
|
|
73 {
|
|
74 register int i, j;
|
|
75 int retval;
|
|
76 ZNotice_t notice;
|
|
77 char header[Z_MAXHEADERLEN];
|
|
78 char **list;
|
|
79 int hdrlen;
|
|
80 int size_avail = Z_MAXPKTLEN-Z_FRAGFUDGE; /* space avail for data,
|
|
81 adjusted below */
|
|
82 int size, start, numok;
|
|
83
|
|
84 /* nitems = 0 means cancel all subscriptions; still need to allocate a */
|
|
85 /* array for one item so we can cancel, however. */
|
|
86
|
|
87 list = (char **)malloc((unsigned)((nitems==0)?1:nitems)*3*sizeof(char *));
|
|
88 if (!list)
|
|
89 return (ENOMEM);
|
|
90
|
|
91 (void) memset((char *)¬ice, 0, sizeof(notice));
|
|
92 notice.z_kind = ACKED;
|
|
93 notice.z_port = port;
|
|
94 notice.z_class = ZEPHYR_CTL_CLASS;
|
|
95 notice.z_class_inst = ZEPHYR_CTL_CLIENT;
|
|
96 notice.z_opcode = opcode;
|
|
97 notice.z_sender = 0;
|
|
98 notice.z_recipient = "";
|
|
99 notice.z_default_format = "";
|
|
100 notice.z_message_len = 0;
|
|
101
|
|
102 /* format the header to figure out how long it is */
|
|
103 retval = Z_FormatHeader(¬ice, header, sizeof(header), &hdrlen, ZAUTH);
|
|
104 if (retval != ZERR_NONE && !authit)
|
|
105 retval = Z_FormatHeader(¬ice, header, sizeof(header),
|
|
106 &hdrlen, ZAUTH);
|
|
107 if (retval != ZERR_NONE) {
|
|
108 free((char *)list);
|
|
109 return(retval);
|
|
110 }
|
|
111
|
|
112 /* compute amount of room left */
|
|
113 size_avail -= hdrlen;
|
|
114 size = size_avail;
|
|
115
|
|
116 /* assemble subs into an array of pointers */
|
|
117 for (i=0;i<nitems;i++) {
|
|
118 list[i*3] = sublist[i].zsub_class;
|
|
119 list[i*3+1] = sublist[i].zsub_classinst;
|
|
120 if (sublist[i].zsub_recipient && *sublist[i].zsub_recipient &&
|
|
121 *sublist[i].zsub_recipient != '*')
|
|
122 list[i*3+2] = ZGetSender();
|
|
123 else
|
|
124 list[i*3+2] = "";
|
|
125 }
|
|
126
|
|
127 start = -1;
|
|
128 i = 0;
|
|
129 numok = 0;
|
|
130 if (!nitems) {
|
|
131 /* there aren't really any, but we need to xmit anyway */
|
|
132 retval = subscr_sendoff(¬ice, list, 0, authit);
|
|
133 free((char *)list);
|
|
134 return(retval);
|
|
135 }
|
|
136 while(i < nitems) {
|
|
137 if (start == -1) {
|
|
138 size = size_avail;
|
|
139 start = i;
|
|
140 numok = 0;
|
|
141 }
|
|
142 if ((j = strlen(list[i*3])
|
|
143 + strlen(list[i*3+1])
|
|
144 + strlen(list[i*3+2]) + 3) <= size) {
|
|
145 /* it will fit in this packet */
|
|
146 size -= j;
|
|
147 numok++;
|
|
148 i++;
|
|
149 continue;
|
|
150 }
|
|
151 if (!numok) { /* a single subscription won't
|
|
152 fit into one packet */
|
|
153 free((char *)list);
|
|
154 return(ZERR_FIELDLEN);
|
|
155 }
|
|
156 retval = subscr_sendoff(¬ice, &list[start*3], numok, authit);
|
|
157 if (retval) {
|
|
158 free((char *)list);
|
|
159 return(retval);
|
|
160 }
|
|
161 start = -1;
|
|
162 }
|
|
163 if (numok)
|
|
164 retval = subscr_sendoff(¬ice, &list[start*3], numok, authit);
|
|
165 free((char *)list);
|
|
166 return(retval);
|
|
167 }
|
|
168
|
|
169 static Code_t
|
|
170 subscr_sendoff(notice, lyst, num, authit)
|
|
171 ZNotice_t *notice;
|
|
172 char **lyst;
|
|
173 int num;
|
|
174 int authit;
|
|
175 {
|
|
176 register Code_t retval;
|
|
177 ZNotice_t retnotice;
|
|
178
|
|
179 retval = ZSendList(notice, lyst, num*3, ZAUTH);
|
|
180 if (retval != ZERR_NONE && !authit)
|
|
181 retval = ZSendList(notice, lyst, num*3, ZNOAUTH);
|
|
182
|
|
183 if (retval != ZERR_NONE)
|
|
184 return (retval);
|
|
185 if ((retval = ZIfNotice(&retnotice, (struct sockaddr_in *)0,
|
|
186 ZCompareUIDPred, (char *)¬ice->z_uid)) !=
|
|
187 ZERR_NONE)
|
|
188 return (retval);
|
|
189 if (retnotice.z_kind == SERVNAK) {
|
|
190 ZFreeNotice(&retnotice);
|
|
191 return (ZERR_SERVNAK);
|
|
192 }
|
|
193 if (retnotice.z_kind != SERVACK) {
|
|
194 ZFreeNotice(&retnotice);
|
|
195 return (ZERR_INTERNAL);
|
|
196 }
|
|
197 ZFreeNotice(&retnotice);
|
|
198 return (ZERR_NONE);
|
|
199 }
|