013704d73bc2dc2e53dd71ab981eea2ad20b774f
[senf.git] / Socket / INetAddressing.cc
1 // $Id$
2 //
3 // Copyright (C) 2006 
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.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 // Definition of non-inline non-template functions
24
25 #include "INetAddressing.hh"
26 //#include "INetAddressing.ih"
27
28 // Custom includes
29 #include <sstream>
30 #include <string.h>
31 #include <sys/socket.h>
32 #include <boost/lexical_cast.hpp>
33
34 //#include "INetAddressing.mpp"
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
37
38 prefix_ satcom::lib::INet4Address::INet4Address(std::string host, unsigned port)
39 {
40     clear();
41     // TODO: gethostbyname einbauen
42     if (::inet_aton(host.c_str(), &addr_.sin_addr) == 0)
43         throw InvalidINetAddressException();
44     addr_.sin_port = htons(port);
45 }
46
47 prefix_ std::string satcom::lib::INet4Address::str()
48     const
49 {
50     std::stringstream s;
51     s << host() << ':' << port();
52     return s.str();
53 }
54
55 prefix_ void satcom::lib::INet4Address::clear()
56 {
57     ::memset(&addr_,0,sizeof(addr_));
58     addr_.sin_family = AF_INET;
59 }
60
61 prefix_ void satcom::lib::INet4Address::assignString(std::string address)
62 {
63     clear();
64     // TODO: gethostbyname einbauen
65     unsigned i = address.find(':');
66     if (i == std::string::npos)
67         throw InvalidINetAddressException();
68     if (::inet_aton(std::string(address,0,i).c_str(), &addr_.sin_addr) == 0)
69         throw InvalidINetAddressException();
70     try {
71         // Replace lexical_cast with strtoul ?
72         addr_.sin_port = htons(boost::lexical_cast< ::u_int16_t >(std::string(address,i+1)));
73     } 
74     catch (boost::bad_lexical_cast const & ex) {
75         throw InvalidINetAddressException();
76     }
77 }
78
79 ///////////////////////////////cc.e////////////////////////////////////////
80 #undef prefix_
81 //#include "INetAddressing.mpp"
82
83 \f
84 // Local Variables:
85 // mode: c++
86 // c-file-style: "satcom"
87 // End: