Minor cleanup
This commit is contained in:
Родитель
3b0a920fad
Коммит
2aa4710745
|
@ -168,16 +168,15 @@ static int juggle(curl_socket_t *sockfdp,
|
||||||
fd_set fds_read;
|
fd_set fds_read;
|
||||||
fd_set fds_write;
|
fd_set fds_write;
|
||||||
fd_set fds_err;
|
fd_set fds_err;
|
||||||
|
curl_socket_t sockfd;
|
||||||
curl_socket_t maxfd;
|
curl_socket_t maxfd;
|
||||||
ssize_t rc;
|
ssize_t rc;
|
||||||
ssize_t len;
|
ssize_t nread_stdin;
|
||||||
ssize_t nread;
|
ssize_t nread_socket;
|
||||||
ssize_t bytes_written;
|
ssize_t bytes_written;
|
||||||
ssize_t bytes_read;
|
ssize_t buffer_len;
|
||||||
ssize_t port_strlen;
|
|
||||||
unsigned char buffer[256]; /* FIX: bigger buffer */
|
unsigned char buffer[256]; /* FIX: bigger buffer */
|
||||||
char data[256];
|
char data[256];
|
||||||
curl_socket_t sockfd;
|
|
||||||
|
|
||||||
timeout.tv_sec = 120;
|
timeout.tv_sec = 120;
|
||||||
timeout.tv_usec = 0;
|
timeout.tv_usec = 0;
|
||||||
|
@ -189,7 +188,9 @@ static int juggle(curl_socket_t *sockfdp,
|
||||||
FD_SET(fileno(stdin), &fds_read);
|
FD_SET(fileno(stdin), &fds_read);
|
||||||
|
|
||||||
switch(*mode) {
|
switch(*mode) {
|
||||||
|
|
||||||
case PASSIVE_LISTEN:
|
case PASSIVE_LISTEN:
|
||||||
|
|
||||||
/* server mode */
|
/* server mode */
|
||||||
sockfd = listenfd;
|
sockfd = listenfd;
|
||||||
/* there's always a socket to wait for */
|
/* there's always a socket to wait for */
|
||||||
|
@ -198,8 +199,9 @@ static int juggle(curl_socket_t *sockfdp,
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PASSIVE_CONNECT:
|
case PASSIVE_CONNECT:
|
||||||
|
|
||||||
sockfd = *sockfdp;
|
sockfd = *sockfdp;
|
||||||
if(-1 == sockfd) {
|
if(CURL_SOCKET_BAD == sockfd) {
|
||||||
/* eeek, we are supposedly connected and then this cannot be -1 ! */
|
/* eeek, we are supposedly connected and then this cannot be -1 ! */
|
||||||
logmsg("socket is -1! on %s:%d", __FILE__, __LINE__);
|
logmsg("socket is -1! on %s:%d", __FILE__, __LINE__);
|
||||||
maxfd = 0; /* stdin */
|
maxfd = 0; /* stdin */
|
||||||
|
@ -212,10 +214,10 @@ static int juggle(curl_socket_t *sockfdp,
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ACTIVE:
|
case ACTIVE:
|
||||||
sockfd = *sockfdp;
|
|
||||||
|
|
||||||
|
sockfd = *sockfdp;
|
||||||
/* sockfd turns CURL_SOCKET_BAD when our connection has been closed */
|
/* sockfd turns CURL_SOCKET_BAD when our connection has been closed */
|
||||||
if(sockfd != CURL_SOCKET_BAD) {
|
if(CURL_SOCKET_BAD != sockfd) {
|
||||||
FD_SET(sockfd, &fds_read);
|
FD_SET(sockfd, &fds_read);
|
||||||
maxfd = sockfd;
|
maxfd = sockfd;
|
||||||
}
|
}
|
||||||
|
@ -226,11 +228,13 @@ static int juggle(curl_socket_t *sockfdp,
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ACTIVE_DISCONNECT:
|
case ACTIVE_DISCONNECT:
|
||||||
|
|
||||||
logmsg("disconnected, no socket to read on");
|
logmsg("disconnected, no socket to read on");
|
||||||
maxfd = 0;
|
maxfd = 0;
|
||||||
sockfd = CURL_SOCKET_BAD;
|
sockfd = CURL_SOCKET_BAD;
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
} /* switch(*mode) */
|
||||||
|
|
||||||
do {
|
do {
|
||||||
rc = select(maxfd + 1, &fds_read, &fds_write, &fds_err, &timeout);
|
rc = select(maxfd + 1, &fds_read, &fds_write, &fds_err, &timeout);
|
||||||
|
@ -260,8 +264,8 @@ static int juggle(curl_socket_t *sockfdp,
|
||||||
|
|
||||||
DATA - plain pass-thru data
|
DATA - plain pass-thru data
|
||||||
*/
|
*/
|
||||||
nread = read(fileno(stdin), buffer, 5);
|
nread_stdin = read(fileno(stdin), buffer, 5);
|
||||||
if(5 == nread) {
|
if(5 == nread_stdin) {
|
||||||
|
|
||||||
logmsg("Received %c%c%c%c (on stdin)",
|
logmsg("Received %c%c%c%c (on stdin)",
|
||||||
buffer[0], buffer[1], buffer[2], buffer[3] );
|
buffer[0], buffer[1], buffer[2], buffer[3] );
|
||||||
|
@ -272,13 +276,13 @@ static int juggle(curl_socket_t *sockfdp,
|
||||||
}
|
}
|
||||||
|
|
||||||
else if(!memcmp("PORT", buffer, 4)) {
|
else if(!memcmp("PORT", buffer, 4)) {
|
||||||
/* question asking us what PORT number we are listening to.
|
/* Question asking us what PORT number we are listening to.
|
||||||
Replies with PORT with "IPv[num]/[port]" */
|
Replies to PORT with "IPv[num]/[port]" */
|
||||||
sprintf((char *)buffer, "IPv%d/%d\n", use_ipv6?6:4, port);
|
sprintf((char *)buffer, "IPv%d/%d\n", use_ipv6?6:4, (int)port);
|
||||||
port_strlen = (ssize_t)strlen((char *)buffer);
|
buffer_len = (ssize_t)strlen((char *)buffer);
|
||||||
sprintf(data, "PORT\n%04x\n", port_strlen);
|
sprintf(data, "PORT\n%04x\n", buffer_len);
|
||||||
write(fileno(stdout), data, 10);
|
write(fileno(stdout), data, 10);
|
||||||
write(fileno(stdout), buffer, port_strlen);
|
write(fileno(stdout), buffer, buffer_len);
|
||||||
}
|
}
|
||||||
else if(!memcmp("QUIT", buffer, 4)) {
|
else if(!memcmp("QUIT", buffer, 4)) {
|
||||||
/* just die */
|
/* just die */
|
||||||
|
@ -290,14 +294,20 @@ static int juggle(curl_socket_t *sockfdp,
|
||||||
|
|
||||||
if(5 != read(fileno(stdin), buffer, 5))
|
if(5 != read(fileno(stdin), buffer, 5))
|
||||||
return FALSE;
|
return FALSE;
|
||||||
buffer[4] = '\0';
|
buffer[5] = '\0';
|
||||||
|
|
||||||
len = (ssize_t)strtol((char *)buffer, NULL, 16);
|
buffer_len = (ssize_t)strtol((char *)buffer, NULL, 16);
|
||||||
if(len != read(fileno(stdin), buffer, len))
|
if (buffer_len > (ssize_t)sizeof(buffer)) {
|
||||||
|
logmsg("Buffer size %d too small for data size %d",
|
||||||
|
(int)sizeof(buffer), buffer_len);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
nread_stdin = read(fileno(stdin), buffer, buffer_len);
|
||||||
|
if(nread_stdin != buffer_len)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
logmsg("> %d bytes data, server => client", len);
|
logmsg("> %d bytes data, server => client", buffer_len);
|
||||||
lograw(buffer, len);
|
lograw(buffer, buffer_len);
|
||||||
|
|
||||||
if(*mode == PASSIVE_LISTEN) {
|
if(*mode == PASSIVE_LISTEN) {
|
||||||
logmsg("*** We are disconnected!");
|
logmsg("*** We are disconnected!");
|
||||||
|
@ -305,10 +315,10 @@ static int juggle(curl_socket_t *sockfdp,
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
/* send away on the socket */
|
/* send away on the socket */
|
||||||
bytes_written = swrite(sockfd, buffer, len);
|
bytes_written = swrite(sockfd, buffer, buffer_len);
|
||||||
if(bytes_written != len) {
|
if(bytes_written != buffer_len) {
|
||||||
logmsg("Not all data was sent. Bytes to send: %d sent: %d",
|
logmsg("Not all data was sent. Bytes to send: %d sent: %d",
|
||||||
len, bytes_written);
|
buffer_len, bytes_written);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -329,19 +339,20 @@ static int juggle(curl_socket_t *sockfdp,
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(nread == -1){
|
else if(-1 == nread_stdin) {
|
||||||
logmsg("read %d from stdin, exiting", nread);
|
logmsg("read %d from stdin, exiting", nread_stdin);
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if((sockfd != CURL_SOCKET_BAD) && (FD_ISSET(sockfd, &fds_read)) ) {
|
if((sockfd != CURL_SOCKET_BAD) && (FD_ISSET(sockfd, &fds_read)) ) {
|
||||||
|
|
||||||
if(*mode == PASSIVE_LISTEN) {
|
if(*mode == PASSIVE_LISTEN) {
|
||||||
/* there's no stream set up yet, this is an indication that there's a
|
/* there's no stream set up yet, this is an indication that there's a
|
||||||
client connecting. */
|
client connecting. */
|
||||||
sockfd = accept(sockfd, NULL, NULL);
|
sockfd = accept(sockfd, NULL, NULL);
|
||||||
if(-1 == sockfd)
|
if(CURL_SOCKET_BAD == sockfd)
|
||||||
logmsg("accept() failed\n");
|
logmsg("accept() failed\n");
|
||||||
else {
|
else {
|
||||||
logmsg("====> Client connect");
|
logmsg("====> Client connect");
|
||||||
|
@ -353,9 +364,9 @@ static int juggle(curl_socket_t *sockfdp,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* read from socket, pass on data to stdout */
|
/* read from socket, pass on data to stdout */
|
||||||
bytes_read = sread(sockfd, buffer, sizeof(buffer));
|
nread_socket = sread(sockfd, buffer, sizeof(buffer));
|
||||||
|
|
||||||
if(bytes_read <= 0) {
|
if(nread_socket <= 0) {
|
||||||
logmsg("====> Client disconnect");
|
logmsg("====> Client disconnect");
|
||||||
write(fileno(stdout), "DISC\n", 5);
|
write(fileno(stdout), "DISC\n", 5);
|
||||||
sclose(sockfd);
|
sclose(sockfd);
|
||||||
|
@ -367,12 +378,12 @@ static int juggle(curl_socket_t *sockfdp,
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
sprintf(data, "DATA\n%04x\n", bytes_read);
|
sprintf(data, "DATA\n%04x\n", nread_socket);
|
||||||
write(fileno(stdout), data, 10);
|
write(fileno(stdout), data, 10);
|
||||||
write(fileno(stdout), buffer, bytes_read);
|
write(fileno(stdout), buffer, nread_socket);
|
||||||
|
|
||||||
logmsg("< %d bytes data, client => server", bytes_read);
|
logmsg("< %d bytes data, client => server", nread_socket);
|
||||||
lograw(buffer, bytes_read);
|
lograw(buffer, nread_socket);
|
||||||
}
|
}
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
|
@ -390,8 +401,8 @@ static curl_socket_t sockdaemon(curl_socket_t sock,
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
if (setsockopt
|
if (setsockopt
|
||||||
(sock, SOL_SOCKET, SO_REUSEADDR, (const void *) &flag,
|
(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&flag,
|
||||||
sizeof(int)) < 0) {
|
sizeof(flag)) < 0) {
|
||||||
perror("setsockopt(SO_REUSEADDR)");
|
perror("setsockopt(SO_REUSEADDR)");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Загрузка…
Ссылка в новой задаче