usr/src/cmd/rad/mod/rad_listen.c
author David Powell <david.e.powell@oracle.com>
Thu, 05 May 2011 13:16:34 -0700
changeset 698 3c730b38d1b0
parent 696 1ce0573e4536
permissions -rw-r--r--
18261 panel packages should depend on visual-panels-core 18262 listen_on_port doesn't fully initialize sockaddr structure
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
391
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
     1
/*
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
     2
 * CDDL HEADER START
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
     3
 *
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
     4
 * The contents of this file are subject to the terms of the
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
     5
 * Common Development and Distribution License (the "License").
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
     6
 * You may not use this file except in compliance with the License.
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
     7
 *
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
     8
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
     9
 * or http://www.opensolaris.org/os/licensing.
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    10
 * See the License for the specific language governing permissions
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    11
 * and limitations under the License.
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    12
 *
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    13
 * When distributing Covered Code, include this CDDL HEADER in each
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    14
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    15
 * If applicable, add the following below this CDDL HEADER, with the
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    16
 * fields enclosed by brackets "[]" replaced with your own identifying
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    17
 * information: Portions Copyright [yyyy] [name of copyright owner]
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    18
 *
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    19
 * CDDL HEADER END
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    20
 */
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    21
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    22
/*
698
3c730b38d1b0 18261 panel packages should depend on visual-panels-core
David Powell <david.e.powell@oracle.com>
parents: 696
diff changeset
    23
 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
391
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    24
 */
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    25
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    26
#include <sys/types.h>
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    27
#include <sys/socket.h>
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    28
#include <stdio.h>
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    29
#include <string.h>
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    30
#include <stdlib.h>
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    31
#include <errno.h>
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    32
#include <unistd.h>
696
1ce0573e4536 18149 - Add tests for connection assertions (connectLocalTCP6)
Dan Labrecque <dan.labrecque@oracle.com>
parents: 540
diff changeset
    33
#include <netinet/in.h>
391
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    34
540
2a0d274b1469 16571 move all content module interfaces into rad_modapi.h
David Powell <david.e.powell@oracle.com>
parents: 391
diff changeset
    35
#include "rad_modapi.h"
391
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    36
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    37
int
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    38
listen_on_port(int port, boolean_t local)
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    39
{
698
3c730b38d1b0 18261 panel packages should depend on visual-panels-core
David Powell <david.e.powell@oracle.com>
parents: 696
diff changeset
    40
	struct sockaddr_in6 sa = {
3c730b38d1b0 18261 panel packages should depend on visual-panels-core
David Powell <david.e.powell@oracle.com>
parents: 696
diff changeset
    41
	    .sin6_family = AF_INET6,
3c730b38d1b0 18261 panel packages should depend on visual-panels-core
David Powell <david.e.powell@oracle.com>
parents: 696
diff changeset
    42
	    .sin6_port = htons(port),
3c730b38d1b0 18261 panel packages should depend on visual-panels-core
David Powell <david.e.powell@oracle.com>
parents: 696
diff changeset
    43
	    .sin6_addr = (local ? in6addr_loopback : in6addr_any)
3c730b38d1b0 18261 panel packages should depend on visual-panels-core
David Powell <david.e.powell@oracle.com>
parents: 696
diff changeset
    44
	};
391
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    45
	int fd, option;
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    46
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    47
	rad_log(RL_DEBUG, "attempting to listen on port %d (%s)", port,
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    48
	    local ? "loopback only" : "all addresses");
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    49
696
1ce0573e4536 18149 - Add tests for connection assertions (connectLocalTCP6)
Dan Labrecque <dan.labrecque@oracle.com>
parents: 540
diff changeset
    50
	if ((fd = socket(AF_INET6, SOCK_STREAM, 0)) == -1) {
391
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    51
		rad_log(RL_ERROR, "failed to create socket: %s",
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    52
		    strerror(errno));
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    53
		return (-1);
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    54
	}
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    55
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    56
	option = 1;
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    57
	if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &option,
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    58
	    sizeof (option)) == -1) {
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    59
		rad_log(RL_ERROR, "failed to set socket options: %s",
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    60
		    strerror(errno));
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    61
		return (-1);
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    62
	}
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    63
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    64
	if (bind(fd, (struct sockaddr *)&sa, sizeof (sa)) == -1) {
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    65
		rad_log(RL_ERROR, "failed to bind to address: %s",
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    66
		    strerror(errno));
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    67
		return (-1);
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    68
	}
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    69
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    70
	if (listen(fd, 15) == -1) {
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    71
		rad_log(RL_ERROR, "failed to listen on socket: %s",
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    72
		    strerror(errno));
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    73
		return (-1);
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    74
	}
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    75
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    76
	rad_log(RL_DEBUG, "listening on port %d (%s)", port,
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    77
	    local ? "loopback only" : "all addresses");
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    78
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    79
	return (fd);
71abce159a62 13357 rad
David Powell <David.Powell@sun.com>
parents:
diff changeset
    80
}