Added full IPv4 support

This commit is contained in:
Thulinma 2011-09-26 12:44:15 +02:00
parent 57b9866b4b
commit 592c1cfbd0
2 changed files with 44 additions and 4 deletions

View file

@ -196,10 +196,10 @@ bool HTTP::Parser::parse(){
std::string varval;
while (tmppost.find('=') != std::string::npos){
size_t found = tmppost.find('=');
varname = urlunescape(tmppost.substr(0, found).c_str());
varname = urlunescape((char*)tmppost.substr(0, found).c_str());
tmppost.erase(0, found+1);
size_t found = tmppost.find('&');
varval = urlunescape(tmppost.substr(0, found).c_str());
found = tmppost.find('&');
varval = urlunescape((char*)tmppost.substr(0, found).c_str());
SetVar(varname, varval);
if (found == std::string::npos){
tmppost.clear();

View file

@ -374,7 +374,47 @@ Socket::Server::Server(int port, std::string hostname, bool nonblock){
}
}else{
#if DEBUG >= 1
fprintf(stderr, "Binding failed! Error: %s\n", strerror(errno));
fprintf(stderr, "Binding failed, retrying as IPv4... (%s)\n", strerror(errno));
#endif
close();
}
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0){
#if DEBUG >= 1
fprintf(stderr, "Could not create socket! Error: %s\n", strerror(errno));
#endif
return;
}
on = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if (nonblock){
int flags = fcntl(sock, F_GETFL, 0);
flags |= O_NONBLOCK;
fcntl(sock, F_SETFL, flags);
}
struct sockaddr_in addr4;
addr4.sin_family = AF_INET;
addr4.sin_port = htons(port);//set port
if (hostname == "0.0.0.0"){
addr4.sin_addr.s_addr = INADDR_ANY;
}else{
inet_pton(AF_INET, hostname.c_str(), &addr4.sin_addr);//set interface, 0.0.0.0 (default) is all
}
ret = bind(sock, (sockaddr*)&addr4, sizeof(addr4));//do the actual bind
if (ret == 0){
ret = listen(sock, 100);//start listening, backlog of 100 allowed
if (ret == 0){
return;
}else{
#if DEBUG >= 1
fprintf(stderr, "Listen failed! Error: %s\n", strerror(errno));
#endif
close();
return;
}
}else{
#if DEBUG >= 1
fprintf(stderr, "IPv4 binding also failed, giving up. (%s)\n", strerror(errno));
#endif
close();
return;