Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

migrating

  • Loading branch information...
commit a9026f2dd4f6b7bf1063b8d7a69a11f4636775a3 1 parent ec7504c
geremy authored
View
195 c-libevent/README.md
@@ -1,193 +1,2 @@
-##### YOU MUST HAVE A PUBNUB ACCOUNT TO USE THE API.
-##### http://www.pubnub.com/account
-
-## PubNub 3.1 Real-time Cloud Push API - C-libevent
-
-www.pubnub.com - PubNub Real-time Push Service in the Cloud.
-
-PubNub is a Massively Scalable Real-time Service for Web and Mobile Games.
-This is a cloud-based service for broadcasting Real-time messages
-to thousands of web and mobile clients simultaneously.
-
-##### PubNub C-libevent Client API Boiler Plate
-
-
-##### Install following libraries
- libevent-2.0.18-stable
- openssl-0.9.8l
- json-c-0.9
-
- Add Pubnub.h to your project
- Include Pubnub.h to your class header
- #include<Pubnub.h>
-
- Make sure your class follow the Pubnub protocol
-
--------------------------------------------------------------------------------
-c-libevent : (Init)
--------------------------------------------------------------------------------
-```C
- //Initialize pubnub state
- Pubnub_overload1(
- "demo", // PUBLISH_KEY
- "demo", // SUBSCRIBE_KEY
- "", // SECRET_KEY
- "", // CIPHER_KEY (cipher key is optional)
- false // SSL_ON?
- );
-```
-
--------------------------------------------------------------------------------
-c-libevent : (Publish)
--------------------------------------------------------------------------------
-```C
- // Create callback function for publish
- static void publish_callback(json_object *obj)
- {
- printf("\n OUTPUT Message:::%s", json_object_to_json_string(obj));
- write(1, json_object_to_json_string(obj), strlen(json_object_to_json_string(obj)));
- }
- // WAS startup configuration
- #ifdef _WIN32
- WSADATA WSAData;
- WSAStartup(0x101, &WSAData);
- #else
- if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
- return (1);
- #endif
- // Initialize pubnub state
- Pubnub_overload1("demo","demo","","",false);
- // Create object of structure struct_publish
- struct_publish args = {
- .channel = "hello_world",
- .message="{\"Name\":\"Happy Birthday To You\"}",
- .cb= publish_callback,
- .type = 1
- };
- // Publish Message (string)
- publish( &args );
- // Publish json_object array
- json_object *my_array = json_object_new_array();
- json_object_array_add(my_array, json_object_new_string("hello"));
- struct struct_publish args2 = {
- .channel = "hello_world",
- .message = my_array,
- .cb = publish_callback,
- .type = 2
- };
- // Publish Message (array of json object)
- publish(&args2);
- // Publish json_object
- json_object * my_object = json_object_new_object();
- json_object_object_add(my_object, "some_val", json_object_new_string("hello"));
- struct struct_publish args1 = {
- .channel = "hello_world",
- .message = my_object,
- .cb = publish_callback,
- .type = 3
- };
- // Publish Message (json object)
- publish(&args1);
-```
-
--------------------------------------------------------------------------------
-c-libevent : (Subscribe)
--------------------------------------------------------------------------------
-```C
- // Create callback function for subscribe
- static void subscribe_callback(json_object *obj)
- {
- printf("\n OUTPUT Message:::%s", json_object_to_json_string(obj));
- write(1, json_object_to_json_string(obj), strlen(json_object_to_json_string(obj)));
- }
- // WAS startup configuration
- #ifdef _WIN32
- WSADATA WSAData;
- WSAStartup(0x101, &WSAData);
- #else
- if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
- return (1);
- #endif
- // Initialize pubnub state
- Pubnub_overload1("demo","demo","","",false);
- // Create object of structure struct_subscribe
- struct_subscribe args = {
- .channel = "hello_world",
- .cb= subscribe_callback
- };
- //call subscribe() method
- subscribe(&args);
-```
-
--------------------------------------------------------------------------------
-c-libevent : (History)
--------------------------------------------------------------------------------
-```C
- // Create callback function for history
- static void history_callback(json_object *obj)
- {
- printf("\n OUTPUT Message:::%s", json_object_to_json_string(obj));
- write(1, json_object_to_json_string(obj), strlen(json_object_to_json_string(obj)));
- }
- // WAS startup configuration
- #ifdef _WIN32
- WSADATA WSAData;
- WSAStartup(0x101, &WSAData);
- #else
- if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
- return (1);
- #endif
- // Initialize pubnub state
- Pubnub_overload1("demo","demo","demo","",false);
- // Create object of structure struct_history
- struct_history args = {
- .channel = "hello_world",
- .limit=2,
- .cb= history_callback
- };
- //call subscribe() method
- history(args);
-```
-
--------------------------------------------------------------------------------
-c-libevent : (Time)
--------------------------------------------------------------------------------
-```C
- //Get the time
- // WAS startup configuration
- #ifdef _WIN32
- WSADATA WSAData;
- WSAStartup(0x101, &WSAData);
- #else
- if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
- return (1);
- #endif
- // Initialize pubnub state
- Pubnub_overload1("demo","demo","","",false);
- // call gettime() function
- double time = getTime();
- // Display result
- printf("Time:::%lf",time);
-```
-
--------------------------------------------------------------------------------
-c-libevent : (UUID)
--------------------------------------------------------------------------------
-```C
- // Get UUID
- // WAS startup configuration
- #ifdef _WIN32
- WSADATA WSAData;
- WSAStartup(0x101, &WSAData);
- #else
- if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
- return (1);
- #endif
- // Initialize pubnub state
- Pubnub_overload1("demo","demo","","",false);
- // call uuid()
- char * str_uuid;
- str_uuid = uuid();
- // Display result
- printf("UUID:::%s",str_uuid);
-```
+##The latest PubNub for C/C++ files can be found at:
+https://github.com/pubnub/c
View
BIN  c-libevent/lib/json-c-0.9.tar.gz
Binary file not shown
View
BIN  c-libevent/lib/libevent-2.0.18-stable.tar.gz
Binary file not shown
View
BIN  c-libevent/lib/openssl-0.9.8l.tar.gz
Binary file not shown
View
73 c-libevent/pubnub/HTTPClient.c
@@ -1,73 +0,0 @@
-#include <errno.h>
-#include <stdlib.h>
-#include <string.h>
-#include <event2/event.h>
-#include <event2/buffer.h>
-#include <event2/http.h>
-#include <event2/http_struct.h>
-#include "Pubnub.h"
-
-void context_free(struct request_context *ctx) {
-
- evhttp_connection_free(ctx->cn);
- event_base_free(ctx->base);
-
- if (ctx->buffer)
- evbuffer_free(ctx->buffer);
-
- evhttp_uri_free(ctx->uri);
-
- free(ctx);
-}
-struct evbuffer *request_url(
- char *host,
- short port,
- char *url,
- void *callback
-
-) {
- struct request_context *ctx = 0;
- ctx = calloc(1, sizeof(*ctx));
- if (!ctx)
- return 0;
-
- ctx->uri = evhttp_uri_parse(host);
- if (!ctx->uri)
- return 0;
- printf("\n\n");
- ctx->base = event_base_new();
- if (!ctx->base)
- return 0;
-
- ctx->buffer = evbuffer_new();
-
- if (ctx->cn == NULL)
- {
- if (ctx->cn)
- evhttp_connection_free(ctx->cn);
- ctx->cn = evhttp_connection_base_new(ctx->base, NULL,
- evhttp_uri_get_host(ctx->uri),
- evhttp_uri_get_port(ctx->uri) != -1 ? evhttp_uri_get_port(ctx->uri) : 80);
-
- }
- ctx->req = evhttp_request_new(callback, ctx);
-
- evhttp_add_header(ctx->req->output_headers, "Host", evhttp_uri_get_host(ctx->uri));
- evhttp_add_header(ctx->req->output_headers,"V", "3.1");
- evhttp_add_header(ctx->req->output_headers,"User-Agent", "C-LibEvent");
- evhttp_add_header(ctx->req->output_headers,"Accept-Encoding", "gzip");
-
- evhttp_connection_set_timeout(ctx->cn,-1);
- evhttp_make_request(ctx->cn, ctx->req, EVHTTP_REQ_GET, url);
- event_base_loop(ctx->base ,EVLOOP_NONBLOCK);
- event_base_dispatch(ctx->base);
-
- struct evbuffer *retData = 0;
- if (ctx->ok)
- {
- retData = ctx->buffer;
- ctx->buffer = 0;
- }
- context_free(ctx);
- return retData;
-}
View
571 c-libevent/pubnub/Pubnub.c
@@ -1,571 +0,0 @@
-#include "Pubnub.h"
-
-PUBNUB pubnub;
-string timeToken = "0";
-char random[32];
-
-/* Function prototypes */
-void init(string, string, string, string, bool);
-void _requestPublish(string * url_components, int size,void (*callback1)(json_object *));
-void _requestHistory(string * url_components, int size);
-void _requestSubcribe(string * url_components, int size);
-double _requestTime(string * url_components, int size);
-string strconcat(string, string);
-string getHMacSHA256(string, string);
-void encode(unsigned char *, char *, char *);
-void encodeURL(char[], char[]);
-
-// Callback function
-static void _commonCallback(struct evhttp_request *req, void *arg);
-void (*callback)(json_object *);
-
-/* Added overloaded method to share cipher_key with cipher_key (plaintext)
- *
- * @param string Publish Key.
- * @param String Subscribe Key.
- * @param string Secret Key.
- * @param string Cipher Key.
- * @param bool SSL Enabled.
- */
-void Pubnub_overload1(string publish_key, string subscribe_key, string secret_key, string cipher_key, bool ssl_on) {
- init(publish_key, subscribe_key, secret_key, cipher_key, ssl_on);
-}
-
-/**
- * PubNub 3.0
- *
- * Prepare PubNub Class State.
- *
- * @param String Publish Key.
- * @param String Subscribe Key.
- * @param String Secret Key.
- * @param bool SSL Enabled.
- */
-void Pubnub_overload2(string publish_key, string subscribe_key, string secret_key, bool ssl_on) {
- init(publish_key, subscribe_key, secret_key, "", ssl_on);
-}
-
-/**
- * PubNub 3.0 without SSL
- *
- * Prepare PubNub Class State.
- *
- * @param string Publish Key.
- * @param string Subscribe Key.
- * @param string Secret Key.
- */
-void Pubnub_overload3(string publish_key, string subscribe_key, string secret_key) {
- init(publish_key, subscribe_key, secret_key, "", 0);
-}
-
-/**
- * PubNub 2.0 Compatibility
- *
- * Prepare PubNub Class State.
- *
- * @param string Publish Key.
- * @param string Subscribe Key.
- */
-void Pubnub_overload4(string publish_key, string subscribe_key) {
- init(publish_key, subscribe_key, "", "", false);
-}
-/**
- * Init
- *
- * Prepare PubNub Class State.
- *
- * @param string Publish Key.
- * @param string Subscribe Key.
- * @param string Secret Key.
- * @param string Cipher Key.
- * @param bool SSL Enabled.
- */
-void init(string publish_key, string subscribe_key, string secret_key, string cipher_key, bool ssl_on) {
- string abc;
- size_t old_size;
- pubnub.LIMIT = _LIMIT;
- pubnub.ORIGIN = _ORIGIN;
- pubnub.PUBLISH_KEY = publish_key;
- pubnub.SUBSCRIBE_KEY = subscribe_key;
- pubnub.SECRET_KEY = secret_key;
- pubnub.CIPHER_KEY = cipher_key;
- pubnub.SSL = ssl_on;
-
- // SSL On?
- if (pubnub.SSL) {
- abc = strconcat("https://", pubnub.ORIGIN);
- } else {
- abc = strconcat("http://", pubnub.ORIGIN);
- }
-
- old_size = strlen(abc);
- pubnub.ORIGIN = malloc(old_size + 1);
- strcpy(pubnub.ORIGIN, abc);
-}
-
-/**
- * Common_callback (static function)
- *
- * @param struct evhttp_request *req.
- * @param void *arg.
- */
-static void _commonCallback(struct evhttp_request *req, void *arg) {
- struct request_context *ctx = (struct request_context *) arg;
- struct evhttp_uri *new_uri = NULL;
- const char *new_location = NULL;
-
- if(req==NULL)
- {
- event_base_loopexit(ctx->base, 0);
- return;
- }
-
- // Response is ready
- switch (req->response_code) {
- case HTTP_OK:
- // Response received.
- event_base_loopexit(ctx->base, 0);
- break;
-
- case HTTP_MOVEPERM:
- case HTTP_MOVETEMP:
- new_location = evhttp_find_header(req->input_headers, "Location");
- if (!new_location)
- return;
-
- new_uri = evhttp_uri_parse(new_location);
- if (!new_uri)
- return;
-
- evhttp_uri_free(ctx->uri);
- ctx->uri = new_uri;
- return;
-
- default:
- // FAILURE
- event_base_loopexit(ctx->base, 0);
- return;
- }
-
- evbuffer_add_buffer(ctx->buffer, req->input_buffer);
- ctx->ok = 1;
-}
-
-void strip(char *s) {
- char *p2 = s;
- while(*s != '\0') {
- if(*s != '\t' && *s != '\n' && *s != '\\') {
- *p2++ = *s++;
- } else {
- ++s;
- }
- }
- *p2 = '\0';
-}
-
-/**
- * Publish
- *
- * Send a message to a channel.
- *
- * @param struct struct_publish *args.
- */
-void publish(struct struct_publish *args) {
- //callback = args->cb;
- string signature = "0";
- string url[7];
- string msg;
- if (args->type == 1) {
- if (strlen(pubnub.CIPHER_KEY) > 0) {
- char* string1=encryptString(pubnub.CIPHER_KEY,args->message);
- json_object *obj=json_object_new_string(string1);
- char* temp = json_object_get_string(obj);
- msg=strconcat("\"",temp);
- msg=strconcat(msg,"\"");
- }else{
- msg = json_object_get_string(json_object_new_string(args->message));
- msg=strconcat("\"",msg);
- msg=strconcat(msg,"\"");
- }
- } else if (args->type == 2) {
- if (strlen(pubnub.CIPHER_KEY) > 0) {
- json_object *obj =encryptJSONArray(pubnub.CIPHER_KEY,args->message);
- msg = json_object_get_string(obj);
- }else{
- msg = json_object_get_string(args->message);
- }
- } else if (args->type == 3) {
- if (strlen(pubnub.CIPHER_KEY) > 0) {
- json_object *obj =encryptJSONObject(pubnub.CIPHER_KEY,args->message);
- msg = json_object_get_string(obj);
- }else{
- msg = json_object_get_string(args->message);
- }
- }
-
- if (strlen(pubnub.SECRET_KEY) > 0) {
- // Generate String to Sign
- string string_to_sign;
- string_to_sign = strconcat(pubnub.PUBLISH_KEY, "/");
- string_to_sign = strconcat(string_to_sign, pubnub.SUBSCRIBE_KEY);
- string_to_sign = strconcat(string_to_sign, "/");
- string_to_sign = strconcat(string_to_sign, pubnub.SECRET_KEY);
- string_to_sign = strconcat(string_to_sign, "/");
- string_to_sign = strconcat(string_to_sign, args->channel);
- string_to_sign = strconcat(string_to_sign, "/");
- string_to_sign = strconcat(string_to_sign, msg);
- signature = getHMacSHA256(pubnub.SECRET_KEY, string_to_sign);
- }
-
- url[0] = "publish";
- url[1] = pubnub.PUBLISH_KEY;
- url[2] = pubnub.SUBSCRIBE_KEY;
- url[3] = signature;
- url[4] = args->channel;
- url[5] = "0";
- url[6] = msg;
-
- _requestPublish(url, 7,args->cb);
-}
-
-/**
- * Subscribe
- *
- * Listen for a message on a channel.
- *
- * @param struct struct_subscribe *args.
- */
-void subscribe(struct struct_subscribe *args) {
- callback = args->cb;
- string url[5];
- int j = 5;
- while (1) {
- j--;
- url[0] = "\subscribe";
- url[1] = pubnub.SUBSCRIBE_KEY;
- url[2] = args->channel;
- url[3] = "0";
- url[4] = timeToken;
-
- // Wait for Message
- _requestSubcribe(url, 5);
- }
-}
-/**
- * History
- *
- * Load history from a channel.
- *
- * @param struct struct_history *args.
- */
-void history(struct struct_history * args) {
- callback = args->cb;
- string url[5];
- url[0] = "history";
- url[1] = pubnub.SUBSCRIBE_KEY;
- url[2] = args->channel;
- url[3] = "0";
- char str[33];
- itoa(args->limit, str, 10);
- url[4] = str;
- return _requestHistory(url, 5);
-}
-
-/**
- * Time
- *
- * Timestamp from PubNub Cloud.
- *
- * @return double timestamp.
- */
-double getTime() {
- string url[2];
- url[0] = "time";
- url[1] = "0";
- return _requestTime(url, 2);
-}
-
-/**
- * UUID
- *
- * 32 digit UUID generation at client side.
- *
- * @return string uuid.
- */
-string uuid() {
- int count;
- unsigned short int length = 32;
- srand((unsigned int) time(0));
- count = 0;
- for (count = 0; count < length;) {
- random[count] = (rand() % 26) + 97;
- random[++count] = (rand() % 10) + 48;
- random[++count] = (rand() % 26) + 65;
- srand(rand());
- }
- random[length - 1] = '\0';
- return random;
-}
-
-/**
- * _requestPublish
- *
- * @param string* url_components.
- * @param int size.
- */
-void _requestPublish(string * url_components, int size,void (*callback1)(json_object *)) {
- string url;
- int i;
- url = "/";
- for (i = 0; i < size; i++) {
- char url2[strlen(url_components[i]) + 1];
- strcpy(url2, url_components[i]);
- url2[strlen(url_components[i])]='\0';
- char enc[sizeof(url2) * 3];
- encodeURL(url2,enc);
- const char*r=enc;
- url = strconcat(url,r);
- if(i != size-1)
- url=strconcat(url,"/");
- }
- struct evbuffer *data = request_url(pubnub.ORIGIN, 80, url, _commonCallback);
-
- if (data) {
- const char *joined1 = evbuffer_pullup(data, -1);
-
- json_object * obj = json_tokener_parse(joined1);
-
- if (callback1 != NULL)
- {
- callback1(json_object_get(obj));
- }
- evbuffer_free(data);
-
- }
-}
-
-/**
- * _requestSubcribe
- *
- * @param string* url_components.
- * @param int size.
- */
-void _requestSubcribe(string * url_components, int size) {
- string url;
- int i;
- url = "/";
- for (i = 0; i < size; i++) {
- char url2[strlen(url_components[i]) + 1];
- strcpy(url2, url_components[i]);
- url2[strlen(url_components[i])]='\0';
- char enc[sizeof(url2) * 3];
- encodeURL(url2,enc);
- const char* r=enc;
- url = strconcat(url,r);
- if(i != size-1)
- url=strconcat(url,"/");
- }
-
- struct evbuffer *data = request_url(pubnub.ORIGIN, 80, url, _commonCallback);
- if (data) {
- const char * joined1 = evbuffer_pullup(data, evbuffer_get_length(data));
- json_object * obj = json_tokener_parse(joined1);
- json_object * timeT = json_object_array_get_idx(obj, 1);
- json_object * meg = json_object_array_get_idx(obj, 0);
- char * time = json_object_to_json_string(timeT);
- char s1[strlen(time) - 1];int
- i = 0, j = 0;
- for (i = 0; i <= strlen(time); i++) {
- if (i == 0 || i == strlen(time) - 1) {
- } else {
- s1[j] = time[i];
- j++;
- }
- }
- s1[strlen(time)] = '\0';
- timeToken = malloc(sizeof(char) * strlen(s1));
- timeToken = strconcat(s1, "");
- json_object * encrypted = json_object_new_object();
- if(strlen(pubnub.CIPHER_KEY) > 0)
- {
- encrypted = decrypt(pubnub.CIPHER_KEY,meg);
- }else
- {
- encrypted=meg;
- }
- if (callback != NULL)
- {
- callback(encrypted);
- }
- evbuffer_free(data);
- }
-}
-
-/**
- * _requestHistory
- *
- * @param string * url_components.
- * @param int size.
- */
-void _requestHistory(string * url_components, int size) {
- string url;
- int i;
- url = "/";
- for (i = 0; i < size; i++) {
- char url2[strlen(url_components[i]) + 1];
- strcpy(url2, url_components[i]);
- url2[strlen(url_components[i])]='\0';
- char enc[sizeof(url2) * 3];
- encodeURL(url2,enc);
- const char*r=enc;
- url = strconcat(url,r);
- if(i != size-1)
- url=strconcat(url,"/");
- }
- struct evbuffer *data = request_url(pubnub.ORIGIN, 80, url, _commonCallback);
-
- if (data) {
- const char *joined1 = evbuffer_pullup(data, -1);
- json_object * obj = json_tokener_parse(joined1);
-
- if(strlen(pubnub.CIPHER_KEY) > 0)
- {
- obj = decryptHistry(pubnub.CIPHER_KEY,obj);
- }
-
- if (callback != NULL)
- {
- callback(obj);
- }
- evbuffer_free(data);
- }
-}
-
-/**
- * _requestTime
- *
- * @param string * url_components.
- * @param int size.
- */
-double _requestTime(string * url_components, int size) {
- double retVAl;
- string url;
- int i;
- url = "/";
- for (i = 0; i < size; i++) {
- char url2[strlen(url_components[i]) + 1];
- strcpy(url2, url_components[i]);
- url2[strlen(url_components[i])]='\0';
- char enc[sizeof(url2) * 3];
- encodeURL(url2,enc);
-
- const char*r=enc;
- url = strconcat(url,r);
- if(i != size-1)
- url=strconcat(url,"/");
- }
- struct evbuffer *data = request_url(pubnub.ORIGIN, 80, url, _commonCallback);
- if (data) {
- const char *joined1 = evbuffer_pullup(data, evbuffer_get_length(data));
- json_object * obj = json_object_new_string_len(joined1, evbuffer_get_length(data));
- char * tim = json_object_get_string(obj);
- tim++;
- tim[strlen(tim) - 1] = 0;
- retVAl = atof(tim);
- evbuffer_free(data);
- }
- return retVAl;
-}
-
-/**
- * getHMacSHA256
- *
- * @param string secret_key.
- * @param string input.
- */
-char* getHMacSHA256(string secret_key,string input)
-{
- unsigned char* key = (unsigned char*) secret_key;
- unsigned char* data = (unsigned char*) input;
- unsigned char* result;
- unsigned int result_len = 32;
- // static char res_hexstring[32];
- int i=0;
- char* sig=(unsigned char*) malloc(sizeof(char) * result_len);
-
- HMAC_CTX ctx;
-
- result = (unsigned char*) malloc(sizeof(char) * result_len);
-
- ENGINE_load_builtin_engines();
- ENGINE_register_all_complete();
-
- HMAC_CTX_init(&ctx);
- HMAC_Init_ex(&ctx, key, 16, EVP_sha256(), NULL);
- HMAC_Update(&ctx, data, 8);
- HMAC_Final(&ctx, result, &result_len);
- HMAC_CTX_cleanup(&ctx);
- for (i = 0; i < result_len; i++)
- {
- sprintf(&(sig[i*2]), "%02x", result[i]);
- }
- return sig;
-}
-
-char rfc3986[256] = { 0 };
-char html5[256] = { 0 };
-
-/**
- * encode
- *
- * @param unsigned char *s
- * @param char *enc.
- * @param char *tb.
- */
-void encode(unsigned char *s, char *enc, char *tb) {
- for (; *s; s++) {
- if (tb[*s])
- sprintf(enc, "%c", tb[*s]);
- else
- sprintf(enc, "%%%02X", *s);
- while (*++enc)
- ;
- }
-}
-
-/**
- * encodeURL
- *
- * @param char url[].
- * @param char enc[].
- */
-void encodeURL(char url[], char enc[]) {
- int i;
- for (i = 0; i < 256; i++) {
- rfc3986[i] =
- isalnum(i) || i == '~' || i == '-' || i == '.' || i == '_' ?
- i : 0;
- html5[i] =
- isalnum(i) || i == '*' || i == '-' || i == '.' || i == '_' ? i :
- (i == ' ') ? '+' : 0;
- }
- encode(url, enc, rfc3986);
-}
-
-/**
- * strconcat
- *
- * @param string s1.
- * @param string s2.
- */
-string strconcat(string s1, string s2) {
-
- size_t old_size;
- string t;
- old_size = strlen(s1);
- t = malloc(old_size + strlen(s2) + 1);
- strcpy(t, s1);
-
- strcpy(t + old_size, s2);
- return t;
-}
View
131 c-libevent/pubnub/Pubnub.h
@@ -1,131 +0,0 @@
-/*
- * Pubnub.h
- */
-#ifndef PUBNUB_H_
-#define PUBNUB_H_
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-
-#include <openssl/engine.h>
-#include <openssl/hmac.h>
-#include <openssl/evp.h>
-#include <openssl/rand.h>
-
-#include <event2/event.h>
-#include <event2/buffer.h>
-#include <event2/http.h>
-#include <event2/http_struct.h>
-
-#include "json.h"
-
-//Constant declarations
-#define _LIMIT 1800;
-#define _ORIGIN "pubsub.pubnub.com";
-
-//Type definitions for char * and true/false for SSL
-typedef char * string;
-typedef enum {true=1,false=0} bool;
-
-typedef enum object_type {
- String=1,
- Array=2,
- JSON_Object=3
-}object_type;
-
-
-/**
- * Structure for publish message
- *
- * @param string channel.
- * @param string message.it may be string or json_object
- * @param void (* cb)(json_object *). //callback
- */
-
-typedef struct struct_publish {
- string channel;
- void* message;
- void (* cb)(json_object *);
- enum object_type type;
-}struct_publish;
-
-/**
- * Structure for subscribe message
- *
- * @param string channel.
- * @param void (* cb)(json_object *). //callback
- */
-
-typedef struct struct_subscribe {
- string channel;
- void (* cb)(json_object *);
-}struct_subscribe;
-
-
-/**
- * Structure for history
- *
- * @param string channel.
- * @param int limit.
- * @param void (* cb)(json_object *). //callback
- */
-
-typedef struct struct_history {
- char* channel;
- int limit;
- void (*cb)(json_object *);
-}struct_history;
-
-/**
- * Structure for contex
- *
- * @param struct evhttp_uri *uri;
- * @param struct event_base *base.
- * @param struct evhttp_connection *cn.
- * @param struct evhttp_request *req.
- * @param struct evbuffer *buffer.
- * @param int ok.
- */
-typedef struct request_context {
- struct evhttp_uri *uri;
- struct event_base *base;
- struct evhttp_connection *cn;
- struct evhttp_request *req;
- struct evbuffer *buffer;
- int ok;
-}request_context;
-
-/**
- * Structure to share cipher_key
- * With cipher_key (plaintext)
- *
- * @param string Publish Key.
- * @param string Subscribe Key.
- * @param string Secret Key.
- * @param string Cipher Key.
- * @param bool SSL Enabled.
- */
-
-typedef struct Pubnub {
- string ORIGIN;
- string PUBLISH_KEY;
- string SUBSCRIBE_KEY;
- string SECRET_KEY;
- string CIPHER_KEY;
- int LIMIT;
- bool SSL;
-}PUBNUB;
-
-// Method declarations
-void Pubnub_overload1 (string publish_key,string subscribe_key,string secret_key,string cipher_key,bool ssl_on);
-void Pubnub_overload2 (string publish_key,string subscribe_key,string secret_key,bool ssl_on);
-void Pubnub_overload3 (string publish_key,string subscribe_key,string secret_key );
-void Pubnub_overload4 (string publish_key,string subscribe_key);
-void publish (struct_publish *);
-void subscribe (struct_subscribe *);
-void history (struct_history *);
-double getTime ();
-string uuid ();
-
-#endif
View
287 c-libevent/pubnub/PubnubCrypto.c
@@ -1,287 +0,0 @@
-#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <openssl/evp.h>
-#include <openssl/bio.h>
-#include <openssl/buffer.h>
-#include "json.h"
-#include "json_object.h"
-#include <stddef.h>
-#include "openssl/md5.h"
-
-#define json_object_object_foreach(obj,key,val) \
- char *key; struct json_object *val; struct lh_entry *entry;\
- for(entry = json_object_get_object(obj)->head; ({ if(entry) { key = (char*)entry->k; val = (struct json_object*)entry->v; } ; entry; }); entry = entry->next )
-
-#define BUFFFERLEN 5000
-unsigned char md5Result[MD5_DIGEST_LENGTH];
-
-char* base64(const unsigned char* input, int length) {
- BIO *bmem, *b64;
- BUF_MEM *bptr;
-
- b64 = BIO_new(BIO_f_base64());
- bmem = BIO_new(BIO_s_mem());
- b64 = BIO_push(b64, bmem);
- BIO_write(b64, input, length);
- BIO_flush(b64);
- BIO_get_mem_ptr(b64, &bptr);
- char *buff = (char *) malloc(bptr->length + 1);
-
- memcpy(buff, bptr->data, bptr->length);
- buff[bptr->length] = 0;
-
- BIO_free_all(b64);
- return buff;
-}
-
-char* unbase64(unsigned char* input, int length) {
- BIO *b64, *bmem;
- unsigned char *buffer = (unsigned char *) malloc(512);
- memset(buffer, 0, length);
-
- b64 = BIO_new(BIO_f_base64());
- bmem = BIO_new_mem_buf(input, length);
- bmem = BIO_push(b64, bmem);
- BIO_read(bmem, buffer, length);
-
- BIO_free_all(bmem);
- return buffer;
-}
-
-void getMD5(char* passward, unsigned char* returnVal) //returnVal must be global
-{
- MD5((unsigned char*) passward, sizeof(passward), returnVal);
-}
-
-char* substring(const char* str, size_t begin, size_t len) {
- if (str == 0 || strlen(str) == 0 || strlen(str) < begin
- || strlen(str) < (begin + len))
- return 0;
-
- return strndup(str + begin, len);
-}
-
-char* rtrim(char *s) {
- char* back = s + strlen(s);
- while (isspace(*--back))
- ;
- *(back + 1) = '\0';
- return s;
-}
-
-char* getStringForDecrypt(char* input) {
- char* temp = malloc(strlen(input));
- strcpy(temp, input);
-
- int len = strlen(temp);
- if (len > 0)
- temp++; //Go past the first char
- //input[len - 4]='\n';
- if (len > 1)
- temp[len - 2] = '\0'; //Replace the last char with a null termination
-
- return temp;
-}
-
-char* encryptString(unsigned char* key, char* input) {
- unsigned char* iv = "0123456789012345";
- getMD5(key, md5Result);
- char* string_to_encrypt = input;
- int in_len = strlen(input);
- int dev = in_len % 8;
- int ans = dev;
- string_to_encrypt = malloc(ans + in_len + 1);
- int i;
- for (i = 0; i < in_len; i++) {
- string_to_encrypt[i] = input[i];
- }
- int j = in_len;
- for (i = 0; i < ans; i++) {
- string_to_encrypt[j] = ' ';
- j++;
- }
- string_to_encrypt[ans + in_len] = '\0';
-
- EVP_CIPHER_CTX en;
- EVP_CIPHER_CTX_init(&en);
- const EVP_CIPHER *cipher_type;
- unsigned char *ciphertext = NULL;
- int input_len = 0;
- cipher_type = EVP_aes_128_cbc();
- EVP_EncryptInit_ex(&en, cipher_type, NULL, md5Result, iv);
- static const int MAX_PADDING_LEN = 16;
- input_len = strlen(string_to_encrypt) + 1;
- ciphertext = (unsigned char *) malloc(input_len + MAX_PADDING_LEN);
-
- if (!EVP_EncryptInit_ex(&en, NULL, NULL, NULL, NULL)) {
- return "ERROR in EVP_EncryptInit_ex";
- }
-
- int bytes_written = 0;
- if (!EVP_EncryptUpdate(&en, ciphertext, &bytes_written,
- (unsigned char *) string_to_encrypt, input_len)) {
- return "ERROR in EVP_EncryptUpdate";
- }
- if (!EVP_EncryptFinal_ex(&en, ciphertext + bytes_written, &bytes_written)) {
- return "ERROR in EVP_EncryptFinal_ex";
- }
- EVP_CIPHER_CTX_cleanup(&en);
-
- char* base64_cipher = base64(ciphertext, strlen(ciphertext));
- strip(base64_cipher);
- return base64_cipher;
-}
-
-char* decryptString(unsigned char* key, char* encrypted) {
- unsigned char* plaintext = NULL;
- unsigned char* iv = "0123456789012345";
- char* ciphertext = malloc(strlen(encrypted));
- strip(encrypted);
- encrypted = strconcat(encrypted, "\n");
- ciphertext = unbase64(encrypted, strlen(encrypted));
- getMD5(key, md5Result);
- int bytes_written = 0;
- EVP_CIPHER_CTX de;
- EVP_CIPHER_CTX_init(&de);
- const EVP_CIPHER *cipher_type;
- cipher_type = EVP_aes_128_cbc();
- EVP_DecryptInit_ex(&de, cipher_type, NULL, md5Result, iv);
- plaintext = (unsigned char *) malloc(strlen(ciphertext));
- if (!EVP_DecryptInit_ex(&de, NULL, NULL, NULL, NULL)) {
- return "ERROR in EVP_DecryptInit_ex";
- }
-
- int plaintext_len = 0;
- if (!EVP_DecryptUpdate(&de, plaintext, &bytes_written, ciphertext,
- strlen(ciphertext))) {
- return "ERROR in EVP_DecryptUpdate";
- }
- plaintext_len += bytes_written;
-
- if (!EVP_DecryptFinal_ex(&de, plaintext + bytes_written, &bytes_written)) {
- return "ERROR in EVP_DecryptFinal_ex";
- }
- plaintext_len += bytes_written;
-
- EVP_CIPHER_CTX_cleanup(&de);
-
- char *ret;
- if (plaintext_len < strlen(plaintext)) {
- ret = substring(plaintext, 0, plaintext_len);
- } else {
- ret = plaintext;
- }
- return rtrim(ret);
-}
-
-json_object* encryptJSONObject(unsigned char* key, json_object* input) {
- json_object * my_object = json_object_new_object();
-
- json_object_object_foreach(input, jkey, jval) {
- char* encrypt = encryptString((unsigned char*) key,
- json_object_to_json_string(jval));
- json_object_object_add(my_object, jkey,
- json_object_new_string(encrypt));
- }
- return my_object;
-}
-
-json_object* decryptJSONObject(unsigned char* key, json_object* input) {
- json_object * my_object = json_object_new_object();
-
- json_object_object_foreach(input, jkey, jval) {
- char* temp = getStringForDecrypt(json_object_to_json_string(jval));
- char* decrypt = decryptString(key, temp); // getAllButFirstAndLast(json_object_to_json_string(val)));
- json_object_object_add(my_object, jkey,
- json_object_new_string(decrypt));
- }
- return my_object;
-}
-
-json_object* encryptJSONArray(unsigned char* key, json_object* input) {
- json_object * my_object = json_object_new_array();
- int i = 0;
-
- for (i = 0; i < json_object_array_length(input); i++) {
- json_object *obj = json_object_array_get_idx(input, i);
- char* encrypt = encryptString((unsigned char*) key,
- json_object_to_json_string(obj));
- json_object_array_add(my_object, json_object_new_string(encrypt));
- }
- return my_object;
-}
-
-json_object* decryptJSONArray(unsigned char* key, json_object* input) {
- json_object * my_object = json_object_new_array();
- int i = 0;
-
- for (i = 0; i < json_object_array_length(input); i++) {
- json_object *obj = json_object_array_get_idx(input, i);
- char* temp = getStringForDecrypt(json_object_to_json_string(obj));
- char* encrypt = decryptString(key, temp);
- json_object_array_add(my_object, json_object_new_string(encrypt));
- }
- return my_object;
-}
-
-json_object* decrypt(char* key, json_object* msg) {
- enum json_type type;
- json_object* encrypted = json_object_new_object();
- json_object* newMessage;
-
- if (json_object_array_length(msg) > 0) {
- newMessage = json_object_array_get_idx(msg, 0);
- char * encrypt;
- char* temp;
- type = json_object_get_type(newMessage);
- switch (type) {
-
- case json_type_object:
- encrypted = decryptJSONObject(key, newMessage);
- break;
- case json_type_array:
- encrypted = decryptJSONArray(key, newMessage);
- break;
- case json_type_string:
- temp = getStringForDecrypt(json_object_to_json_string(newMessage));
- char* test = strconcat(temp, "");
- encrypt = decryptString(key, test);
- encrypted = json_object_new_string(encrypt);
- break;
- }
- }
- return encrypted;
-}
-
-json_object* decryptHistry(char* key, json_object* msg) {
- int i = 0;
- char* temp;
- char* encrypt;
- json_object* my_array = json_object_new_array();
- json_object* encrypted = json_object_new_object();
-
- for (i = 0; i < json_object_array_length(msg); i++) {
- json_object *obj = json_object_array_get_idx(msg, i);
-
- enum json_type type;
- type = json_object_get_type(obj);
- switch (type) {
-
- case json_type_object:
- encrypted = decryptJSONObject(key, obj);
- break;
- case json_type_array:
- encrypted = decryptJSONArray(key, obj);
- break;
-// case json_type_string:
-// temp = getStringForDecrypt(json_object_to_json_string(obj));
-// char* test = strconcat(temp, "");
-// encrypt = decryptString(key, test);
-// encrypted = json_object_new_string(encrypt);
-// break;
- }
- json_object_array_add(my_array, encrypted);
- }
- return my_array;
-}
View
38 c-libevent/tests/HistoryExample.c
@@ -1,38 +0,0 @@
-/*
- *
- * Example of how to use history function of this library
- *
- * */
-
-#include<stdio.h>
-#include <string.h>
-#include "../pubnub/Pubnub.h"
-
-// create callback method used in history structure
-
-static void history_callback(json_object *obj) {
- printf("\n Message(s)::%s", json_object_to_json_string(obj));
- write(1, json_object_to_json_string(obj),
- strlen(json_object_to_json_string(obj)));
-}
-
-int main() {
-
-#ifdef _WIN32
- WSADATA WSAData;
- WSAStartup(0x101, &WSAData);
-#else
- if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
- return (1);
-#endif
- // create a structure instance to pass as an argument to history function
- struct struct_history args = { .channel = "hello_world", .limit = 2, .cb = history_callback };
-
- // initialize Pubnub state
- Pubnub_overload1("demo", "demo", "demo", "", false);//[Cipher key is Optional]
-
- // call history function
- history(&args);
-
- return 0;
-}
View
43 c-libevent/tests/PublishExample.c
@@ -1,43 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-#include "../pubnub/Pubnub.h"
-#include "json.h"
-
-
-static void publish_callback_json_object(json_object *obj) {
- printf("\n%s",json_object_to_json_string(obj));
-}
-
-static void publish_callback_json_array(json_object *obj) {
- printf("\n%s",json_object_to_json_string(obj));
-}
-
-int main() {
-#ifdef _WIN32
- WSADATA WSAData;
- WSAStartup(0x101, &WSAData);
-#else
- if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
- return (1);
-#endif
-
- // initialize Pubnub state
- Pubnub_overload1("demo", "demo", "demo", "", true);//[Cipher key is Optional]
-
-
- // publish json_object
- json_object * my_object = json_object_new_object();
- json_object_object_add(my_object, "some_val", json_object_new_string("Hello"));
- struct struct_publish args1 = { .channel = "hello_world", .message = my_object,
- .cb = publish_callback_json_object, .type = 3 };
- publish(&args1);
-
- // publish json_object array
- json_object *my_array = json_object_new_array();
- json_object_array_add(my_array, json_object_new_string("hello"));
- struct struct_publish args2 = { .channel = "hello_world1", .message = my_array, .cb = publish_callback_json_array, .type = 2 };
- //publish(&args2);
-
- //getch();
- return 0;
-}
View
29 c-libevent/tests/SubscribeExample.c
@@ -1,29 +0,0 @@
-#include<stdio.h>
-#include <string.h>
-#include "../pubnub/Pubnub.h"
-
-static void subscribe_callback(json_object *obj) {
- printf("\n Message::%s", json_object_to_json_string(obj));
- string str = strconcat(json_object_to_json_string(obj), "\n");
- write(1, str, strlen(str));
-}
-
-int main() {
-
-#ifdef _WIN32
- WSADATA WSAData;
- WSAStartup(0x101, &WSAData);
-#else
- if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
- return (1);
-#endif
-
- struct struct_subscribe args = { .channel = "hello_world", .cb = subscribe_callback };
- // initialize Pubnub state
- Pubnub_overload1("demo", "demo", "demo", "", true);//[Cipher key is Optional]
-
- // call subscribe function
- subscribe(&args);
-
- return 0;
-}
View
23 c-libevent/tests/TimeExample.c
@@ -1,23 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-#include "../pubnub/Pubnub.h"
-
-int main() {
-
-#ifdef _WIN32
- WSADATA WSAData;
- WSAStartup(0x101, &WSAData);
-#else
- if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
- return (1);
-#endif
-
- // initialize Pubnub state
- Pubnub_overload1("demo", "demo", "", "", false);//[Cipher key is Optional]
-
- double dob = getTime();
- printf("Time:::%lf", dob);
-
- return 0;
-}
-
View
21 c-libevent/tests/UUIDExample.c
@@ -1,21 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-#include "../pubnub/Pubnub.h"
-
-int main() {
-
-#ifdef _WIN32
- WSADATA WSAData;
- WSAStartup(0x101, &WSAData);
-#else
- if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
- return (1);
-#endif
-
- // initialize Pubnub state
- Pubnub_overload1("demo", "demo", "", "", false);//[Cipher key is Optional]
-
- printf("UUID:::%s", uuid());
-
- return 0;
-}
Please sign in to comment.
Something went wrong with that request. Please try again.