1*10725SJohn.Forte@Sun.COM /*
2*10725SJohn.Forte@Sun.COM * CDDL HEADER START
3*10725SJohn.Forte@Sun.COM *
4*10725SJohn.Forte@Sun.COM * The contents of this file are subject to the terms of the
5*10725SJohn.Forte@Sun.COM * Common Development and Distribution License (the "License").
6*10725SJohn.Forte@Sun.COM * You may not use this file except in compliance with the License.
7*10725SJohn.Forte@Sun.COM *
8*10725SJohn.Forte@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*10725SJohn.Forte@Sun.COM * or http://www.opensolaris.org/os/licensing.
10*10725SJohn.Forte@Sun.COM * See the License for the specific language governing permissions
11*10725SJohn.Forte@Sun.COM * and limitations under the License.
12*10725SJohn.Forte@Sun.COM *
13*10725SJohn.Forte@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
14*10725SJohn.Forte@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*10725SJohn.Forte@Sun.COM * If applicable, add the following below this CDDL HEADER, with the
16*10725SJohn.Forte@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying
17*10725SJohn.Forte@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner]
18*10725SJohn.Forte@Sun.COM *
19*10725SJohn.Forte@Sun.COM * CDDL HEADER END
20*10725SJohn.Forte@Sun.COM */
21*10725SJohn.Forte@Sun.COM /*
22*10725SJohn.Forte@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23*10725SJohn.Forte@Sun.COM * Use is subject to license terms.
24*10725SJohn.Forte@Sun.COM */
25*10725SJohn.Forte@Sun.COM
26*10725SJohn.Forte@Sun.COM #include <stdio.h>
27*10725SJohn.Forte@Sun.COM #include <stdlib.h>
28*10725SJohn.Forte@Sun.COM #include <string.h>
29*10725SJohn.Forte@Sun.COM #include <strings.h>
30*10725SJohn.Forte@Sun.COM #include <sys/types.h>
31*10725SJohn.Forte@Sun.COM #include <errno.h>
32*10725SJohn.Forte@Sun.COM #include <syslog.h>
33*10725SJohn.Forte@Sun.COM #include <unistd.h>
34*10725SJohn.Forte@Sun.COM #include <sys/types.h>
35*10725SJohn.Forte@Sun.COM #include <sys/socket.h>
36*10725SJohn.Forte@Sun.COM #include <sys/time.h>
37*10725SJohn.Forte@Sun.COM #include <netinet/in.h>
38*10725SJohn.Forte@Sun.COM #include <arpa/inet.h>
39*10725SJohn.Forte@Sun.COM #include <netdb.h>
40*10725SJohn.Forte@Sun.COM #include <sys/stat.h>
41*10725SJohn.Forte@Sun.COM #include <sys/sdt.h>
42*10725SJohn.Forte@Sun.COM #include <signal.h>
43*10725SJohn.Forte@Sun.COM #include <fcntl.h>
44*10725SJohn.Forte@Sun.COM #include <libstmfproxy.h>
45*10725SJohn.Forte@Sun.COM
46*10725SJohn.Forte@Sun.COM /*
47*10725SJohn.Forte@Sun.COM * NOTE:
48*10725SJohn.Forte@Sun.COM * This is demo code to be used with the existing demo proxy daemon
49*10725SJohn.Forte@Sun.COM * svc-stmfproxy in /usr/demo/comstar.
50*10725SJohn.Forte@Sun.COM */
51*10725SJohn.Forte@Sun.COM
52*10725SJohn.Forte@Sun.COM struct _s_handle {
53*10725SJohn.Forte@Sun.COM int sockfd;
54*10725SJohn.Forte@Sun.COM };
55*10725SJohn.Forte@Sun.COM
56*10725SJohn.Forte@Sun.COM typedef struct _s_handle s_handle_t;
57*10725SJohn.Forte@Sun.COM
58*10725SJohn.Forte@Sun.COM static ssize_t
pt_socket_recv(void * handle,void * buf,size_t len)59*10725SJohn.Forte@Sun.COM pt_socket_recv(void *handle, void *buf, size_t len)
60*10725SJohn.Forte@Sun.COM {
61*10725SJohn.Forte@Sun.COM s_handle_t *sh = handle;
62*10725SJohn.Forte@Sun.COM
63*10725SJohn.Forte@Sun.COM return (recv(sh->sockfd, buf, len, MSG_WAITALL));
64*10725SJohn.Forte@Sun.COM }
65*10725SJohn.Forte@Sun.COM
66*10725SJohn.Forte@Sun.COM static ssize_t
pt_socket_send(void * handle,void * buf,size_t len)67*10725SJohn.Forte@Sun.COM pt_socket_send(void *handle, void *buf, size_t len)
68*10725SJohn.Forte@Sun.COM {
69*10725SJohn.Forte@Sun.COM s_handle_t *sh = handle;
70*10725SJohn.Forte@Sun.COM
71*10725SJohn.Forte@Sun.COM return (send(sh->sockfd, buf, len, 0));
72*10725SJohn.Forte@Sun.COM }
73*10725SJohn.Forte@Sun.COM
74*10725SJohn.Forte@Sun.COM static void *
pt_socket_connect(int server_node,char * server)75*10725SJohn.Forte@Sun.COM pt_socket_connect(int server_node, char *server)
76*10725SJohn.Forte@Sun.COM {
77*10725SJohn.Forte@Sun.COM int sfd, new_sfd;
78*10725SJohn.Forte@Sun.COM s_handle_t *sh = NULL;
79*10725SJohn.Forte@Sun.COM int on = 1;
80*10725SJohn.Forte@Sun.COM struct sockaddr_in cli_addr, serv_addr;
81*10725SJohn.Forte@Sun.COM struct sockaddr_in sin;
82*10725SJohn.Forte@Sun.COM int cliLen = sizeof (cli_addr);
83*10725SJohn.Forte@Sun.COM
84*10725SJohn.Forte@Sun.COM if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) <= 0) {
85*10725SJohn.Forte@Sun.COM syslog(LOG_DAEMON|LOG_WARNING,
86*10725SJohn.Forte@Sun.COM "socket() call failed: %d", errno);
87*10725SJohn.Forte@Sun.COM return (NULL);
88*10725SJohn.Forte@Sun.COM }
89*10725SJohn.Forte@Sun.COM
90*10725SJohn.Forte@Sun.COM if (server_node) {
91*10725SJohn.Forte@Sun.COM
92*10725SJohn.Forte@Sun.COM if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on,
93*10725SJohn.Forte@Sun.COM sizeof (on)) < 0) {
94*10725SJohn.Forte@Sun.COM syslog(LOG_DAEMON|LOG_WARNING,
95*10725SJohn.Forte@Sun.COM "setsockopt() failed: %d", errno);
96*10725SJohn.Forte@Sun.COM goto serv_out;
97*10725SJohn.Forte@Sun.COM }
98*10725SJohn.Forte@Sun.COM
99*10725SJohn.Forte@Sun.COM bzero(&serv_addr, sizeof (serv_addr));
100*10725SJohn.Forte@Sun.COM serv_addr.sin_family = AF_INET;
101*10725SJohn.Forte@Sun.COM serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
102*10725SJohn.Forte@Sun.COM /* XXX get from smf? */
103*10725SJohn.Forte@Sun.COM serv_addr.sin_port = htons(6543);
104*10725SJohn.Forte@Sun.COM
105*10725SJohn.Forte@Sun.COM if (bind(sfd, (struct sockaddr *)&serv_addr,
106*10725SJohn.Forte@Sun.COM sizeof (serv_addr)) < 0) {
107*10725SJohn.Forte@Sun.COM syslog(LOG_DAEMON|LOG_WARNING, "bind() call failed: %d",
108*10725SJohn.Forte@Sun.COM errno);
109*10725SJohn.Forte@Sun.COM goto serv_out;
110*10725SJohn.Forte@Sun.COM }
111*10725SJohn.Forte@Sun.COM
112*10725SJohn.Forte@Sun.COM (void) listen(sfd, 5);
113*10725SJohn.Forte@Sun.COM
114*10725SJohn.Forte@Sun.COM new_sfd = accept(sfd, (struct sockaddr *)&cli_addr, &cliLen);
115*10725SJohn.Forte@Sun.COM
116*10725SJohn.Forte@Sun.COM if (new_sfd < 0) {
117*10725SJohn.Forte@Sun.COM syslog(LOG_DAEMON|LOG_WARNING, "accept failed: %d",
118*10725SJohn.Forte@Sun.COM errno);
119*10725SJohn.Forte@Sun.COM goto serv_out;
120*10725SJohn.Forte@Sun.COM }
121*10725SJohn.Forte@Sun.COM sh = malloc(sizeof (*sh));
122*10725SJohn.Forte@Sun.COM sh->sockfd = new_sfd;
123*10725SJohn.Forte@Sun.COM serv_out:
124*10725SJohn.Forte@Sun.COM close(sfd);
125*10725SJohn.Forte@Sun.COM } else {
126*10725SJohn.Forte@Sun.COM struct hostent *hp;
127*10725SJohn.Forte@Sun.COM
128*10725SJohn.Forte@Sun.COM /*
129*10725SJohn.Forte@Sun.COM * Assume IP dot notation or if that fails, gethostbyname()
130*10725SJohn.Forte@Sun.COM * If that fails, return
131*10725SJohn.Forte@Sun.COM */
132*10725SJohn.Forte@Sun.COM if ((inet_aton(server, &sin.sin_addr)) == 0) {
133*10725SJohn.Forte@Sun.COM if ((hp = gethostbyname(server)) != NULL) {
134*10725SJohn.Forte@Sun.COM memcpy(&sin.sin_addr.s_addr, hp->h_addr,
135*10725SJohn.Forte@Sun.COM hp->h_length);
136*10725SJohn.Forte@Sun.COM } else {
137*10725SJohn.Forte@Sun.COM syslog(LOG_DAEMON|LOG_CRIT,
138*10725SJohn.Forte@Sun.COM "Cannot get IP address for %s", server);
139*10725SJohn.Forte@Sun.COM (void) close(sfd);
140*10725SJohn.Forte@Sun.COM return (NULL);
141*10725SJohn.Forte@Sun.COM }
142*10725SJohn.Forte@Sun.COM } else {
143*10725SJohn.Forte@Sun.COM fprintf(stderr,
144*10725SJohn.Forte@Sun.COM "Sorry, cannot use ip address format\n");
145*10725SJohn.Forte@Sun.COM (void) close(sfd);
146*10725SJohn.Forte@Sun.COM return (NULL);
147*10725SJohn.Forte@Sun.COM }
148*10725SJohn.Forte@Sun.COM sin.sin_family = AF_INET;
149*10725SJohn.Forte@Sun.COM /* XXX pass in from smf */
150*10725SJohn.Forte@Sun.COM sin.sin_port = htons(6543);
151*10725SJohn.Forte@Sun.COM
152*10725SJohn.Forte@Sun.COM while (connect(sfd, (struct sockaddr *)&sin,
153*10725SJohn.Forte@Sun.COM sizeof (sin)) < 0) {
154*10725SJohn.Forte@Sun.COM close(sfd);
155*10725SJohn.Forte@Sun.COM if (errno == ECONNREFUSED) {
156*10725SJohn.Forte@Sun.COM /* get a fresh socket and retry */
157*10725SJohn.Forte@Sun.COM sfd = socket(AF_INET, SOCK_STREAM, 0);
158*10725SJohn.Forte@Sun.COM if (sfd < 0) {
159*10725SJohn.Forte@Sun.COM syslog(LOG_DAEMON|LOG_WARNING,
160*10725SJohn.Forte@Sun.COM "socket() call failed: %d", errno);
161*10725SJohn.Forte@Sun.COM return (NULL);
162*10725SJohn.Forte@Sun.COM }
163*10725SJohn.Forte@Sun.COM sleep(2);
164*10725SJohn.Forte@Sun.COM } else {
165*10725SJohn.Forte@Sun.COM syslog(LOG_DAEMON|LOG_CRIT,
166*10725SJohn.Forte@Sun.COM "Cannot connect %s - %d", server, errno);
167*10725SJohn.Forte@Sun.COM return (NULL);
168*10725SJohn.Forte@Sun.COM }
169*10725SJohn.Forte@Sun.COM }
170*10725SJohn.Forte@Sun.COM sh = malloc(sizeof (*sh));
171*10725SJohn.Forte@Sun.COM sh->sockfd = sfd;
172*10725SJohn.Forte@Sun.COM }
173*10725SJohn.Forte@Sun.COM return (sh);
174*10725SJohn.Forte@Sun.COM }
175*10725SJohn.Forte@Sun.COM
176*10725SJohn.Forte@Sun.COM pt_ops_t pt_socket_ops = {
177*10725SJohn.Forte@Sun.COM pt_socket_connect,
178*10725SJohn.Forte@Sun.COM pt_socket_send,
179*10725SJohn.Forte@Sun.COM pt_socket_recv
180*10725SJohn.Forte@Sun.COM };
181*10725SJohn.Forte@Sun.COM
182*10725SJohn.Forte@Sun.COM int
stmf_proxy_transport_init(char * transport,pt_ops_t ** pt_ops)183*10725SJohn.Forte@Sun.COM stmf_proxy_transport_init(char *transport, pt_ops_t **pt_ops)
184*10725SJohn.Forte@Sun.COM {
185*10725SJohn.Forte@Sun.COM if (strcmp(transport, "sockets") == 0) {
186*10725SJohn.Forte@Sun.COM *pt_ops = &pt_socket_ops;
187*10725SJohn.Forte@Sun.COM return (0);
188*10725SJohn.Forte@Sun.COM } else {
189*10725SJohn.Forte@Sun.COM return (-1);
190*10725SJohn.Forte@Sun.COM }
191*10725SJohn.Forte@Sun.COM }
192