1 /* $NetBSD: ccmsg.c,v 1.4 2014/12/10 04:38:01 christos Exp $ */
4 * Portions Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Portions Copyright (C) 2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
12 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
13 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
14 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Portions Copyright (C) 2001 Nominum, Inc.
21 * Permission to use, copy, modify, and/or distribute this software for any
22 * purpose with or without fee is hereby granted, provided that the above
23 * copyright notice and this permission notice appear in all copies.
25 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
26 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
28 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
29 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
30 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
31 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
34 /* Id: ccmsg.c,v 1.10 2007/08/28 07:20:43 tbox Exp */
41 #include <isc/result.h>
45 #include <isccc/events.h>
46 #include <isccc/ccmsg.h>
48 #define CCMSG_MAGIC ISC_MAGIC('C', 'C', 'm', 's')
49 #define VALID_CCMSG(foo) ISC_MAGIC_VALID(foo, CCMSG_MAGIC)
51 static void recv_length(isc_task_t
*, isc_event_t
*);
52 static void recv_message(isc_task_t
*, isc_event_t
*);
56 recv_length(isc_task_t
*task
, isc_event_t
*ev_in
) {
57 isc_socketevent_t
*ev
= (isc_socketevent_t
*)ev_in
;
59 isccc_ccmsg_t
*ccmsg
= ev_in
->ev_arg
;
63 INSIST(VALID_CCMSG(ccmsg
));
67 if (ev
->result
!= ISC_R_SUCCESS
) {
68 ccmsg
->result
= ev
->result
;
75 ccmsg
->size
= ntohl(ccmsg
->size
);
76 if (ccmsg
->size
== 0) {
77 ccmsg
->result
= ISC_R_UNEXPECTEDEND
;
80 if (ccmsg
->size
> ccmsg
->maxsize
) {
81 ccmsg
->result
= ISC_R_RANGE
;
85 region
.base
= isc_mem_get(ccmsg
->mctx
, ccmsg
->size
);
86 region
.length
= ccmsg
->size
;
87 if (region
.base
== NULL
) {
88 ccmsg
->result
= ISC_R_NOMEMORY
;
92 isc_buffer_init(&ccmsg
->buffer
, region
.base
, region
.length
);
93 result
= isc_socket_recv(ccmsg
->sock
, ®ion
, 0,
94 task
, recv_message
, ccmsg
);
95 if (result
!= ISC_R_SUCCESS
) {
96 ccmsg
->result
= result
;
100 isc_event_free(&ev_in
);
104 isc_task_send(ccmsg
->task
, &dev
);
106 isc_event_free(&ev_in
);
111 recv_message(isc_task_t
*task
, isc_event_t
*ev_in
) {
112 isc_socketevent_t
*ev
= (isc_socketevent_t
*)ev_in
;
114 isccc_ccmsg_t
*ccmsg
= ev_in
->ev_arg
;
118 INSIST(VALID_CCMSG(ccmsg
));
122 if (ev
->result
!= ISC_R_SUCCESS
) {
123 ccmsg
->result
= ev
->result
;
127 ccmsg
->result
= ISC_R_SUCCESS
;
128 isc_buffer_add(&ccmsg
->buffer
, ev
->n
);
129 ccmsg
->address
= ev
->address
;
132 isc_task_send(ccmsg
->task
, &dev
);
134 isc_event_free(&ev_in
);
138 isccc_ccmsg_init(isc_mem_t
*mctx
, isc_socket_t
*sock
, isccc_ccmsg_t
*ccmsg
) {
139 REQUIRE(mctx
!= NULL
);
140 REQUIRE(sock
!= NULL
);
141 REQUIRE(ccmsg
!= NULL
);
143 ccmsg
->magic
= CCMSG_MAGIC
;
145 ccmsg
->buffer
.base
= NULL
;
146 ccmsg
->buffer
.length
= 0;
147 ccmsg
->maxsize
= 4294967295U; /* Largest message possible. */
150 ccmsg
->task
= NULL
; /* None yet. */
151 ccmsg
->result
= ISC_R_UNEXPECTED
; /* None yet. */
153 * Should probably initialize the event here, but it can wait.
159 isccc_ccmsg_setmaxsize(isccc_ccmsg_t
*ccmsg
, unsigned int maxsize
) {
160 REQUIRE(VALID_CCMSG(ccmsg
));
162 ccmsg
->maxsize
= maxsize
;
167 isccc_ccmsg_readmessage(isccc_ccmsg_t
*ccmsg
,
168 isc_task_t
*task
, isc_taskaction_t action
, void *arg
)
173 REQUIRE(VALID_CCMSG(ccmsg
));
174 REQUIRE(task
!= NULL
);
175 REQUIRE(ccmsg
->task
== NULL
); /* not currently in use */
177 if (ccmsg
->buffer
.base
!= NULL
) {
178 isc_mem_put(ccmsg
->mctx
, ccmsg
->buffer
.base
,
179 ccmsg
->buffer
.length
);
180 ccmsg
->buffer
.base
= NULL
;
181 ccmsg
->buffer
.length
= 0;
185 ccmsg
->action
= action
;
187 ccmsg
->result
= ISC_R_UNEXPECTED
; /* unknown right now */
189 ISC_EVENT_INIT(&ccmsg
->event
, sizeof(isc_event_t
), 0, 0,
190 ISCCC_EVENT_CCMSG
, action
, arg
, ccmsg
,
193 region
.base
= (unsigned char *)&ccmsg
->size
;
194 region
.length
= 4; /* isc_uint32_t */
195 result
= isc_socket_recv(ccmsg
->sock
, ®ion
, 0,
196 ccmsg
->task
, recv_length
, ccmsg
);
198 if (result
!= ISC_R_SUCCESS
)
205 isccc_ccmsg_cancelread(isccc_ccmsg_t
*ccmsg
) {
206 REQUIRE(VALID_CCMSG(ccmsg
));
208 isc_socket_cancel(ccmsg
->sock
, NULL
, ISC_SOCKCANCEL_RECV
);
213 isccc_ccmsg_freebuffer(isccc_ccmsg_t
*ccmsg
) {
214 REQUIRE(VALID_CCMSG(ccmsg
));
216 if (ccmsg
->buffer
.base
== NULL
)
219 isc_mem_put(ccmsg
->mctx
, ccmsg
->buffer
.base
, ccmsg
->buffer
.length
);
220 ccmsg
->buffer
.base
= NULL
;
221 ccmsg
->buffer
.length
= 0;
226 isccc_ccmsg_invalidate(isccc_ccmsg_t
*ccmsg
) {
227 REQUIRE(VALID_CCMSG(ccmsg
));
231 if (ccmsg
->buffer
.base
!= NULL
) {
232 isc_mem_put(ccmsg
->mctx
, ccmsg
->buffer
.base
,
233 ccmsg
->buffer
.length
);
234 ccmsg
->buffer
.base
= NULL
;
235 ccmsg
->buffer
.length
= 0;