2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * Shim library which should be LD_PRELOADed before running applications
2N/A * that interact with NCA but do not explicitly use the AF_NCA family.
2N/A * This library overloads AF_INET's version of bind(3SOCKET) with AF_NCA's
2N/A * version. The new version of bind checks to see if that the port is one
2N/A * NCA is listening on, closes the socket(3SOCKET), and opens a new one
2N/A * the family AF_NCA. Afterwards, the real bind(3SOCKET) is called
2N/A * descriptors, etc. *
2N/A *
2N/A * Compile: cc -Kpic -G -o ncad_addr.so ncad_addr.c -lsocket -lnsl
2N/A * Use: LD_PRELOAD=/path/to/ncad_addr.so my_program
2N/A */
2N/A
2N/A#include <stdio.h>
2N/A#include <assert.h>
2N/A#include <dlfcn.h>
2N/A#include <door.h>
2N/A#include <errno.h>
2N/A#include <fcntl.h>
2N/A#include <inet/nd.h>
2N/A#include <unistd.h>
2N/A#include <stropts.h>
2N/A#include <sys/stat.h>
2N/A#include <string.h>
2N/A#include <stdlib.h>
2N/A#include <sys/mman.h>
2N/A#include <netdb.h>
2N/A#include <ctype.h>
2N/A#include <sys/types.h>
2N/A#include <sys/socket.h>
2N/A#include <netinet/in.h>
2N/A#include <arpa/inet.h>
2N/A
2N/A#pragma weak bind = nca_bind
2N/A#pragma init(ncad_init)
2N/A#pragma fini(ncad_fini)
2N/A
2N/A#define SEPARATOR '/'
2N/A
2N/Atypedef int sfunc1_t(int, int, int);
2N/Atypedef int sfunc2_t(int, const struct sockaddr *, socklen_t);
2N/A
2N/Astatic sfunc1_t *real_socket;
2N/Astatic sfunc2_t *real_bind;
2N/A
2N/A/*
2N/A * It is used to represent an address NCA is willing to handle.
2N/A */
2N/Atypedef struct nca_address_s {
2N/A uint16_t port; /* port, in network byte order */
2N/A ipaddr_t ipaddr; /* IP address, in network byte order */
2N/A} nca_address_t;
2N/A
2N/Astatic uint32_t addrcount; /* current address count */
2N/Astatic uint32_t addrcapacity; /* capacity of ncaaddrs */
2N/Astatic nca_address_t *ncaaddrs; /* array for all addresses */
2N/A
2N/A/*
2N/A * It loads all NCA addresses from a configuration file. A NCA address
2N/A * entry is: ncaport=IPaddress:port. The line above can be repeatly for other
2N/A * addresses. If IPaddress is '*', then it is translated into INADDR_ANY.
2N/A */
2N/Astatic void
2N/Ancad_init(void)
2N/A{
2N/A uint16_t port;
2N/A ipaddr_t addr;
2N/A FILE *fp;
2N/A char *s, *p, *q;
2N/A char buffer[1024];
2N/A const char *filename = "/etc/nca/ncaport.conf";
2N/A
2N/A real_socket = (sfunc1_t *)dlsym(RTLD_NEXT, "socket");
2N/A real_bind = (sfunc2_t *)dlsym(RTLD_NEXT, "bind");
2N/A
2N/A if ((fp = fopen(filename, "rF")) == NULL) {
2N/A (void) fprintf(stderr, "Failed to open file %s for reading in "
2N/A " ncad_addr.so. Error = %s\n",
2N/A filename,
2N/A (p = strerror(errno)) ? p : "unknown error");
2N/A return;
2N/A }
2N/A
2N/A while (fgets(buffer, sizeof (buffer), fp) != NULL) {
2N/A s = buffer;
2N/A
2N/A /* remove '\n' at the end from fgets() */
2N/A p = strchr(s, '\n');
2N/A if (p != NULL)
2N/A *p = '\0';
2N/A
2N/A /* remove spaces from the front */
2N/A while (*s != '\0' && isspace(*s))
2N/A s++;
2N/A
2N/A if (*s == '\0' || *s == '#')
2N/A continue;
2N/A
2N/A /* it should start with ncaport= */
2N/A p = strchr(s, '=');
2N/A if (p == NULL || strncasecmp(s, "ncaport", 7) != 0)
2N/A continue;
2N/A
2N/A p++;
2N/A while (*p != '\0' && isspace(*p))
2N/A p++;
2N/A
2N/A q = strchr(p, SEPARATOR);
2N/A if (q == NULL)
2N/A continue;
2N/A *q++ = '\0';
2N/A if (strcmp(p, "*") == 0) {
2N/A addr = INADDR_ANY;
2N/A } else {
2N/A if (inet_pton(AF_INET, p, &addr) != 1) {
2N/A struct in6_addr addr6;
2N/A
2N/A if (inet_pton(AF_INET6, p, &addr6) == 1) {
2N/A (void) fprintf(stderr,
2N/A "NCA does not support IPv6\n");
2N/A } else {
2N/A (void) fprintf(stderr,
2N/A "Invalid IP address: %s\n", p);
2N/A }
2N/A continue;
2N/A }
2N/A }
2N/A port = atoi(q);
2N/A
2N/A /* array is full, expand it */
2N/A if (addrcount == addrcapacity) {
2N/A if (addrcapacity == 0)
2N/A addrcapacity = 64;
2N/A else
2N/A addrcapacity *= 2;
2N/A ncaaddrs = realloc(ncaaddrs,
2N/A addrcapacity * sizeof (nca_address_t));
2N/A if (ncaaddrs == NULL) {
2N/A (void) fprintf(stderr, "out of memory");
2N/A break;
2N/A }
2N/A }
2N/A
2N/A ncaaddrs[addrcount].ipaddr = addr;
2N/A ncaaddrs[addrcount].port = htons(port);
2N/A addrcount++;
2N/A }
2N/A
2N/A (void) fclose(fp);
2N/A}
2N/A
2N/A/*
2N/A * It destroys memory at the end of program.
2N/A */
2N/Astatic void
2N/Ancad_fini(void)
2N/A{
2N/A if (ncaaddrs != NULL) {
2N/A free(ncaaddrs);
2N/A ncaaddrs = NULL;
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * If the bind is happening on a port NCA is listening on, close
2N/A * the socket and open a new one with family AF_NCA.
2N/A */
2N/Astatic int
2N/Anca_bind(int sock, const struct sockaddr *name, socklen_t namelen)
2N/A{
2N/A struct sockaddr_in sin;
2N/A int new_sock;
2N/A int i;
2N/A
2N/A if (sock < 0) {
2N/A errno = EBADF;
2N/A return (-1);
2N/A }
2N/A
2N/A if (real_socket == NULL) {
2N/A if ((real_socket = (sfunc1_t *)dlsym(RTLD_NEXT, "socket"))
2N/A == NULL) {
2N/A errno = EAGAIN;
2N/A exit(-1);
2N/A }
2N/A }
2N/A
2N/A if (real_bind == NULL) {
2N/A if ((real_bind = (sfunc2_t *)dlsym(RTLD_NEXT, "bind"))
2N/A == NULL) {
2N/A errno = EAGAIN;
2N/A exit(-1);
2N/A }
2N/A }
2N/A
2N/A if (name == NULL ||
2N/A ncaaddrs == NULL ||
2N/A name->sa_family != AF_INET ||
2N/A namelen != sizeof (sin)) {
2N/A return (real_bind(sock, name, namelen));
2N/A }
2N/A
2N/A (void) memcpy(&sin, name, sizeof (sin));
2N/A
2N/A /*
2N/A * If it is one of the addresses NCA is handling, convert it
2N/A * to NCA socket.
2N/A */
2N/A for (i = 0; i < addrcount; i++) {
2N/A if (sin.sin_port == ncaaddrs[i].port &&
2N/A (sin.sin_addr.s_addr == ncaaddrs[i].ipaddr ||
2N/A ncaaddrs[i].ipaddr == INADDR_ANY)) {
2N/A /* convert to NCA socket */
2N/A new_sock = real_socket(AF_NCA, SOCK_STREAM, 0);
2N/A if (new_sock >= 0) {
2N/A (void) dup2(new_sock, sock);
2N/A (void) close(new_sock);
2N/A sin.sin_family = AF_NCA;
2N/A }
2N/A break;
2N/A }
2N/A }
2N/A
2N/A return (real_bind(sock, (struct sockaddr *)&sin, namelen));
2N/A}