]> git.proxmox.com Git - mirror_ovs.git/blame - lib/stream-tcp.c
netdev-linux, netdev-bsd: Make access to AF_INET socket thread-safe.
[mirror_ovs.git] / lib / stream-tcp.c
CommitLineData
c34b65c7 1/*
798e1352 2 * Copyright (c) 2008, 2009, 2010, 2012, 2013 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>
23#include <netinet/tcp.h>
24#include <stdlib.h>
25#include <string.h>
3762274e 26#include <sys/socket.h>
c34b65c7
BP
27#include <unistd.h>
28#include "packets.h"
29#include "socket-util.h"
30#include "util.h"
31#include "stream-provider.h"
32#include "stream-fd.h"
c34b65c7 33#include "vlog.h"
5136ce49 34
d98e6007 35VLOG_DEFINE_THIS_MODULE(stream_tcp);
c34b65c7
BP
36
37/* Active TCP. */
38
39static int
40new_tcp_stream(const char *name, int fd, int connect_status,
f125905c 41 const struct sockaddr_in *remote, struct stream **streamp)
c34b65c7
BP
42{
43 struct sockaddr_in local;
44 socklen_t local_len = sizeof local;
45 int on = 1;
46 int retval;
47
48 /* Get the local IP and port information */
49 retval = getsockname(fd, (struct sockaddr *)&local, &local_len);
50 if (retval) {
51 memset(&local, 0, sizeof local);
52 }
53
54 retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
55 if (retval) {
10a89ef0 56 VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s", name, ovs_strerror(errno));
c34b65c7
BP
57 close(fd);
58 return errno;
59 }
60
7921b912 61 retval = new_fd_stream(name, fd, connect_status, streamp);
c34b65c7
BP
62 if (!retval) {
63 struct stream *stream = *streamp;
64 stream_set_remote_ip(stream, remote->sin_addr.s_addr);
65 stream_set_remote_port(stream, remote->sin_port);
66 stream_set_local_ip(stream, local.sin_addr.s_addr);
67 stream_set_local_port(stream, local.sin_port);
68 }
69 return retval;
70}
71
72static int
f125905c 73tcp_open(const char *name, char *suffix, struct stream **streamp, uint8_t dscp)
c34b65c7
BP
74{
75 struct sockaddr_in sin;
76 int fd, error;
77
f125905c 78 error = inet_open_active(SOCK_STREAM, suffix, 0, &sin, &fd, dscp);
c34b65c7
BP
79 if (fd >= 0) {
80 return new_tcp_stream(name, fd, error, &sin, streamp);
81 } else {
10a89ef0 82 VLOG_ERR("%s: connect: %s", name, ovs_strerror(error));
c34b65c7
BP
83 return error;
84 }
85}
86
da327b18 87const struct stream_class tcp_stream_class = {
c34b65c7 88 "tcp", /* name */
f1936eb6 89 true, /* needs_probes */
c34b65c7
BP
90 tcp_open, /* open */
91 NULL, /* close */
92 NULL, /* connect */
93 NULL, /* recv */
94 NULL, /* send */
539e96f6
BP
95 NULL, /* run */
96 NULL, /* run_wait */
c34b65c7
BP
97 NULL, /* wait */
98};
99\f
100/* Passive TCP. */
101
102static int ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
103 struct stream **streamp);
104
105static int
f125905c
MM
106ptcp_open(const char *name OVS_UNUSED, char *suffix, struct pstream **pstreamp,
107 uint8_t dscp)
c34b65c7 108{
42967038
BP
109 struct sockaddr_in sin;
110 char bound_name[128];
798e1352 111 int error;
c34b65c7
BP
112 int fd;
113
f125905c 114 fd = inet_open_passive(SOCK_STREAM, suffix, -1, &sin, dscp);
c34b65c7
BP
115 if (fd < 0) {
116 return -fd;
c34b65c7 117 }
42967038
BP
118
119 sprintf(bound_name, "ptcp:%"PRIu16":"IP_FMT,
ed36537e 120 ntohs(sin.sin_port), IP_ARGS(sin.sin_addr.s_addr));
798e1352
BP
121 error = new_fd_pstream(bound_name, fd, ptcp_accept, set_dscp, NULL,
122 pstreamp);
123 if (!error) {
124 pstream_set_bound_port(*pstreamp, sin.sin_port);
125 }
126 return error;
c34b65c7
BP
127}
128
129static int
130ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
131 struct stream **streamp)
132{
db5a1019
AW
133 const struct sockaddr_in *sin = ALIGNED_CAST(const struct sockaddr_in *,
134 sa);
c34b65c7
BP
135 char name[128];
136
137 if (sa_len == sizeof(struct sockaddr_in) && sin->sin_family == AF_INET) {
ed36537e 138 sprintf(name, "tcp:"IP_FMT, IP_ARGS(sin->sin_addr.s_addr));
c34b65c7
BP
139 sprintf(strchr(name, '\0'), ":%"PRIu16, ntohs(sin->sin_port));
140 } else {
141 strcpy(name, "tcp");
142 }
143 return new_tcp_stream(name, fd, 0, sin, streamp);
144}
145
da327b18 146const struct pstream_class ptcp_pstream_class = {
c34b65c7 147 "ptcp",
f1936eb6 148 true,
c34b65c7
BP
149 ptcp_open,
150 NULL,
151 NULL,
f89b7ce5
IY
152 NULL,
153 NULL,
c34b65c7
BP
154};
155