]> git.proxmox.com Git - mirror_ovs.git/blob - lib/stream-tcp.c
stream: Eliminate pstream_set_dscp().
[mirror_ovs.git] / lib / stream-tcp.c
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2012, 2013, 2014, 2015 Nicira, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18 #include "stream.h"
19 #include <errno.h>
20 #include <inttypes.h>
21 #include <sys/types.h>
22 #include <netinet/in.h>
23 #include <netdb.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/socket.h>
27 #include <unistd.h>
28 #include "dynamic-string.h"
29 #include "packets.h"
30 #include "socket-util.h"
31 #include "util.h"
32 #include "stream-provider.h"
33 #include "stream-fd.h"
34 #include "openvswitch/vlog.h"
35
36 VLOG_DEFINE_THIS_MODULE(stream_tcp);
37
38 /* Active TCP. */
39
40 static int
41 new_tcp_stream(const char *name, int fd, int connect_status,
42 struct stream **streamp)
43 {
44 if (connect_status == 0) {
45 setsockopt_tcp_nodelay(fd);
46 }
47
48 return new_fd_stream(name, fd, connect_status, AF_INET, streamp);
49 }
50
51 static int
52 tcp_open(const char *name, char *suffix, struct stream **streamp, uint8_t dscp)
53 {
54 int fd, error;
55
56 error = inet_open_active(SOCK_STREAM, suffix, 0, NULL, &fd, dscp);
57 if (fd >= 0) {
58 return new_tcp_stream(name, fd, error, streamp);
59 } else {
60 VLOG_ERR("%s: connect: %s", name, ovs_strerror(error));
61 return error;
62 }
63 }
64
65 const struct stream_class tcp_stream_class = {
66 "tcp", /* name */
67 true, /* needs_probes */
68 tcp_open, /* open */
69 NULL, /* close */
70 NULL, /* connect */
71 NULL, /* recv */
72 NULL, /* send */
73 NULL, /* run */
74 NULL, /* run_wait */
75 NULL, /* wait */
76 };
77
78 #ifdef _WIN32
79 static int
80 windows_open(const char *name, char *suffix, struct stream **streamp,
81 uint8_t dscp)
82 {
83 int error, port;
84 FILE *file;
85 char *suffix_new, *path;
86
87 /* If the path does not contain a ':', assume it is relative to
88 * OVS_RUNDIR. */
89 if (!strchr(suffix, ':')) {
90 path = xasprintf("%s/%s", ovs_rundir(), suffix);
91 } else {
92 path = xstrdup(suffix);
93 }
94
95 file = fopen(path, "r");
96 if (!file) {
97 error = errno;
98 VLOG_DBG("%s: could not open %s (%s)", name, suffix,
99 ovs_strerror(error));
100 return error;
101 }
102
103 error = fscanf(file, "%d", &port);
104 if (error != 1) {
105 VLOG_ERR("failed to read port from %s", suffix);
106 fclose(file);
107 return EINVAL;
108 }
109 fclose(file);
110
111 suffix_new = xasprintf("127.0.0.1:%d", port);
112
113 error = tcp_open(name, suffix_new, streamp, dscp);
114
115 free(suffix_new);
116 free(path);
117 return error;
118 }
119
120 const struct stream_class windows_stream_class = {
121 "unix", /* name */
122 false, /* needs_probes */
123 windows_open, /* open */
124 NULL, /* close */
125 NULL, /* connect */
126 NULL, /* recv */
127 NULL, /* send */
128 NULL, /* run */
129 NULL, /* run_wait */
130 NULL, /* wait */
131 };
132 #endif
133 \f
134 /* Passive TCP. */
135
136 static int ptcp_accept(int fd, const struct sockaddr_storage *,
137 size_t, struct stream **streamp);
138
139 static int
140 new_pstream(char *suffix, const char *name, struct pstream **pstreamp,
141 int dscp, char *unlink_path, bool kernel_print_port)
142 {
143 char bound_name[SS_NTOP_BUFSIZE + 16];
144 char addrbuf[SS_NTOP_BUFSIZE];
145 struct sockaddr_storage ss;
146 int error;
147 uint16_t port;
148 int fd;
149 char *conn_name = CONST_CAST(char *, name);
150
151 fd = inet_open_passive(SOCK_STREAM, suffix, -1, &ss, dscp,
152 kernel_print_port);
153 if (fd < 0) {
154 return -fd;
155 }
156
157 port = ss_get_port(&ss);
158 if (!conn_name) {
159 snprintf(bound_name, sizeof bound_name, "ptcp:%"PRIu16":%s",
160 port, ss_format_address(&ss, addrbuf, sizeof addrbuf));
161 conn_name = bound_name;
162 }
163
164 error = new_fd_pstream(conn_name, fd, ptcp_accept, unlink_path, pstreamp);
165 if (!error) {
166 pstream_set_bound_port(*pstreamp, htons(port));
167 }
168 return error;
169 }
170
171 static int
172 ptcp_open(const char *name OVS_UNUSED, char *suffix, struct pstream **pstreamp,
173 uint8_t dscp)
174 {
175 return new_pstream(suffix, NULL, pstreamp, dscp, NULL, true);
176 }
177
178 static int
179 ptcp_accept(int fd, const struct sockaddr_storage *ss,
180 size_t ss_len OVS_UNUSED, struct stream **streamp)
181 {
182 char name[SS_NTOP_BUFSIZE + 16];
183 char addrbuf[SS_NTOP_BUFSIZE];
184
185 snprintf(name, sizeof name, "tcp:%s:%"PRIu16,
186 ss_format_address(ss, addrbuf, sizeof addrbuf),
187 ss_get_port(ss));
188 return new_tcp_stream(name, fd, 0, streamp);
189 }
190
191 const struct pstream_class ptcp_pstream_class = {
192 "ptcp",
193 true,
194 ptcp_open,
195 NULL,
196 NULL,
197 NULL,
198 };
199
200 #ifdef _WIN32
201 static int
202 pwindows_open(const char *name, char *suffix, struct pstream **pstreamp,
203 uint8_t dscp)
204 {
205 int error;
206 char *suffix_new, *path;
207 FILE *file;
208 struct pstream *listener;
209
210 suffix_new = xstrdup("0:127.0.0.1");
211
212 /* If the path does not contain a ':', assume it is relative to
213 * OVS_RUNDIR. */
214 if (!strchr(suffix, ':')) {
215 path = xasprintf("%s/%s", ovs_rundir(), suffix);
216 } else {
217 path = xstrdup(suffix);
218 }
219
220 error = new_pstream(suffix_new, name, pstreamp, dscp, path, false);
221 if (error) {
222 goto exit;
223 }
224 listener = *pstreamp;
225
226 file = fopen(path, "w");
227 if (!file) {
228 error = errno;
229 VLOG_DBG("could not open %s (%s)", path, ovs_strerror(error));
230 goto exit;
231 }
232
233 fprintf(file, "%d\n", ntohs(listener->bound_port));
234 if (fflush(file) == EOF) {
235 error = EIO;
236 VLOG_ERR("write failed for %s", path);
237 fclose(file);
238 goto exit;
239 }
240 fclose(file);
241
242 exit:
243 free(suffix_new);
244 return error;
245 }
246
247 const struct pstream_class pwindows_pstream_class = {
248 "punix",
249 false,
250 pwindows_open,
251 NULL,
252 NULL,
253 NULL,
254 NULL,
255 };
256 #endif