socket-util: Allow binding without a port number in inet_open_passive().
[sliver-openvswitch.git] / lib / stream-tcp.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
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 <unistd.h>
27 #include "packets.h"
28 #include "socket-util.h"
29 #include "util.h"
30 #include "stream-provider.h"
31 #include "stream-fd.h"
32
33 #include "vlog.h"
34 #define THIS_MODULE VLM_stream_tcp
35
36 /* Active TCP. */
37
38 static int
39 new_tcp_stream(const char *name, int fd, int connect_status,
40               const struct sockaddr_in *remote, struct stream **streamp)
41 {
42     struct sockaddr_in local;
43     socklen_t local_len = sizeof local;
44     int on = 1;
45     int retval;
46
47     /* Get the local IP and port information */
48     retval = getsockname(fd, (struct sockaddr *)&local, &local_len);
49     if (retval) {
50         memset(&local, 0, sizeof local);
51     }
52
53     retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
54     if (retval) {
55         VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s", name, strerror(errno));
56         close(fd);
57         return errno;
58     }
59
60     retval = new_fd_stream(name, fd, connect_status, NULL, streamp);
61     if (!retval) {
62         struct stream *stream = *streamp;
63         stream_set_remote_ip(stream, remote->sin_addr.s_addr);
64         stream_set_remote_port(stream, remote->sin_port);
65         stream_set_local_ip(stream, local.sin_addr.s_addr);
66         stream_set_local_port(stream, local.sin_port);
67     }
68     return retval;
69 }
70
71 static int
72 tcp_open(const char *name, char *suffix, struct stream **streamp)
73 {
74     struct sockaddr_in sin;
75     int fd, error;
76
77     error = inet_open_active(SOCK_STREAM, suffix, 0, &sin, &fd);
78     if (fd >= 0) {
79         return new_tcp_stream(name, fd, error, &sin, streamp);
80     } else {
81         VLOG_ERR("%s: connect: %s", name, strerror(error));
82         return error;
83     }
84 }
85
86 struct stream_class tcp_stream_class = {
87     "tcp",                      /* name */
88     tcp_open,                   /* open */
89     NULL,                       /* close */
90     NULL,                       /* connect */
91     NULL,                       /* recv */
92     NULL,                       /* send */
93     NULL,                       /* run */
94     NULL,                       /* run_wait */
95     NULL,                       /* wait */
96 };
97 \f
98 /* Passive TCP. */
99
100 static int ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
101                        struct stream **streamp);
102
103 static int
104 ptcp_open(const char *name UNUSED, char *suffix, struct pstream **pstreamp)
105 {
106     int fd;
107
108     fd = inet_open_passive(SOCK_STREAM, suffix, -1);
109     if (fd < 0) {
110         return -fd;
111     } else {
112         return new_fd_pstream("ptcp", fd, ptcp_accept, NULL, pstreamp);
113     }
114 }
115
116 static int
117 ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
118             struct stream **streamp)
119 {
120     const struct sockaddr_in *sin = (const struct sockaddr_in *) sa;
121     char name[128];
122
123     if (sa_len == sizeof(struct sockaddr_in) && sin->sin_family == AF_INET) {
124         sprintf(name, "tcp:"IP_FMT, IP_ARGS(&sin->sin_addr));
125         sprintf(strchr(name, '\0'), ":%"PRIu16, ntohs(sin->sin_port));
126     } else {
127         strcpy(name, "tcp");
128     }
129     return new_tcp_stream(name, fd, 0, sin, streamp);
130 }
131
132 struct pstream_class ptcp_pstream_class = {
133     "ptcp",
134     ptcp_open,
135     NULL,
136     NULL,
137     NULL
138 };
139