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