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
7 * $Id: ZSubs.c 2295 2009-03-15 22:13:16Z kcr $
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: ZSubs.c 2295 2009-03-15 22:13:16Z kcr $";
20 static Code_t Z_Subscriptions(register ZSubscription_t *sublist,
21 int nitems, unsigned int port,
22 char *opcode, int authit);
23 static Code_t subscr_sendoff(ZNotice_t *notice, char **lyst, int num,
28 ZPunt(ZSubscription_t *sublist,
32 return (Z_Subscriptions(sublist, nitems, port, "SUPPRESS", 1));
37 ZSubscribeTo(ZSubscription_t *sublist,
41 return (Z_Subscriptions(sublist, nitems, port, CLIENT_SUBSCRIBE, 1));
45 ZSubscribeToSansDefaults(ZSubscription_t *sublist,
49 return (Z_Subscriptions(sublist, nitems, port, CLIENT_SUBSCRIBE_NODEFS,
54 ZUnsubscribeTo(ZSubscription_t *sublist,
58 return (Z_Subscriptions(sublist, nitems, port, CLIENT_UNSUBSCRIBE, 1));
62 ZCancelSubscriptions(unsigned int port)
64 return (Z_Subscriptions((ZSubscription_t *)0, 0, port,
65 CLIENT_CANCELSUB, 0));
69 * This routine must do its own fragmentation. Subscriptions must
70 * not be broken across packet boundaries, or else the server will
75 Z_Subscriptions(register ZSubscription_t *sublist,
84 char header[Z_MAXHEADERLEN];
88 int size_avail = Z_MAXPKTLEN-Z_FRAGFUDGE; /* space avail for data,
90 int size, start, numok;
92 /* nitems = 0 means cancel all subscriptions; still need to allocate a */
93 /* array for one item so we can cancel, however. */
95 list = (char **)malloc((unsigned)((nitems==0)?1:nitems)*3*sizeof(char *));
99 (void) memset((char *)¬ice, 0, sizeof(notice));
100 notice.z_kind = ACKED;
101 notice.z_port = port;
102 notice.z_class = ZEPHYR_CTL_CLASS;
103 notice.z_class_inst = ZEPHYR_CTL_CLIENT;
104 notice.z_opcode = opcode;
106 notice.z_recipient = "";
107 notice.z_default_format = "";
108 notice.z_message_len = 0;
110 /* format the header to figure out how long it is */
111 retval = Z_FormatHeader(¬ice, header, sizeof(header), &hdrlen, ZAUTH);
112 if (retval != ZERR_NONE && !authit)
113 retval = Z_FormatHeader(¬ice, header, sizeof(header),
115 if (retval != ZERR_NONE) {
120 /* compute amount of room left */
121 size_avail -= hdrlen;
124 /* assemble subs into an array of pointers */
125 for (i=0;i<nitems;i++) {
126 list[i*3] = sublist[i].zsub_class;
127 list[i*3+1] = sublist[i].zsub_classinst;
128 recip = sublist[i].zsub_recipient;
129 if (recip && *recip == '*')
131 if (!recip || (*recip != 0 && *recip != '@'))
132 recip = ZGetSender();
140 /* there aren't really any, but we need to xmit anyway */
141 retval = subscr_sendoff(¬ice, list, 0, authit);
151 if ((j = strlen(list[i*3])
152 + strlen(list[i*3+1])
153 + strlen(list[i*3+2]) + 3) <= size) {
154 /* it will fit in this packet */
160 if (!numok) { /* a single subscription won't
161 fit into one packet */
163 return(ZERR_FIELDLEN);
165 retval = subscr_sendoff(¬ice, &list[start*3], numok, authit);
173 retval = subscr_sendoff(¬ice, &list[start*3], numok, authit);
179 subscr_sendoff(ZNotice_t *notice,
184 register Code_t retval;
187 retval = ZSendList(notice, lyst, num*3, ZAUTH);
188 if (retval != ZERR_NONE && !authit)
189 retval = ZSendList(notice, lyst, num*3, ZNOAUTH);
191 if (retval != ZERR_NONE)
193 if ((retval = ZIfNotice(&retnotice, (struct sockaddr_in *)0,
194 ZCompareUIDPred, (char *)¬ice->z_uid)) !=
197 if (retnotice.z_kind == SERVNAK) {
198 ZFreeNotice(&retnotice);
199 return (ZERR_SERVNAK);
201 if (retnotice.z_kind != SERVACK) {
202 ZFreeNotice(&retnotice);
203 return (ZERR_INTERNAL);
205 ZFreeNotice(&retnotice);