socketpair.c revision 133105
10Sstevel@tonic-gate/*- 20Sstevel@tonic-gate * Copyright (c) 2004 Robert N. M. Watson 30Sstevel@tonic-gate * All rights reserved. 40Sstevel@tonic-gate * 50Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 60Sstevel@tonic-gate * modification, are permitted provided that the following conditions 70Sstevel@tonic-gate * are met: 80Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 90Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 100Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 110Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 120Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 150Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 160Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 170Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 180Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 190Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 200Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 210Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 220Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 230Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 240Sstevel@tonic-gate * SUCH DAMAGE. 250Sstevel@tonic-gate * 260Sstevel@tonic-gate * $FreeBSD: head/tools/regression/sockets/socketpair/socketpair.c 133105 2004-08-04 03:46:35Z rwatson $ 270Sstevel@tonic-gate */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate#include <sys/types.h> 300Sstevel@tonic-gate#include <sys/socket.h> 310Sstevel@tonic-gate 320Sstevel@tonic-gate#include <errno.h> 330Sstevel@tonic-gate#include <stdio.h> 340Sstevel@tonic-gate#include <stdlib.h> 350Sstevel@tonic-gate#include <string.h> 360Sstevel@tonic-gate#include <unistd.h> 370Sstevel@tonic-gate 380Sstevel@tonic-gate/* 390Sstevel@tonic-gate * Open, then close a set of UNIX domain socket pairs for datagram and 400Sstevel@tonic-gate * stream. 410Sstevel@tonic-gate * 420Sstevel@tonic-gate * Confirm that we can't open INET datagram or stream socket pairs. 430Sstevel@tonic-gate * 440Sstevel@tonic-gate * More tests should be added, including confirming that sending on either 450Sstevel@tonic-gate * endpoint results in data at the other, that the right kind of socket was 460Sstevel@tonic-gate * created (stream vs. datagram), and that message boundaries fall in the 470Sstevel@tonic-gate * right places. 480Sstevel@tonic-gate */ 490Sstevel@tonic-gateint 500Sstevel@tonic-gatemain(int argc, char *argv[]) 510Sstevel@tonic-gate{ 520Sstevel@tonic-gate int sv[2]; 530Sstevel@tonic-gate 540Sstevel@tonic-gate /* 550Sstevel@tonic-gate * UNIX domain socket pair, datagram. 560Sstevel@tonic-gate */ 570Sstevel@tonic-gate if (socketpair(PF_UNIX, SOCK_DGRAM, 0, sv) != 0) { 580Sstevel@tonic-gate fprintf(stderr, "socketpair(PF_UNIX, SOCK_DGRAM): %s\n", 590Sstevel@tonic-gate strerror(errno)); 600Sstevel@tonic-gate fprintf(stderr, "FAIL\n"); 610Sstevel@tonic-gate exit(-1); 620Sstevel@tonic-gate } 630Sstevel@tonic-gate if (close(sv[0]) != 0) { 640Sstevel@tonic-gate fprintf(stderr, "socketpair(PF_UNIX, SOCK_DGRAM) close 0: %s\n", 650Sstevel@tonic-gate strerror(errno)); 660Sstevel@tonic-gate fprintf(stderr, "FAIL\n"); 670Sstevel@tonic-gate exit(-1); 680Sstevel@tonic-gate } 690Sstevel@tonic-gate if (close(sv[1]) != 0) { 700Sstevel@tonic-gate fprintf(stderr, "socketpair(PF_UNIX, SOCK_DGRAM) close 1: %s\n", 710Sstevel@tonic-gate strerror(errno)); 720Sstevel@tonic-gate fprintf(stderr, "FAIL\n"); 730Sstevel@tonic-gate exit(-1); 740Sstevel@tonic-gate } 750Sstevel@tonic-gate 760Sstevel@tonic-gate /* 770Sstevel@tonic-gate * UNIX domain socket pair, stream. 780Sstevel@tonic-gate */ 790Sstevel@tonic-gate if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) != 0) { 800Sstevel@tonic-gate fprintf(stderr, "socketpair(PF_UNIX, SOCK_STREAM): %s\n", 810Sstevel@tonic-gate strerror(errno)); 820Sstevel@tonic-gate fprintf(stderr, "FAIL\n"); 830Sstevel@tonic-gate exit(-1); 840Sstevel@tonic-gate } 850Sstevel@tonic-gate if (close(sv[0]) != 0) { 860Sstevel@tonic-gate fprintf(stderr, "socketpair(PF_UNIX, SOCK_STREAM) close 0: %s\n", 870Sstevel@tonic-gate strerror(errno)); 880Sstevel@tonic-gate fprintf(stderr, "FAIL\n"); 890Sstevel@tonic-gate exit(-1); 900Sstevel@tonic-gate } 910Sstevel@tonic-gate if (close(sv[1]) != 0) { 920Sstevel@tonic-gate fprintf(stderr, "socketpair(PF_UNIX, SOCK_STREAM) close 1: " 930Sstevel@tonic-gate "%s\n", strerror(errno)); 940Sstevel@tonic-gate fprintf(stderr, "FAIL\n"); 950Sstevel@tonic-gate exit(-1); 960Sstevel@tonic-gate } 970Sstevel@tonic-gate 980Sstevel@tonic-gate /* 990Sstevel@tonic-gate * Confirm that PF_INET datagram socket pair creation fails. 1000Sstevel@tonic-gate */ 1010Sstevel@tonic-gate if (socketpair(PF_INET, SOCK_DGRAM, 0, sv) == 0) { 1020Sstevel@tonic-gate fprintf(stderr, "socketpair(PF_INET, SOCK_DGRAM): opened\n"); 1030Sstevel@tonic-gate fprintf(stderr, "FAIL\n"); 1040Sstevel@tonic-gate exit(-1); 1050Sstevel@tonic-gate } 1060Sstevel@tonic-gate if (errno != EOPNOTSUPP) { 1070Sstevel@tonic-gate fprintf(stderr, "socketpair(PF_INET, SOCK_DGRAM): %s\n", 1080Sstevel@tonic-gate strerror(errno)); 1090Sstevel@tonic-gate fprintf(stderr, "FAIL\n"); 1100Sstevel@tonic-gate } 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate /* 1130Sstevel@tonic-gate * Confirm that PF_INET stream socket pair creation fails. 1140Sstevel@tonic-gate */ 1150Sstevel@tonic-gate if (socketpair(PF_INET, SOCK_STREAM, 0, sv) == 0) { 1160Sstevel@tonic-gate fprintf(stderr, "socketpair(PF_INET, SOCK_STREAM): opened\n"); 1170Sstevel@tonic-gate fprintf(stderr, "FAIL\n"); 1180Sstevel@tonic-gate exit(-1); 1190Sstevel@tonic-gate } 1200Sstevel@tonic-gate if (errno != EOPNOTSUPP) { 1210Sstevel@tonic-gate fprintf(stderr, "socketpair(PF_INET, SOCK_STREAM): %s\n", 1220Sstevel@tonic-gate strerror(errno)); 1230Sstevel@tonic-gate fprintf(stderr, "FAIL\n"); 1240Sstevel@tonic-gate } 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate fprintf(stderr, "PASS\n"); 1270Sstevel@tonic-gate exit(0); 1280Sstevel@tonic-gate} 1290Sstevel@tonic-gate