NEW FILE HEADER / COPYRIGHT FORMAT
[senf.git] / Socket / Protocols / UN / UNProtocol.cc
1 // Copyright (C) 2007 
2 // Fraunhofer Institute for Open Communication Systems (FOKUS) 
3 // Kompetenzzentrum NETwork research (NET)
4 //     David Wagner <dw6@berlios.de>
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the
18 // Free Software Foundation, Inc.,
19 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
21 /** \file
22     \brief UNProtocol non-inline non-template implementation */
23
24 #include "UNProtocol.hh"
25 //#include "UNProtocol.ih"
26
27 // Custom includes
28 #include <fstream>
29 #include <sys/socket.h>
30 #include <sys/ioctl.h>
31 #include <linux/sockios.h> // for SIOCINQ / SIOCOUTQ
32 #include "../../../Utils/Exception.hh"
33
34 //#include "UNProtocol.mpp"
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
37 prefix_ unsigned senf::UNProtocol::available()
38     const
39 {
40     int n;
41     if (::ioctl(fd(),SIOCINQ,&n) < 0)
42         throwErrno();
43     return n;
44 }
45
46 prefix_ bool senf::UNProtocol::eof()
47     const
48 {
49     return false;
50 }
51
52 prefix_ void senf::UNProtocol::connect(UNSocketAddress const & address) 
53     const 
54 {
55     if(::connect(fd(), address.sockaddr_p(), sizeof(sockaddr_un)) < 0)
56         throwErrno();
57 }
58
59 prefix_ void senf::UNProtocol::bind(UNSocketAddress const & address) 
60     const 
61 {
62     if(::bind(fd(), address.sockaddr_p(), sizeof(sockaddr_un)) < 0)
63         throwErrno();
64     
65 }
66
67
68
69 prefix_ void senf::UNProtocol::close() 
70     const
71 {
72     check_and_unlink();
73   
74     SocketProtocol::close();
75 }
76
77 prefix_ void senf::UNProtocol::terminate() 
78     const
79 {
80     check_and_unlink();
81     
82     SocketProtocol::terminate();
83 }
84
85 prefix_ void senf::UNProtocol::check_and_unlink()
86     const
87 {
88     typedef ClientSocketHandle<MakeSocketPolicy<UNAddressingPolicy>::policy> UNSocketHandle;
89     try {
90         UNSocketAddress una (static_socket_cast<UNSocketHandle>(fh()).local());
91         ::unlink(una.path().c_str());
92     }
93     catch (SystemException & e) {
94     }
95 }
96     
97 // //  struct sockaddr_un test;
98 // //  socklen_t len;
99 // //  memset( (char*)&test, 0xff, sizeof( test));
100 // //  int fd = inputSocket.fd() ;
101 // ////    printf( "fd: %d\n", fd);
102 // //
103 // //  int r = getsockname( fd, (struct sockaddr *)&test, &len);
104 // //  if( r < 0){
105 // //    perror( "bla:");
106 // //  }
107 // //  else{
108 // //    printf( "name: %d %d %s\n", r, len , test.sun_path);
109 // //    unsigned char *p = (unsigned char*) &test;for( r=0; r< len; r++) printf( "%2.2x ", (int)(p[r])); printf ("\n");
110 // //  }
111 //     struct sockaddr_un test;
112 //     socklen_t len = sizeof( test);
113 //     int r = ::getsockname(fd(), (struct sockaddr *)&test, &len);
114 //     if( r == 0 && ::strlen(test.sun_path) > 0){
115 //       ::unlink( test.sun_path);
116 //     }
117
118 ///////////////////////////////cc.e////////////////////////////////////////
119 #undef prefix_
120 //#include "UNProtocol.mpp"
121
122 \f
123 // Local Variables:
124 // mode: c++
125 // fill-column: 100
126 // comment-column: 40
127 // c-file-style: "senf"
128 // indent-tabs-mode: nil
129 // ispell-local-dictionary: "american"
130 // compile-command: "scons -u test"
131 // End: