stream-tcp: Change the connection name for pwindows.
[cascardo/ovs.git] / lib / stream-tcp.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2012, 2013, 2014 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 <netinet/tcp.h>
24 #include <netdb.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/socket.h>
28 #include <unistd.h>
29 #include "dynamic-string.h"
30 #include "packets.h"
31 #include "socket-util.h"
32 #include "util.h"
33 #include "stream-provider.h"
34 #include "stream-fd.h"
35 #include "vlog.h"
36
37 VLOG_DEFINE_THIS_MODULE(stream_tcp);
38
39 /* Active TCP. */
40
41 static int
42 new_tcp_stream(const char *name, int fd, int connect_status,
43                struct stream **streamp)
44 {
45     struct sockaddr_storage local;
46     socklen_t local_len = sizeof local;
47     int on = 1;
48     int retval;
49
50     /* Get the local IP and port information */
51     retval = getsockname(fd, (struct sockaddr *) &local, &local_len);
52     if (retval) {
53         memset(&local, 0, sizeof local);
54     }
55
56     retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
57     if (retval) {
58         int error = sock_errno();
59         VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s",
60                  name, sock_strerror(error));
61         closesocket(fd);
62         return error;
63     }
64
65     return new_fd_stream(name, fd, connect_status, streamp);
66 }
67
68 static int
69 tcp_open(const char *name, char *suffix, struct stream **streamp, uint8_t dscp)
70 {
71     int fd, error;
72
73     error = inet_open_active(SOCK_STREAM, suffix, 0, NULL, &fd, dscp);
74     if (fd >= 0) {
75         return new_tcp_stream(name, fd, error, streamp);
76     } else {
77         VLOG_ERR("%s: connect: %s", name, ovs_strerror(error));
78         return error;
79     }
80 }
81
82 const struct stream_class tcp_stream_class = {
83     "tcp",                      /* name */
84     true,                       /* needs_probes */
85     tcp_open,                   /* open */
86     NULL,                       /* close */
87     NULL,                       /* connect */
88     NULL,                       /* recv */
89     NULL,                       /* send */
90     NULL,                       /* run */
91     NULL,                       /* run_wait */
92     NULL,                       /* wait */
93 };
94
95 #ifdef _WIN32
96 static int
97 windows_open(const char *name, char *suffix, struct stream **streamp,
98              uint8_t dscp)
99 {
100     int error, port;
101     FILE *file;
102     char *suffix_new, *path;
103
104     /* If the path does not contain a ':', assume it is relative to
105      * OVS_RUNDIR. */
106     if (!strchr(suffix, ':')) {
107         path = xasprintf("%s/%s", ovs_rundir(), suffix);
108     } else {
109         path = xstrdup(suffix);
110     }
111
112     file = fopen(path, "r");
113     if (!file) {
114         error = errno;
115         VLOG_DBG("%s: could not open %s (%s)", name, suffix,
116                  ovs_strerror(error));
117         return error;
118     }
119
120     error = fscanf(file, "%d", &port);
121     if (error != 1) {
122         VLOG_ERR("failed to read port from %s", suffix);
123         fclose(file);
124         return EINVAL;
125     }
126     fclose(file);
127
128     suffix_new = xasprintf("127.0.0.1:%d", port);
129
130     error = tcp_open(name, suffix_new, streamp, dscp);
131
132     free(suffix_new);
133     free(path);
134     return error;
135 }
136
137 const struct stream_class windows_stream_class = {
138     "unix",                     /* name */
139     false,                      /* needs_probes */
140     windows_open,                  /* open */
141     NULL,                       /* close */
142     NULL,                       /* connect */
143     NULL,                       /* recv */
144     NULL,                       /* send */
145     NULL,                       /* run */
146     NULL,                       /* run_wait */
147     NULL,                       /* wait */
148 };
149 #endif
150 \f
151 /* Passive TCP. */
152
153 static int ptcp_accept(int fd, const struct sockaddr_storage *,
154                        size_t, struct stream **streamp);
155
156 static int
157 new_pstream(char *suffix, const char *name, struct pstream **pstreamp,
158             int dscp, char *unlink_path, bool kernel_print_port)
159 {
160     char bound_name[SS_NTOP_BUFSIZE + 16];
161     char addrbuf[SS_NTOP_BUFSIZE];
162     struct sockaddr_storage ss;
163     int error;
164     uint16_t port;
165     int fd;
166     char *conn_name = CONST_CAST(char *, name);
167
168     fd = inet_open_passive(SOCK_STREAM, suffix, -1, &ss, dscp,
169                            kernel_print_port);
170     if (fd < 0) {
171         return -fd;
172     }
173
174     port = ss_get_port(&ss);
175     if (!conn_name) {
176         snprintf(bound_name, sizeof bound_name, "ptcp:%"PRIu16":%s",
177                  port, ss_format_address(&ss, addrbuf, sizeof addrbuf));
178         conn_name = bound_name;
179     }
180
181     error = new_fd_pstream(conn_name, fd, ptcp_accept, set_dscp, unlink_path,
182                            pstreamp);
183     if (!error) {
184         pstream_set_bound_port(*pstreamp, htons(port));
185     }
186     return error;
187 }
188
189 static int
190 ptcp_open(const char *name OVS_UNUSED, char *suffix, struct pstream **pstreamp,
191           uint8_t dscp)
192 {
193     return new_pstream(suffix, NULL, pstreamp, dscp, NULL, true);
194 }
195
196 static int
197 ptcp_accept(int fd, const struct sockaddr_storage *ss,
198             size_t ss_len OVS_UNUSED, struct stream **streamp)
199 {
200     char name[SS_NTOP_BUFSIZE + 16];
201     char addrbuf[SS_NTOP_BUFSIZE];
202
203     snprintf(name, sizeof name, "tcp:%s:%"PRIu16,
204              ss_format_address(ss, addrbuf, sizeof addrbuf),
205              ss_get_port(ss));
206     return new_tcp_stream(name, fd, 0, streamp);
207 }
208
209 const struct pstream_class ptcp_pstream_class = {
210     "ptcp",
211     true,
212     ptcp_open,
213     NULL,
214     NULL,
215     NULL,
216     NULL,
217 };
218
219 #ifdef _WIN32
220 static int
221 pwindows_open(const char *name, char *suffix, struct pstream **pstreamp,
222               uint8_t dscp)
223 {
224     int error;
225     char *suffix_new, *path;
226     FILE *file;
227     struct pstream *listener;
228
229     suffix_new = xstrdup("0:127.0.0.1");
230
231     /* If the path does not contain a ':', assume it is relative to
232      * OVS_RUNDIR. */
233     if (!strchr(suffix, ':')) {
234         path = xasprintf("%s/%s", ovs_rundir(), suffix);
235     } else {
236         path = xstrdup(suffix);
237     }
238
239     error = new_pstream(suffix_new, name, pstreamp, dscp, path, false);
240     if (error) {
241         goto exit;
242     }
243     listener = *pstreamp;
244
245     file = fopen(path, "w");
246     if (!file) {
247         error = errno;
248         VLOG_DBG("could not open %s (%s)", path, ovs_strerror(error));
249         goto exit;
250     }
251
252     fprintf(file, "%d\n", ntohs(listener->bound_port));
253     if (fflush(file) == EOF) {
254         error = EIO;
255         VLOG_ERR("write failed for %s", path);
256         fclose(file);
257         goto exit;
258     }
259     fclose(file);
260
261 exit:
262     free(suffix_new);
263     return error;
264 }
265
266 const struct pstream_class pwindows_pstream_class = {
267     "punix",
268     false,
269     pwindows_open,
270     NULL,
271     NULL,
272     NULL,
273     NULL,
274 };
275 #endif