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>
44 #include "proto_impl.h"
46 #define PROTO_CONN_MAGIC 0x907041c
49 struct proto *pc_proto;
52 #define PROTO_SIDE_CLIENT 0
53 #define PROTO_SIDE_SERVER_LISTEN 1
54 #define PROTO_SIDE_SERVER_WORK 2
57 static TAILQ_HEAD(, proto) protos = TAILQ_HEAD_INITIALIZER(protos);
60 proto_register(struct proto *proto, bool isdefault)
62 static bool seen_default = false;
65 TAILQ_INSERT_HEAD(&protos, proto, prt_next);
67 PJDLOG_ASSERT(!seen_default);
69 TAILQ_INSERT_TAIL(&protos, proto, prt_next);
73 static struct proto_conn *
74 proto_alloc(struct proto *proto, int side)
76 struct proto_conn *conn;
78 PJDLOG_ASSERT(proto != NULL);
79 PJDLOG_ASSERT(side == PROTO_SIDE_CLIENT ||
80 side == PROTO_SIDE_SERVER_LISTEN ||
81 side == PROTO_SIDE_SERVER_WORK);
83 conn = malloc(sizeof(*conn));
85 conn->pc_proto = proto;
87 conn->pc_magic = PROTO_CONN_MAGIC;
93 proto_free(struct proto_conn *conn)
96 PJDLOG_ASSERT(conn != NULL);
97 PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
98 PJDLOG_ASSERT(conn->pc_side == PROTO_SIDE_CLIENT ||
99 conn->pc_side == PROTO_SIDE_SERVER_LISTEN ||
100 conn->pc_side == PROTO_SIDE_SERVER_WORK);
101 PJDLOG_ASSERT(conn->pc_proto != NULL);
103 bzero(conn, sizeof(*conn));
108 proto_common_setup(const char *srcaddr, const char *dstaddr,
109 struct proto_conn **connp, int side)
112 struct proto_conn *conn;
116 PJDLOG_ASSERT(side == PROTO_SIDE_CLIENT ||
117 side == PROTO_SIDE_SERVER_LISTEN);
119 TAILQ_FOREACH(proto, &protos, prt_next) {
120 if (side == PROTO_SIDE_CLIENT) {
121 if (proto->prt_client == NULL)
124 ret = proto->prt_client(srcaddr, dstaddr, &ctx);
125 } else /* if (side == PROTO_SIDE_SERVER_LISTEN) */ {
126 if (proto->prt_server == NULL)
129 ret = proto->prt_server(dstaddr, &ctx);
133 * ret == -1 - dstaddr is not for this protocol
134 * ret > 0 - right protocol, but an error occurred
140 /* Unrecognized address. */
145 /* An error occurred. */
149 conn = proto_alloc(proto, side);
151 if (proto->prt_close != NULL)
152 proto->prt_close(ctx);
163 proto_client(const char *srcaddr, const char *dstaddr,
164 struct proto_conn **connp)
167 return (proto_common_setup(srcaddr, dstaddr, connp, PROTO_SIDE_CLIENT));
171 proto_connect(struct proto_conn *conn, int timeout)
175 PJDLOG_ASSERT(conn != NULL);
176 PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
177 PJDLOG_ASSERT(conn->pc_side == PROTO_SIDE_CLIENT);
178 PJDLOG_ASSERT(conn->pc_proto != NULL);
179 PJDLOG_ASSERT(conn->pc_proto->prt_connect != NULL);
180 PJDLOG_ASSERT(timeout >= -1);
182 ret = conn->pc_proto->prt_connect(conn->pc_ctx, timeout);
192 proto_connect_wait(struct proto_conn *conn, int timeout)
196 PJDLOG_ASSERT(conn != NULL);
197 PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
198 PJDLOG_ASSERT(conn->pc_side == PROTO_SIDE_CLIENT);
199 PJDLOG_ASSERT(conn->pc_proto != NULL);
200 PJDLOG_ASSERT(conn->pc_proto->prt_connect_wait != NULL);
201 PJDLOG_ASSERT(timeout >= 0);
203 ret = conn->pc_proto->prt_connect_wait(conn->pc_ctx, timeout);
213 proto_server(const char *addr, struct proto_conn **connp)
216 return (proto_common_setup(NULL, addr, connp, PROTO_SIDE_SERVER_LISTEN));
220 proto_accept(struct proto_conn *conn, struct proto_conn **newconnp)
222 struct proto_conn *newconn;
225 PJDLOG_ASSERT(conn != NULL);
226 PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
227 PJDLOG_ASSERT(conn->pc_side == PROTO_SIDE_SERVER_LISTEN);
228 PJDLOG_ASSERT(conn->pc_proto != NULL);
229 PJDLOG_ASSERT(conn->pc_proto->prt_accept != NULL);
231 newconn = proto_alloc(conn->pc_proto, PROTO_SIDE_SERVER_WORK);
235 ret = conn->pc_proto->prt_accept(conn->pc_ctx, &newconn->pc_ctx);
248 proto_send(const struct proto_conn *conn, const void *data, size_t size)
252 PJDLOG_ASSERT(conn != NULL);
253 PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
254 PJDLOG_ASSERT(conn->pc_proto != NULL);
255 PJDLOG_ASSERT(conn->pc_proto->prt_send != NULL);
257 ret = conn->pc_proto->prt_send(conn->pc_ctx, data, size, -1);
266 proto_recv(const struct proto_conn *conn, void *data, size_t size)
270 PJDLOG_ASSERT(conn != NULL);
271 PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
272 PJDLOG_ASSERT(conn->pc_proto != NULL);
273 PJDLOG_ASSERT(conn->pc_proto->prt_recv != NULL);
275 ret = conn->pc_proto->prt_recv(conn->pc_ctx, data, size, NULL);
284 proto_connection_send(const struct proto_conn *conn, struct proto_conn *mconn)
286 const char *protoname;
289 PJDLOG_ASSERT(conn != NULL);
290 PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
291 PJDLOG_ASSERT(conn->pc_proto != NULL);
292 PJDLOG_ASSERT(conn->pc_proto->prt_send != NULL);
293 PJDLOG_ASSERT(mconn != NULL);
294 PJDLOG_ASSERT(mconn->pc_magic == PROTO_CONN_MAGIC);
295 PJDLOG_ASSERT(mconn->pc_proto != NULL);
296 fd = proto_descriptor(mconn);
297 PJDLOG_ASSERT(fd >= 0);
298 protoname = mconn->pc_proto->prt_name;
299 PJDLOG_ASSERT(protoname != NULL);
301 ret = conn->pc_proto->prt_send(conn->pc_ctx, protoname,
302 strlen(protoname) + 1, fd);
312 proto_connection_recv(const struct proto_conn *conn, bool client,
313 struct proto_conn **newconnp)
317 struct proto_conn *newconn;
320 PJDLOG_ASSERT(conn != NULL);
321 PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
322 PJDLOG_ASSERT(conn->pc_proto != NULL);
323 PJDLOG_ASSERT(conn->pc_proto->prt_recv != NULL);
324 PJDLOG_ASSERT(newconnp != NULL);
326 bzero(protoname, sizeof(protoname));
328 ret = conn->pc_proto->prt_recv(conn->pc_ctx, protoname,
329 sizeof(protoname) - 1, &fd);
335 PJDLOG_ASSERT(fd >= 0);
337 TAILQ_FOREACH(proto, &protos, prt_next) {
338 if (strcmp(proto->prt_name, protoname) == 0)
346 newconn = proto_alloc(proto,
347 client ? PROTO_SIDE_CLIENT : PROTO_SIDE_SERVER_WORK);
350 PJDLOG_ASSERT(newconn->pc_proto->prt_wrap != NULL);
351 ret = newconn->pc_proto->prt_wrap(fd, client, &newconn->pc_ctx);
364 proto_descriptor(const struct proto_conn *conn)
367 PJDLOG_ASSERT(conn != NULL);
368 PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
369 PJDLOG_ASSERT(conn->pc_proto != NULL);
370 PJDLOG_ASSERT(conn->pc_proto->prt_descriptor != NULL);
372 return (conn->pc_proto->prt_descriptor(conn->pc_ctx));
376 proto_address_match(const struct proto_conn *conn, const char *addr)
379 PJDLOG_ASSERT(conn != NULL);
380 PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
381 PJDLOG_ASSERT(conn->pc_proto != NULL);
382 PJDLOG_ASSERT(conn->pc_proto->prt_address_match != NULL);
384 return (conn->pc_proto->prt_address_match(conn->pc_ctx, addr));
388 proto_local_address(const struct proto_conn *conn, char *addr, size_t size)
391 PJDLOG_ASSERT(conn != NULL);
392 PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
393 PJDLOG_ASSERT(conn->pc_proto != NULL);
394 PJDLOG_ASSERT(conn->pc_proto->prt_local_address != NULL);
396 conn->pc_proto->prt_local_address(conn->pc_ctx, addr, size);
400 proto_remote_address(const struct proto_conn *conn, char *addr, size_t size)
403 PJDLOG_ASSERT(conn != NULL);
404 PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
405 PJDLOG_ASSERT(conn->pc_proto != NULL);
406 PJDLOG_ASSERT(conn->pc_proto->prt_remote_address != NULL);
408 conn->pc_proto->prt_remote_address(conn->pc_ctx, addr, size);
412 proto_timeout(const struct proto_conn *conn, int timeout)
417 PJDLOG_ASSERT(conn != NULL);
418 PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
419 PJDLOG_ASSERT(conn->pc_proto != NULL);
421 fd = proto_descriptor(conn);
427 if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1)
429 if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1)
436 proto_close(struct proto_conn *conn)
439 PJDLOG_ASSERT(conn != NULL);
440 PJDLOG_ASSERT(conn->pc_magic == PROTO_CONN_MAGIC);
441 PJDLOG_ASSERT(conn->pc_proto != NULL);
442 PJDLOG_ASSERT(conn->pc_proto->prt_close != NULL);
444 conn->pc_proto->prt_close(conn->pc_ctx);