Merge citrix branch into master.
[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 static int
40 new_tcp_vconn(const char *name, int fd, int connect_status,
41               const struct sockaddr_in *remote, struct vconn **vconnp)
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, strerror(errno));
57         close(fd);
58         return errno;
59     }
60
61     retval = new_stream_vconn(name, fd, connect_status, true, vconnp);
62     if (!retval) {
63         struct vconn *vconn = *vconnp;
64         vconn_set_remote_ip(vconn, remote->sin_addr.s_addr);
65         vconn_set_remote_port(vconn, remote->sin_port);
66         vconn_set_local_ip(vconn, local.sin_addr.s_addr);
67         vconn_set_local_port(vconn, local.sin_port);
68     }
69     return retval;
70 }
71
72 static int
73 tcp_open(const char *name, char *suffix, struct vconn **vconnp)
74 {
75     struct sockaddr_in sin;
76     int fd, error;
77
78     error = tcp_open_active(suffix, OFP_TCP_PORT, NULL, &fd);
79     if (fd >= 0) {
80         return new_tcp_vconn(name, fd, error, &sin, vconnp);
81     } else {
82         VLOG_ERR("%s: connect: %s", name, strerror(error));
83         return error;
84     }
85 }
86
87 struct vconn_class tcp_vconn_class = {
88     "tcp",                      /* name */
89     tcp_open,                   /* open */
90     NULL,                       /* close */
91     NULL,                       /* connect */
92     NULL,                       /* recv */
93     NULL,                       /* send */
94     NULL,                       /* wait */
95 };
96 \f
97 /* Passive TCP. */
98
99 static int ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
100                        struct vconn **vconnp);
101
102 static int
103 ptcp_open(const char *name UNUSED, char *suffix, struct pvconn **pvconnp)
104 {
105     int fd;
106
107     fd = tcp_open_passive(suffix, OFP_TCP_PORT);
108     if (fd < 0) {
109         return -fd;
110     } else {
111         return new_pstream_pvconn("ptcp", fd, ptcp_accept, pvconnp);
112     }
113 }
114
115 static int
116 ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
117             struct vconn **vconnp)
118 {
119     const struct sockaddr_in *sin = (const struct sockaddr_in *) sa;
120     char name[128];
121
122     if (sa_len == sizeof(struct sockaddr_in) && sin->sin_family == AF_INET) {
123         sprintf(name, "tcp:"IP_FMT, IP_ARGS(&sin->sin_addr));
124         if (sin->sin_port != htons(OFP_TCP_PORT)) {
125             sprintf(strchr(name, '\0'), ":%"PRIu16, ntohs(sin->sin_port));
126         }
127     } else {
128         strcpy(name, "tcp");
129     }
130     return new_tcp_vconn(name, fd, 0, sin, vconnp);
131 }
132
133 struct pvconn_class ptcp_pvconn_class = {
134     "ptcp",
135     ptcp_open,
136     NULL,
137     NULL,
138     NULL
139 };
140