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 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 16 * 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 27 * SUCH DAMAGE. 28 */ 29 30#include <sys/cdefs.h>
| 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 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 16 * 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 27 * SUCH DAMAGE. 28 */ 29 30#include <sys/cdefs.h>
|
31__FBSDID("$FreeBSD: head/sbin/hastd/proto_socketpair.c 218194 2011-02-02 15:53:09Z pjd $");
| 31__FBSDID("$FreeBSD: head/sbin/hastd/proto_socketpair.c 219818 2011-03-21 08:54:59Z pjd $");
|
32 33#include <sys/types.h> 34#include <sys/socket.h> 35 36#include <errno.h> 37#include <stdbool.h> 38#include <stdint.h> 39#include <stdio.h> 40#include <string.h> 41#include <unistd.h> 42 43#include "hast.h" 44#include "pjdlog.h" 45#include "proto_impl.h" 46 47#define SP_CTX_MAGIC 0x50c3741 48struct sp_ctx { 49 int sp_magic; 50 int sp_fd[2]; 51 int sp_side; 52#define SP_SIDE_UNDEF 0 53#define SP_SIDE_CLIENT 1 54#define SP_SIDE_SERVER 2 55}; 56 57static void sp_close(void *ctx); 58 59static int
| 32 33#include <sys/types.h> 34#include <sys/socket.h> 35 36#include <errno.h> 37#include <stdbool.h> 38#include <stdint.h> 39#include <stdio.h> 40#include <string.h> 41#include <unistd.h> 42 43#include "hast.h" 44#include "pjdlog.h" 45#include "proto_impl.h" 46 47#define SP_CTX_MAGIC 0x50c3741 48struct sp_ctx { 49 int sp_magic; 50 int sp_fd[2]; 51 int sp_side; 52#define SP_SIDE_UNDEF 0 53#define SP_SIDE_CLIENT 1 54#define SP_SIDE_SERVER 2 55}; 56 57static void sp_close(void *ctx); 58 59static int
|
60sp_client(const char *addr, void **ctxp)
| 60sp_client(const char *srcaddr, const char *dstaddr, void **ctxp)
|
61{ 62 struct sp_ctx *spctx; 63 int ret; 64
| 61{ 62 struct sp_ctx *spctx; 63 int ret; 64
|
65 if (strcmp(addr, "socketpair://") != 0)
| 65 if (strcmp(dstaddr, "socketpair://") != 0)
|
66 return (-1); 67
| 66 return (-1); 67
|
| 68 PJDLOG_ASSERT(srcaddr == NULL); 69
|
68 spctx = malloc(sizeof(*spctx)); 69 if (spctx == NULL) 70 return (errno); 71 72 if (socketpair(PF_UNIX, SOCK_STREAM, 0, spctx->sp_fd) < 0) { 73 ret = errno; 74 free(spctx); 75 return (ret); 76 } 77 78 spctx->sp_side = SP_SIDE_UNDEF; 79 spctx->sp_magic = SP_CTX_MAGIC; 80 *ctxp = spctx; 81 82 return (0); 83} 84 85static int 86sp_send(void *ctx, const unsigned char *data, size_t size, int fd) 87{ 88 struct sp_ctx *spctx = ctx; 89 int sock; 90 91 PJDLOG_ASSERT(spctx != NULL); 92 PJDLOG_ASSERT(spctx->sp_magic == SP_CTX_MAGIC); 93 94 switch (spctx->sp_side) { 95 case SP_SIDE_UNDEF: 96 /* 97 * If the first operation done by the caller is proto_send(), 98 * we assume this is the client. 99 */ 100 /* FALLTHROUGH */ 101 spctx->sp_side = SP_SIDE_CLIENT; 102 /* Close other end. */ 103 close(spctx->sp_fd[1]); 104 spctx->sp_fd[1] = -1; 105 case SP_SIDE_CLIENT: 106 PJDLOG_ASSERT(spctx->sp_fd[0] >= 0); 107 sock = spctx->sp_fd[0]; 108 break; 109 case SP_SIDE_SERVER: 110 PJDLOG_ASSERT(spctx->sp_fd[1] >= 0); 111 sock = spctx->sp_fd[1]; 112 break; 113 default: 114 PJDLOG_ABORT("Invalid socket side (%d).", spctx->sp_side); 115 } 116 117 /* Someone is just trying to decide about side. */ 118 if (data == NULL) 119 return (0); 120 121 return (proto_common_send(sock, data, size, fd)); 122} 123 124static int 125sp_recv(void *ctx, unsigned char *data, size_t size, int *fdp) 126{ 127 struct sp_ctx *spctx = ctx; 128 int fd; 129 130 PJDLOG_ASSERT(spctx != NULL); 131 PJDLOG_ASSERT(spctx->sp_magic == SP_CTX_MAGIC); 132 133 switch (spctx->sp_side) { 134 case SP_SIDE_UNDEF: 135 /* 136 * If the first operation done by the caller is proto_recv(), 137 * we assume this is the server. 138 */ 139 /* FALLTHROUGH */ 140 spctx->sp_side = SP_SIDE_SERVER; 141 /* Close other end. */ 142 close(spctx->sp_fd[0]); 143 spctx->sp_fd[0] = -1; 144 case SP_SIDE_SERVER: 145 PJDLOG_ASSERT(spctx->sp_fd[1] >= 0); 146 fd = spctx->sp_fd[1]; 147 break; 148 case SP_SIDE_CLIENT: 149 PJDLOG_ASSERT(spctx->sp_fd[0] >= 0); 150 fd = spctx->sp_fd[0]; 151 break; 152 default: 153 PJDLOG_ABORT("Invalid socket side (%d).", spctx->sp_side); 154 } 155 156 /* Someone is just trying to decide about side. */ 157 if (data == NULL) 158 return (0); 159 160 return (proto_common_recv(fd, data, size, fdp)); 161} 162 163static int 164sp_descriptor(const void *ctx) 165{ 166 const struct sp_ctx *spctx = ctx; 167 168 PJDLOG_ASSERT(spctx != NULL); 169 PJDLOG_ASSERT(spctx->sp_magic == SP_CTX_MAGIC); 170 PJDLOG_ASSERT(spctx->sp_side == SP_SIDE_CLIENT || 171 spctx->sp_side == SP_SIDE_SERVER); 172 173 switch (spctx->sp_side) { 174 case SP_SIDE_CLIENT: 175 PJDLOG_ASSERT(spctx->sp_fd[0] >= 0); 176 return (spctx->sp_fd[0]); 177 case SP_SIDE_SERVER: 178 PJDLOG_ASSERT(spctx->sp_fd[1] >= 0); 179 return (spctx->sp_fd[1]); 180 } 181 182 PJDLOG_ABORT("Invalid socket side (%d).", spctx->sp_side); 183} 184 185static void 186sp_close(void *ctx) 187{ 188 struct sp_ctx *spctx = ctx; 189 190 PJDLOG_ASSERT(spctx != NULL); 191 PJDLOG_ASSERT(spctx->sp_magic == SP_CTX_MAGIC); 192 193 switch (spctx->sp_side) { 194 case SP_SIDE_UNDEF: 195 PJDLOG_ASSERT(spctx->sp_fd[0] >= 0); 196 close(spctx->sp_fd[0]); 197 spctx->sp_fd[0] = -1; 198 PJDLOG_ASSERT(spctx->sp_fd[1] >= 0); 199 close(spctx->sp_fd[1]); 200 spctx->sp_fd[1] = -1; 201 break; 202 case SP_SIDE_CLIENT: 203 PJDLOG_ASSERT(spctx->sp_fd[0] >= 0); 204 close(spctx->sp_fd[0]); 205 spctx->sp_fd[0] = -1; 206 PJDLOG_ASSERT(spctx->sp_fd[1] == -1); 207 break; 208 case SP_SIDE_SERVER: 209 PJDLOG_ASSERT(spctx->sp_fd[1] >= 0); 210 close(spctx->sp_fd[1]); 211 spctx->sp_fd[1] = -1; 212 PJDLOG_ASSERT(spctx->sp_fd[0] == -1); 213 break; 214 default: 215 PJDLOG_ABORT("Invalid socket side (%d).", spctx->sp_side); 216 } 217 218 spctx->sp_magic = 0; 219 free(spctx); 220} 221 222static struct hast_proto sp_proto = { 223 .hp_name = "socketpair", 224 .hp_client = sp_client, 225 .hp_send = sp_send, 226 .hp_recv = sp_recv, 227 .hp_descriptor = sp_descriptor, 228 .hp_close = sp_close 229}; 230 231static __constructor void 232sp_ctor(void) 233{ 234 235 proto_register(&sp_proto, false); 236}
| 70 spctx = malloc(sizeof(*spctx)); 71 if (spctx == NULL) 72 return (errno); 73 74 if (socketpair(PF_UNIX, SOCK_STREAM, 0, spctx->sp_fd) < 0) { 75 ret = errno; 76 free(spctx); 77 return (ret); 78 } 79 80 spctx->sp_side = SP_SIDE_UNDEF; 81 spctx->sp_magic = SP_CTX_MAGIC; 82 *ctxp = spctx; 83 84 return (0); 85} 86 87static int 88sp_send(void *ctx, const unsigned char *data, size_t size, int fd) 89{ 90 struct sp_ctx *spctx = ctx; 91 int sock; 92 93 PJDLOG_ASSERT(spctx != NULL); 94 PJDLOG_ASSERT(spctx->sp_magic == SP_CTX_MAGIC); 95 96 switch (spctx->sp_side) { 97 case SP_SIDE_UNDEF: 98 /* 99 * If the first operation done by the caller is proto_send(), 100 * we assume this is the client. 101 */ 102 /* FALLTHROUGH */ 103 spctx->sp_side = SP_SIDE_CLIENT; 104 /* Close other end. */ 105 close(spctx->sp_fd[1]); 106 spctx->sp_fd[1] = -1; 107 case SP_SIDE_CLIENT: 108 PJDLOG_ASSERT(spctx->sp_fd[0] >= 0); 109 sock = spctx->sp_fd[0]; 110 break; 111 case SP_SIDE_SERVER: 112 PJDLOG_ASSERT(spctx->sp_fd[1] >= 0); 113 sock = spctx->sp_fd[1]; 114 break; 115 default: 116 PJDLOG_ABORT("Invalid socket side (%d).", spctx->sp_side); 117 } 118 119 /* Someone is just trying to decide about side. */ 120 if (data == NULL) 121 return (0); 122 123 return (proto_common_send(sock, data, size, fd)); 124} 125 126static int 127sp_recv(void *ctx, unsigned char *data, size_t size, int *fdp) 128{ 129 struct sp_ctx *spctx = ctx; 130 int fd; 131 132 PJDLOG_ASSERT(spctx != NULL); 133 PJDLOG_ASSERT(spctx->sp_magic == SP_CTX_MAGIC); 134 135 switch (spctx->sp_side) { 136 case SP_SIDE_UNDEF: 137 /* 138 * If the first operation done by the caller is proto_recv(), 139 * we assume this is the server. 140 */ 141 /* FALLTHROUGH */ 142 spctx->sp_side = SP_SIDE_SERVER; 143 /* Close other end. */ 144 close(spctx->sp_fd[0]); 145 spctx->sp_fd[0] = -1; 146 case SP_SIDE_SERVER: 147 PJDLOG_ASSERT(spctx->sp_fd[1] >= 0); 148 fd = spctx->sp_fd[1]; 149 break; 150 case SP_SIDE_CLIENT: 151 PJDLOG_ASSERT(spctx->sp_fd[0] >= 0); 152 fd = spctx->sp_fd[0]; 153 break; 154 default: 155 PJDLOG_ABORT("Invalid socket side (%d).", spctx->sp_side); 156 } 157 158 /* Someone is just trying to decide about side. */ 159 if (data == NULL) 160 return (0); 161 162 return (proto_common_recv(fd, data, size, fdp)); 163} 164 165static int 166sp_descriptor(const void *ctx) 167{ 168 const struct sp_ctx *spctx = ctx; 169 170 PJDLOG_ASSERT(spctx != NULL); 171 PJDLOG_ASSERT(spctx->sp_magic == SP_CTX_MAGIC); 172 PJDLOG_ASSERT(spctx->sp_side == SP_SIDE_CLIENT || 173 spctx->sp_side == SP_SIDE_SERVER); 174 175 switch (spctx->sp_side) { 176 case SP_SIDE_CLIENT: 177 PJDLOG_ASSERT(spctx->sp_fd[0] >= 0); 178 return (spctx->sp_fd[0]); 179 case SP_SIDE_SERVER: 180 PJDLOG_ASSERT(spctx->sp_fd[1] >= 0); 181 return (spctx->sp_fd[1]); 182 } 183 184 PJDLOG_ABORT("Invalid socket side (%d).", spctx->sp_side); 185} 186 187static void 188sp_close(void *ctx) 189{ 190 struct sp_ctx *spctx = ctx; 191 192 PJDLOG_ASSERT(spctx != NULL); 193 PJDLOG_ASSERT(spctx->sp_magic == SP_CTX_MAGIC); 194 195 switch (spctx->sp_side) { 196 case SP_SIDE_UNDEF: 197 PJDLOG_ASSERT(spctx->sp_fd[0] >= 0); 198 close(spctx->sp_fd[0]); 199 spctx->sp_fd[0] = -1; 200 PJDLOG_ASSERT(spctx->sp_fd[1] >= 0); 201 close(spctx->sp_fd[1]); 202 spctx->sp_fd[1] = -1; 203 break; 204 case SP_SIDE_CLIENT: 205 PJDLOG_ASSERT(spctx->sp_fd[0] >= 0); 206 close(spctx->sp_fd[0]); 207 spctx->sp_fd[0] = -1; 208 PJDLOG_ASSERT(spctx->sp_fd[1] == -1); 209 break; 210 case SP_SIDE_SERVER: 211 PJDLOG_ASSERT(spctx->sp_fd[1] >= 0); 212 close(spctx->sp_fd[1]); 213 spctx->sp_fd[1] = -1; 214 PJDLOG_ASSERT(spctx->sp_fd[0] == -1); 215 break; 216 default: 217 PJDLOG_ABORT("Invalid socket side (%d).", spctx->sp_side); 218 } 219 220 spctx->sp_magic = 0; 221 free(spctx); 222} 223 224static struct hast_proto sp_proto = { 225 .hp_name = "socketpair", 226 .hp_client = sp_client, 227 .hp_send = sp_send, 228 .hp_recv = sp_recv, 229 .hp_descriptor = sp_descriptor, 230 .hp_close = sp_close 231}; 232 233static __constructor void 234sp_ctor(void) 235{ 236 237 proto_register(&sp_proto, false); 238}
|