1197781Srwatson/*- 2197781Srwatson * Copyright (c) 2009 Robert N. M. Watson 3197781Srwatson * All rights reserved. 4197781Srwatson * 5197781Srwatson * This software was developed at the University of Cambridge Computer 6197781Srwatson * Laboratory with support from a grant from Google, Inc. 7197781Srwatson * 8197781Srwatson * Redistribution and use in source and binary forms, with or without 9197781Srwatson * modification, are permitted provided that the following conditions 10197781Srwatson * are met: 11197781Srwatson * 1. Redistributions of source code must retain the above copyright 12197781Srwatson * notice, this list of conditions and the following disclaimer. 13197781Srwatson * 2. Redistributions in binary form must reproduce the above copyright 14197781Srwatson * notice, this list of conditions and the following disclaimer in the 15197781Srwatson * documentation and/or other materials provided with the distribution. 16197781Srwatson * 17197781Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18197781Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19197781Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20197781Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21197781Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22197781Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23197781Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24197781Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25197781Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26197781Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27197781Srwatson * SUCH DAMAGE. 28197781Srwatson */ 29197781Srwatson 30197781Srwatson#include <sys/cdefs.h> 31197781Srwatson__FBSDID("$FreeBSD$"); 32197781Srwatson 33197781Srwatson#include <sys/socket.h> 34197781Srwatson#include <sys/un.h> 35197781Srwatson 36197781Srwatson#include <err.h> 37197781Srwatson#include <errno.h> 38197781Srwatson#include <limits.h> 39197781Srwatson#include <stdio.h> 40197781Srwatson#include <string.h> 41197781Srwatson#include <unistd.h> 42197781Srwatson 43197781Srwatson#define FAILERR(str) err(-1, "%s: %s", __func__, str) 44197781Srwatson#define FAILERRX(str) errx(-1, "%s: %s", __func__, str) 45197781Srwatson 46197781Srwatsonstatic void 47197781Srwatsontest_socket(void) 48197781Srwatson{ 49197781Srwatson int s; 50197781Srwatson 51197781Srwatson s = socket(PF_LOCAL, SOCK_SEQPACKET, 0); 52197781Srwatson if (s < 0) 53197781Srwatson FAILERR("socket"); 54197781Srwatson (void)close(s); 55197781Srwatson} 56197781Srwatson 57197781Srwatsonstatic void 58197781Srwatsontest_socketpair(void) 59197781Srwatson{ 60197781Srwatson int sv[2]; 61197781Srwatson 62197781Srwatson if (socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sv) < 0) 63197781Srwatson FAILERR("socketpair"); 64197781Srwatson (void)close(sv[0]); 65197781Srwatson (void)close(sv[1]); 66197781Srwatson} 67197781Srwatson 68197781Srwatsonstatic void 69197781Srwatsontest_listen_unbound(void) 70197781Srwatson{ 71197781Srwatson int s; 72197781Srwatson 73197781Srwatson s = socket(PF_LOCAL, SOCK_SEQPACKET, 0); 74197781Srwatson if (s < 0) 75197781Srwatson FAILERR("socket"); 76197781Srwatson if (listen(s, -1) == 0) 77197781Srwatson FAILERRX("listen"); 78197781Srwatson (void)close(s); 79197781Srwatson} 80197781Srwatson 81197781Srwatsonstatic void 82197781Srwatsontest_bind(void) 83197781Srwatson{ 84197781Srwatson struct sockaddr_un sun; 85197781Srwatson char path[PATH_MAX]; 86197781Srwatson int s; 87197781Srwatson 88197781Srwatson snprintf(path, sizeof(path), "/tmp/lds.XXXXXXXXX"); 89197781Srwatson if (mktemp(path) == NULL) 90197781Srwatson FAILERR("mktemp"); 91197781Srwatson s = socket(PF_LOCAL, SOCK_SEQPACKET, 0); 92197781Srwatson if (s < 0) 93197781Srwatson FAILERR("socket"); 94197781Srwatson bzero(&sun, sizeof(sun)); 95197781Srwatson sun.sun_family = AF_LOCAL; 96197781Srwatson sun.sun_len = sizeof(sun); 97197781Srwatson strlcpy(sun.sun_path, path, sizeof(sun.sun_path)); 98197781Srwatson if (bind(s, (struct sockaddr *)&sun, sizeof(sun)) < 0) 99197781Srwatson FAILERR("bind"); 100197781Srwatson close(s); 101197781Srwatson (void)unlink(path); 102197781Srwatson} 103197781Srwatson 104197781Srwatsonstatic void 105197781Srwatsontest_listen_bound(void) 106197781Srwatson{ 107197781Srwatson struct sockaddr_un sun; 108197781Srwatson char path[PATH_MAX]; 109197781Srwatson int s; 110197781Srwatson 111197781Srwatson snprintf(path, sizeof(path), "/tmp/lds.XXXXXXXXX"); 112197781Srwatson if (mktemp(path) == NULL) 113197781Srwatson FAILERR("mktemp"); 114197781Srwatson s = socket(PF_LOCAL, SOCK_SEQPACKET, 0); 115197781Srwatson if (s < 0) 116197781Srwatson FAILERR("socket"); 117197781Srwatson bzero(&sun, sizeof(sun)); 118197781Srwatson sun.sun_family = AF_LOCAL; 119197781Srwatson sun.sun_len = sizeof(sun); 120197781Srwatson strlcpy(sun.sun_path, path, sizeof(sun.sun_path)); 121197781Srwatson if (bind(s, (struct sockaddr *)&sun, sizeof(sun)) < 0) 122197781Srwatson FAILERR("bind"); 123197781Srwatson if (listen(s, -1)) { 124197781Srwatson (void)unlink(path); 125197781Srwatson FAILERR("bind"); 126197781Srwatson } 127197781Srwatson close(s); 128197781Srwatson (void)unlink(path); 129197781Srwatson} 130197781Srwatson 131197781Srwatsonint 132197781Srwatsonmain(int argc, char *argv[]) 133197781Srwatson{ 134197781Srwatson 135197781Srwatson test_socket(); 136197781Srwatson test_socketpair(); 137197781Srwatson test_listen_unbound(); 138197781Srwatson test_bind(); 139197781Srwatson test_listen_bound(); 140197781Srwatson} 141