Beispiel #1
0
PTR_WEIBO_ENTITY show_multiple_weibo_byids(const char* access_token, char** weibo_ids, uint32_t count)
{
  const char* func_name = __func__;
  debug_log_enter(FINE, func_name, "spd", access_token, weibo_ids, count);
  char s[2048] = {};
  uint32_t i = 0;
  uint32_t cnt = count>MAX_BATCH_SIZE?MAX_BATCH_SIZE:count;
  cJSON* root = NULL;
  cJSON* statuses = NULL;
  PTR_WEIBO_ENTITY list_head = NULL;
  PTR_WEIBO_ENTITY weibo = NULL;
  PTR_HTTP_REQUEST request = alloc_http_request(2, 0, 0, 0);
  PTR_HTTP_RESPONSE response = NULL;

  memset(s, 0, sizeof(char)*sizeof(s));
  sprintf(s, "%s", weibo_ids[0]);
  for (i=1; i<cnt; i++) {
    sprintf(s, "%s,%s", s, weibo_ids[i]);
  }
  s[strlen(s)] = '\0';

  request->params[0].name = "access_token";
  request->params[0].value = access_token;
  request->params[1].name = "ids";
  request->params[1].value = s;

  response = https_get(WEIBO_SHOW_WEIBO_BATCH_URL, request);
  if (response->status_code != 200) {
    free_http_request(request);
    free_http_response(response);
    return NULL;
  }

  root = cJSON_Parse((char*)(response->body));
  if (check_api_error(root)) {
    free_http_request(request);
    free_http_response(response);
    cJSON_Delete(root);
    return NULL;
  }

  statuses = cJSON_GetObjectItem(root, "statuses");
  cnt = cJSON_GetArraySize(statuses);

  for (i=0; i<cnt; i++) {
    weibo = create_weibo_entity_from_json(cJSON_GetArrayItem(statuses, i));
    weibo->next = list_head;
    weibo->prev = NULL;
    if (list_head != NULL) {
      list_head->prev = weibo;
    }
    list_head = weibo;
  }

  free_http_request(request);
  free_http_response(response);
  cJSON_Delete(root);
  debug_log_exit(FINE, func_name);
  return list_head;
}
Beispiel #2
0
PTR_WEIBO_ENTITY show_single_weibo_byid(const char* access_token, const char* weibo_id)
{
  const char* func_name = __func__;
  debug_log_enter(FINE, func_name, "ss", access_token, weibo_id);
  char s[20] = {};
  cJSON* root = NULL;
  PTR_WEIBO_ENTITY weibo = NULL;
  PTR_HTTP_REQUEST request = alloc_http_request(2, 0, 0, 0);
  PTR_HTTP_RESPONSE response = NULL;
  request->params[0].name = "access_token";
  request->params[0].value = access_token;
  request->params[1].name = "id";
  request->params[1].value = weibo_id;

  response = https_get(WEIBO_SHOW_WEIBO_URL, request);
  if (response->status_code != 200) {
    free_http_request(request);
    free_http_response(response);
    return NULL;
  }

  root = cJSON_Parse((char*)(response->body));
  if (check_api_error(root)) {
    free_http_request(request);
    free_http_response(response);
    cJSON_Delete(root);
    return NULL;
  }
  weibo = create_weibo_entity_from_json(root);
  free_http_request(request);
  free_http_response(response);
  cJSON_Delete(root);
  debug_log_exit(FINE, func_name);
  return weibo;
}
Beispiel #3
0
PTR_WEIBO_ENTITY get_user_timeline(const char* access_token, const char* uid, int page)
{
  const char* func_name = __func__;
  debug_log_enter(FINE, func_name, "sd", access_token, page);
  char s[20] = {};
  int i=0, cnt = 0;
  cJSON* root = NULL;
  cJSON* statuses = NULL;
  PTR_WEIBO_ENTITY list_head = NULL;
  PTR_WEIBO_ENTITY weibo = NULL;
  PTR_HTTP_REQUEST request = alloc_http_request(3, 0, 0, 0);
  PTR_HTTP_RESPONSE response = NULL;
  request->params[0].name = "access_token";
  request->params[0].value = access_token;
  request->params[1].name = "page";
  snprintf(s, 20, "%d", page);
  request->params[1].value = s;
  request->params[2].name = "uid";
  request->params[2].value = uid;

  response = https_get(WEIBO_GET_USER_TIMELINE_URL, request);
  if (response->status_code != 200) {
    free_http_request(request);
    free_http_response(response);
    return NULL;
  }

  root = cJSON_Parse((char*)(response->body));
  if (check_api_error(root)) {
    free_http_request(request);
    free_http_response(response);
    cJSON_Delete(root);
    return NULL;
  }

  statuses = cJSON_GetObjectItem(root, "statuses");
  cnt = cJSON_GetArraySize(statuses);

  for (i=0; i<cnt; i++) {
    weibo = create_weibo_entity_from_json(cJSON_GetArrayItem(statuses, i));
    weibo->next = list_head;
    weibo->prev = NULL;
    if (list_head != NULL) {
      list_head->prev = weibo;
    }
    list_head = weibo;
  }

  free_http_request(request);
  free_http_response(response);
  cJSON_Delete(root);
  debug_log_exit(FINE, func_name);
  return list_head;
}
Beispiel #4
0
PTR_WEIBO_ENTITY get_user_timeline_byname(const char* access_token, const char* name, int page)
{
  const char* func_name = __func__;
  debug_log_enter(FINE, func_name, "ssd", access_token, name, page);
  char s[20] = {};
  char *weibo_id = NULL;
  int i = 0, cnt = 0;
  cJSON* root = NULL;
  PTR_WEIBO_ENTITY list_head = NULL;
  PTR_WEIBO_ENTITY weibo = NULL;
  PTR_HTTP_REQUEST request = alloc_http_request(4, 0, 0, 0);
  PTR_HTTP_RESPONSE response = NULL;
  request->params[0].name = "access_token";
  request->params[0].value = access_token;
  request->params[1].name = "name";
  request->params[1].value = name;
  snprintf(s, 20, "%d", page);
  request->params[2].name="page";
  request->params[2].value=s;
  request->params[3].name="count";
  request->params[3].value="5"; /* hard code as 5 for api invocation limit */

  response = https_get(WEIBO_GET_USER_TIMELINE_IDS_URL, request);
  if (response->status_code != 200) {
    free_http_request(request);
    free_http_response(response);
    return NULL;
  }

  root = cJSON_Parse((char*)(response->body));
  if (check_api_error(root)) {
    free_http_request(request);
    free_http_response(response);
    cJSON_Delete(root);
    return NULL;
  }
  cnt = cJSON_GetArraySize(cJSON_GetObjectItem(root, "statuses"));
  for (i=0; i<cnt; i++) {
    weibo_id = (cJSON_GetArrayItem(cJSON_GetObjectItem(root, "statuses"), i))->valuestring;
    weibo = show_single_weibo_byid(access_token, weibo_id);
    weibo->next = list_head;
    weibo->prev = NULL;
    if (list_head != NULL) {
      list_head->prev = weibo;
    }
    list_head = weibo;
  }
  free_http_request(request);
  free_http_response(response);
  cJSON_Delete(root);
  debug_log_exit(FINE, func_name);
  return list_head;
}
Beispiel #5
0
PTR_WEIBO_ENTITY get_user_timeline_byids(const char* access_token, const char* uid, int page)
{
  const char* func_name = __func__;
  debug_log_enter(FINE, func_name, "ssd", access_token, uid, page);
  char s[20] = {};
  char **weibo_ids = NULL;
  int i = 0, cnt = 0;
  cJSON* root = NULL;
  PTR_WEIBO_ENTITY list_head = NULL;
  PTR_WEIBO_ENTITY weibo = NULL;
  PTR_HTTP_REQUEST request = alloc_http_request(3, 0, 0, 0);
  PTR_HTTP_RESPONSE response = NULL;
  request->params[0].name = "access_token";
  request->params[0].value = access_token;
  request->params[1].name = "uid";
  request->params[1].value = uid;
  snprintf(s, 20, "%d", page);
  request->params[2].name="page";
  request->params[2].value=s;

  response = https_get(WEIBO_GET_USER_TIMELINE_IDS_URL, request);
  if (response->status_code != 200) {
    free_http_request(request);
    free_http_response(response);
    return NULL;
  }

  root = cJSON_Parse((char*)(response->body));
  if (check_api_error(root)) {
    free_http_request(request);
    free_http_response(response);
    cJSON_Delete(root);
    return NULL;
  }
  cnt = cJSON_GetArraySize(cJSON_GetObjectItem(root, "statuses"));
  weibo_ids = (char**) malloc(sizeof(char*)*cnt);
  for (i=0; i<cnt; i++) {
    weibo_ids[i] = (cJSON_GetArrayItem(cJSON_GetObjectItem(root, "statuses"), i))->valuestring;
  }
  list_head = show_multiple_weibo_byids(access_token, weibo_ids, cnt);
  free_http_request(request);
  free_http_response(response);
  free(weibo_ids);
  cJSON_Delete(root);
  debug_log_exit(FINE, func_name);
  return list_head;
}
Beispiel #6
0
/** Initiates a transaction with the auth server, either to authenticate or to
 * update the traffic counters at the server
@param authresponse Returns the information given by the central server 
@param request_type Use the REQUEST_TYPE_* defines in centralserver.h
@param ip IP adress of the client this request is related to
@param mac MAC adress of the client this request is related to
@param token Authentification token of the client
@param incoming Current counter of the client's total incoming traffic, in bytes 
@param outgoing Current counter of the client's total outgoing traffic, in bytes 
*/
t_authcode
auth_server_request(t_authresponse * authresponse, const char *request_type, const char *ip, const char *mac,
                    const char *token, unsigned long long int incoming, unsigned long long int outgoing)
{
    int sockfd;
    char buf[MAX_BUF];
    char *tmp;
    char *safe_token;
    t_auth_serv *auth_server = NULL;
    auth_server = get_auth_server();

    /* Blanket default is error. */
    authresponse->authcode = AUTH_ERROR;

    sockfd = connect_auth_server();

        /**
	 * TODO: XXX change the PHP so we can harmonize stage as request_type
	 * everywhere.
	 */
    memset(buf, 0, sizeof(buf));
    safe_token = httpdUrlEncode(token);
    snprintf(buf, (sizeof(buf) - 1),
             "GET %s%sstage=%s&ip=%s&mac=%s&token=%s&incoming=%llu&outgoing=%llu&gw_id=%s HTTP/1.0\r\n"
             "User-Agent: WiFiDog %s\r\n"
             "Host: %s\r\n"
             "\r\n",
             auth_server->authserv_path,
             auth_server->authserv_auth_script_path_fragment,
             request_type,
             ip,
             mac, safe_token, incoming, outgoing, config_get_config()->gw_id, VERSION, auth_server->authserv_hostname);

    free(safe_token);

    char *res;
#ifdef USE_CYASSL
    if (auth_server->authserv_use_ssl) {
        res = https_get(sockfd, buf, auth_server->authserv_hostname);
    } else {
        res = http_get(sockfd, buf);
    }
#endif
#ifndef USE_CYASSL
    res = http_get(sockfd, buf);
#endif
    if (NULL == res) {
        debug(LOG_ERR, "There was a problem talking to the auth server!");
        return (AUTH_ERROR);
    }

    if ((tmp = strstr(res, "Auth: "))) {
        if (sscanf(tmp, "Auth: %d", (int *)&authresponse->authcode) == 1) {
            debug(LOG_INFO, "Auth server returned authentication code %d", authresponse->authcode);
            free(res);
            return (authresponse->authcode);
        } else {
            debug(LOG_WARNING, "Auth server did not return expected authentication code");
            free(res);
            return (AUTH_ERROR);
        }
    }
    free(res);
    return (AUTH_ERROR);
}
Beispiel #7
0
/** @internal
 * This function does the actual request.
 */
static void
ping(void)
{
    char request[MAX_BUF];
    FILE *fh;
    int sockfd;
    unsigned long int sys_uptime = 0;
    unsigned int sys_memfree = 0;
    float sys_load = 0;
    t_auth_serv *auth_server = NULL;
    auth_server = get_auth_server();
    static int authdown = 0;

    debug(LOG_DEBUG, "Entering ping()");
    memset(request, 0, sizeof(request));

    /*
     * The ping thread does not really try to see if the auth server is actually
     * working. Merely that there is a web server listening at the port. And that
     * is done by connect_auth_server() internally.
     */
    sockfd = connect_auth_server();
    if (sockfd == -1) {
        /*
         * No auth servers for me to talk to
         */
        if (!authdown) {
            fw_set_authdown();
            authdown = 1;
        }
        return;
    }

    /*
     * Populate uptime, memfree and load
     */
    if ((fh = fopen("/proc/uptime", "r"))) {
        if (fscanf(fh, "%lu", &sys_uptime) != 1)
            debug(LOG_CRIT, "Failed to read uptime");

        fclose(fh);
    }
    if ((fh = fopen("/proc/meminfo", "r"))) {
        while (!feof(fh)) {
            if (fscanf(fh, "MemFree: %u", &sys_memfree) == 0) {
                /* Not on this line */
                while (!feof(fh) && fgetc(fh) != '\n') ;
            } else {
                /* Found it */
                break;
            }
        }
        fclose(fh);
    }
    if ((fh = fopen("/proc/loadavg", "r"))) {
        if (fscanf(fh, "%f", &sys_load) != 1)
            debug(LOG_CRIT, "Failed to read loadavg");

        fclose(fh);
    }

    /*
     * Prep & send request
     */
    snprintf(request, sizeof(request) - 1,
             "GET %s%sgw_id=%s&sys_uptime=%lu&sys_memfree=%u&sys_load=%.2f&wifidog_uptime=%lu&prop=%s&network_id=%s&lat=%s&lon=%s&node_name=%s HTTP/1.0\r\n"
             "User-Agent: WiFiDog-ffw %s\r\n"
             "Host: %s\r\n"
             "\r\n",
             auth_server->authserv_path,
             auth_server->authserv_ping_script_path_fragment,
             config_get_config()->gw_id,
             sys_uptime,
             sys_memfree,
             sys_load,
             (long unsigned int)((long unsigned int)time(NULL) - (long unsigned int)started_time),
             config_get_config()->owner,
             config_get_config()->network,
             config_get_config()->lat,
             config_get_config()->lon,
             config_get_config()->node_name,
             VERSION, auth_server->authserv_hostname);

    char *res;
#ifdef USE_CYASSL
    if (auth_server->authserv_use_ssl) {
        res = https_get(sockfd, request, auth_server->authserv_hostname);
    } else {
        res = http_get(sockfd, request);
    }
#endif
#ifndef USE_CYASSL
    res = http_get(sockfd, request);
#endif
    if (NULL == res) {
        debug(LOG_ERR, "There was a problem pinging the auth server!");
        if (!authdown) {
            fw_set_authdown();
            authdown = 1;
        }
    } else if (strstr(res, "Pong") == 0) {
        debug(LOG_WARNING, "Auth server did NOT say Pong!");
        if (!authdown) {
            fw_set_authdown();
            authdown = 1;
        }
        free(res);
    } else {
        debug(LOG_DEBUG, "Auth Server Says: Pong");
        if (authdown) {
            fw_set_authup();
            authdown = 0;
        }
        free(res);
    }
    return;
}
Beispiel #8
0
std::string api::order_book()
{
	return https_get("order_book/");
}
Beispiel #9
0
std::string api::ticker()
{
	return https_get("ticker/");
}