/** name of server in URL to fetch HTTPS from */
#define URLNAME "data.iana.org"
/** path on HTTPS server to xml file */
-#define XMLNAME "/root-anchors/root-anchors.xml"
+#define XMLNAME "root-anchors/root-anchors.xml"
/** path on HTTPS server to p7s file */
-#define P7SNAME "/root-anchors/root-anchors.p7s"
+#define P7SNAME "root-anchors/root-anchors.p7s"
/** port number for https access */
#define HTTPS_PORT 443
+#ifdef USE_WINSOCK
+/* sneakily reuse the the wsa_strerror function, on windows */
+char* wsa_strerror(int err);
+#endif
+
/** verbosity for this application */
static int verb = 0;
}
if(!res) {
if(verb) printf("out of memory\n");
+ ub_ctx_delete(ctx);
exit(0);
}
for(i = 0; res->data[i]; i++) {
fd = socket(ip->len==(socklen_t)sizeof(struct sockaddr_in)?
AF_INET:AF_INET6, SOCK_STREAM, 0);
if(fd == -1) {
+#ifndef USE_WINSOCK
if(verb) printf("socket: %s\n", strerror(errno));
+#else
+ if(verb) printf("socket: %s\n",
+ wsa_strerror(WSAGetLastError()));
+#endif
return -1;
}
if(connect(fd, (struct sockaddr*)&ip->addr, ip->len) < 0) {
+#ifndef USE_WINSOCK
if(verb) printf("connect: %s\n", strerror(errno));
+#else
+ if(verb) printf("connect: %s\n",
+ wsa_strerror(WSAGetLastError()));
+#endif
fd_close(fd);
return -1;
}
size_t len;
char* body;
BIO* mem = BIO_new(BIO_s_mem());
+ if(verb>=3) printf("do_chunked_read\n");
if(!mem) {
if(verb) printf("out of memory\n");
return NULL;
if(verb>=2) printf("chunk header: %s\n", buf);
if(!parse_chunk_header(buf, &len)) {
BIO_free(mem);
+ if(verb>=3) printf("could not parse chunk header\n");
return NULL;
}
if(verb>=2) printf("chunk len: %d\n", (int)len);
static int
write_http_get(SSL* ssl, char* pathname, char* urlname)
{
- if(write_ssl_line(ssl, "GET %s HTTP/1.1", pathname) &&
+ if(write_ssl_line(ssl, "GET /%s HTTP/1.1", pathname) &&
write_ssl_line(ssl, "Host: %s", urlname) &&
write_ssl_line(ssl, "User-Agent: unbound-anchor/%s",
PACKAGE_VERSION) &&
BIO* tmp = do_chunked_read(ssl);
char* d = NULL;
size_t l;
+ if(!tmp) {
+ if(verb) printf("could not read from https\n");
+ return NULL;
+ }
l = (size_t)BIO_get_mem_data(tmp, &d);
if(verb>=2) printf("chunked data is %d\n", (int)l);
if(l == 0 || d == NULL) {
BIO_free(ds);
}
+#ifdef USE_WINSOCK
+static void do_wsa_cleanup(void) { WSACleanup(); }
+#endif
+
/** perform actual certupdate work */
static int
do_certupdate(char* root_anchor_file, char* root_cert_file,
ip_list = resolve_name(urlname, port, res_conf, root_hints, debugconf,
ip4only, ip6only);
+#ifdef USE_WINSOCK
+ if(1) { /* libunbound finished, startup WSA for the https connection */
+ WSADATA wsa_data;
+ int r;
+ if((r = WSAStartup(MAKEWORD(2,2), &wsa_data)) != 0) {
+ if(verb) printf("WSAStartup failed: %s\n",
+ wsa_strerror(r));
+ exit(0);
+ }
+ atexit(&do_wsa_cleanup);
+ }
+#endif
+
/* fetch the necessary files over HTTPS */
xml = https(ip_list, xmlname, urlname);
p7s = https(ip_list, p7sname, urlname);
{
char* buf, *at;
size_t len, avail, header_reserve=1024;
- FILE* in = fopen(fname, "r");
+ FILE* in = fopen(fname,
+#ifndef USE_WINSOCK
+ "r"
+#else
+ "rb"
+#endif
+ );
int r;
const char* rcode = "200 OK";
if(!in) {
char* at = buf;
size_t avail = sizeof(buf);
int r;
- FILE* in = fopen(fname, "r");
+ FILE* in = fopen(fname,
+#ifndef USE_WINSOCK
+ "r"
+#else
+ "rb"
+#endif
+ );
const char* rcode = "200 OK";
if(!in) {
rcode = "404 File not found";
size_t red = in?fread(tmpbuf, 1, avail-16, in):0;
/* prepare chunk */
r = snprintf(at, avail, "%x\r\n", (unsigned)red);
+ if(verb >= 3)
+ {printf("chunk len %x\n", (unsigned)red); fflush(stdout);}
at += r;
avail -= r;
if(red != 0) {
(void)inet_ntop((int)((struct sockaddr_in*)from)->sin_family,
a, out, (socklen_t)sizeof(out));
printf("%s requests %s\n", out, combined);
+ fflush(stdout);
}
if(vs == 10)
provide_file_10(ssl, combined);
struct sockaddr_storage from;
socklen_t flen = (socklen_t)sizeof(from);
int s = accept(fd, (struct sockaddr*)&from, &flen);
+ if(verb) fflush(stdout);
if(s != -1) {
SSL* ssl = setup_ssl(s, sslctx);
+ if(verb) fflush(stdout);
if(ssl) {
service_ssl(ssl, &from, flen);
+ if(verb) fflush(stdout);
SSL_shutdown(ssl);
SSL_free(ssl);
}
fd_close(s);
} else if (verb >=2) log_errno("accept");
+ if(verb) fflush(stdout);
}
/* if we get a kill signal, the process dies and the OS reaps us */
if(verb) printf("petal end\n");
int c;
int port = 443;
char* addr = "127.0.0.1", *key = "petal.key", *cert = "petal.pem";
+#ifdef USE_WINSOCK
+ WSADATA wsa_data;
+ if((c=WSAStartup(MAKEWORD(2,2), &wsa_data)) != 0)
+ { printf("WSAStartup failed\n"); exit(1); }
+ atexit((void (*)(void))WSACleanup);
+#endif
+
/* parse the options */
while( (c=getopt(argc, argv, "a:c:k:hp:v")) != -1) {
switch(c) {