1 /* $NetBSD: tcpmsg.c,v 1.4 2014/12/10 04:37:58 christos Exp $ */
4 * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-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 DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: tcpmsg.c,v 1.31 2007/06/19 23:47:16 tbox Exp */
30 #include <dns/events.h>
31 #include <dns/result.h>
32 #include <dns/tcpmsg.h>
35 #include <stdio.h> /* Required for printf. */
36 #define XDEBUG(x) printf x
41 #define TCPMSG_MAGIC ISC_MAGIC('T', 'C', 'P', 'm')
42 #define VALID_TCPMSG(foo) ISC_MAGIC_VALID(foo, TCPMSG_MAGIC)
44 static void recv_length(isc_task_t
*, isc_event_t
*);
45 static void recv_message(isc_task_t
*, isc_event_t
*);
49 recv_length(isc_task_t
*task
, isc_event_t
*ev_in
) {
50 isc_socketevent_t
*ev
= (isc_socketevent_t
*)ev_in
;
52 dns_tcpmsg_t
*tcpmsg
= ev_in
->ev_arg
;
56 INSIST(VALID_TCPMSG(tcpmsg
));
59 tcpmsg
->address
= ev
->address
;
61 if (ev
->result
!= ISC_R_SUCCESS
) {
62 tcpmsg
->result
= ev
->result
;
69 tcpmsg
->size
= ntohs(tcpmsg
->size
);
70 if (tcpmsg
->size
== 0) {
71 tcpmsg
->result
= ISC_R_UNEXPECTEDEND
;
74 if (tcpmsg
->size
> tcpmsg
->maxsize
) {
75 tcpmsg
->result
= ISC_R_RANGE
;
79 region
.base
= isc_mem_get(tcpmsg
->mctx
, tcpmsg
->size
);
80 region
.length
= tcpmsg
->size
;
81 if (region
.base
== NULL
) {
82 tcpmsg
->result
= ISC_R_NOMEMORY
;
85 XDEBUG(("Allocated %d bytes\n", tcpmsg
->size
));
87 isc_buffer_init(&tcpmsg
->buffer
, region
.base
, region
.length
);
88 result
= isc_socket_recv(tcpmsg
->sock
, ®ion
, 0,
89 task
, recv_message
, tcpmsg
);
90 if (result
!= ISC_R_SUCCESS
) {
91 tcpmsg
->result
= result
;
95 isc_event_free(&ev_in
);
99 isc_task_send(tcpmsg
->task
, &dev
);
101 isc_event_free(&ev_in
);
106 recv_message(isc_task_t
*task
, isc_event_t
*ev_in
) {
107 isc_socketevent_t
*ev
= (isc_socketevent_t
*)ev_in
;
109 dns_tcpmsg_t
*tcpmsg
= ev_in
->ev_arg
;
113 INSIST(VALID_TCPMSG(tcpmsg
));
115 dev
= &tcpmsg
->event
;
116 tcpmsg
->address
= ev
->address
;
118 if (ev
->result
!= ISC_R_SUCCESS
) {
119 tcpmsg
->result
= ev
->result
;
123 tcpmsg
->result
= ISC_R_SUCCESS
;
124 isc_buffer_add(&tcpmsg
->buffer
, ev
->n
);
126 XDEBUG(("Received %d bytes (of %d)\n", ev
->n
, tcpmsg
->size
));
129 isc_task_send(tcpmsg
->task
, &dev
);
131 isc_event_free(&ev_in
);
135 dns_tcpmsg_init(isc_mem_t
*mctx
, isc_socket_t
*sock
, dns_tcpmsg_t
*tcpmsg
) {
136 REQUIRE(mctx
!= NULL
);
137 REQUIRE(sock
!= NULL
);
138 REQUIRE(tcpmsg
!= NULL
);
140 tcpmsg
->magic
= TCPMSG_MAGIC
;
142 tcpmsg
->buffer
.base
= NULL
;
143 tcpmsg
->buffer
.length
= 0;
144 tcpmsg
->maxsize
= 65535; /* Largest message possible. */
147 tcpmsg
->task
= NULL
; /* None yet. */
148 tcpmsg
->result
= ISC_R_UNEXPECTED
; /* None yet. */
150 * Should probably initialize the event here, but it can wait.
156 dns_tcpmsg_setmaxsize(dns_tcpmsg_t
*tcpmsg
, unsigned int maxsize
) {
157 REQUIRE(VALID_TCPMSG(tcpmsg
));
158 REQUIRE(maxsize
< 65536);
160 tcpmsg
->maxsize
= maxsize
;
165 dns_tcpmsg_readmessage(dns_tcpmsg_t
*tcpmsg
,
166 isc_task_t
*task
, isc_taskaction_t action
, void *arg
)
171 REQUIRE(VALID_TCPMSG(tcpmsg
));
172 REQUIRE(task
!= NULL
);
173 REQUIRE(tcpmsg
->task
== NULL
); /* not currently in use */
175 if (tcpmsg
->buffer
.base
!= NULL
) {
176 isc_mem_put(tcpmsg
->mctx
, tcpmsg
->buffer
.base
,
177 tcpmsg
->buffer
.length
);
178 tcpmsg
->buffer
.base
= NULL
;
179 tcpmsg
->buffer
.length
= 0;
183 tcpmsg
->action
= action
;
185 tcpmsg
->result
= ISC_R_UNEXPECTED
; /* unknown right now */
187 ISC_EVENT_INIT(&tcpmsg
->event
, sizeof(isc_event_t
), 0, 0,
188 DNS_EVENT_TCPMSG
, action
, arg
, tcpmsg
,
191 region
.base
= (unsigned char *)&tcpmsg
->size
;
192 region
.length
= 2; /* isc_uint16_t */
193 result
= isc_socket_recv(tcpmsg
->sock
, ®ion
, 0,
194 tcpmsg
->task
, recv_length
, tcpmsg
);
196 if (result
!= ISC_R_SUCCESS
)
203 dns_tcpmsg_cancelread(dns_tcpmsg_t
*tcpmsg
) {
204 REQUIRE(VALID_TCPMSG(tcpmsg
));
206 isc_socket_cancel(tcpmsg
->sock
, NULL
, ISC_SOCKCANCEL_RECV
);
210 dns_tcpmsg_keepbuffer(dns_tcpmsg_t
*tcpmsg
, isc_buffer_t
*buffer
) {
211 REQUIRE(VALID_TCPMSG(tcpmsg
));
212 REQUIRE(buffer
!= NULL
);
214 *buffer
= tcpmsg
->buffer
;
215 tcpmsg
->buffer
.base
= NULL
;
216 tcpmsg
->buffer
.length
= 0;
221 dns_tcpmsg_freebuffer(dns_tcpmsg_t
*tcpmsg
) {
222 REQUIRE(VALID_TCPMSG(tcpmsg
));
224 if (tcpmsg
->buffer
.base
== NULL
)
227 isc_mem_put(tcpmsg
->mctx
, tcpmsg
->buffer
.base
, tcpmsg
->buffer
.length
);
228 tcpmsg
->buffer
.base
= NULL
;
229 tcpmsg
->buffer
.length
= 0;
234 dns_tcpmsg_invalidate(dns_tcpmsg_t
*tcpmsg
) {
235 REQUIRE(VALID_TCPMSG(tcpmsg
));
239 if (tcpmsg
->buffer
.base
!= NULL
) {
240 isc_mem_put(tcpmsg
->mctx
, tcpmsg
->buffer
.base
,
241 tcpmsg
->buffer
.length
);
242 tcpmsg
->buffer
.base
= NULL
;
243 tcpmsg
->buffer
.length
= 0;