1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It contains source for the ZSubscribeTo, ZUnsubscribeTo, and
3 * ZCancelSubscriptions functions.
5 * Created by: Robert French
9 * Copyright (c) 1987,1988 by the Massachusetts Institute of Technology.
10 * For copying and distribution information, see the file
17 static const char rcsid_ZSubscriptions_c[] = "$Id$";
20 static Code_t Z_Subscriptions __P((register ZSubscription_t *sublist,
21 int nitems, unsigned int port,
22 char *opcode, int authit));
23 static Code_t subscr_sendoff __P((ZNotice_t *notice, char **lyst, int num,
26 Code_t ZSubscribeTo(sublist, nitems, port)
27 ZSubscription_t *sublist;
31 return (Z_Subscriptions(sublist, nitems, port, CLIENT_SUBSCRIBE, 1));
34 Code_t ZSubscribeToSansDefaults(sublist, nitems, port)
35 ZSubscription_t *sublist;
39 return (Z_Subscriptions(sublist, nitems, port, CLIENT_SUBSCRIBE_NODEFS,
43 Code_t ZUnsubscribeTo(sublist, nitems, port)
44 ZSubscription_t *sublist;
48 return (Z_Subscriptions(sublist, nitems, port, CLIENT_UNSUBSCRIBE, 1));
51 Code_t ZCancelSubscriptions(port)
54 return (Z_Subscriptions((ZSubscription_t *)0, 0, port,
55 CLIENT_CANCELSUB, 0));
59 * This routine must do its own fragmentation. Subscriptions must
60 * not be broken across packet boundaries, or else the server will
65 Z_Subscriptions(sublist, nitems, port, opcode, authit)
66 register ZSubscription_t *sublist;
75 char header[Z_MAXHEADERLEN];
79 int size_avail = Z_MAXPKTLEN-Z_FRAGFUDGE; /* space avail for data,
81 int size, start, numok;
83 /* nitems = 0 means cancel all subscriptions; still need to allocate a */
84 /* array for one item so we can cancel, however. */
86 list = (char **)malloc((unsigned)((nitems==0)?1:nitems)*3*sizeof(char *));
90 (void) memset((char *)¬ice, 0, sizeof(notice));
91 notice.z_kind = ACKED;
93 notice.z_class = ZEPHYR_CTL_CLASS;
94 notice.z_class_inst = ZEPHYR_CTL_CLIENT;
95 notice.z_opcode = opcode;
97 notice.z_recipient = "";
98 notice.z_default_format = "";
99 notice.z_message_len = 0;
101 /* format the header to figure out how long it is */
102 retval = Z_FormatHeader(¬ice, header, sizeof(header), &hdrlen, ZAUTH);
103 if (retval != ZERR_NONE && !authit)
104 retval = Z_FormatHeader(¬ice, header, sizeof(header),
106 if (retval != ZERR_NONE) {
111 /* compute amount of room left */
112 size_avail -= hdrlen;
115 /* assemble subs into an array of pointers */
116 for (i=0;i<nitems;i++) {
117 list[i*3] = sublist[i].zsub_class;
118 list[i*3+1] = sublist[i].zsub_classinst;
119 recip = sublist[i].zsub_recipient;
120 if (recip && *recip == '*')
122 if (!recip || (*recip != 0 && *recip != '@'))
123 recip = ZGetSender();
131 /* there aren't really any, but we need to xmit anyway */
132 retval = subscr_sendoff(¬ice, list, 0, authit);
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 */
151 if (!numok) { /* a single subscription won't
152 fit into one packet */
154 return(ZERR_FIELDLEN);
156 retval = subscr_sendoff(¬ice, &list[start*3], numok, authit);
164 retval = subscr_sendoff(¬ice, &list[start*3], numok, authit);
170 subscr_sendoff(notice, lyst, num, authit)
176 register Code_t retval;
179 retval = ZSendList(notice, lyst, num*3, ZAUTH);
180 if (retval != ZERR_NONE && !authit)
181 retval = ZSendList(notice, lyst, num*3, ZNOAUTH);
183 if (retval != ZERR_NONE)
185 if ((retval = ZIfNotice(&retnotice, (struct sockaddr_in *)0,
186 ZCompareUIDPred, (char *)¬ice->z_uid)) !=
189 if (retnotice.z_kind == SERVNAK) {
190 ZFreeNotice(&retnotice);
191 return (ZERR_SERVNAK);
193 if (retnotice.z_kind != SERVACK) {
194 ZFreeNotice(&retnotice);
195 return (ZERR_INTERNAL);
197 ZFreeNotice(&retnotice);