1/*-
2 * Copyright (c) 2009-2010 The FreeBSD Foundation
3 * All rights reserved.
4 *
5 * This software was developed by Pawel Jakub Dawidek under sponsorship from
6 * the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without
--- 14 unchanged lines hidden (view full) ---
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
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/sbin/hastd/proto.c 218194 2011-02-02 15:53:09Z pjd $");
32
33#include <sys/types.h>
34#include <sys/queue.h>
35#include <sys/socket.h>
36
37#include <errno.h>
38#include <stdint.h>
39#include <string.h>
--- 60 unchanged lines hidden (view full) ---
100 conn->pc_side == PROTO_SIDE_SERVER_WORK);
101 PJDLOG_ASSERT(conn->pc_proto != NULL);
102
103 bzero(conn, sizeof(*conn));
104 free(conn);
105}
106
107static int
108proto_common_setup(const char *addr, struct proto_conn **connp, int side)
109{
110 struct hast_proto *proto;
111 struct proto_conn *conn;
112 void *ctx;
113 int ret;
114
115 PJDLOG_ASSERT(side == PROTO_SIDE_CLIENT ||
116 side == PROTO_SIDE_SERVER_LISTEN);
117
118 TAILQ_FOREACH(proto, &protos, hp_next) {
119 if (side == PROTO_SIDE_CLIENT) {
120 if (proto->hp_client == NULL)
121 ret = -1;
122 else
123 ret = proto->hp_client(addr, &ctx);
124 } else /* if (side == PROTO_SIDE_SERVER_LISTEN) */ {
125 if (proto->hp_server == NULL)
126 ret = -1;
127 else
128 ret = proto->hp_server(addr, &ctx);
129 }
130 /*
131 * ret == 0 - success
132 * ret == -1 - addr is not for this protocol
133 * ret > 0 - right protocol, but an error occured
134 */
135 if (ret >= 0)
136 break;
137 }
138 if (proto == NULL) {
139 /* Unrecognized address. */
140 errno = EINVAL;
--- 13 unchanged lines hidden (view full) ---
154 }
155 conn->pc_ctx = ctx;
156 *connp = conn;
157
158 return (0);
159}
160
161int
162proto_client(const char *addr, struct proto_conn **connp)
163{
164
165 return (proto_common_setup(addr, connp, PROTO_SIDE_CLIENT));
166}
167
168int
169proto_connect(struct proto_conn *conn, int timeout)
170{
171 int ret;
172
173 PJDLOG_ASSERT(conn != NULL);
--- 32 unchanged lines hidden (view full) ---
206
207 return (0);
208}
209
210int
211proto_server(const char *addr, struct proto_conn **connp)
212{
213
214 return (proto_common_setup(addr, connp, PROTO_SIDE_SERVER_LISTEN));
215}
216
217int
218proto_accept(struct proto_conn *conn, struct proto_conn **newconnp)
219{
220 struct proto_conn *newconn;
221 int ret;
222
--- 222 unchanged lines hidden ---
2 * Copyright (c) 2009-2010 The FreeBSD Foundation
3 * All rights reserved.
4 *
5 * This software was developed by Pawel Jakub Dawidek under sponsorship from
6 * the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without
--- 14 unchanged lines hidden (view full) ---
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
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/sbin/hastd/proto.c 218194 2011-02-02 15:53:09Z pjd $");
32
33#include <sys/types.h>
34#include <sys/queue.h>
35#include <sys/socket.h>
36
37#include <errno.h>
38#include <stdint.h>
39#include <string.h>
--- 60 unchanged lines hidden (view full) ---
100 conn->pc_side == PROTO_SIDE_SERVER_WORK);
101 PJDLOG_ASSERT(conn->pc_proto != NULL);
102
103 bzero(conn, sizeof(*conn));
104 free(conn);
105}
106
107static int
108proto_common_setup(const char *addr, struct proto_conn **connp, int side)
109{
110 struct hast_proto *proto;
111 struct proto_conn *conn;
112 void *ctx;
113 int ret;
114
115 PJDLOG_ASSERT(side == PROTO_SIDE_CLIENT ||
116 side == PROTO_SIDE_SERVER_LISTEN);
117
118 TAILQ_FOREACH(proto, &protos, hp_next) {
119 if (side == PROTO_SIDE_CLIENT) {
120 if (proto->hp_client == NULL)
121 ret = -1;
122 else
123 ret = proto->hp_client(addr, &ctx);
124 } else /* if (side == PROTO_SIDE_SERVER_LISTEN) */ {
125 if (proto->hp_server == NULL)
126 ret = -1;
127 else
128 ret = proto->hp_server(addr, &ctx);
129 }
130 /*
131 * ret == 0 - success
132 * ret == -1 - addr is not for this protocol
133 * ret > 0 - right protocol, but an error occured
134 */
135 if (ret >= 0)
136 break;
137 }
138 if (proto == NULL) {
139 /* Unrecognized address. */
140 errno = EINVAL;
--- 13 unchanged lines hidden (view full) ---
154 }
155 conn->pc_ctx = ctx;
156 *connp = conn;
157
158 return (0);
159}
160
161int
162proto_client(const char *addr, struct proto_conn **connp)
163{
164
165 return (proto_common_setup(addr, connp, PROTO_SIDE_CLIENT));
166}
167
168int
169proto_connect(struct proto_conn *conn, int timeout)
170{
171 int ret;
172
173 PJDLOG_ASSERT(conn != NULL);
--- 32 unchanged lines hidden (view full) ---
206
207 return (0);
208}
209
210int
211proto_server(const char *addr, struct proto_conn **connp)
212{
213
214 return (proto_common_setup(addr, connp, PROTO_SIDE_SERVER_LISTEN));
215}
216
217int
218proto_accept(struct proto_conn *conn, struct proto_conn **newconnp)
219{
220 struct proto_conn *newconn;
221 int ret;
222
--- 222 unchanged lines hidden ---