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/*
2N/A * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <strings.h>
2N/A#include <sys/types.h>
2N/A#include <errno.h>
2N/A#include <syslog.h>
2N/A#include <unistd.h>
2N/A#include <sys/types.h>
2N/A#include <sys/socket.h>
2N/A#include <sys/time.h>
2N/A#include <netinet/in.h>
2N/A#include <arpa/inet.h>
2N/A#include <netdb.h>
2N/A#include <sys/stat.h>
2N/A#include <sys/sdt.h>
2N/A#include <signal.h>
2N/A#include <fcntl.h>
2N/A#include <libstmfproxy.h>
2N/A
2N/A/*
2N/A * NOTE:
2N/A * This is demo code to be used with the existing demo proxy daemon
2N/A * svc-stmfproxy in /usr/demo/comstar.
2N/A */
2N/A
2N/Astruct _s_handle {
2N/A int sockfd;
2N/A};
2N/A
2N/Atypedef struct _s_handle s_handle_t;
2N/A
2N/Astatic ssize_t
2N/Apt_socket_recv(void *handle, void *buf, size_t len)
2N/A{
2N/A s_handle_t *sh = handle;
2N/A
2N/A return (recv(sh->sockfd, buf, len, MSG_WAITALL));
2N/A}
2N/A
2N/Astatic ssize_t
2N/Apt_socket_send(void *handle, void *buf, size_t len)
2N/A{
2N/A s_handle_t *sh = handle;
2N/A
2N/A return (send(sh->sockfd, buf, len, 0));
2N/A}
2N/A
2N/Astatic void *
2N/Apt_socket_connect(int server_node, char *server)
2N/A{
2N/A int sfd, new_sfd;
2N/A s_handle_t *sh = NULL;
2N/A int on = 1;
2N/A struct sockaddr_in cli_addr, serv_addr;
2N/A struct sockaddr_in sin;
2N/A socklen_t cliLen = sizeof (cli_addr);
2N/A
2N/A if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) <= 0) {
2N/A syslog(LOG_DAEMON|LOG_WARNING,
2N/A "socket() call failed: %d", errno);
2N/A return (NULL);
2N/A }
2N/A
2N/A if (server_node) {
2N/A
2N/A if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on,
2N/A sizeof (on)) < 0) {
2N/A syslog(LOG_DAEMON|LOG_WARNING,
2N/A "setsockopt() failed: %d", errno);
2N/A goto serv_out;
2N/A }
2N/A
2N/A bzero(&serv_addr, sizeof (serv_addr));
2N/A serv_addr.sin_family = AF_INET;
2N/A serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
2N/A /* XXX get from smf? */
2N/A serv_addr.sin_port = htons(6543);
2N/A
2N/A if (bind(sfd, (struct sockaddr *)&serv_addr,
2N/A sizeof (serv_addr)) < 0) {
2N/A syslog(LOG_DAEMON|LOG_WARNING, "bind() call failed: %d",
2N/A errno);
2N/A goto serv_out;
2N/A }
2N/A
2N/A (void) listen(sfd, 5);
2N/A
2N/A new_sfd = accept(sfd, (struct sockaddr *)&cli_addr, &cliLen);
2N/A
2N/A if (new_sfd < 0) {
2N/A syslog(LOG_DAEMON|LOG_WARNING, "accept failed: %d",
2N/A errno);
2N/A goto serv_out;
2N/A }
2N/A sh = malloc(sizeof (*sh));
2N/A sh->sockfd = new_sfd;
2N/Aserv_out:
2N/A close(sfd);
2N/A } else {
2N/A struct hostent *hp;
2N/A
2N/A /*
2N/A * Assume IP dot notation or if that fails, gethostbyname()
2N/A * If that fails, return
2N/A */
2N/A if ((inet_aton(server, &sin.sin_addr)) == 0) {
2N/A if ((hp = gethostbyname(server)) != NULL) {
2N/A memcpy(&sin.sin_addr.s_addr, hp->h_addr,
2N/A hp->h_length);
2N/A } else {
2N/A syslog(LOG_DAEMON|LOG_CRIT,
2N/A "Cannot get IP address for %s", server);
2N/A (void) close(sfd);
2N/A return (NULL);
2N/A }
2N/A } else {
2N/A fprintf(stderr,
2N/A "Sorry, cannot use ip address format\n");
2N/A (void) close(sfd);
2N/A return (NULL);
2N/A }
2N/A sin.sin_family = AF_INET;
2N/A /* XXX pass in from smf */
2N/A sin.sin_port = htons(6543);
2N/A
2N/A while (connect(sfd, (struct sockaddr *)&sin,
2N/A sizeof (sin)) < 0) {
2N/A close(sfd);
2N/A if (errno == ECONNREFUSED) {
2N/A /* get a fresh socket and retry */
2N/A sfd = socket(AF_INET, SOCK_STREAM, 0);
2N/A if (sfd < 0) {
2N/A syslog(LOG_DAEMON|LOG_WARNING,
2N/A "socket() call failed: %d", errno);
2N/A return (NULL);
2N/A }
2N/A sleep(2);
2N/A } else {
2N/A syslog(LOG_DAEMON|LOG_CRIT,
2N/A "Cannot connect %s - %d", server, errno);
2N/A return (NULL);
2N/A }
2N/A }
2N/A sh = malloc(sizeof (*sh));
2N/A sh->sockfd = sfd;
2N/A }
2N/A return (sh);
2N/A}
2N/A
2N/Apt_ops_t pt_socket_ops = {
2N/A pt_socket_connect,
2N/A pt_socket_send,
2N/A pt_socket_recv
2N/A};
2N/A
2N/Aint
2N/Astmf_proxy_transport_init(char *transport, pt_ops_t **pt_ops)
2N/A{
2N/A if (strcmp(transport, "sockets") == 0) {
2N/A *pt_ops = &pt_socket_ops;
2N/A return (0);
2N/A } else {
2N/A return (-1);
2N/A }
2N/A}