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