Fix documentation typos
[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 /** \file
24     \brief INet[46]Address and INet[46]AddressingPolicy non-inline non-template implementation
25  */
26
27 #include "INetAddressing.hh"
28 //#include "INetAddressing.ih"
29
30 // Custom includes
31 #include <sstream>
32 #include <string.h>
33 #include <sys/socket.h>
34 #include <boost/lexical_cast.hpp>
35
36 //#include "INetAddressing.mpp"
37 #define prefix_
38 ///////////////////////////////cc.p////////////////////////////////////////
39
40 prefix_ senf::INet4Address::INet4Address(std::string host, unsigned port)
41 {
42     clear();
43     /** \todo  gethostbyname support */
44     if (::inet_aton(host.c_str(), &addr_.sin_addr) == 0)
45         throw InvalidINetAddressException();
46     addr_.sin_port = htons(port);
47 }
48
49 prefix_ std::string senf::INet4Address::str()
50     const
51 {
52     std::stringstream s;
53     s << host() << ':' << port();
54     return s.str();
55 }
56
57 prefix_ void senf::INet4Address::clear()
58 {
59     ::memset(&addr_,0,sizeof(addr_));
60     addr_.sin_family = AF_INET;
61 }
62
63 prefix_ void senf::INet4Address::assignString(std::string address)
64 {
65     clear();
66     unsigned i = address.find(':');
67     if (i == std::string::npos)
68         throw InvalidINetAddressException();
69     // The temporary string in the next expr is guaranteed to live
70     // until end-of-statement
71     if (::inet_aton(std::string(address,0,i).c_str(), &addr_.sin_addr) == 0)
72         throw InvalidINetAddressException();
73     try {
74         // Replace lexical_cast with strtoul ?
75         addr_.sin_port = htons(boost::lexical_cast< ::u_int16_t >(std::string(address,i+1)));
76     } 
77     catch (boost::bad_lexical_cast const &) {
78         throw InvalidINetAddressException();
79     }
80 }
81
82 ///////////////////////////////cc.e////////////////////////////////////////
83 #undef prefix_
84 //#include "INetAddressing.mpp"
85
86 \f
87 // Local Variables:
88 // mode: c++
89 // c-file-style: "senf"
90 // End: