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/queue.h>
40 #include "proto_impl.h"
42 #define PROTO_CONN_MAGIC 0x907041c
45 struct hast_proto *pc_proto;
48 #define PROTO_SIDE_CLIENT 0
49 #define PROTO_SIDE_SERVER_LISTEN 1
50 #define PROTO_SIDE_SERVER_WORK 2
53 static LIST_HEAD(, hast_proto) protos = LIST_HEAD_INITIALIZER(protos);
56 proto_register(struct hast_proto *proto)
59 LIST_INSERT_HEAD(&protos, proto, hp_next);
63 proto_common_setup(const char *addr, struct proto_conn **connp, int side)
65 struct hast_proto *proto;
66 struct proto_conn *conn;
70 assert(side == PROTO_SIDE_CLIENT || side == PROTO_SIDE_SERVER_LISTEN);
72 conn = malloc(sizeof(*conn));
76 LIST_FOREACH(proto, &protos, hp_next) {
77 if (side == PROTO_SIDE_CLIENT)
78 ret = proto->hp_client(addr, &ctx);
79 else /* if (side == PROTO_SIDE_SERVER_LISTEN) */
80 ret = proto->hp_server(addr, &ctx);
83 * ret == -1 - addr is not for this protocol
84 * ret > 0 - right protocol, but an error occured
90 /* Unrecognized address. */
96 /* An error occured. */
101 conn->pc_proto = proto;
103 conn->pc_side = side;
104 conn->pc_magic = PROTO_CONN_MAGIC;
110 proto_client(const char *addr, struct proto_conn **connp)
113 return (proto_common_setup(addr, connp, PROTO_SIDE_CLIENT));
117 proto_connect(struct proto_conn *conn)
121 assert(conn != NULL);
122 assert(conn->pc_magic == PROTO_CONN_MAGIC);
123 assert(conn->pc_side == PROTO_SIDE_CLIENT);
124 assert(conn->pc_proto != NULL);
126 ret = conn->pc_proto->hp_connect(conn->pc_ctx);
136 proto_server(const char *addr, struct proto_conn **connp)
139 return (proto_common_setup(addr, connp, PROTO_SIDE_SERVER_LISTEN));
143 proto_accept(struct proto_conn *conn, struct proto_conn **newconnp)
145 struct proto_conn *newconn;
148 assert(conn != NULL);
149 assert(conn->pc_magic == PROTO_CONN_MAGIC);
150 assert(conn->pc_side == PROTO_SIDE_SERVER_LISTEN);
151 assert(conn->pc_proto != NULL);
153 newconn = malloc(sizeof(*newconn));
157 ret = conn->pc_proto->hp_accept(conn->pc_ctx, &newconn->pc_ctx);
164 newconn->pc_proto = conn->pc_proto;
165 newconn->pc_side = PROTO_SIDE_SERVER_WORK;
166 newconn->pc_magic = PROTO_CONN_MAGIC;
173 proto_send(struct proto_conn *conn, const void *data, size_t size)
177 assert(conn != NULL);
178 assert(conn->pc_magic == PROTO_CONN_MAGIC);
179 assert(conn->pc_proto != NULL);
181 ret = conn->pc_proto->hp_send(conn->pc_ctx, data, size);
190 proto_recv(struct proto_conn *conn, void *data, size_t size)
194 assert(conn != NULL);
195 assert(conn->pc_magic == PROTO_CONN_MAGIC);
196 assert(conn->pc_proto != NULL);
198 ret = conn->pc_proto->hp_recv(conn->pc_ctx, data, size);
207 proto_descriptor(const struct proto_conn *conn)
210 assert(conn != NULL);
211 assert(conn->pc_magic == PROTO_CONN_MAGIC);
212 assert(conn->pc_proto != NULL);
214 return (conn->pc_proto->hp_descriptor(conn->pc_ctx));
218 proto_address_match(const struct proto_conn *conn, const char *addr)
221 assert(conn != NULL);
222 assert(conn->pc_magic == PROTO_CONN_MAGIC);
223 assert(conn->pc_proto != NULL);
225 return (conn->pc_proto->hp_address_match(conn->pc_ctx, addr));
229 proto_local_address(const struct proto_conn *conn, char *addr, size_t size)
232 assert(conn != NULL);
233 assert(conn->pc_magic == PROTO_CONN_MAGIC);
234 assert(conn->pc_proto != NULL);
236 conn->pc_proto->hp_local_address(conn->pc_ctx, addr, size);
240 proto_remote_address(const struct proto_conn *conn, char *addr, size_t size)
243 assert(conn != NULL);
244 assert(conn->pc_magic == PROTO_CONN_MAGIC);
245 assert(conn->pc_proto != NULL);
247 conn->pc_proto->hp_remote_address(conn->pc_ctx, addr, size);
251 proto_close(struct proto_conn *conn)
254 assert(conn != NULL);
255 assert(conn->pc_magic == PROTO_CONN_MAGIC);
256 assert(conn->pc_proto != NULL);
258 conn->pc_proto->hp_close(conn->pc_ctx);