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(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,
27 ZSubscribeTo(ZSubscription_t *sublist,
31 return (Z_Subscriptions(sublist, nitems, port, CLIENT_SUBSCRIBE, 1));
35 ZSubscribeToSansDefaults(ZSubscription_t *sublist,
39 return (Z_Subscriptions(sublist, nitems, port, CLIENT_SUBSCRIBE_NODEFS,
44 ZUnsubscribeTo(ZSubscription_t *sublist,
48 return (Z_Subscriptions(sublist, nitems, port, CLIENT_UNSUBSCRIBE, 1));
52 ZCancelSubscriptions(unsigned int 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(register ZSubscription_t *sublist,
74 char header[Z_MAXHEADERLEN];
78 int size_avail = Z_MAXPKTLEN-Z_FRAGFUDGE; /* space avail for data,
80 int size, start, numok;
82 /* nitems = 0 means cancel all subscriptions; still need to allocate a */
83 /* array for one item so we can cancel, however. */
85 list = (char **)malloc((unsigned)((nitems==0)?1:nitems)*3*sizeof(char *));
89 (void) memset((char *)¬ice, 0, sizeof(notice));
90 notice.z_kind = ACKED;
92 notice.z_class = ZEPHYR_CTL_CLASS;
93 notice.z_class_inst = ZEPHYR_CTL_CLIENT;
94 notice.z_opcode = opcode;
96 notice.z_recipient = "";
97 notice.z_default_format = "";
98 notice.z_message_len = 0;
100 /* format the header to figure out how long it is */
101 retval = Z_FormatHeader(¬ice, header, sizeof(header), &hdrlen, ZAUTH);
102 if (retval != ZERR_NONE && !authit)
103 retval = Z_FormatHeader(¬ice, header, sizeof(header),
105 if (retval != ZERR_NONE) {
110 /* compute amount of room left */
111 size_avail -= hdrlen;
114 /* assemble subs into an array of pointers */
115 for (i=0;i<nitems;i++) {
116 list[i*3] = sublist[i].zsub_class;
117 list[i*3+1] = sublist[i].zsub_classinst;
118 recip = sublist[i].zsub_recipient;
119 if (recip && *recip == '*')
121 if (!recip || (*recip != 0 && *recip != '@'))
122 recip = ZGetSender();
130 /* there aren't really any, but we need to xmit anyway */
131 retval = subscr_sendoff(¬ice, list, 0, authit);
141 if ((j = strlen(list[i*3])
142 + strlen(list[i*3+1])
143 + strlen(list[i*3+2]) + 3) <= size) {
144 /* it will fit in this packet */
150 if (!numok) { /* a single subscription won't
151 fit into one packet */
153 return(ZERR_FIELDLEN);
155 retval = subscr_sendoff(¬ice, &list[start*3], numok, authit);
163 retval = subscr_sendoff(¬ice, &list[start*3], numok, authit);
169 subscr_sendoff(ZNotice_t *notice,
174 register Code_t retval;
177 retval = ZSendList(notice, lyst, num*3, ZAUTH);
178 if (retval != ZERR_NONE && !authit)
179 retval = ZSendList(notice, lyst, num*3, ZNOAUTH);
181 if (retval != ZERR_NONE)
183 if ((retval = ZIfNotice(&retnotice, (struct sockaddr_in *)0,
184 ZCompareUIDPred, (char *)¬ice->z_uid)) !=
187 if (retnotice.z_kind == SERVNAK) {
188 ZFreeNotice(&retnotice);
189 return (ZERR_SERVNAK);
191 if (retnotice.z_kind != SERVACK) {
192 ZFreeNotice(&retnotice);
193 return (ZERR_INTERNAL);
195 ZFreeNotice(&retnotice);