1 /*
2  * Copyright (c) 2011 NLNet Labs. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
27 /**
28  * TCP connections.
29  *
30  */
31 
32 #ifndef WIRE_TCPSET_H
33 #define WIRE_TCPSET_H
34 
35 #include "config.h"
36 #include <stdint.h>
37 
38 typedef struct tcp_conn_struct tcp_conn_type;
39 typedef struct tcp_set_struct tcp_set_type;
40 
41 #include "status.h"
42 #include "wire/buffer.h"
43 #include "wire/xfrd.h"
44 
45 #define TCPSET_MAX 50
46 
47 /**
48  * tcp connection.
49  *
50  */
51 struct tcp_conn_struct {
52    int fd;
53    /* how many bytes have been read/written - total, incl. tcp length bytes */
54    uint32_t total_bytes;
55    /* msg len bytes */
56    uint16_t msglen;
57    /* packet buffer of connection */
58    buffer_type* packet;
59    /* state: reading or writing */
60    unsigned is_reading : 1;
61 };
62 
63 /*
64  * Set of tcp connections.
65  *
66  */
67 struct tcp_set_struct {
68     tcp_conn_type* tcp_conn[TCPSET_MAX];
69     xfrd_type* tcp_waiting_first;
70     xfrd_type* tcp_waiting_last;
71     size_t tcp_count;
72 };
73 
74 /**
75  * Create a tcp connection.
76  * \param[in] allocator memory allocator
77  * \return tcp_conn_type* TCP connection.
78  *
79  */
80 extern tcp_conn_type* tcp_conn_create(void);
81 
82 /**
83  * Create a set of tcp connections.
84  * \param[in] allocator memory allocator
85  * \return tcp_set_type* set of tcp connection.
86  *
87  */
88 extern tcp_set_type* tcp_set_create(void);
89 
90 /**
91  * Make tcp connection ready for reading.
92  * \param[in] tcp tcp connection
93  *
94  */
95 extern void tcp_conn_ready(tcp_conn_type* tcp);
96 
97 /*
98  * Read from a tcp connection.
99  * On first call, make sure total_bytes = 0, msglen=0, buffer clear,
100  * and the packet and fd need to be set.
101  * \param[in] tcp tcp connection
102  * \return int -1 on error,
103  *              0 on short read,
104  *              1 on completed read.
105  *
106  */
107 extern int tcp_conn_read(tcp_conn_type* tcp);
108 
109 /*
110  * Write to a tcp connection.
111  * On first call, make sure total_bytes=0, msglen=limit, buffer filled,
112  * and the packet and fd need to be set.
113  * \param[in] tcp tcp connection
114  * \return int -1 on error,
115  *              0 on short write,
116  *              1 on completed write.
117  *
118  */
119 extern int tcp_conn_write(tcp_conn_type* tcp);
120 
121 /**
122  * Clean up set of tcp connections.
123  * \param[in] set set of tcp connections
124  * \param[in] allocator memory allocator
125  *
126  */
127 extern void tcp_set_cleanup(tcp_set_type* set);
128 
129 #endif /* WIRE_TCPSET_H */
130