Skip to content

Commit

Permalink
Full iOS beta for OpenVPN Connect.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesyonan committed Aug 1, 2012
1 parent 33e7670 commit 7331ee3
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 127 deletions.
6 changes: 3 additions & 3 deletions client/ovpncli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
#include <iostream>

// Set up export of our public interface unless
// CLIENT_API_VISIBILITY_HIDDEN is defined
// OPENVPN_CORE_API_VISIBILITY_HIDDEN is defined
#if defined(__GNUC__)
#define OPENVPN_CLIENT_EXPORT
#ifndef CLIENT_API_VISIBILITY_HIDDEN
#ifndef OPENVPN_CORE_API_VISIBILITY_HIDDEN
#pragma GCC visibility push(default)
#endif
#include "ovpncli.hpp" // public interface
#ifndef CLIENT_API_VISIBILITY_HIDDEN
#ifndef OPENVPN_CORE_API_VISIBILITY_HIDDEN
#pragma GCC visibility pop
#endif
#else
Expand Down
39 changes: 22 additions & 17 deletions openvpn/applecrypto/cf/cf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@
#include <string>
#include <algorithm>

#include <CoreFoundation/CFBase.h>
#include <CoreFoundation/CFString.h>
#include <CoreFoundation/CFArray.h>
#include <CoreFoundation/CFDictionary.h>
#include <CoreFoundation/CFData.h>
#include <CoreFoundation/CFNumber.h>
#include <CoreFoundation/CoreFoundation.h>

#include <openvpn/common/types.hpp>
#include <openvpn/common/exception.hpp>
Expand Down Expand Up @@ -83,13 +78,22 @@ namespace openvpn {

T operator()() const { return obj_; }

CFTypeRef generic() const { return (CFTypeRef)obj_; }

T release()
{
T ret = obj_;
obj_ = NULL;
return ret;
}

CFTypeRef generic_release()
{
T ret = obj_;
obj_ = NULL;
return (CFTypeRef)ret;
}

// Intended for use with Core Foundation methods that require
// a T* for saving a (non-borrowed) return value
T* mod_ref()
Expand Down Expand Up @@ -153,11 +157,21 @@ namespace openvpn {
return String(CFStringCreateWithCString(kCFAllocatorDefault, str, kCFStringEncodingUTF8));
}

inline String string(CFStringRef str)
{
return String(str, BORROW);
}

inline String string(const std::string& str)
{
return String(CFStringCreateWithCString(kCFAllocatorDefault, str.c_str(), kCFStringEncodingUTF8));
}

inline String string(const std::string* str)
{
return String(CFStringCreateWithCString(kCFAllocatorDefault, str->c_str(), kCFStringEncodingUTF8));
}

inline Number number_from_int(const int n)
{
return Number(CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &n));
Expand Down Expand Up @@ -248,8 +262,8 @@ namespace openvpn {
return NULL;
}

template <typename DICT>
inline CFTypeRef dict_index(const DICT& dict, const char *key)
template <typename DICT, typename KEY>
inline CFTypeRef dict_index(const DICT& dict, const KEY key)
{
if (dict.defined())
{
Expand All @@ -260,15 +274,6 @@ namespace openvpn {
return NULL;
}

template <typename DICT>
inline CFTypeRef dict_index(const DICT& dict, CFStringRef key)
{
if (dict.defined() && key)
return CFDictionaryGetValue(dict(), key);
else
return NULL;
}

// string methods

OPENVPN_SIMPLE_EXCEPTION(cppstring_error);
Expand Down
166 changes: 69 additions & 97 deletions openvpn/applecrypto/cf/cfhelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,78 +18,60 @@ namespace openvpn {

// Lookup or create (if absent) an item in a mutable dictionary.
// Return the item, which will be owned by base.
template <typename KEY>
inline CFTypeRef dict_get_create(CFMutableDictionaryRef base,
CFStringRef key,
const KEY& key,
CFTypeRef (*create_method)())
{
if (base)
{
CFTypeRef ret = CFDictionaryGetValue(base, key); // try lookup first
String keystr = string(key);
CFTypeRef ret = CFDictionaryGetValue(base, keystr()); // try lookup first
if (!ret)
{
// doesn't exist, must create
ret = (*create_method)();
CFDictionaryAddValue(base, key, ret);
CFDictionaryAddValue(base, keystr(), ret);
CFRelease(ret); // because ret is now owned by base
}
return ret;
}
return NULL;
}

// variation on above function that accepts a char * key
inline CFTypeRef dict_get_create(CFMutableDictionaryRef base,
const char *key,
CFTypeRef (*create_method)())
{
String keystr = string(key);
return dict_get_create(base, keystr(), create_method);
}

// lookup a dict in another dict (base) and return or create if absent

inline MutableDict dict_get_create_dict(MutableDict& base, CFStringRef key)
{
return CF::mutable_dict_cast(CF::dict_get_create(base(), key, CF::mutable_dict_new));
}

inline MutableDict dict_get_create_dict(MutableDict& base, const char *key)
template <typename KEY>
inline MutableDict dict_get_create_dict(MutableDict& base, const KEY& key)
{
String keystr = string(key);
return CF::mutable_dict_cast(CF::dict_get_create(base(), keystr(), CF::mutable_dict_new));
return mutable_dict_cast(dict_get_create(base(), keystr(), mutable_dict_new));
}

// lookup an array in a dict (base) and return or create if absent

inline MutableArray dict_get_create_array(MutableDict& base, CFStringRef key)
{
return CF::mutable_array_cast(CF::dict_get_create(base(), key, CF::mutable_array_new));
}

inline MutableArray dict_get_create_array(MutableDict& base, const char *key)
template <typename KEY>
inline MutableArray dict_get_create_array(MutableDict& base, const KEY& key)
{
String keystr = string(key);
return CF::mutable_array_cast(CF::dict_get_create(base(), keystr(), CF::mutable_array_new));
return mutable_array_cast(dict_get_create(base(), keystr(), mutable_array_new));
}


// lookup a string in a dictionary (DICT should be a Dict or a MutableDict)
template <typename DICT>
inline std::string dict_get_str(const DICT& dict, CFStringRef key)
// lookup an object in a dictionary (DICT should be a Dict or a MutableDict)
template <typename DICT, typename KEY>
inline CFTypeRef dict_get_obj(const DICT& dict, const KEY& key)
{
return cppstring(string_cast(dict_index(dict, key)));
return dict_index(dict, key);
}

// lookup a string in a dictionary (DICT should be a Dict or a MutableDict)
template <typename DICT>
inline std::string dict_get_str(const DICT& dict, const char *key)
template <typename DICT, typename KEY>
inline std::string dict_get_str(const DICT& dict, const KEY& key)
{
return cppstring(string_cast(dict_index(dict, key)));
}

// lookup an integer in a dictionary (DICT should be a Dict or a MutableDict)
template <typename DICT>
inline int dict_get_int(const DICT& dict, const char *key, const int default_value)
template <typename DICT, typename KEY>
inline int dict_get_int(const DICT& dict, const KEY& key, const int default_value)
{
int ret;
Number num = number_cast(dict_index(dict, key));
Expand All @@ -99,95 +81,94 @@ namespace openvpn {
return default_value;
}

// set a string in a mutable dictionary

inline void dict_set_str(MutableDict& dict, CFStringRef key, CFStringRef value)
// lookup a boolean in a dictionary (DICT should be a Dict or a MutableDict)
template <typename DICT, typename KEY>
inline bool dict_get_bool(const DICT& dict, const KEY& key, const bool default_value)
{
CFDictionarySetValue(dict(), key, value);
Bool b = bool_cast(dict_index(dict, key));
if (b.defined())
{
if (b() == kCFBooleanTrue)
return true;
else if (b() == kCFBooleanFalse)
return false;
}
return default_value;
}

inline void dict_set_str(MutableDict& dict, const char *key, CFStringRef value)
// set a CFTypeRef in a mutable dictionary

template <typename KEY>
inline void dict_set_obj(MutableDict& dict, const KEY& key, CFTypeRef value)
{
String keystr = string(key);
CFDictionarySetValue(dict(), keystr(), value);
}

inline void dict_set_str(MutableDict& dict, CFStringRef key, const std::string& value)
{
String valstr = string(value);
CFDictionarySetValue(dict(), key, valstr());
}
// set a string in a mutable dictionary

inline void dict_set_str(MutableDict& dict, const char* key, const std::string& value)
template <typename KEY, typename VALUE>
inline void dict_set_str(MutableDict& dict, const KEY& key, const VALUE& value)
{
String keystr = string(key);
String valstr = string(value);
CFDictionarySetValue(dict(), keystr(), valstr());
}

// append string to a mutable array

inline void array_append_str(MutableArray& array, const std::string& value)
{
String valstr = string(value);
CFArrayAppendValue(array(), valstr());
}

// set a number in a mutable dictionary

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_int(MutableDict& dict, const char *key, int value)
template <typename KEY>
inline void dict_set_int(MutableDict& dict, const KEY& key, int value)
{
String keystr = string(key);
Number num = number_from_int(value);
CFDictionarySetValue(dict(), keystr(), num());
}

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_int32(MutableDict& dict, const char *key, SInt32 value)
template <typename KEY>
inline void dict_set_int32(MutableDict& dict, const KEY& key, SInt32 value)
{
String keystr = string(key);
Number num = number_from_int32(value);
CFDictionarySetValue(dict(), keystr(), num());
}

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_long_long(MutableDict& dict, const char *key, long long value)
template <typename KEY>
inline void dict_set_long_long(MutableDict& dict, const KEY& key, long long value)
{
String keystr = string(key);
Number num = number_from_long_long(value);
CFDictionarySetValue(dict(), keystr(), num());
}

inline void dict_set_index(MutableDict& dict, CFStringRef key, CFIndex value)
template <typename KEY>
inline void dict_set_index(MutableDict& dict, const KEY& key, CFIndex value)
{
String keystr = string(key);
Number num = number_from_index(value);
CFDictionarySetValue(dict(), key, num());
CFDictionarySetValue((CFMutableDictionaryRef)dict(), keystr(), num());
}

inline void dict_set_index(MutableDict& dict, const char *key, CFIndex value)
// set a boolean in a mutable dictionary

template <typename KEY>
inline void dict_set_bool(MutableDict& dict, const KEY& key, bool value)
{
String keystr = string(key);
Number num = number_from_index(value);
CFDictionarySetValue((CFMutableDictionaryRef)dict(), keystr(), num());
CFBooleanRef boolref = value ? kCFBooleanTrue : kCFBooleanFalse;
CFDictionarySetValue(dict(), keystr(), boolref);
}

// append number to a mutable array
// append string to a mutable array

template <typename VALUE>
inline void array_append_str(MutableArray& array, const VALUE& value)
{
String valstr = string(value);
CFArrayAppendValue(array(), valstr());
}

// append a number to a mutable array

inline void array_append_int(MutableArray& array, int value)
{
Expand All @@ -201,25 +182,16 @@ namespace openvpn {
CFArrayAppendValue(array(), num());
}

inline void array_append_index(MutableArray& array, CFIndex value)
inline void array_append_long_long(MutableArray& array, long long value)
{
Number num = number_from_index(value);
Number num = number_from_long_long(value);
CFArrayAppendValue(array(), num());
}

// set a boolean in a mutable dictionary

inline void dict_set_bool(MutableDict& dict, CFStringRef key, bool value)
{
CFBooleanRef boolref = value ? kCFBooleanTrue : kCFBooleanFalse;
CFDictionarySetValue(dict(), key, boolref);
}

inline void dict_set_bool(MutableDict& dict, const char *key, bool value)
inline void array_append_index(MutableArray& array, CFIndex value)
{
String keystr = string(key);
CFBooleanRef boolref = value ? kCFBooleanTrue : kCFBooleanFalse;
CFDictionarySetValue(dict(), keystr(), boolref);
Number num = number_from_index(value);
CFArrayAppendValue(array(), num());
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions openvpn/applecrypto/ssl/sslctx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace openvpn {
struct Config
{
enum {
DEBUG = 1<<0,
SSL_DEBUG_FLAG = 1<<0,
};
typedef unsigned int Flags;

Expand All @@ -57,7 +57,7 @@ namespace openvpn {

void enable_debug()
{
flags |= DEBUG;
flags |= SSL_DEBUG_FLAG;
}

void load_identity(const std::string& subject_match)
Expand Down
Loading

0 comments on commit 7331ee3

Please sign in to comment.