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,v 1.21 1999/01/22 23:19:30 ghudson Exp $
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,v 1.21 1999/01/22 23:19:30 ghudson Exp $";
20 static Code_t Z_Subscriptions __P((char *galaxy,
21 register ZSubscription_t *sublist,
22 int nitems, unsigned int port,
23 char *opcode, int authit));
24 static Code_t subscr_sendoff __P((ZNotice_t *notice, char **lyst, int num,
27 Code_t ZSubscribeTo(galaxy, sublist, nitems, port)
29 ZSubscription_t *sublist;
33 return (Z_Subscriptions(galaxy, sublist, nitems, port,
34 CLIENT_SUBSCRIBE, 1));
37 Code_t ZSubscribeToSansDefaults(galaxy, sublist, nitems, port)
39 ZSubscription_t *sublist;
43 return (Z_Subscriptions(galaxy, sublist, nitems, port,
44 CLIENT_SUBSCRIBE_NODEFS, 1));
47 Code_t ZUnsubscribeTo(galaxy, sublist, nitems, port)
49 ZSubscription_t *sublist;
53 return (Z_Subscriptions(galaxy, sublist, nitems, port,
54 CLIENT_UNSUBSCRIBE, 1));
57 Code_t ZCancelSubscriptions(galaxy, port)
61 return (Z_Subscriptions(galaxy, (ZSubscription_t *)0, 0, port,
62 CLIENT_CANCELSUB, 0));
66 * This routine must do its own fragmentation. Subscriptions must
67 * not be broken across packet boundaries, or else the server will
72 Z_Subscriptions(galaxy, sublist, nitems, port, opcode, authit)
74 register ZSubscription_t *sublist;
83 char header[Z_MAXHEADERLEN];
87 int size_avail = Z_MAXPKTLEN-Z_FRAGFUDGE; /* space avail for data,
89 int size, start, numok;
91 /* nitems = 0 means cancel all subscriptions; still need to allocate a */
92 /* array for one item so we can cancel, however. */
94 list = (char **)malloc((unsigned)((nitems==0)?1:nitems)*3*sizeof(char *));
98 (void) memset((char *)¬ice, 0, sizeof(notice));
99 notice.z_kind = ACKED;
100 notice.z_port = port;
101 notice.z_class = ZEPHYR_CTL_CLASS;
102 notice.z_class_inst = ZEPHYR_CTL_CLIENT;
103 notice.z_opcode = opcode;
105 notice.z_recipient = "";
106 notice.z_default_format = "";
107 notice.z_message_len = 0;
108 notice.z_dest_galaxy = galaxy;
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(notice, lyst, num, authit)
185 register Code_t retval;
188 retval = ZSendList(notice, lyst, num*3, ZAUTH);
189 if (retval != ZERR_NONE && !authit)
190 retval = ZSendList(notice, lyst, num*3, ZNOAUTH);
192 if (retval != ZERR_NONE)
194 if ((retval = ZIfNotice(&retnotice, (struct sockaddr_in *)0,
195 ZCompareUIDPred, (char *)¬ice->z_uid)) !=
198 if (retnotice.z_kind == SERVNAK) {
199 ZFreeNotice(&retnotice);
200 return (ZERR_SERVNAK);
202 if (retnotice.z_kind != SERVACK) {
203 ZFreeNotice(&retnotice);
204 return (ZERR_INTERNAL);
206 ZFreeNotice(&retnotice);