Skip to content

Commit

Permalink
Minor CF enhancements for iOS client.
Browse files Browse the repository at this point in the history
Added Reachability::reachableVia method.
  • Loading branch information
jamesyonan committed Jul 28, 2012
1 parent 223ebe8 commit 33e7670
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
25 changes: 20 additions & 5 deletions openvpn/applecrypto/cf/cf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ namespace openvpn {
std::swap(obj_, other.obj_);
}

void reset(T obj, const Own own=OWN)
void reset(T obj=NULL, const Own own=OWN)
{
if (own == BORROW && obj)
CFRetain(obj);
Expand Down Expand Up @@ -208,14 +208,19 @@ namespace openvpn {
return dict((const void **)keys.c_data(), (const void **)values.c_data(), std::min(keys.size(), values.size()));
}

inline MutableArray mutable_array()
inline Dict const_dict(MutableDict& mdict)
{
return MutableArray(CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks));
return Dict(mdict(), CF::BORROW);
}

inline MutableDict mutable_dict()
inline MutableArray mutable_array(const CFIndex capacity=0)
{
return MutableDict(CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
return MutableArray(CFArrayCreateMutable(kCFAllocatorDefault, capacity, &kCFTypeArrayCallBacks));
}

inline MutableDict mutable_dict(const CFIndex capacity=0)
{
return MutableDict(CFDictionaryCreateMutable(kCFAllocatorDefault, capacity, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
}

inline Error error(CFStringRef domain, CFIndex code, CFDictionaryRef userInfo)
Expand Down Expand Up @@ -316,6 +321,16 @@ namespace openvpn {
return s1.defined() && s2.defined() && CFStringCompare(s1(), s2(), compareOptions) == kCFCompareEqualTo;
}

// property lists
inline Data plist(CFTypeRef obj)
{
return Data(CFPropertyListCreateData(kCFAllocatorDefault,
obj,
kCFPropertyListBinaryFormat_v1_0,
0,
NULL));
}

} // namespace CF
} // namespace openvpn

Expand Down
22 changes: 11 additions & 11 deletions openvpn/applecrypto/cf/cfhelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,52 +135,52 @@ namespace openvpn {

// set a number in a mutable dictionary

inline void dict_set_num_int(MutableDict& dict, CFStringRef key, int value)
inline void dict_set_int(MutableDict& dict, CFStringRef key, int value)
{
Number num = number_from_int(value);
CFDictionarySetValue(dict(), key, num());
}

inline void dict_set_num_int(MutableDict& dict, const char *key, int value)
inline void dict_set_int(MutableDict& dict, const char *key, int value)
{
String keystr = string(key);
Number num = number_from_int(value);
CFDictionarySetValue(dict(), keystr(), num());
}

inline void dict_set_num_int32(MutableDict& dict, CFStringRef key, SInt32 value)
inline void dict_set_int32(MutableDict& dict, CFStringRef key, SInt32 value)
{
Number num = number_from_int32(value);
CFDictionarySetValue(dict(), key, num());
}

inline void dict_set_num_int32(MutableDict& dict, const char *key, SInt32 value)
inline void dict_set_int32(MutableDict& dict, const char *key, SInt32 value)
{
String keystr = string(key);
Number num = number_from_int32(value);
CFDictionarySetValue(dict(), keystr(), num());
}

inline void dict_set_num_long_long(MutableDict& dict, CFStringRef key, long long value)
inline void dict_set_long_long(MutableDict& dict, CFStringRef key, long long value)
{
Number num = number_from_long_long(value);
CFDictionarySetValue(dict(), key, num());
}

inline void dict_set_num_long_long(MutableDict& dict, const char *key, long long value)
inline void dict_set_long_long(MutableDict& dict, const char *key, long long value)
{
String keystr = string(key);
Number num = number_from_long_long(value);
CFDictionarySetValue(dict(), keystr(), num());
}

inline void dict_set_num_index(MutableDict& dict, CFStringRef key, CFIndex value)
inline void dict_set_index(MutableDict& dict, CFStringRef key, CFIndex value)
{
Number num = number_from_index(value);
CFDictionarySetValue(dict(), key, num());
}

inline void dict_set_num_index(MutableDict& dict, const char *key, CFIndex value)
inline void dict_set_index(MutableDict& dict, const char *key, CFIndex value)
{
String keystr = string(key);
Number num = number_from_index(value);
Expand All @@ -189,19 +189,19 @@ namespace openvpn {

// append number to a mutable array

inline void array_append_num_int(MutableArray& array, int value)
inline void array_append_int(MutableArray& array, int value)
{
Number num = number_from_int(value);
CFArrayAppendValue(array(), num());
}

inline void array_append_num_int32(MutableArray& array, SInt32 value)
inline void array_append_int32(MutableArray& array, SInt32 value)
{
Number num = number_from_int32(value);
CFArrayAppendValue(array(), num());
}

inline void array_append_num_index(MutableArray& array, CFIndex value)
inline void array_append_index(MutableArray& array, CFIndex value)
{
Number num = number_from_index(value);
CFArrayAppendValue(array(), num());
Expand Down
12 changes: 11 additions & 1 deletion openvpn/applecrypto/util/reachable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ namespace openvpn {
bool defined() const { return bool(didRetrieveFlags); }
bool reachable() const { return bool(flags & kSCNetworkReachabilityFlagsReachable); }
bool connectionRequired() const { return bool(flags & kSCNetworkReachabilityFlagsConnectionRequired); }
bool isWWAN() const { return bool(flags & kSCNetworkReachabilityFlagsIsWWAN); }
bool isWWAN() const { return bool(flags & kSCNetworkReachabilityFlagsIsWWAN); } // cellular

bool reachableVia(const std::string& net_type) const
{
if (net_type == "cellular")
return reachable() && isWWAN();
else if (net_type == "wifi")
return reachable() && !isWWAN();
else
return reachable();
}

std::string to_string() const {
std::ostringstream out;
Expand Down

0 comments on commit 33e7670

Please sign in to comment.