450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte/*
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte * CDDL HEADER START
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte *
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte * The contents of this file are subject to the terms of the
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte * Common Development and Distribution License (the "License").
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte * You may not use this file except in compliance with the License.
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte *
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte * or http://www.opensolaris.org/os/licensing.
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte * See the License for the specific language governing permissions
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte * and limitations under the License.
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte *
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte * When distributing Covered Code, include this CDDL HEADER in each
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte * If applicable, add the following below this CDDL HEADER, with the
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte * fields enclosed by brackets "[]" replaced with your own identifying
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte * information: Portions Copyright [yyyy] [name of copyright owner]
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte *
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte * CDDL HEADER END
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte */
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte/*
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte * Use is subject to license terms.
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte */
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte#include <stdio.h>
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte#include <stdlib.h>
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte#include <string.h>
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte#include <strings.h>
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte#include <sys/types.h>
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte#include <errno.h>
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte#include <syslog.h>
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte#include <unistd.h>
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte#include <sys/types.h>
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte#include <sys/socket.h>
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte#include <sys/time.h>
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte#include <netinet/in.h>
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte#include <arpa/inet.h>
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte#include <netdb.h>
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte#include <sys/stat.h>
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte#include <sys/sdt.h>
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte#include <signal.h>
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte#include <fcntl.h>
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte#include <libstmfproxy.h>
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte/*
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte * NOTE:
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte * This is demo code to be used with the existing demo proxy daemon
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte * svc-stmfproxy in /usr/demo/comstar.
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte */
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte
450396635f70344c58b6b1e4db38cf17ff34445cJohn Fortestruct _s_handle {
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte int sockfd;
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte};
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte
450396635f70344c58b6b1e4db38cf17ff34445cJohn Fortetypedef struct _s_handle s_handle_t;
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte
450396635f70344c58b6b1e4db38cf17ff34445cJohn Fortestatic ssize_t
450396635f70344c58b6b1e4db38cf17ff34445cJohn Fortept_socket_recv(void *handle, void *buf, size_t len)
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte{
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte s_handle_t *sh = handle;
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte return (recv(sh->sockfd, buf, len, MSG_WAITALL));
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte}
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte
450396635f70344c58b6b1e4db38cf17ff34445cJohn Fortestatic ssize_t
450396635f70344c58b6b1e4db38cf17ff34445cJohn Fortept_socket_send(void *handle, void *buf, size_t len)
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte{
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte s_handle_t *sh = handle;
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte return (send(sh->sockfd, buf, len, 0));
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte}
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte
450396635f70344c58b6b1e4db38cf17ff34445cJohn Fortestatic void *
450396635f70344c58b6b1e4db38cf17ff34445cJohn Fortept_socket_connect(int server_node, char *server)
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte{
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte int sfd, new_sfd;
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte s_handle_t *sh = NULL;
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte int on = 1;
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte struct sockaddr_in cli_addr, serv_addr;
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte struct sockaddr_in sin;
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte int cliLen = sizeof (cli_addr);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) <= 0) {
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte syslog(LOG_DAEMON|LOG_WARNING,
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte "socket() call failed: %d", errno);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte return (NULL);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte }
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte if (server_node) {
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on,
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte sizeof (on)) < 0) {
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte syslog(LOG_DAEMON|LOG_WARNING,
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte "setsockopt() failed: %d", errno);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte goto serv_out;
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte }
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte bzero(&serv_addr, sizeof (serv_addr));
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte serv_addr.sin_family = AF_INET;
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte /* XXX get from smf? */
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte serv_addr.sin_port = htons(6543);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte if (bind(sfd, (struct sockaddr *)&serv_addr,
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte sizeof (serv_addr)) < 0) {
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte syslog(LOG_DAEMON|LOG_WARNING, "bind() call failed: %d",
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte errno);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte goto serv_out;
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte }
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte (void) listen(sfd, 5);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte new_sfd = accept(sfd, (struct sockaddr *)&cli_addr, &cliLen);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte if (new_sfd < 0) {
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte syslog(LOG_DAEMON|LOG_WARNING, "accept failed: %d",
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte errno);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte goto serv_out;
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte }
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte sh = malloc(sizeof (*sh));
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte sh->sockfd = new_sfd;
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forteserv_out:
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte close(sfd);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte } else {
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte struct hostent *hp;
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte /*
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte * Assume IP dot notation or if that fails, gethostbyname()
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte * If that fails, return
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte */
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte if ((inet_aton(server, &sin.sin_addr)) == 0) {
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte if ((hp = gethostbyname(server)) != NULL) {
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte memcpy(&sin.sin_addr.s_addr, hp->h_addr,
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte hp->h_length);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte } else {
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte syslog(LOG_DAEMON|LOG_CRIT,
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte "Cannot get IP address for %s", server);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte (void) close(sfd);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte return (NULL);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte }
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte } else {
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte fprintf(stderr,
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte "Sorry, cannot use ip address format\n");
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte (void) close(sfd);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte return (NULL);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte }
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte sin.sin_family = AF_INET;
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte /* XXX pass in from smf */
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte sin.sin_port = htons(6543);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte while (connect(sfd, (struct sockaddr *)&sin,
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte sizeof (sin)) < 0) {
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte close(sfd);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte if (errno == ECONNREFUSED) {
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte /* get a fresh socket and retry */
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte sfd = socket(AF_INET, SOCK_STREAM, 0);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte if (sfd < 0) {
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte syslog(LOG_DAEMON|LOG_WARNING,
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte "socket() call failed: %d", errno);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte return (NULL);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte }
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte sleep(2);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte } else {
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte syslog(LOG_DAEMON|LOG_CRIT,
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte "Cannot connect %s - %d", server, errno);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte return (NULL);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte }
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte }
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte sh = malloc(sizeof (*sh));
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte sh->sockfd = sfd;
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte }
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte return (sh);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte}
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte
450396635f70344c58b6b1e4db38cf17ff34445cJohn Fortept_ops_t pt_socket_ops = {
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte pt_socket_connect,
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte pt_socket_send,
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte pt_socket_recv
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte};
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forteint
450396635f70344c58b6b1e4db38cf17ff34445cJohn Fortestmf_proxy_transport_init(char *transport, pt_ops_t **pt_ops)
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte{
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte if (strcmp(transport, "sockets") == 0) {
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte *pt_ops = &pt_socket_ops;
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte return (0);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte } else {
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte return (-1);
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte }
450396635f70344c58b6b1e4db38cf17ff34445cJohn Forte}