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