4830a12185b9be7f2eadfbe73dc3d0f181503f49
[senf.git] / senf / Socket / Protocols / BSDAddressingPolicy.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 BSDAddressingPolicyMixin non-inline non-template implementation
25  */
26
27 #include "BSDAddressingPolicy.hh"
28 //#include "BSDAddressingPolicy.ih"
29
30 // Custom includes
31 #include <sys/socket.h>
32 #include <sys/types.h>
33 #include <senf/Utils/Exception.hh>
34
35 //#include "BSDAddressingPolicy.mpp"
36 #define prefix_
37 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38
39 prefix_ void senf::BSDAddressingPolicyMixinBase::do_local(FileHandle const & handle,
40                                                                  struct sockaddr * addr,
41                                                                  socklen_t * len)
42 {
43     if (::getsockname(handle.fd(),addr,len) < 0)
44         SENF_THROW_SYSTEM_EXCEPTION("");
45 }
46
47 prefix_ void senf::BSDAddressingPolicyMixinBase::do_peer(FileHandle const & handle,
48                                                                 struct sockaddr * addr,
49                                                                 socklen_t * len)
50 {
51     if (::getpeername(handle.fd(),addr,len) < 0)
52         SENF_THROW_SYSTEM_EXCEPTION("");
53 }
54
55 prefix_ void senf::BSDAddressingPolicyMixinBase::do_bind(FileHandle const & handle,
56                                                                 struct sockaddr const * addr,
57                                                                 socklen_t len)
58 {
59     if (::bind(handle.fd(),addr,len) < 0)
60         SENF_THROW_SYSTEM_EXCEPTION("");
61 }
62
63 prefix_ void senf::BSDAddressingPolicyMixinBase::do_connect(FileHandle const & handle,
64                                                                    struct sockaddr const * addr,
65                                                                    socklen_t len)
66 {
67     while(1) {
68         if (::connect(handle.fd(),addr,len) < 0)
69             switch (errno) {
70             case EINPROGRESS: {
71                 handle.waitWriteable();
72                 int err = 0;
73                 socklen_t len = sizeof(err);
74                 if (::getsockopt(handle.fd(),SOL_SOCKET,SO_ERROR,&err,&len) < 0)
75                     SENF_THROW_SYSTEM_EXCEPTION("");
76                 if (err != 0)
77                     throw SystemException(err SENF_EXC_DEBUGINFO);
78                 return;
79             }
80             case EINTR:
81                 break;
82             default:
83                 SENF_THROW_SYSTEM_EXCEPTION("");
84             }
85         else
86             return;
87     }
88 }
89
90 //-/////////////////////////////////////////////////////////////////////////////////////////////////
91 #undef prefix_
92 //#include "BSDAddressingPolicy.mpp"
93
94 \f
95 // Local Variables:
96 // mode: c++
97 // fill-column: 100
98 // c-file-style: "senf"
99 // indent-tabs-mode: nil
100 // ispell-local-dictionary: "american"
101 // compile-command: "scons -u test"
102 // comment-column: 40
103 // End: