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