-
Notifications
You must be signed in to change notification settings - Fork 0
/
rddnsclient.c
141 lines (123 loc) · 4.17 KB
/
rddnsclient.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright (C) 2002 John Todd Larason <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "rddnsclient.h"
#include "httpclient.h"
#include "crypt.h"
#include <stdio.h>
#include <string.h>
struct decrypt_callback_data
{
char * plaintext_buffer;
u32 plaintext_buffer_len;
u32 plaintext_len;
u32 t;
};
/* XXX this only handles one chunk */
static void decrypt_callback(unsigned char * cyphertext, size_t cyphertext_len, void * vp)
{
struct decrypt_callback_data * d = vp;
rtv_decrypt(cyphertext, cyphertext_len,
d->plaintext_buffer, d->plaintext_buffer_len,
&d->t, &d->plaintext_len, 0);
free(cyphertext);
}
static int rddns_query(unsigned const char * path,
unsigned const char * querystring,
unsigned char * result_buffer, size_t result_buffer_len)
{
struct decrypt_callback_data decrypt_data;
unsigned char urlbuf[1024];
unsigned char cyphertext[64];
unsigned char * p;
unsigned int i;
struct hc * hc;
u32 cyphertext_len;
decrypt_data.plaintext_buffer = result_buffer;
decrypt_data.plaintext_buffer_len = result_buffer_len;
if (rtv_encrypt(querystring, strlen(querystring),
cyphertext, sizeof cyphertext, &cyphertext_len, 0) < 0) {
fprintf(stderr, "ERROR: encrypt failure\n");
return -1;
}
/* rddns-rns.replaytv.net */
if (strlen("http://64.124.73.112%s?q=") + 2*cyphertext_len + 1 > sizeof urlbuf) {
fprintf(stderr, "ERROR rddns_lookup: urlbuf too small\n");
return -1;
}
p = urlbuf + sprintf(urlbuf, "http://64.124.73.112%s?q=", path);
for (i = 0; i < cyphertext_len; i++) {
p += sprintf(p, "%02x", cyphertext[i]);
}
hc = hc_start_request(urlbuf);
if (!hc) {
fprintf(stderr, "ERROR rddns_lookup: hc_start_request %s failed\n",
urlbuf);
return -1;
}
if (hc_send_request(hc) < 0) {
fprintf(stderr, "ERROR rddns_lookup: hc_send_request failed\n");
return -1;
}
if (hc_get_status(hc)/100 != 2) {
fprintf(stderr, "ERROR rddns_lookup http status %d\n",
hc_get_status(hc));
return -1;
}
if (hc_read_pieces(hc, decrypt_callback, &decrypt_data) < 0) {
fprintf(stderr, "ERROR rddns_lookup read failed\n");
return -1;
}
hc_free(hc);
return 0;
}
int rddns_lookup(unsigned const char * ISN,
unsigned char * result_buffer, size_t result_buffer_len)
{
unsigned char querystring[32];
if (!ISN) {
fprintf(stderr, "ERROR: rddns_lookup: null ISN\n");
return -1;
}
if (strlen(ISN) != 17) {
fprintf(stderr, "ERROR: rddns_lookup: invalid ISN\n");
return -1;
}
sprintf(querystring, "isn=%s", ISN);
if (rddns_query("/rd/servlet/gu", querystring,
result_buffer, result_buffer_len) < 0) {
fprintf(stderr, "ERROR: rddns_lookup: rddns_query failed\n");
return -1;
}
return 0;
}
int rddns_update(unsigned const char * ISN, unsigned short port,
unsigned char * result_buffer, size_t result_buffer_len)
{
unsigned char querystring[64];
if (!ISN) {
fprintf(stderr, "ERROR: rddns_lookup: null ISN\n");
return -1;
}
if (strlen(ISN) != 17) {
fprintf(stderr, "ERROR: rddns_lookup: invalid ISN\n");
return -1;
}
sprintf(querystring, "isn=%s&port=%d", ISN, port);
if (rddns_query("/rd/servlet/ul", querystring,
result_buffer, result_buffer_len) < 0) {
fprintf(stderr, "ERROR: rddns_lookup: rddns_query failed\n");
return -1;
}
return 0;
}