2 * Copyright (c) 2009-2010 The FreeBSD Foundation
5 * This software was developed by Pawel Jakub Dawidek under sponsorship from
6 * the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
34 #include <sys/queue.h>
35 #include <sys/socket.h>
42 #include "proto_impl.h"
44 #define PROTO_CONN_MAGIC 0x907041c
47 struct hast_proto *pc_proto;
50 #define PROTO_SIDE_CLIENT 0
51 #define PROTO_SIDE_SERVER_LISTEN 1
52 #define PROTO_SIDE_SERVER_WORK 2
55 static TAILQ_HEAD(, hast_proto) protos = TAILQ_HEAD_INITIALIZER(protos);
58 proto_register(struct hast_proto *proto, bool isdefault)
60 static bool seen_default = false;
63 TAILQ_INSERT_HEAD(&protos, proto, hp_next);
65 assert(!seen_default);
67 TAILQ_INSERT_TAIL(&protos, proto, hp_next);
72 proto_common_setup(const char *addr, struct proto_conn **connp, int side)
74 struct hast_proto *proto;
75 struct proto_conn *conn;
79 assert(side == PROTO_SIDE_CLIENT || side == PROTO_SIDE_SERVER_LISTEN);
81 conn = malloc(sizeof(*conn));
85 TAILQ_FOREACH(proto, &protos, hp_next) {
86 if (side == PROTO_SIDE_CLIENT)
87 ret = proto->hp_client(addr, &ctx);
88 else /* if (side == PROTO_SIDE_SERVER_LISTEN) */
89 ret = proto->hp_server(addr, &ctx);
92 * ret == -1 - addr is not for this protocol
93 * ret > 0 - right protocol, but an error occured
99 /* Unrecognized address. */
105 /* An error occured. */
110 conn->pc_proto = proto;
112 conn->pc_side = side;
113 conn->pc_magic = PROTO_CONN_MAGIC;
119 proto_client(const char *addr, struct proto_conn **connp)
122 return (proto_common_setup(addr, connp, PROTO_SIDE_CLIENT));
126 proto_connect(struct proto_conn *conn)
130 assert(conn != NULL);
131 assert(conn->pc_magic == PROTO_CONN_MAGIC);
132 assert(conn->pc_side == PROTO_SIDE_CLIENT);
133 assert(conn->pc_proto != NULL);
135 ret = conn->pc_proto->hp_connect(conn->pc_ctx);
145 proto_server(const char *addr, struct proto_conn **connp)
148 return (proto_common_setup(addr, connp, PROTO_SIDE_SERVER_LISTEN));
152 proto_accept(struct proto_conn *conn, struct proto_conn **newconnp)
154 struct proto_conn *newconn;
157 assert(conn != NULL);
158 assert(conn->pc_magic == PROTO_CONN_MAGIC);
159 assert(conn->pc_side == PROTO_SIDE_SERVER_LISTEN);
160 assert(conn->pc_proto != NULL);
162 newconn = malloc(sizeof(*newconn));
166 ret = conn->pc_proto->hp_accept(conn->pc_ctx, &newconn->pc_ctx);
173 newconn->pc_proto = conn->pc_proto;
174 newconn->pc_side = PROTO_SIDE_SERVER_WORK;
175 newconn->pc_magic = PROTO_CONN_MAGIC;
182 proto_send(const struct proto_conn *conn, const void *data, size_t size)
186 assert(conn != NULL);
187 assert(conn->pc_magic == PROTO_CONN_MAGIC);
188 assert(conn->pc_proto != NULL);
190 ret = conn->pc_proto->hp_send(conn->pc_ctx, data, size);
199 proto_recv(const struct proto_conn *conn, void *data, size_t size)
203 assert(conn != NULL);
204 assert(conn->pc_magic == PROTO_CONN_MAGIC);
205 assert(conn->pc_proto != NULL);
207 ret = conn->pc_proto->hp_recv(conn->pc_ctx, data, size);
216 proto_descriptor(const struct proto_conn *conn)
219 assert(conn != NULL);
220 assert(conn->pc_magic == PROTO_CONN_MAGIC);
221 assert(conn->pc_proto != NULL);
223 return (conn->pc_proto->hp_descriptor(conn->pc_ctx));
227 proto_address_match(const struct proto_conn *conn, const char *addr)
230 assert(conn != NULL);
231 assert(conn->pc_magic == PROTO_CONN_MAGIC);
232 assert(conn->pc_proto != NULL);
234 return (conn->pc_proto->hp_address_match(conn->pc_ctx, addr));
238 proto_local_address(const struct proto_conn *conn, char *addr, size_t size)
241 assert(conn != NULL);
242 assert(conn->pc_magic == PROTO_CONN_MAGIC);
243 assert(conn->pc_proto != NULL);
245 conn->pc_proto->hp_local_address(conn->pc_ctx, addr, size);
249 proto_remote_address(const struct proto_conn *conn, char *addr, size_t size)
252 assert(conn != NULL);
253 assert(conn->pc_magic == PROTO_CONN_MAGIC);
254 assert(conn->pc_proto != NULL);
256 conn->pc_proto->hp_remote_address(conn->pc_ctx, addr, size);
260 proto_timeout(const struct proto_conn *conn, int timeout)
265 assert(conn != NULL);
266 assert(conn->pc_magic == PROTO_CONN_MAGIC);
267 assert(conn->pc_proto != NULL);
269 fd = proto_descriptor(conn);
275 if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0)
277 if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
284 proto_close(struct proto_conn *conn)
287 assert(conn != NULL);
288 assert(conn->pc_magic == PROTO_CONN_MAGIC);
289 assert(conn->pc_proto != NULL);
291 conn->pc_proto->hp_close(conn->pc_ctx);