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 LIST_HEAD(, hast_proto) protos = LIST_HEAD_INITIALIZER(protos);
58 proto_register(struct hast_proto *proto)
61 LIST_INSERT_HEAD(&protos, proto, hp_next);
65 proto_common_setup(const char *addr, struct proto_conn **connp, int side)
67 struct hast_proto *proto;
68 struct proto_conn *conn;
72 assert(side == PROTO_SIDE_CLIENT || side == PROTO_SIDE_SERVER_LISTEN);
74 conn = malloc(sizeof(*conn));
78 LIST_FOREACH(proto, &protos, hp_next) {
79 if (side == PROTO_SIDE_CLIENT)
80 ret = proto->hp_client(addr, &ctx);
81 else /* if (side == PROTO_SIDE_SERVER_LISTEN) */
82 ret = proto->hp_server(addr, &ctx);
85 * ret == -1 - addr is not for this protocol
86 * ret > 0 - right protocol, but an error occured
92 /* Unrecognized address. */
98 /* An error occured. */
103 conn->pc_proto = proto;
105 conn->pc_side = side;
106 conn->pc_magic = PROTO_CONN_MAGIC;
112 proto_client(const char *addr, struct proto_conn **connp)
115 return (proto_common_setup(addr, connp, PROTO_SIDE_CLIENT));
119 proto_connect(struct proto_conn *conn)
123 assert(conn != NULL);
124 assert(conn->pc_magic == PROTO_CONN_MAGIC);
125 assert(conn->pc_side == PROTO_SIDE_CLIENT);
126 assert(conn->pc_proto != NULL);
128 ret = conn->pc_proto->hp_connect(conn->pc_ctx);
138 proto_server(const char *addr, struct proto_conn **connp)
141 return (proto_common_setup(addr, connp, PROTO_SIDE_SERVER_LISTEN));
145 proto_accept(struct proto_conn *conn, struct proto_conn **newconnp)
147 struct proto_conn *newconn;
150 assert(conn != NULL);
151 assert(conn->pc_magic == PROTO_CONN_MAGIC);
152 assert(conn->pc_side == PROTO_SIDE_SERVER_LISTEN);
153 assert(conn->pc_proto != NULL);
155 newconn = malloc(sizeof(*newconn));
159 ret = conn->pc_proto->hp_accept(conn->pc_ctx, &newconn->pc_ctx);
166 newconn->pc_proto = conn->pc_proto;
167 newconn->pc_side = PROTO_SIDE_SERVER_WORK;
168 newconn->pc_magic = PROTO_CONN_MAGIC;
175 proto_send(struct proto_conn *conn, const void *data, size_t size)
179 assert(conn != NULL);
180 assert(conn->pc_magic == PROTO_CONN_MAGIC);
181 assert(conn->pc_proto != NULL);
183 ret = conn->pc_proto->hp_send(conn->pc_ctx, data, size);
192 proto_recv(struct proto_conn *conn, void *data, size_t size)
196 assert(conn != NULL);
197 assert(conn->pc_magic == PROTO_CONN_MAGIC);
198 assert(conn->pc_proto != NULL);
200 ret = conn->pc_proto->hp_recv(conn->pc_ctx, data, size);
209 proto_descriptor(const struct proto_conn *conn)
212 assert(conn != NULL);
213 assert(conn->pc_magic == PROTO_CONN_MAGIC);
214 assert(conn->pc_proto != NULL);
216 return (conn->pc_proto->hp_descriptor(conn->pc_ctx));
220 proto_address_match(const struct proto_conn *conn, const char *addr)
223 assert(conn != NULL);
224 assert(conn->pc_magic == PROTO_CONN_MAGIC);
225 assert(conn->pc_proto != NULL);
227 return (conn->pc_proto->hp_address_match(conn->pc_ctx, addr));
231 proto_local_address(const struct proto_conn *conn, char *addr, size_t size)
234 assert(conn != NULL);
235 assert(conn->pc_magic == PROTO_CONN_MAGIC);
236 assert(conn->pc_proto != NULL);
238 conn->pc_proto->hp_local_address(conn->pc_ctx, addr, size);
242 proto_remote_address(const struct proto_conn *conn, char *addr, size_t size)
245 assert(conn != NULL);
246 assert(conn->pc_magic == PROTO_CONN_MAGIC);
247 assert(conn->pc_proto != NULL);
249 conn->pc_proto->hp_remote_address(conn->pc_ctx, addr, size);
253 proto_timeout(const struct proto_conn *conn, int timeout)
258 assert(conn != NULL);
259 assert(conn->pc_magic == PROTO_CONN_MAGIC);
260 assert(conn->pc_proto != NULL);
262 fd = proto_descriptor(conn);
268 if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0)
270 if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
277 proto_close(struct proto_conn *conn)
280 assert(conn != NULL);
281 assert(conn->pc_magic == PROTO_CONN_MAGIC);
282 assert(conn->pc_proto != NULL);
284 conn->pc_proto->hp_close(conn->pc_ctx);