• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
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 
18 #include "util.h"
19 
20 #include <android-base/format.h>
21 #include <android-base/parseint.h>
22 #include <server_configurable_flags/get_flags.h>
23 
24 using android::base::ParseInt;
25 using server_configurable_flags::GetServerConfigurableFlag;
26 
sockaddrSize(const sockaddr * sa)27 socklen_t sockaddrSize(const sockaddr* sa) {
28     if (sa == nullptr) return 0;
29 
30     switch (sa->sa_family) {
31         case AF_INET:
32             return sizeof(sockaddr_in);
33         case AF_INET6:
34             return sizeof(sockaddr_in6);
35         default:
36             return 0;
37     }
38 }
39 
sockaddrSize(const sockaddr_storage & ss)40 socklen_t sockaddrSize(const sockaddr_storage& ss) {
41     return sockaddrSize(reinterpret_cast<const sockaddr*>(&ss));
42 }
43 
getExperimentFlagInt(const std::string & flagName,int defaultValue)44 int getExperimentFlagInt(const std::string& flagName, int defaultValue) {
45     int val = defaultValue;
46     ParseInt(GetServerConfigurableFlag("netd_native", flagName, ""), &val);
47     return val;
48 }
49 
timestampToString(const std::chrono::system_clock::time_point & ts)50 std::string timestampToString(const std::chrono::system_clock::time_point& ts) {
51     using std::chrono::duration_cast;
52     using std::chrono::milliseconds;
53     const auto time_sec = std::chrono::system_clock::to_time_t(ts);
54     char buf[32];
55     std::strftime(buf, sizeof(buf), "%H:%M:%S", std::localtime(&time_sec));
56     int ms = duration_cast<milliseconds>(ts.time_since_epoch()).count() % 1000;
57     return fmt::format("{}.{:03d}", buf, ms);
58 }
59