switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Socket / Protocols / INet / TCPSocketHandle.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief TCPv4SocketHandle and TCPv6SocketHandle non-inline non-template implementation
30  */
31
32 #include "TCPSocketHandle.hh"
33 //#include "TCPSocketHandle.ih"
34
35 // Custom includes
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/ioctl.h>
39
40 #include <senf/Utils/Exception.hh>
41
42 //#include "TCPSocketHandle.mpp"
43 #define prefix_
44 //-/////////////////////////////////////////////////////////////////////////////////////////////////
45
46 //-/////////////////////////////////////////////////////////////////////////////////////////////////
47 // senf::TCPv4SocketProtocol
48
49 prefix_ void senf::TCPv4SocketProtocol::init_client()
50     const
51 {
52     int sock = ::socket(PF_INET,SOCK_STREAM,0);
53     if (sock < 0)
54         SENF_THROW_SYSTEM_EXCEPTION("");
55     fd(sock);
56 }
57
58 prefix_ void
59 senf::TCPv4SocketProtocol::init_client(INet4SocketAddress const & address)
60     const
61 {
62     init_client();
63     clientHandle().connect(address);
64 }
65
66 prefix_ void senf::TCPv4SocketProtocol::init_server()
67     const
68 {
69     int sock = ::socket(PF_INET,SOCK_STREAM,0);
70     if (sock < 0)
71         SENF_THROW_SYSTEM_EXCEPTION("");
72     fd(sock);
73 }
74
75 prefix_ void senf::TCPv4SocketProtocol::init_server(INet4SocketAddress const & address,
76                                                            unsigned backlog)
77     const
78 {
79     init_server();
80     reuseaddr(true);
81     serverHandle().bind(address);
82     if (::listen(fd(),backlog) < 0)
83         SENF_THROW_SYSTEM_EXCEPTION("");
84 }
85
86 //-/////////////////////////////////////////////////////////////////////////////////////////////////
87 // senf::TCPv6SocketProtocol::
88
89 prefix_ void senf::TCPv6SocketProtocol::init_client()
90     const
91 {
92     int sock = ::socket(PF_INET6,SOCK_STREAM,0);
93     if (sock < 0)
94         SENF_THROW_SYSTEM_EXCEPTION("");
95     fd(sock);
96 }
97
98 prefix_ void
99 senf::TCPv6SocketProtocol::init_client(INet6SocketAddress const & address)
100     const
101 {
102     init_client();
103     clientHandle().connect(address);
104 }
105
106 prefix_ void senf::TCPv6SocketProtocol::init_server()
107     const
108 {
109     int sock = ::socket(PF_INET6,SOCK_STREAM,0);
110     if (sock < 0)
111         SENF_THROW_SYSTEM_EXCEPTION("");
112     fd(sock);
113 }
114
115 prefix_ void senf::TCPv6SocketProtocol::init_server(INet6SocketAddress const & address,
116                                                     unsigned backlog)
117     const
118 {
119     init_server();
120     serverHandle().bind(address);
121     reuseaddr(true);
122     if (::listen(fd(),backlog) < 0)
123         SENF_THROW_SYSTEM_EXCEPTION("");
124 }
125
126 //-/////////////////////////////////////////////////////////////////////////////////////////////////
127 #undef prefix_
128 //#include "TCPSocketHandle.mpp"
129
130 \f
131 // Local Variables:
132 // mode: c++
133 // fill-column: 100
134 // c-file-style: "senf"
135 // indent-tabs-mode: nil
136 // ispell-local-dictionary: "american"
137 // compile-command: "scons -u test"
138 // comment-column: 40
139 // End: