3916d06fd0f4dbac4040184553528cf780f7584c
[senf.git] / Socket / Protocols / UN / UNAddressing.cc
1 // Copyright (C) 2007 
2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum NETwork research (NET)
4 //     David Wagner <david.wagner@fokus.fraunhofer.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 UNAddressing non-inline non-template implementation */
23
24 #include "UNAddressing.hh"
25 //#include "UNAddressing.ih"
26
27 // Custom includes
28 #include <stdio.h>
29 #include <boost/operators.hpp>
30 #include <boost/filesystem/path.hpp>
31 #include "../../../Utils/SafeBool.hh"
32 //#include "UNAddressing.mpp"
33 #define prefix_
34 ///////////////////////////////cc.p////////////////////////////////////////
35 prefix_ senf::UNSocketAddress::UNSocketAddress(boost::filesystem::path p)
36 {
37     chdir(p.branch_path().string().c_str());
38 //Check if the unix domain socket already exists...
39     if(!remove(p.leaf().c_str()))
40         printf("File already existed and therefore was deleted!\n");
41     else
42         printf("File not found, will be created.\n");
43     fflush(stdout);
44
45     sockAddr.sun_family = AF_UNIX; 
46     strcpy(sockAddr.sun_path, p.string().c_str());
47 }
48
49     prefix_ senf::UNSocketAddress fromString(std::string s)
50 {
51     return senf::UNSocketAddress::UNSocketAddress(boost::filesystem::path(s));
52 }
53
54     prefix_ senf::UNSocketAddress fromPath(boost::filesystem::path p)
55 {
56     return senf::UNSocketAddress::UNSocketAddress(p);
57 }
58     prefix_ std::string senf::UNSocketAddress::path()
59         const
60 {
61     return std::string(sockAddr.sun_path);
62 }
63
64 prefix_ sockaddr_un senf::UNSocketAddress::sockaddr()
65 {
66     struct sockaddr_un out; 
67     out.sun_family = sockAddr.sun_family;
68     strcpy(out.sun_path, sockAddr.sun_path);
69     return out; 
70 }
71
72 prefix_ sockaddr * senf::UNSocketAddress::sockaddr_p()
73 {
74     return reinterpret_cast <struct sockaddr  *> (&sockAddr); 
75 }
76
77
78 prefix_ sockaddr const  * senf::UNSocketAddress::sockaddr_p()
79     const
80 {
81     return reinterpret_cast <struct sockaddr const  *> (&sockAddr); 
82 }
83
84 prefix_ unsigned senf::UNSocketAddress::sockaddr_len()
85 {
86     return sizeof(sockAddr);
87 }
88
89 prefix_ std::ostream & operator<<(std::ostream & os, senf::UNSocketAddress::UNSocketAddress const & addr){
90     os << addr.path();
91     return os;
92 }
93
94
95 ///////////////////////////////cc.e////////////////////////////////////////
96 #undef prefix_
97 //#include "UNAddressing.mpp"
98
99 \f
100 // Local Variables:
101 // mode: c++
102 // fill-column: 100
103 // comment-column: 40
104 // c-file-style: "senf"
105 // indent-tabs-mode: nil
106 // ispell-local-dictionary: "american"
107 // compile-command: "scons -u test"
108 // End: