Home | History | Annotate | Download | only in server
      1 /*
      2  * Copyright (C) 2012 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 #ifndef _NETD_CONSTANTS_H
     18 #define _NETD_CONSTANTS_H
     19 
     20 #include <string>
     21 #include <list>
     22 #include <ifaddrs.h>
     23 #include <netdb.h>
     24 #include <stdarg.h>
     25 
     26 #include <chrono>
     27 
     28 #include <private/android_filesystem_config.h>
     29 
     30 #include "utils/RWLock.h"
     31 
     32 const int PROTECT_MARK = 0x1;
     33 const int MAX_SYSTEM_UID = AID_APP - 1;
     34 
     35 extern const size_t SHA256_SIZE;
     36 
     37 extern const char * const OEM_SCRIPT_PATH;
     38 extern const char * const ADD;
     39 extern const char * const DEL;
     40 
     41 enum IptablesTarget { V4, V6, V4V6 };
     42 
     43 int execIptablesRestore(IptablesTarget target, const std::string& commands);
     44 int execIptablesRestoreWithOutput(IptablesTarget target, const std::string& commands,
     45                                   std::string *output);
     46 int execIptablesRestoreCommand(IptablesTarget target, const std::string& table,
     47                                const std::string& command, std::string *output);
     48 bool isIfaceName(const std::string& name);
     49 int parsePrefix(const char *prefix, uint8_t *family, void *address, int size, uint8_t *prefixlen);
     50 void blockSigpipe();
     51 void setCloseOnExec(const char *sock);
     52 
     53 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
     54 
     55 #define __INT_STRLEN(i) sizeof(#i)
     56 #define _INT_STRLEN(i) __INT_STRLEN(i)
     57 #define INT32_STRLEN _INT_STRLEN(INT32_MIN)
     58 #define UINT32_STRLEN _INT_STRLEN(UINT32_MAX)
     59 #define UINT32_HEX_STRLEN sizeof("0x12345678")
     60 
     61 #define WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
     62 
     63 const uid_t INVALID_UID = static_cast<uid_t>(-1);
     64 
     65 
     66 struct AddrinfoDeleter {
     67     void operator()(struct addrinfo* p) const {
     68         if (p != nullptr) {
     69             freeaddrinfo(p);
     70         }
     71     }
     72 };
     73 
     74 typedef std::unique_ptr<struct addrinfo, struct AddrinfoDeleter> ScopedAddrinfo;
     75 
     76 
     77 struct IfaddrsDeleter {
     78     void operator()(struct ifaddrs *p) const {
     79         if (p != nullptr) {
     80             freeifaddrs(p);
     81         }
     82     }
     83 };
     84 
     85 typedef std::unique_ptr<struct ifaddrs, struct IfaddrsDeleter> ScopedIfaddrs;
     86 
     87 namespace android {
     88 namespace net {
     89 
     90 /**
     91  * This lock exists to make NetdNativeService RPCs (which come in on multiple Binder threads)
     92  * coexist with the commands in CommandListener.cpp. These are presumed not thread-safe because
     93  * CommandListener has only one user (NetworkManagementService), which is connected through a
     94  * FrameworkListener that passes in commands one at a time.
     95  */
     96 extern android::RWLock gBigNetdLock;
     97 
     98 }  // namespace net
     99 }  // namespace android
    100 
    101 #endif  // _NETD_CONSTANTS_H
    102