Mercurial > notdcc
comparison dcclib/aop.c @ 0:c7f6b056b673
First import of vendor version
author | Peter Gervai <grin@grin.hu> |
---|---|
date | Tue, 10 Mar 2009 13:49:58 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c7f6b056b673 |
---|---|
1 /* Distributed Checksum Clearinghouse | |
2 * | |
3 * ask for and administrative operation | |
4 * | |
5 * Copyright (c) 2008 by Rhyolite Software, LLC | |
6 * | |
7 * This agreement is not applicable to any entity which sells anti-spam | |
8 * solutions to others or provides an anti-spam solution as part of a | |
9 * security solution sold to other entities, or to a private network | |
10 * which employs the DCC or uses data provided by operation of the DCC | |
11 * but does not provide corresponding data to other users. | |
12 * | |
13 * Permission to use, copy, modify, and distribute this software without | |
14 * changes for any purpose with or without fee is hereby granted, provided | |
15 * that the above copyright notice and this permission notice appear in all | |
16 * copies and any distributed versions or copies are either unchanged | |
17 * or not called anything similar to "DCC" or "Distributed Checksum | |
18 * Clearinghouse". | |
19 * | |
20 * Parties not eligible to receive a license under this agreement can | |
21 * obtain a commercial license to use DCC by contacting Rhyolite Software | |
22 * at sales@rhyolite.com. | |
23 * | |
24 * A commercial license would be for Distributed Checksum and Reputation | |
25 * Clearinghouse software. That software includes additional features. This | |
26 * free license for Distributed ChecksumClearinghouse Software does not in any | |
27 * way grant permision to use Distributed Checksum and Reputation Clearinghouse | |
28 * software | |
29 * | |
30 * THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE, LLC DISCLAIMS ALL | |
31 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | |
32 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE, LLC | |
33 * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES | |
34 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, | |
35 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, | |
36 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |
37 * SOFTWARE. | |
38 * | |
39 * Rhyolite Software DCC 1.3.103-1.39 $Revision$ | |
40 */ | |
41 | |
42 #include "dcc_clnt.h" | |
43 | |
44 | |
45 /* ask for an administrative operation */ | |
46 DCC_OPS /* DCC_OP_INVALID=failed, else result */ | |
47 dcc_aop(DCC_EMSG emsg, /* result if DCC_OP_ERROR or _INVALID */ | |
48 DCC_CLNT_CTXT *ctxt, | |
49 DCC_CLNT_FGS clnt_fgs, /* DCC_CLNT_FG_* */ | |
50 SRVR_INX anum, /* server index or NO_SRVR */ | |
51 time_t dsecs, /* fudge timestamp by this */ | |
52 DCC_AOPS aop, | |
53 u_int32_t val1, u_char val2, u_char val3, | |
54 u_char val4, u_char *val5, u_int val5_len, | |
55 DCC_OP_RESP *resp, | |
56 DCC_SOCKU *resp_su) /* IP address of server used */ | |
57 { | |
58 DCC_ADMN_REQ req; | |
59 DCC_EMSG loc_emsg; | |
60 DCC_OP_RESP resp0; | |
61 char resp_buf[DCC_OPBUF]; | |
62 | |
63 memset(&req, 0, sizeof(req)); | |
64 req.date = htonl(time(0) + dsecs); | |
65 req.aop = aop; | |
66 req.val1 = ntohl(val1); | |
67 req.val2 = val2; | |
68 req.val3 = val3; | |
69 req.val4 = val4; | |
70 if (val5_len != 0) { | |
71 if (val5_len > MAX_DCC_ADMN_REQ_VAL5) | |
72 dcc_logbad(EX_SOFTWARE, "bogus aop val5 length %d", | |
73 val5_len); | |
74 memcpy(req.val5, val5, val5_len); | |
75 } | |
76 if (!resp) { | |
77 resp = &resp0; | |
78 } else if (clnt_fgs & DCC_CLNT_FG_RETRANS) { | |
79 req.hdr.op_nums.r = resp->hdr.op_nums.r; | |
80 } | |
81 memset(resp, 0, sizeof(*resp)); | |
82 if (!dcc_clnt_op(loc_emsg, ctxt, | |
83 clnt_fgs | DCC_CLNT_FG_NO_FAIL, &anum, 0, resp_su, | |
84 &req.hdr, sizeof(req)-MAX_DCC_ADMN_REQ_VAL5+val5_len, | |
85 DCC_OP_ADMN, | |
86 resp, sizeof(*resp))) { | |
87 dcc_pemsg(dcc_ex_code, emsg, "%s: %s", | |
88 dcc_aop2str(resp_buf, sizeof(resp_buf), aop, val1), | |
89 loc_emsg); | |
90 return DCC_OP_INVALID; | |
91 } | |
92 | |
93 if (resp->hdr.op == DCC_OP_OK) | |
94 return DCC_OP_OK; | |
95 if (resp->hdr.op == DCC_OP_ADMN) { | |
96 /* clear signature after possible string */ | |
97 int len = (ntohs(resp->hdr.len) | |
98 - (sizeof(resp->resp) | |
99 - sizeof(resp->resp.val.string))); | |
100 if (len < ISZ(resp->resp.val.string)) | |
101 resp->resp.val.string[len] = '\0'; | |
102 return DCC_OP_ADMN; | |
103 } | |
104 if (resp->hdr.op == DCC_OP_ERROR) { | |
105 dcc_pemsg(dcc_ex_code, emsg, "%s: %s", | |
106 dcc_aop2str(resp_buf, sizeof(resp_buf), aop, val1), | |
107 resp->resp.val.string); | |
108 return DCC_OP_ERROR; | |
109 } | |
110 dcc_pemsg(EX_PROTOCOL, emsg, "%s unexpected response: %s", | |
111 dcc_aop2str(0, 0, aop, val1), | |
112 dcc_hdr_op2str(resp_buf, sizeof(resp_buf), &resp->hdr)); | |
113 return DCC_OP_INVALID; | |
114 } | |
115 | |
116 | |
117 | |
118 /* try for a long time or until the server hears */ | |
119 u_char /* 1=ok, 0=failed */ | |
120 dcc_aop_persist(DCC_EMSG emsg, | |
121 DCC_CLNT_CTXT *ctxt, | |
122 DCC_CLNT_FGS clnt_fgs, | |
123 u_char debug, | |
124 DCC_AOPS aop, u_int32_t val1, | |
125 int secs, /* try for this long */ | |
126 DCC_OP_RESP *aop_resp) /* results here */ | |
127 { | |
128 struct timeval begin, now, op_start; | |
129 char buf1[DCC_OPBUF]; | |
130 char buf2[DCC_OPBUF]; | |
131 u_char complained, fast; | |
132 time_t us; | |
133 DCC_OPS result; | |
134 | |
135 gettimeofday(&begin, 0); | |
136 complained = 0; | |
137 fast = 0; | |
138 | |
139 for (;;) { | |
140 /* This kludge on the transaction ID makes all of our | |
141 * requests appear to be retransmissions of a single request. | |
142 * This is nice for operations such as DCC_AOP_FLOD_SHUTDOWN | |
143 * that are not idempotent when the server has been stalled by | |
144 * the operating system. */ | |
145 gettimeofday(&op_start, 0); | |
146 | |
147 result = dcc_aop(emsg, ctxt, clnt_fgs, NO_SRVR, 0, | |
148 aop, val1, 0, 0, 0, 0, 0, aop_resp, 0); | |
149 /* finished if that worked */ | |
150 if (result == DCC_OP_ADMN || result == DCC_OP_OK) | |
151 return 1; | |
152 | |
153 /* use the same transaction ID next time */ | |
154 if (aop_resp) | |
155 clnt_fgs |= DCC_CLNT_FG_RETRANS; | |
156 | |
157 if (result != DCC_OP_ERROR && result != DCC_OP_INVALID) | |
158 dcc_pemsg(EX_UNAVAILABLE, emsg, "%s: %s", | |
159 dcc_aop2str(buf1, sizeof(buf1), aop, val1), | |
160 aop_resp | |
161 ? dcc_hdr_op2str(buf2, sizeof(buf2), | |
162 &aop_resp->hdr) | |
163 : ""); | |
164 | |
165 /* deal with time change */ | |
166 gettimeofday(&now, 0); | |
167 us = tv_diff2us(&now, &begin); | |
168 if (us < 0) | |
169 begin = op_start = now; | |
170 | |
171 /* eventually give up */ | |
172 if (us/DCC_US >= secs) | |
173 return 0; | |
174 | |
175 us = tv_diff2us(&now, &op_start); | |
176 if (us >= DCC_US) { | |
177 fast = 0; | |
178 } else { | |
179 /* assume the server is dead if it persistently fails | |
180 * immediately */ | |
181 if (result == DCC_OP_INVALID && ++fast > 4) { | |
182 if (debug) | |
183 quiet_trace_msg("%s: assume dccd dead", | |
184 dcc_aop2str(buf1, | |
185 sizeof(buf1), | |
186 aop, val1)); | |
187 return 0; | |
188 } | |
189 /* explicit delay since the request didn't delay */ | |
190 usleep(1000); | |
191 } | |
192 | |
193 /* sometimes emit a message if we are going to try again */ | |
194 if ((result != DCC_OP_ERROR && result != DCC_OP_INVALID) | |
195 && (debug || !complained)) { | |
196 complained = 1; | |
197 dcc_error_msg("%s", emsg); | |
198 emsg[0] = '\0'; | |
199 } | |
200 } | |
201 } |