/src/openvswitch/lib/stream-tcp.c
Line | Count | Source (jump to first uncovered line) |
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 "openvswitch/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 | | /* Takes ownership of 'name'. */ |
41 | | static int |
42 | | new_tcp_stream(char *name, int fd, int connect_status, struct stream **streamp) |
43 | 0 | { |
44 | 0 | if (connect_status == 0) { |
45 | 0 | setsockopt_tcp_nodelay(fd); |
46 | 0 | } |
47 | |
|
48 | 0 | return new_fd_stream(name, fd, connect_status, AF_INET, streamp); |
49 | 0 | } |
50 | | |
51 | | static int |
52 | | tcp_open(const char *name, char *suffix, struct stream **streamp, uint8_t dscp) |
53 | 0 | { |
54 | 0 | int fd, error; |
55 | |
|
56 | 0 | error = inet_open_active(SOCK_STREAM, suffix, -1, NULL, &fd, dscp); |
57 | 0 | if (fd >= 0) { |
58 | 0 | return new_tcp_stream(xstrdup(name), fd, error, streamp); |
59 | 0 | } else { |
60 | 0 | VLOG_ERR("%s: connect: %s", name, ovs_strerror(error)); |
61 | 0 | return error; |
62 | 0 | } |
63 | 0 | } |
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 | | /* Passive TCP. */ |
79 | | |
80 | | static int ptcp_accept(int fd, const struct sockaddr_storage *, |
81 | | size_t, struct stream **streamp); |
82 | | |
83 | | static int |
84 | | new_pstream(char *suffix, const char *name, struct pstream **pstreamp, |
85 | | int dscp, char *unlink_path, bool kernel_print_port) |
86 | 0 | { |
87 | 0 | struct sockaddr_storage ss; |
88 | 0 | int error; |
89 | 0 | int fd; |
90 | |
|
91 | 0 | fd = inet_open_passive(SOCK_STREAM, suffix, -1, &ss, dscp, |
92 | 0 | kernel_print_port); |
93 | 0 | if (fd < 0) { |
94 | 0 | return -fd; |
95 | 0 | } |
96 | | |
97 | 0 | struct ds bound_name = DS_EMPTY_INITIALIZER; |
98 | 0 | if (!name) { |
99 | 0 | ds_put_format(&bound_name, "ptcp:%"PRIu16":", ss_get_port(&ss)); |
100 | 0 | ss_format_address(&ss, &bound_name); |
101 | 0 | } else { |
102 | 0 | ds_put_cstr(&bound_name, name); |
103 | 0 | } |
104 | |
|
105 | 0 | error = new_fd_pstream(ds_steal_cstr(&bound_name), fd, |
106 | 0 | ptcp_accept, unlink_path, pstreamp); |
107 | 0 | if (!error) { |
108 | 0 | pstream_set_bound_port(*pstreamp, htons(ss_get_port(&ss))); |
109 | 0 | } |
110 | 0 | return error; |
111 | 0 | } |
112 | | |
113 | | static int |
114 | | ptcp_open(const char *name OVS_UNUSED, char *suffix, struct pstream **pstreamp, |
115 | | uint8_t dscp) |
116 | 0 | { |
117 | 0 | return new_pstream(suffix, NULL, pstreamp, dscp, NULL, true); |
118 | 0 | } |
119 | | |
120 | | static int |
121 | | ptcp_accept(int fd, const struct sockaddr_storage *ss, |
122 | | size_t ss_len OVS_UNUSED, struct stream **streamp) |
123 | 0 | { |
124 | 0 | struct ds name = DS_EMPTY_INITIALIZER; |
125 | 0 | ds_put_cstr(&name, "tcp:"); |
126 | 0 | ss_format_address(ss, &name); |
127 | 0 | ds_put_format(&name, ":%"PRIu16, ss_get_port(ss)); |
128 | |
|
129 | 0 | return new_tcp_stream(ds_steal_cstr(&name), fd, 0, streamp); |
130 | 0 | } |
131 | | |
132 | | const struct pstream_class ptcp_pstream_class = { |
133 | | "ptcp", |
134 | | true, |
135 | | ptcp_open, |
136 | | NULL, |
137 | | NULL, |
138 | | NULL, |
139 | | }; |