Newer
Older
Daniel Stenberg
committed
arg++;
if(argc>arg)
serverlogfile = argv[arg++];
Daniel Stenberg
committed
}
else if(!strcmp("--ipv6", argv[arg])) {
#ifdef ENABLE_IPV6
ipv_inuse = "IPv6";
use_ipv6 = TRUE;
Daniel Stenberg
committed
#endif
arg++;
}
else if(!strcmp("--ipv4", argv[arg])) {
/* for completeness, we support this option as well */
#ifdef ENABLE_IPV6
ipv_inuse = "IPv4";
use_ipv6 = FALSE;
#endif
Daniel Stenberg
committed
arg++;
}
else if(!strcmp("--bindonly", argv[arg])) {
bind_only = TRUE;
arg++;
}
Daniel Stenberg
committed
else if(!strcmp("--port", argv[arg])) {
arg++;
if(argc>arg) {
fprintf(stderr, "sockfilt: invalid --port argument (%s)\n",
argv[arg]);
return 0;
}
Daniel Stenberg
committed
arg++;
}
}
else if(!strcmp("--connect", argv[arg])) {
/* Asked to actively connect to the specified local port instead of
doing a passive server-style listening. */
arg++;
if(argc>arg) {
fprintf(stderr, "sockfilt: invalid --connect argument (%s)\n",
argv[arg]);
return 0;
}
Daniel Stenberg
committed
arg++;
}
}
else if(!strcmp("--addr", argv[arg])) {
/* Set an IP address to use with --connect; otherwise use localhost */
arg++;
if(argc>arg) {
addr = argv[arg];
arg++;
}
}
Daniel Stenberg
committed
else {
puts("Usage: sockfilt [option]\n"
" --version\n"
" --verbose\n"
Daniel Stenberg
committed
" --logfile [file]\n"
" --pidfile [file]\n"
" --ipv4\n"
" --ipv6\n"
" --bindonly\n"
" --port [port]\n"
" --connect [port]\n"
" --addr [address]");
return 0;
Daniel Stenberg
committed
}
}
Daniel Stenberg
committed
win32_init();
atexit(win32_cleanup);
#endif
install_signal_handlers();
Yang Tse
committed
#ifdef ENABLE_IPV6
if(!use_ipv6)
#endif
sock = socket(AF_INET, SOCK_STREAM, 0);
#ifdef ENABLE_IPV6
else
sock = socket(AF_INET6, SOCK_STREAM, 0);
#endif
Daniel Stenberg
committed
Yang Tse
committed
if(CURL_SOCKET_BAD == sock) {
error = SOCKERRNO;
logmsg("Error creating socket: (%d) %s",
error, strerror(error));
Daniel Stenberg
committed
if(connectport) {
/* Active mode, we should connect to the given port number */
mode = ACTIVE;
#ifdef ENABLE_IPV6
if(!use_ipv6) {
#endif
memset(&me.sa4, 0, sizeof(me.sa4));
me.sa4.sin_family = AF_INET;
me.sa4.sin_port = htons(connectport);
me.sa4.sin_addr.s_addr = INADDR_ANY;
if (!addr)
addr = "127.0.0.1";
Curl_inet_pton(AF_INET, addr, &me.sa4.sin_addr);
Daniel Stenberg
committed
rc = connect(sock, &me.sa, sizeof(me.sa4));
Daniel Stenberg
committed
#ifdef ENABLE_IPV6
}
else {
memset(&me.sa6, 0, sizeof(me.sa6));
me.sa6.sin6_family = AF_INET6;
me.sa6.sin6_port = htons(connectport);
if (!addr)
addr = "::1";
Curl_inet_pton(AF_INET6, addr, &me.sa6.sin6_addr);
Daniel Stenberg
committed
rc = connect(sock, &me.sa, sizeof(me.sa6));
Daniel Stenberg
committed
}
#endif /* ENABLE_IPV6 */
if(rc) {
Yang Tse
committed
error = SOCKERRNO;
logmsg("Error connecting to port %hu: (%d) %s",
connectport, error, strerror(error));
Daniel Stenberg
committed
}
logmsg("====> Client connect");
msgsock = sock; /* use this as stream */
}
else {
/* passive daemon style */
sock = sockdaemon(sock, &port);
if(CURL_SOCKET_BAD == sock) {
write_stdout("FAIL\n", 5);
msgsock = CURL_SOCKET_BAD; /* no stream socket yet */
Daniel Stenberg
committed
}
logmsg("Running %s version", ipv_inuse);
Daniel Stenberg
committed
if(connectport)
logmsg("Connected to port %hu", connectport);
else if(bind_only)
logmsg("Bound without listening on port %hu", port);
Daniel Stenberg
committed
else
logmsg("Listening on port %hu", port);
Daniel Stenberg
committed
wrotepidfile = write_pidfile(pidname);
if(!wrotepidfile) {
write_stdout("FAIL\n", 5);
do {
juggle_again = juggle(&msgsock, sock, &mode);
} while(juggle_again);
Daniel Stenberg
committed
sockfilt_cleanup:
if((msgsock != sock) && (msgsock != CURL_SOCKET_BAD))
sclose(msgsock);
if(sock != CURL_SOCKET_BAD)
unlink(pidname);
restore_signal_handlers();
if(got_exit_signal) {
logmsg("============> sockfilt exits with signal (%d)", exit_signal);
/*
* To properly set the return status of the process we
* must raise the same signal SIGINT or SIGTERM that we
* caught and let the old handler take care of it.
*/
raise(exit_signal);
}
Daniel Stenberg
committed
logmsg("============> sockfilt quits");