4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
30 #include <sys/types.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
44 #include <libstmfproxy.h>
48 * This is demo code to be used with the existing demo proxy daemon
49 * svc-stmfproxy in /usr/demo/comstar.
56 typedef struct _s_handle s_handle_t
;
59 pt_socket_recv(void *handle
, void *buf
, size_t len
)
61 s_handle_t
*sh
= handle
;
63 return (recv(sh
->sockfd
, buf
, len
, MSG_WAITALL
));
67 pt_socket_send(void *handle
, void *buf
, size_t len
)
69 s_handle_t
*sh
= handle
;
71 return (send(sh
->sockfd
, buf
, len
, 0));
75 pt_socket_connect(int server_node
, char *server
)
78 s_handle_t
*sh
= NULL
;
80 struct sockaddr_in cli_addr
, serv_addr
;
81 struct sockaddr_in sin
;
82 int cliLen
= sizeof (cli_addr
);
84 if ((sfd
= socket(AF_INET
, SOCK_STREAM
, 0)) <= 0) {
85 syslog(LOG_DAEMON
|LOG_WARNING
,
86 "socket() call failed: %d", errno
);
92 if (setsockopt(sfd
, SOL_SOCKET
, SO_REUSEADDR
, &on
,
94 syslog(LOG_DAEMON
|LOG_WARNING
,
95 "setsockopt() failed: %d", errno
);
99 bzero(&serv_addr
, sizeof (serv_addr
));
100 serv_addr
.sin_family
= AF_INET
;
101 serv_addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
102 /* XXX get from smf? */
103 serv_addr
.sin_port
= htons(6543);
105 if (bind(sfd
, (struct sockaddr
*)&serv_addr
,
106 sizeof (serv_addr
)) < 0) {
107 syslog(LOG_DAEMON
|LOG_WARNING
, "bind() call failed: %d",
112 (void) listen(sfd
, 5);
114 new_sfd
= accept(sfd
, (struct sockaddr
*)&cli_addr
, &cliLen
);
117 syslog(LOG_DAEMON
|LOG_WARNING
, "accept failed: %d",
121 sh
= malloc(sizeof (*sh
));
122 sh
->sockfd
= new_sfd
;
129 * Assume IP dot notation or if that fails, gethostbyname()
130 * If that fails, return
132 if ((inet_aton(server
, &sin
.sin_addr
)) == 0) {
133 if ((hp
= gethostbyname(server
)) != NULL
) {
134 memcpy(&sin
.sin_addr
.s_addr
, hp
->h_addr
,
137 syslog(LOG_DAEMON
|LOG_CRIT
,
138 "Cannot get IP address for %s", server
);
144 "Sorry, cannot use ip address format\n");
148 sin
.sin_family
= AF_INET
;
149 /* XXX pass in from smf */
150 sin
.sin_port
= htons(6543);
152 while (connect(sfd
, (struct sockaddr
*)&sin
,
155 if (errno
== ECONNREFUSED
) {
156 /* get a fresh socket and retry */
157 sfd
= socket(AF_INET
, SOCK_STREAM
, 0);
159 syslog(LOG_DAEMON
|LOG_WARNING
,
160 "socket() call failed: %d", errno
);
165 syslog(LOG_DAEMON
|LOG_CRIT
,
166 "Cannot connect %s - %d", server
, errno
);
170 sh
= malloc(sizeof (*sh
));
176 pt_ops_t pt_socket_ops
= {
183 stmf_proxy_transport_init(char *transport
, pt_ops_t
**pt_ops
)
185 if (strcmp(transport
, "sockets") == 0) {
186 *pt_ops
= &pt_socket_ops
;