c0a9343ce27fb1c066dbee25251d1751b97c574c
[senf.git] / senf / Socket / ReadWritePolicy.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 ReadPolicy and WritePolicy non-inline non-template implementation
25  */
26
27 #include "ReadWritePolicy.hh"
28 //#include "ReadWritePolicy.ih"
29
30 // Custom includes
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <unistd.h>
34 #include <errno.h>
35
36
37 //#include "ReadWritePolicy.mpp"
38 #define prefix_
39 //-/////////////////////////////////////////////////////////////////////////////////////////////////
40
41 prefix_ unsigned senf::ReadablePolicy::read(FileHandle & handle, char * buffer,
42                                                    unsigned size)
43 {
44     int rv = -1;
45     do {
46         rv = ::read(handle.fd(), buffer, size);
47         if (rv < 0)
48             switch(errno) {
49             case EINTR:
50                 break;
51             case EAGAIN:
52                 // This means, the socket is non-blocking an no data was available
53                 rv = 0;
54                 break;
55             default:
56                 SENF_THROW_SYSTEM_EXCEPTION("");
57             }
58     } while (rv<0);
59     return rv;
60 }
61
62 prefix_ unsigned senf::ReadablePolicy::do_readfrom(FileHandle & handle, char * buffer,
63                                                           unsigned size,
64                                                           struct ::sockaddr * addr, socklen_t * len)
65 {
66     int rv = -1;
67     do {
68         rv = ::recvfrom(handle.fd(),buffer, size, 0, addr, len);
69         if (rv < 0)
70             switch (errno) {
71             case EINTR:
72                 break;
73             case EAGAIN:
74                 rv = 0;
75                 break;
76             default:
77                 SENF_THROW_SYSTEM_EXCEPTION("");
78             }
79     } while (rv<0);
80     return rv;
81 }
82
83 prefix_ unsigned senf::WriteablePolicy::do_write(FileHandle & handle, char const * buffer,
84                                                         unsigned size)
85 {
86     int rv = -1;
87     do {
88         rv = ::write(handle.fd(), buffer, size);
89         if (rv < 0)
90             switch (errno) {
91             case EINTR:
92                 break;
93             case EAGAIN:
94             case ENOBUFS:
95                 // According to the man page this should not happen, since packets are just silently being dropped.
96                 // It does happen in NetEmu using small TxQueues on WLAN interfaces 
97             case ECONNREFUSED:
98                 // Writing to a UDP socket seems return this error code if a corresponding ICMP
99                 // error code has been received before (at least on linux). This is inconsistent
100                 // since I cannot rely on getting ECONNREFUSED. I therefore ignore this error. TCP
101                 // sockets will return this error on connect() and not on write(). Therefore we can
102                 // unconditionally ignore this error here.
103                 rv = 0;
104                 break;
105             default:
106                 SENF_THROW_SYSTEM_EXCEPTION("");
107             }
108     } while (rv<0);
109     return rv;
110 }
111
112 prefix_ unsigned senf::WriteablePolicy::do_writeto(FileHandle & handle,
113                                                           char const * buffer, unsigned size,
114                                                           struct sockaddr const * addr, socklen_t len)
115 {
116     int rv = -1;
117     do {
118         rv = ::sendto(handle.fd(), buffer, size, 0, addr, len);
119         if (rv < 0)
120             switch (errno) {
121             case EINTR:
122                 break;
123             case EAGAIN:
124             case ENOBUFS:
125                 // According to the man page this should not happen, since packets are just silently being dropped.
126                 // It does happen in NetEmu using small TxQueues on WLAN interfaces 
127                 rv = 0;
128                 break;
129             default:
130                 SENF_THROW_SYSTEM_EXCEPTION("");
131             }
132     } while (rv<0);
133     return rv;
134 }
135
136 //-/////////////////////////////////////////////////////////////////////////////////////////////////
137 #undef prefix_
138 //#include "ReadWritePolicy.mpp"
139
140 \f
141 // Local Variables:
142 // mode: c++
143 // fill-column: 100
144 // c-file-style: "senf"
145 // indent-tabs-mode: nil
146 // ispell-local-dictionary: "american"
147 // compile-command: "scons -u test"
148 // comment-column: 40
149 // End: