Created
February 3, 2015 19:23
-
-
Save moduscreate/67fdb40d9154cfe056a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* File: main.c | |
* Author: jgarcia | |
* | |
* Created on February 3, 2015, 7:04 AM | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <curl/curl.h> | |
CURL *curl; | |
char *url = "https://support.us.playstation.com/"; | |
//char *url = "file://status.html"; | |
struct MemoryStruct { | |
char *memory; // Used to store the result from the | |
size_t size; | |
}; | |
static size_t write_memory_callback(void *contents, size_t size, size_t nmemb, void *userp) { | |
size_t realsize = size * nmemb; | |
struct MemoryStruct *mem = (struct MemoryStruct *)userp; | |
mem->memory = realloc(mem->memory, mem->size + realsize + 1); | |
if(mem->memory == NULL) { | |
/* out of memory! */ | |
printf("not enough memory (realloc returned NULL)\n"); | |
return 0; | |
} | |
memcpy(&(mem->memory[mem->size]), contents, realsize); | |
mem->size += realsize; | |
mem->memory[mem->size] = 0; | |
return realsize; | |
} | |
// Inspired by: http://curl.haxx.se/libcurl/c/getinmemory.html | |
int get_psn_status() { | |
int psn_status = 0; | |
CURL *curl_handle; | |
CURLcode res; | |
struct MemoryStruct chunk; | |
chunk.memory = malloc(1); /* will be grown as needed by the realloc above */ | |
chunk.size = 0; /* no data at this point */ | |
curl_global_init(CURL_GLOBAL_ALL); | |
curl_handle = curl_easy_init(); | |
curl_easy_setopt(curl_handle, CURLOPT_URL, url); | |
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_memory_callback); | |
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk); | |
/* some servers don't like requests that are made without a user-agent | |
field, so we provide one */ | |
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); | |
res = curl_easy_perform(curl_handle); | |
/* check for errors */ | |
if (res != CURLE_OK) { | |
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); | |
psn_status = -1; | |
} | |
else { | |
char *output = (char *)chunk.memory; | |
printf("%lu bytes retrieved\n", (long)chunk.size); | |
char *pointer = strstr(output, "PSN Status: ONLINE"); | |
if (pointer) { | |
//printf("FOUND [%s]\n", pointer); | |
psn_status = 1; | |
} | |
} | |
/* cleanup curl stuff */ | |
curl_easy_cleanup(curl_handle); | |
if(chunk.memory) { | |
free(chunk.memory); | |
} | |
/* we're done with libcurl, so clean it up */ | |
curl_global_cleanup(); | |
return psn_status; | |
} | |
int main(void) { | |
int status = get_psn_status(); | |
if (status == 1) { | |
printf("PSN is up, bitch\n"); | |
} | |
else if (status == 0) { | |
printf("PSN is down\n"); | |
} | |
else if (status == -1) { | |
printf("Could not connect to PSN\n"); | |
} | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment