ccmsg.c revision 1.3 1 /* $NetBSD: ccmsg.c,v 1.3 2019/01/09 16:55:18 christos Exp $ */
2
3 /*
4 * Portions Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 *
13 * Portions Copyright (C) 2001 Nominum, Inc.
14 *
15 * Permission to use, copy, modify, and/or distribute this software for any
16 * purpose with or without fee is hereby granted, provided that the above
17 * copyright notice and this permission notice appear in all copies.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
20 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
22 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
23 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
24 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
25 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 */
27
28
29 /*! \file */
30
31 #include <config.h>
32
33 #include <inttypes.h>
34
35 #include <isc/mem.h>
36 #include <isc/result.h>
37 #include <isc/task.h>
38 #include <isc/util.h>
39
40 #include <isccc/events.h>
41 #include <isccc/ccmsg.h>
42
43 #define CCMSG_MAGIC ISC_MAGIC('C', 'C', 'm', 's')
44 #define VALID_CCMSG(foo) ISC_MAGIC_VALID(foo, CCMSG_MAGIC)
45
46 static void recv_length(isc_task_t *, isc_event_t *);
47 static void recv_message(isc_task_t *, isc_event_t *);
48
49
50 static void
51 recv_length(isc_task_t *task, isc_event_t *ev_in) {
52 isc_socketevent_t *ev = (isc_socketevent_t *)ev_in;
53 isc_event_t *dev;
54 isccc_ccmsg_t *ccmsg = ev_in->ev_arg;
55 isc_region_t region;
56 isc_result_t result;
57
58 INSIST(VALID_CCMSG(ccmsg));
59
60 dev = &ccmsg->event;
61
62 if (ev->result != ISC_R_SUCCESS) {
63 ccmsg->result = ev->result;
64 goto send_and_free;
65 }
66
67 /*
68 * Success.
69 */
70 ccmsg->size = ntohl(ccmsg->size);
71 if (ccmsg->size == 0) {
72 ccmsg->result = ISC_R_UNEXPECTEDEND;
73 goto send_and_free;
74 }
75 if (ccmsg->size > ccmsg->maxsize) {
76 ccmsg->result = ISC_R_RANGE;
77 goto send_and_free;
78 }
79
80 region.base = isc_mem_get(ccmsg->mctx, ccmsg->size);
81 region.length = ccmsg->size;
82 if (region.base == NULL) {
83 ccmsg->result = ISC_R_NOMEMORY;
84 goto send_and_free;
85 }
86
87 isc_buffer_init(&ccmsg->buffer, region.base, region.length);
88 result = isc_socket_recv(ccmsg->sock, ®ion, 0,
89 task, recv_message, ccmsg);
90 if (result != ISC_R_SUCCESS) {
91 ccmsg->result = result;
92 goto send_and_free;
93 }
94
95 isc_event_free(&ev_in);
96 return;
97
98 send_and_free:
99 isc_task_send(ccmsg->task, &dev);
100 ccmsg->task = NULL;
101 isc_event_free(&ev_in);
102 return;
103 }
104
105 static void
106 recv_message(isc_task_t *task, isc_event_t *ev_in) {
107 isc_socketevent_t *ev = (isc_socketevent_t *)ev_in;
108 isc_event_t *dev;
109 isccc_ccmsg_t *ccmsg = ev_in->ev_arg;
110
111 (void)task;
112
113 INSIST(VALID_CCMSG(ccmsg));
114
115 dev = &ccmsg->event;
116
117 if (ev->result != ISC_R_SUCCESS) {
118 ccmsg->result = ev->result;
119 goto send_and_free;
120 }
121
122 ccmsg->result = ISC_R_SUCCESS;
123 isc_buffer_add(&ccmsg->buffer, ev->n);
124 ccmsg->address = ev->address;
125
126 send_and_free:
127 isc_task_send(ccmsg->task, &dev);
128 ccmsg->task = NULL;
129 isc_event_free(&ev_in);
130 }
131
132 void
133 isccc_ccmsg_init(isc_mem_t *mctx, isc_socket_t *sock, isccc_ccmsg_t *ccmsg) {
134 REQUIRE(mctx != NULL);
135 REQUIRE(sock != NULL);
136 REQUIRE(ccmsg != NULL);
137
138 ccmsg->magic = CCMSG_MAGIC;
139 ccmsg->size = 0;
140 ccmsg->buffer.base = NULL;
141 ccmsg->buffer.length = 0;
142 ccmsg->maxsize = 4294967295U; /* Largest message possible. */
143 ccmsg->mctx = mctx;
144 ccmsg->sock = sock;
145 ccmsg->task = NULL; /* None yet. */
146 ccmsg->result = ISC_R_UNEXPECTED; /* None yet. */
147 /*
148 * Should probably initialize the event here, but it can wait.
149 */
150 }
151
152
153 void
154 isccc_ccmsg_setmaxsize(isccc_ccmsg_t *ccmsg, unsigned int maxsize) {
155 REQUIRE(VALID_CCMSG(ccmsg));
156
157 ccmsg->maxsize = maxsize;
158 }
159
160
161 isc_result_t
162 isccc_ccmsg_readmessage(isccc_ccmsg_t *ccmsg,
163 isc_task_t *task, isc_taskaction_t action, void *arg)
164 {
165 isc_result_t result;
166 isc_region_t region;
167
168 REQUIRE(VALID_CCMSG(ccmsg));
169 REQUIRE(task != NULL);
170 REQUIRE(ccmsg->task == NULL); /* not currently in use */
171
172 if (ccmsg->buffer.base != NULL) {
173 isc_mem_put(ccmsg->mctx, ccmsg->buffer.base,
174 ccmsg->buffer.length);
175 ccmsg->buffer.base = NULL;
176 ccmsg->buffer.length = 0;
177 }
178
179 ccmsg->task = task;
180 ccmsg->action = action;
181 ccmsg->arg = arg;
182 ccmsg->result = ISC_R_UNEXPECTED; /* unknown right now */
183
184 ISC_EVENT_INIT(&ccmsg->event, sizeof(isc_event_t), 0, 0,
185 ISCCC_EVENT_CCMSG, action, arg, ccmsg,
186 NULL, NULL);
187
188 region.base = (unsigned char *)&ccmsg->size;
189 region.length = 4; /* uint32_t */
190 result = isc_socket_recv(ccmsg->sock, ®ion, 0,
191 ccmsg->task, recv_length, ccmsg);
192
193 if (result != ISC_R_SUCCESS)
194 ccmsg->task = NULL;
195
196 return (result);
197 }
198
199 void
200 isccc_ccmsg_cancelread(isccc_ccmsg_t *ccmsg) {
201 REQUIRE(VALID_CCMSG(ccmsg));
202
203 isc_socket_cancel(ccmsg->sock, NULL, ISC_SOCKCANCEL_RECV);
204 }
205
206 #if 0
207 void
208 isccc_ccmsg_freebuffer(isccc_ccmsg_t *ccmsg) {
209 REQUIRE(VALID_CCMSG(ccmsg));
210
211 if (ccmsg->buffer.base == NULL)
212 return;
213
214 isc_mem_put(ccmsg->mctx, ccmsg->buffer.base, ccmsg->buffer.length);
215 ccmsg->buffer.base = NULL;
216 ccmsg->buffer.length = 0;
217 }
218 #endif
219
220 void
221 isccc_ccmsg_invalidate(isccc_ccmsg_t *ccmsg) {
222 REQUIRE(VALID_CCMSG(ccmsg));
223
224 ccmsg->magic = 0;
225
226 if (ccmsg->buffer.base != NULL) {
227 isc_mem_put(ccmsg->mctx, ccmsg->buffer.base,
228 ccmsg->buffer.length);
229 ccmsg->buffer.base = NULL;
230 ccmsg->buffer.length = 0;
231 }
232 }
233