OSDN Git Service

コメント追加
[ultramonkey-l7/ultramonkey-l7-v3.git] / l7vsd / include / endpoint.h
1 /*!
2  *    @file    endpoint.h
3  *    @brief    endpoint serialize helper class
4  *
5  * L7VSD: Linux Virtual Server for Layer7 Load Balancing
6  * Copyright (C) 2009  NTT COMWARE Corporation.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (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 GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  **********************************************************************/
24
25 #ifndef    ENDPOINT_H
26 #define    ENDPOINT_H
27
28 #include <boost/serialization/nvp.hpp>
29 #include <boost/serialization/split_free.hpp>
30
31 namespace boost {
32 namespace serialization {
33
34 template<class Archive, class T>
35 inline void serialize(
36     Archive & ar,
37     boost::asio::ip::basic_endpoint<T> & t,
38     const unsigned int file_version 
39 ){
40     boost::serialization::split_free( ar, t, file_version );
41 }
42
43 template<class Archive, class T>
44 inline void save(
45     Archive & ar,
46     boost::asio::ip::basic_endpoint<T> const& t,
47     const unsigned int /* file_version */
48 ){
49     std::string     addr    = t.address().to_string();
50     unsigned short  port    = t.port();
51     unsigned long   scope_id = 0;
52     if( addr.find_first_of( "%" ) != std::string::npos ){
53         std::string addr_ = addr.substr( 0, addr.find_first_of( "%" ) );
54         std::string ifname = addr.substr( addr.find_first_of( "%" ) + 1 );
55         scope_id = if_nametoindex( ifname.c_str() );
56         if( scope_id == 0 )
57             scope_id = atoi(ifname.c_str());
58         ar << boost::serialization::make_nvp( "addr", addr_ );
59     }else{
60         ar << boost::serialization::make_nvp( "addr", addr );    
61     }
62     ar << boost::serialization::make_nvp( "port", port );
63     ar << boost::serialization::make_nvp( "scope_id", scope_id );
64 }
65
66 template<class Archive, class T>
67 inline void load(
68     Archive & ar,
69     boost::asio::ip::basic_endpoint<T>& t,
70     const unsigned int /* file_version */ 
71 ){
72     std::string     addr;
73     unsigned short  port;
74     unsigned long   scope_id;
75     ar >> boost::serialization::make_nvp( "addr", addr );
76     ar >> boost::serialization::make_nvp( "port", port );
77     ar >> boost::serialization::make_nvp( "scope_id", scope_id );
78     boost::asio::ip::address tmp = boost::asio::ip::address::from_string( addr );
79     if ( tmp.is_v6() ){
80         boost::asio::ip::address_v6 v6addr = boost::asio::ip::address_v6::from_string( addr );
81         v6addr.scope_id(scope_id);
82         t = boost::asio::ip::basic_endpoint<T>( v6addr, port );
83     }else{
84         t = boost::asio::ip::basic_endpoint<T>( boost::asio::ip::address_v4::from_string( addr ), port );
85     }
86 }
87
88 }    // namespace serialization
89 }    // namespace boost
90
91 #endif    // ENDPOINT_H