Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
namecoin-hosts
/*
Copyright (c) 2011, Chris Double
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <stdio.h>
#include <memory.h>
#include <jansson.h>
#include <curl/curl.h>
static json_t* scanNames(const char* server, const char* user, const char* password);
#define BUFFER_SIZE (10 * 1024 * 1024) /* 10 MB */
struct write_result
{
char *data;
int pos;
};
static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream)
{
struct write_result *result = (struct write_result *)stream;
if(result->pos + size * nmemb >= BUFFER_SIZE - 1)
{
fprintf(stderr, "error: too small buffer\n");
return 0;
}
memcpy(result->data + result->pos, ptr, size * nmemb);
result->pos += size * nmemb;
return size * nmemb;
}
static char *request(const char *url, const char* auth)
{
CURL *curl;
CURLcode status;
char *data;
long code;
struct curl_slist* slist = NULL;
curl = curl_easy_init();
data = malloc(BUFFER_SIZE);
if(!curl || !data)
return NULL;
struct write_result write_result = {
.data = data,
.pos = 0
};
slist = curl_slist_append(slist, "content-type: application/json");
const char* send = "{\"version\": \"1.1\",\"method\": \"name_scan\",\"params\": [ \"\", 99999 ],\"id\": 1}";
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_response);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &write_result);
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_easy_setopt(curl, CURLOPT_USERPWD, auth);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, send);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(send));
status = curl_easy_perform(curl);
curl_slist_free_all(slist);
if(status != 0)
{
fprintf(stderr, "error: unable to request data from %s:\n", url);
fprintf(stderr, "%s\n", curl_easy_strerror(status));
return NULL;
}
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
if(code != 200)
{
fprintf(stderr, "error: server responded with code %ld\n", code);
return NULL;
}
curl_easy_cleanup(curl);
curl_global_cleanup();
/* zero-terminate the result */
data[write_result.pos] = '\0';
return data;
}
static json_t* scanNames(const char* server, const char* user, const char* password)
{
json_error_t error;
json_t* root;
json_t* results;
int i;
char url[256];
char auth[128];
char* text;
snprintf(url, sizeof(url), "http://%s/", server);
snprintf(auth, sizeof(auth), "%s:%s", user, password);
text = request(url, auth);
if(!text)
return NULL;
root = json_loads(text, 0, &error);
free(text);
if(!root)
{
fprintf(stderr, "error: on line %d: %s\n", error.line, error.text);
return NULL;
}
results = json_object_get(root, "result");
if(!json_is_array(results))
{
fprintf(stderr, "error: results is not an array\n");
json_decref(root);
return NULL;
}
return results;
}
static int is_valid_name(const char* s)
{
int len = strlen(s);
int i;
for(i=0;i<len;++i)
if(s[i] == ' ' || s[i] == '\t' || s[i] == '\n' || s[i] == '\r')
return 0;
return 1;
}
static is_valid_address(const char* s)
{
int len = strlen(s);
int i;
for(i=0;i<len;++i)
if(!isdigit(s[i]) && !(s[i] == '.'))
return 0;
return 1;
}
int main(int argc, char* argv[])
{
json_t* results;
int i;
if (argc < 4) {
fprintf(stderr, "Usage: %s <server:port> <username> <password>\n", argv[0]);
return 0;
}
results = scanNames(argv[1], argv[2], argv[3]);
if (!results) {
fprintf(stderr, "Could not scan names\n");
return 0;
}
for(i = 0; i < json_array_size(results); i++)
{
json_t *result;
json_t *name;
json_t *value;
const char* n;
void* iter;
result = json_array_get(results, i);
if(!json_is_object(result))
{
fprintf(stderr, "error: result %d is not an object\n", i + 1);
continue;
}
name = json_object_get(result, "name");
if(!json_is_string(name))
{
fprintf(stderr, "error: result %d: name is not a string\n", i + 1);
continue;
}
value = json_object_get(result, "value");
if(!json_is_string(value))
{
fprintf(stderr, "error: result %d: value is not a string\n", i + 1);
continue;
}
n = json_string_value(name);
if(strlen(n) > 2 && n[0] == 'd' && n[1] == '/') {
json_error_t error;
json_t* entry;
json_t* map;
n += 2;
entry = json_loads(json_string_value(value), 0, &error);
if (!json_is_object(entry)) {
// fprintf(stderr, "error: [%s] on line %d: %s\n", n, error.line, error.text);
continue;
}
map = json_object_get(entry, "map");
if (!map || !json_is_object(map)) {
// fprintf(stderr, "error: [%s] on line %d: %s\n", n, error.line, error.text);
continue;
}
iter = json_object_iter(map);
while (iter) {
const char* key = json_object_iter_key(iter);
json_t* value = json_object_iter_value(iter);
if (value && json_is_string(value)) {
if (is_valid_name(key) && is_valid_address(json_string_value(value)))
printf("%s\t%s%s%s.bit\n", json_string_value(value), key, strlen(key) == 0 ? "" : ".", n);
}
iter = json_object_iter_next(map, iter);
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment