66296d5a42f4fc0bda12d30dd944a551fb57d752
[senf.git] / Socket / Protocols / INet / TCPSocketHandle.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS) 
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY 
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief TCPv4SocketHandle and TCPv6SocketHandle non-inline non-template implementation
25  */
26
27 #include "TCPSocketHandle.hh"
28 //#include "TCPSocketHandle.ih"
29
30 // Custom includes
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <sys/ioctl.h>
34
35 #include "../../../Utils/Exception.hh"
36
37 //#include "TCPSocketHandle.mpp"
38 #define prefix_
39 ///////////////////////////////cc.p////////////////////////////////////////
40
41 ///////////////////////////////////////////////////////////////////////////
42 // senf::TCPv4SocketProtocol
43
44 prefix_ void senf::TCPv4SocketProtocol::init_client()
45     const
46 {
47     int sock = ::socket(PF_INET,SOCK_STREAM,0);
48     if (sock < 0)
49         throwErrno();
50     fd(sock);
51 }
52
53 prefix_ void
54 senf::TCPv4SocketProtocol::init_client(INet4SocketAddress const & address)
55     const
56 {
57     init_client();
58     connect(address);
59 }
60
61 prefix_ void senf::TCPv4SocketProtocol::init_server()
62     const
63 {
64     int sock = ::socket(PF_INET,SOCK_STREAM,0);
65     if (sock < 0)
66         throwErrno();
67     fd(sock);
68 }
69
70 prefix_ void senf::TCPv4SocketProtocol::init_server(INet4SocketAddress const & address,
71                                                            unsigned backlog)
72     const
73 {
74     init_server();
75     bind(address);
76     reuseaddr(true);
77     if (::listen(fd(),backlog) < 0)
78         throwErrno();
79 }
80
81 prefix_ std::auto_ptr<senf::SocketProtocol> senf::TCPv4SocketProtocol::clone()
82     const
83 {
84     return std::auto_ptr<SocketProtocol>(new TCPv4SocketProtocol());
85 }
86
87 ///////////////////////////////////////////////////////////////////////////
88 // senf::TCPv6SocketProtocol::
89
90 prefix_ void senf::TCPv6SocketProtocol::init_client()
91     const
92 {
93     int sock = ::socket(PF_INET6,SOCK_STREAM,0);
94     if (sock < 0)
95         throwErrno();
96     fd(sock);
97 }
98
99 prefix_ void
100 senf::TCPv6SocketProtocol::init_client(INet6SocketAddress const & address)
101     const
102 {
103     init_client();
104     connect(address);
105 }
106
107 prefix_ void senf::TCPv6SocketProtocol::init_server()
108     const
109 {
110     int sock = ::socket(PF_INET6,SOCK_STREAM,0);
111     if (sock < 0)
112         throwErrno();
113     fd(sock);
114 }
115
116 prefix_ void senf::TCPv6SocketProtocol::init_server(INet6SocketAddress const & address,
117                                                     unsigned backlog)
118     const
119 {
120     init_server();
121     bind(address);
122     reuseaddr(true);
123     if (::listen(fd(),backlog) < 0)
124         throwErrno();
125 }
126
127 prefix_ std::auto_ptr<senf::SocketProtocol> senf::TCPv6SocketProtocol::clone()
128     const
129 {
130     return std::auto_ptr<SocketProtocol>(new TCPv6SocketProtocol());
131 }
132
133 ///////////////////////////////cc.e////////////////////////////////////////
134 #undef prefix_
135 //#include "TCPSocketHandle.mpp"
136
137 \f
138 // Local Variables:
139 // mode: c++
140 // fill-column: 100
141 // c-file-style: "senf"
142 // indent-tabs-mode: nil
143 // ispell-local-dictionary: "american"
144 // compile-command: "scons -u test"
145 // comment-column: 40
146 // End: