Have rconn and vconn export information about IPs and ports
[sliver-openvswitch.git] / lib / vconn-tcp.c
1 /*
2  * Copyright (c) 2008, 2009 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 "vconn.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 "openflow/openflow.h"
31 #include "vconn-provider.h"
32 #include "vconn-stream.h"
33
34 #include "vlog.h"
35 #define THIS_MODULE VLM_vconn_tcp
36
37 /* Active TCP. */
38
39 void tcp_connect_success_cb(struct vconn *vconn, int fd);
40
41 static int
42 new_tcp_vconn(const char *name, int fd, int connect_status,
43               const struct sockaddr_in *sin, struct vconn **vconnp)
44 {
45     int on = 1;
46     int retval;
47
48     retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
49     if (retval) {
50         VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s", name, strerror(errno));
51         close(fd);
52         return errno;
53     }
54
55     return new_stream_vconn(name, fd, connect_status, 
56                             sin->sin_addr.s_addr, sin->sin_port, 
57                             true, tcp_connect_success_cb, vconnp);
58 }
59
60 static int
61 tcp_open(const char *name, char *suffix, struct vconn **vconnp)
62 {
63     char *save_ptr;
64     const char *host_name;
65     const char *port_string;
66     struct sockaddr_in sin;
67     int retval;
68     int fd;
69
70     host_name = strtok_r(suffix, ":", &save_ptr);
71     port_string = strtok_r(NULL, ":", &save_ptr);
72     if (!host_name) {
73         ovs_error(0, "%s: bad peer name format", name);
74         return EAFNOSUPPORT;
75     }
76
77     memset(&sin, 0, sizeof sin);
78     sin.sin_family = AF_INET;
79     if (lookup_ip(host_name, &sin.sin_addr)) {
80         return ENOENT;
81     }
82     sin.sin_port = htons(port_string ? atoi(port_string) : OFP_TCP_PORT);
83
84     fd = socket(AF_INET, SOCK_STREAM, 0);
85     if (fd < 0) {
86         VLOG_ERR("%s: socket: %s", name, strerror(errno));
87         return errno;
88     }
89
90     retval = set_nonblocking(fd);
91     if (retval) {
92         close(fd);
93         return retval;
94     }
95
96     retval = connect(fd, (struct sockaddr *) &sin, sizeof sin);
97     if (retval < 0) {
98         if (errno == EINPROGRESS) {
99             return new_tcp_vconn(name, fd, EAGAIN, &sin, vconnp);
100         } else {
101             int error = errno;
102             VLOG_ERR("%s: connect: %s", name, strerror(error));
103             close(fd);
104             return error;
105         }
106     } else {
107         return new_tcp_vconn(name, fd, 0, &sin, vconnp);
108     }
109 }
110
111 void
112 tcp_connect_success_cb(struct vconn *vconn, int fd)
113 {
114     int retval;
115     struct sockaddr_in local_addr;
116     socklen_t addrlen = sizeof(local_addr);
117
118     /* Get the local IP and port information */
119     retval = getsockname(fd, (struct sockaddr *)&local_addr, &addrlen);
120     if (retval) {
121         memset(&local_addr, 0, sizeof local_addr);
122     }
123     vconn_set_local_ip(vconn, local_addr.sin_addr.s_addr);
124     vconn_set_local_port(vconn, local_addr.sin_port);
125 }
126
127 struct vconn_class tcp_vconn_class = {
128     "tcp",                      /* name */
129     tcp_open,                   /* open */
130     NULL,                       /* close */
131     NULL,                       /* connect */
132     NULL,                       /* recv */
133     NULL,                       /* send */
134     NULL,                       /* wait */
135 };
136 \f
137 /* Passive TCP. */
138
139 static int ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
140                        struct vconn **vconnp);
141
142 static int
143 ptcp_open(const char *name, char *suffix, struct pvconn **pvconnp)
144 {
145     struct sockaddr_in sin;
146     int retval;
147     int fd;
148     unsigned int yes  = 1;
149
150     fd = socket(AF_INET, SOCK_STREAM, 0);
151     if (fd < 0) {
152         VLOG_ERR("%s: socket: %s", name, strerror(errno));
153         return errno;
154     }
155
156     if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) < 0) {
157         VLOG_ERR("%s: setsockopt(SO_REUSEADDR): %s", name, strerror(errno));
158         return errno;
159     }
160
161     memset(&sin, 0, sizeof sin);
162     sin.sin_family = AF_INET;
163     sin.sin_addr.s_addr = htonl(INADDR_ANY);
164     sin.sin_port = htons(atoi(suffix) ? atoi(suffix) : OFP_TCP_PORT);
165     retval = bind(fd, (struct sockaddr *) &sin, sizeof sin);
166     if (retval < 0) {
167         int error = errno;
168         VLOG_ERR("%s: bind: %s", name, strerror(error));
169         close(fd);
170         return error;
171     }
172
173     return new_pstream_pvconn("ptcp", fd, ptcp_accept, pvconnp);
174 }
175
176 static int
177 ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
178             struct vconn **vconnp)
179 {
180     const struct sockaddr_in *sin = (const struct sockaddr_in *) sa;
181     char name[128];
182
183     if (sa_len == sizeof(struct sockaddr_in) && sin->sin_family == AF_INET) {
184         sprintf(name, "tcp:"IP_FMT, IP_ARGS(&sin->sin_addr));
185         if (sin->sin_port != htons(OFP_TCP_PORT)) {
186             sprintf(strchr(name, '\0'), ":%"PRIu16, ntohs(sin->sin_port));
187         }
188     } else {
189         strcpy(name, "tcp");
190     }
191     return new_tcp_vconn(name, fd, 0, sin, vconnp);
192 }
193
194 struct pvconn_class ptcp_pvconn_class = {
195     "ptcp",
196     ptcp_open,
197     NULL,
198     NULL,
199     NULL
200 };
201