]> git.proxmox.com Git - mirror_ovs.git/blame - lib/stream-tcp.c
stream: Make [p]stream_init() take ownership of 'name' parameter.
[mirror_ovs.git] / lib / stream-tcp.c
CommitLineData
c34b65c7 1/*
c2e3cbaf 2 * Copyright (c) 2008, 2009, 2010, 2012, 2013, 2014, 2015 Nicira, Inc.
c34b65c7
BP
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>
e731d71b 23#include <netdb.h>
c34b65c7
BP
24#include <stdlib.h>
25#include <string.h>
3762274e 26#include <sys/socket.h>
c34b65c7 27#include <unistd.h>
3e8a2ad1 28#include "openvswitch/dynamic-string.h"
c34b65c7
BP
29#include "packets.h"
30#include "socket-util.h"
31#include "util.h"
32#include "stream-provider.h"
33#include "stream-fd.h"
e6211adc 34#include "openvswitch/vlog.h"
5136ce49 35
d98e6007 36VLOG_DEFINE_THIS_MODULE(stream_tcp);
c34b65c7
BP
37
38/* Active TCP. */
39
b7636967 40/* Takes ownership of 'name'. */
c34b65c7 41static int
b7636967 42new_tcp_stream(char *name, int fd, int connect_status, struct stream **streamp)
c34b65c7 43{
b7cefbf7
GS
44 if (connect_status == 0) {
45 setsockopt_tcp_nodelay(fd);
c34b65c7
BP
46 }
47
b7cefbf7 48 return new_fd_stream(name, fd, connect_status, AF_INET, streamp);
c34b65c7
BP
49}
50
51static int
f125905c 52tcp_open(const char *name, char *suffix, struct stream **streamp, uint8_t dscp)
c34b65c7 53{
c34b65c7
BP
54 int fd, error;
55
a8d81967 56 error = inet_open_active(SOCK_STREAM, suffix, 0, NULL, &fd, dscp);
c34b65c7 57 if (fd >= 0) {
b7636967 58 return new_tcp_stream(xstrdup(name), fd, error, streamp);
c34b65c7 59 } else {
10a89ef0 60 VLOG_ERR("%s: connect: %s", name, ovs_strerror(error));
c34b65c7
BP
61 return error;
62 }
63}
64
da327b18 65const struct stream_class tcp_stream_class = {
c34b65c7 66 "tcp", /* name */
f1936eb6 67 true, /* needs_probes */
c34b65c7
BP
68 tcp_open, /* open */
69 NULL, /* close */
70 NULL, /* connect */
71 NULL, /* recv */
72 NULL, /* send */
539e96f6
BP
73 NULL, /* run */
74 NULL, /* run_wait */
c34b65c7
BP
75 NULL, /* wait */
76};
77\f
78/* Passive TCP. */
79
e731d71b
AS
80static int ptcp_accept(int fd, const struct sockaddr_storage *,
81 size_t, struct stream **streamp);
c34b65c7
BP
82
83static int
01420a2c
GS
84new_pstream(char *suffix, const char *name, struct pstream **pstreamp,
85 int dscp, char *unlink_path, bool kernel_print_port)
c34b65c7 86{
e731d71b
AS
87 char bound_name[SS_NTOP_BUFSIZE + 16];
88 char addrbuf[SS_NTOP_BUFSIZE];
89 struct sockaddr_storage ss;
798e1352 90 int error;
b52ecd96 91 uint16_t port;
c34b65c7 92 int fd;
01420a2c 93 char *conn_name = CONST_CAST(char *, name);
c34b65c7 94
b52ecd96
GS
95 fd = inet_open_passive(SOCK_STREAM, suffix, -1, &ss, dscp,
96 kernel_print_port);
c34b65c7
BP
97 if (fd < 0) {
98 return -fd;
c34b65c7 99 }
42967038 100
e731d71b 101 port = ss_get_port(&ss);
01420a2c
GS
102 if (!conn_name) {
103 snprintf(bound_name, sizeof bound_name, "ptcp:%"PRIu16":%s",
104 port, ss_format_address(&ss, addrbuf, sizeof addrbuf));
105 conn_name = bound_name;
106 }
e731d71b 107
b7636967
BP
108 error = new_fd_pstream(xstrdup(conn_name), fd,
109 ptcp_accept, unlink_path, pstreamp);
798e1352 110 if (!error) {
e731d71b 111 pstream_set_bound_port(*pstreamp, htons(port));
798e1352
BP
112 }
113 return error;
c34b65c7
BP
114}
115
b52ecd96
GS
116static int
117ptcp_open(const char *name OVS_UNUSED, char *suffix, struct pstream **pstreamp,
118 uint8_t dscp)
119{
01420a2c 120 return new_pstream(suffix, NULL, pstreamp, dscp, NULL, true);
b52ecd96
GS
121}
122
c34b65c7 123static int
e731d71b
AS
124ptcp_accept(int fd, const struct sockaddr_storage *ss,
125 size_t ss_len OVS_UNUSED, struct stream **streamp)
c34b65c7 126{
e731d71b
AS
127 char name[SS_NTOP_BUFSIZE + 16];
128 char addrbuf[SS_NTOP_BUFSIZE];
c34b65c7 129
e731d71b
AS
130 snprintf(name, sizeof name, "tcp:%s:%"PRIu16,
131 ss_format_address(ss, addrbuf, sizeof addrbuf),
132 ss_get_port(ss));
b7636967 133 return new_tcp_stream(xstrdup(name), fd, 0, streamp);
c34b65c7
BP
134}
135
da327b18 136const struct pstream_class ptcp_pstream_class = {
c34b65c7 137 "ptcp",
f1936eb6 138 true,
c34b65c7
BP
139 ptcp_open,
140 NULL,
141 NULL,
f89b7ce5 142 NULL,
c34b65c7 143};