2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <sys/types.h>
22 #include <netinet/in.h>
23 #include <netinet/tcp.h>
26 #include <sys/socket.h>
29 #include "socket-util.h"
31 #include "stream-provider.h"
32 #include "stream-fd.h"
35 VLOG_DEFINE_THIS_MODULE(stream_tcp
);
40 new_tcp_stream(const char *name
, int fd
, int connect_status
,
41 const struct sockaddr_in
*remote
, struct stream
**streamp
)
43 struct sockaddr_in local
;
44 socklen_t local_len
= sizeof local
;
48 /* Get the local IP and port information */
49 retval
= getsockname(fd
, (struct sockaddr
*)&local
, &local_len
);
51 memset(&local
, 0, sizeof local
);
54 retval
= setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, &on
, sizeof on
);
56 VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s", name
, strerror(errno
));
61 retval
= new_fd_stream(name
, fd
, connect_status
, NULL
, streamp
);
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
);
73 tcp_open(const char *name
, char *suffix
, struct stream
**streamp
)
75 struct sockaddr_in sin
;
78 error
= inet_open_active(SOCK_STREAM
, suffix
, 0, &sin
, &fd
);
80 return new_tcp_stream(name
, fd
, error
, &sin
, streamp
);
82 VLOG_ERR("%s: connect: %s", name
, strerror(error
));
87 const struct stream_class tcp_stream_class
= {
101 static int ptcp_accept(int fd
, const struct sockaddr
*sa
, size_t sa_len
,
102 struct stream
**streamp
);
105 ptcp_open(const char *name OVS_UNUSED
, char *suffix
, struct pstream
**pstreamp
)
107 struct sockaddr_in sin
;
108 char bound_name
[128];
111 fd
= inet_open_passive(SOCK_STREAM
, suffix
, -1, &sin
);
116 sprintf(bound_name
, "ptcp:%"PRIu16
":"IP_FMT
,
117 ntohs(sin
.sin_port
), IP_ARGS(&sin
.sin_addr
.s_addr
));
118 return new_fd_pstream(bound_name
, fd
, ptcp_accept
, NULL
, pstreamp
);
122 ptcp_accept(int fd
, const struct sockaddr
*sa
, size_t sa_len
,
123 struct stream
**streamp
)
125 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*) sa
;
128 if (sa_len
== sizeof(struct sockaddr_in
) && sin
->sin_family
== AF_INET
) {
129 sprintf(name
, "tcp:"IP_FMT
, IP_ARGS(&sin
->sin_addr
));
130 sprintf(strchr(name
, '\0'), ":%"PRIu16
, ntohs(sin
->sin_port
));
134 return new_tcp_stream(name
, fd
, 0, sin
, streamp
);
137 const struct pstream_class ptcp_pstream_class
= {