b3237d6183c56fb56a390dd5a3b8b833b9c013db
[sliver-openvswitch.git] / lib / stream-tcp.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2012, 2013 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 <stdlib.h>
25 #include <string.h>
26 #include <sys/socket.h>
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"
33 #include "vlog.h"
34
35 VLOG_DEFINE_THIS_MODULE(stream_tcp);
36
37 /* Active TCP. */
38
39 static int
40 new_tcp_stream(const char *name, int fd, int connect_status,
41                struct stream **streamp)
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) {
56         VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s", name, ovs_strerror(errno));
57         close(fd);
58         return errno;
59     }
60
61     return new_fd_stream(name, fd, connect_status, streamp);
62 }
63
64 static int
65 tcp_open(const char *name, char *suffix, struct stream **streamp, uint8_t dscp)
66 {
67     int fd, error;
68
69     error = inet_open_active(SOCK_STREAM, suffix, 0, NULL, &fd, dscp);
70     if (fd >= 0) {
71         return new_tcp_stream(name, fd, error, streamp);
72     } else {
73         VLOG_ERR("%s: connect: %s", name, ovs_strerror(error));
74         return error;
75     }
76 }
77
78 const struct stream_class tcp_stream_class = {
79     "tcp",                      /* name */
80     true,                       /* needs_probes */
81     tcp_open,                   /* open */
82     NULL,                       /* close */
83     NULL,                       /* connect */
84     NULL,                       /* recv */
85     NULL,                       /* send */
86     NULL,                       /* run */
87     NULL,                       /* run_wait */
88     NULL,                       /* wait */
89 };
90 \f
91 /* Passive TCP. */
92
93 static int ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
94                        struct stream **streamp);
95
96 static int
97 ptcp_open(const char *name OVS_UNUSED, char *suffix, struct pstream **pstreamp,
98           uint8_t dscp)
99 {
100     struct sockaddr_in sin;
101     char bound_name[128];
102     int error;
103     int fd;
104
105     fd = inet_open_passive(SOCK_STREAM, suffix, -1, &sin, dscp);
106     if (fd < 0) {
107         return -fd;
108     }
109
110     sprintf(bound_name, "ptcp:%"PRIu16":"IP_FMT,
111             ntohs(sin.sin_port), IP_ARGS(sin.sin_addr.s_addr));
112     error = new_fd_pstream(bound_name, fd, ptcp_accept, set_dscp, NULL,
113                            pstreamp);
114     if (!error) {
115         pstream_set_bound_port(*pstreamp, sin.sin_port);
116     }
117     return error;
118 }
119
120 static int
121 ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
122             struct stream **streamp)
123 {
124     const struct sockaddr_in *sin = ALIGNED_CAST(const struct sockaddr_in *,
125                                                  sa);
126     char name[128];
127
128     if (sa_len == sizeof(struct sockaddr_in) && sin->sin_family == AF_INET) {
129         sprintf(name, "tcp:"IP_FMT, IP_ARGS(sin->sin_addr.s_addr));
130         sprintf(strchr(name, '\0'), ":%"PRIu16, ntohs(sin->sin_port));
131     } else {
132         strcpy(name, "tcp");
133     }
134     return new_tcp_stream(name, fd, 0, streamp);
135 }
136
137 const struct pstream_class ptcp_pstream_class = {
138     "ptcp",
139     true,
140     ptcp_open,
141     NULL,
142     NULL,
143     NULL,
144     NULL,
145 };
146