|
1 /* |
|
2 * Copyright 2004-2006 Intel Corporation |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 */ |
|
16 |
|
17 #ifdef HAVE_CONFIG_H |
|
18 # include <dtn-config.h> |
|
19 #endif |
|
20 |
|
21 #ifdef __linux__ |
|
22 |
|
23 #include <oasys/io/IPSocket.h> |
|
24 #include <oasys/io/NetUtils.h> |
|
25 |
|
26 #include "EthernetScheme.h" |
|
27 #include "EndpointID.h" |
|
28 |
|
29 namespace dtn { |
|
30 |
|
31 template <> |
|
32 EthernetScheme* oasys::Singleton<EthernetScheme>::instance_ = 0; |
|
33 |
|
34 /* |
|
35 * Parse out an ethernet address from the ssp. |
|
36 * |
|
37 * @return true if the string is a valid ethernet address, false if not. |
|
38 */ |
|
39 bool |
|
40 EthernetScheme::parse(const std::string& ssp, eth_addr_t* addr) |
|
41 { |
|
42 // XXX/jakob - for now, assume it's a correctly formatted ethernet URI |
|
43 // eth://00:01:02:03:04:05 or eth:00:01:02:03:04:05 |
|
44 |
|
45 const char* str = ssp.c_str(); |
|
46 if (str[0] == '/' && str[1] == '/') { |
|
47 str = str + 2; |
|
48 } |
|
49 |
|
50 // this is a nasty hack. grab the ethernet address out of there, |
|
51 // assuming everything is correct |
|
52 int r = sscanf(str, "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx", |
|
53 &addr->octet[0], |
|
54 &addr->octet[1], |
|
55 &addr->octet[2], |
|
56 &addr->octet[3], |
|
57 &addr->octet[4], |
|
58 &addr->octet[5]); |
|
59 if (r != 6) { |
|
60 return false; |
|
61 } |
|
62 |
|
63 return true; |
|
64 } |
|
65 |
|
66 /** |
|
67 * Validate that the SSP in the given URI is legitimate for |
|
68 * this scheme. If the 'is_pattern' parameter is true, then |
|
69 * the ssp is being validated as an EndpointIDPattern. |
|
70 * |
|
71 * @return true if valid |
|
72 */ |
|
73 bool |
|
74 EthernetScheme::validate(const URI& uri, bool is_pattern) |
|
75 { |
|
76 (void)is_pattern; |
|
77 |
|
78 // make sure it's a valid url |
|
79 eth_addr_t addr; |
|
80 return parse(uri.ssp(), &addr); |
|
81 } |
|
82 |
|
83 /** |
|
84 * Match the given ssp with the given pattern. |
|
85 * |
|
86 * @return true if it matches |
|
87 */ |
|
88 bool |
|
89 EthernetScheme::match(const EndpointIDPattern& pattern, |
|
90 const EndpointID& eid) |
|
91 { |
|
92 // sanity check |
|
93 ASSERT(pattern.scheme() == this); |
|
94 |
|
95 log_debug_p("/dtn/scheme/ethernet", |
|
96 "matching %s against %s.", pattern.ssp().c_str(), eid.ssp().c_str()); |
|
97 |
|
98 size_t patternlen = pattern.ssp().length(); |
|
99 |
|
100 if (pattern.ssp() == eid.ssp()) |
|
101 return true; |
|
102 |
|
103 if (patternlen >= 1 && pattern.ssp()[patternlen-1] == '*') { |
|
104 patternlen--; |
|
105 |
|
106 if (pattern.ssp().substr(0, patternlen) == |
|
107 eid.ssp().substr(0, patternlen)) |
|
108 { |
|
109 return true; |
|
110 } |
|
111 } |
|
112 |
|
113 return false; |
|
114 } |
|
115 |
|
116 EndpointID::singleton_info_t |
|
117 EthernetScheme::is_singleton(const URI& uri) |
|
118 { |
|
119 (void)uri; |
|
120 return EndpointID::SINGLETON; |
|
121 } |
|
122 |
|
123 char* |
|
124 EthernetScheme::to_string(u_int8_t* addr, char* outstring) |
|
125 { |
|
126 sprintf(outstring,"eth://%02X:%02X:%02X:%02X:%02X:%02X", |
|
127 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); |
|
128 |
|
129 return outstring; |
|
130 } |
|
131 |
|
132 |
|
133 } // namespace dtn |
|
134 |
|
135 #endif /* __linux__ */ |