Make sure buffer is zero-terminated when getting external ip

Also switch to HTTP 1.0 to avoid chunked transfer coding.
This commit is contained in:
Erik Ekman 2014-01-29 23:12:22 +01:00
parent f11328306d
commit 8baad91156

View file

@ -103,7 +103,9 @@ static int get_external_ip(struct in_addr *ip)
int sock; int sock;
struct addrinfo *addr; struct addrinfo *addr;
int res; int res;
const char *getstr = "GET /ip/ HTTP/1.1\r\nHost: api.externalip.net\r\n\r\n"; const char *getstr = "GET /ip/ HTTP/1.0\r\n"
/* HTTP 1.0 to avoid chunked transfer coding */
"Host: api.externalip.net\r\n\r\n";
char buf[512]; char buf[512];
char *b; char *b;
int len; int len;
@ -124,7 +126,9 @@ static int get_external_ip(struct in_addr *ip)
res = write(sock, getstr, strlen(getstr)); res = write(sock, getstr, strlen(getstr));
if (res != strlen(getstr)) return 4; if (res != strlen(getstr)) return 4;
res = read(sock, buf, sizeof(buf)); /* Zero buf before receiving, leave at least one zero at the end */
memset(buf, 0, sizeof(buf));
res = read(sock, buf, sizeof(buf) - 1);
if (res < 0) return 5; if (res < 0) return 5;
len = res; len = res;