proto_socketpair.c revision 219818
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 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 60sp_client(const char *srcaddr, const char *dstaddr, void **ctxp) 61{ 62 struct sp_ctx *spctx; 63 int ret; 64 65 if (strcmp(dstaddr, "socketpair://") != 0) 66 return (-1); 67 68 PJDLOG_ASSERT(srcaddr == NULL); 69 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} 239