2002-04-13 13:20:19 +08:00
|
|
|
/* $Id: sock.c,v 1.24 2002-04-13 05:20:19 rjkaes Exp $
|
2000-02-17 01:32:49 +08:00
|
|
|
*
|
|
|
|
* Sockets are created and destroyed here. When a new connection comes in from
|
|
|
|
* a client, we need to copy the socket and the create a second socket to the
|
|
|
|
* remote server the client is trying to connect to. Also, the listening
|
|
|
|
* socket is created and destroyed here. Sounds more impressive than it
|
|
|
|
* actually is.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998 Steven Young
|
|
|
|
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com)
|
|
|
|
* Copyright (C) 2000 Chris Lightfoot (chris@ex-parrot.com)
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2, or (at your option) any
|
|
|
|
* later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tinyproxy.h"
|
2000-09-12 07:56:32 +08:00
|
|
|
|
2000-02-17 01:32:49 +08:00
|
|
|
#include "log.h"
|
2000-09-12 07:56:32 +08:00
|
|
|
#include "sock.h"
|
2000-02-17 01:32:49 +08:00
|
|
|
#include "utils.h"
|
2000-09-12 07:56:32 +08:00
|
|
|
|
|
|
|
/*
|
2001-08-29 12:00:22 +08:00
|
|
|
* The mutex is used for locking around any calls which access global
|
|
|
|
* variables.
|
2000-09-12 07:56:32 +08:00
|
|
|
* - rjkaes
|
|
|
|
*/
|
2001-08-29 12:00:22 +08:00
|
|
|
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
2000-09-12 07:56:32 +08:00
|
|
|
|
2001-08-29 12:00:22 +08:00
|
|
|
#define LOCK() pthread_mutex_lock(&mutex);
|
|
|
|
#define UNLOCK() pthread_mutex_unlock(&mutex);
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2001-12-15 13:58:30 +08:00
|
|
|
/*
|
|
|
|
* The mutex is used for locking around accesses to gethostbyname()
|
|
|
|
* function.
|
|
|
|
*/
|
|
|
|
static pthread_mutex_t gethostbyname_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
#define LOOKUP_LOCK() pthread_mutex_lock(&gethostbyname_mutex);
|
|
|
|
#define LOOKUP_UNLOCK() pthread_mutex_unlock(&gethostbyname_mutex);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Take a string host address and return a struct in_addr so we can connect
|
|
|
|
* to the remote host.
|
|
|
|
*
|
|
|
|
* Return a negative if there is a problem.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
lookup_domain(struct in_addr *addr, char *domain)
|
|
|
|
{
|
|
|
|
struct hostent *resolv;
|
|
|
|
|
|
|
|
if (!addr || !domain)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First check to see if the domain is in dotted-decimal format.
|
|
|
|
*/
|
|
|
|
if (inet_aton(domain, (struct in_addr *)addr) != 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Okay, it's an alpha-numeric domain, so look it up.
|
|
|
|
*/
|
|
|
|
LOOKUP_LOCK();
|
|
|
|
if (!(resolv = gethostbyname(domain))) {
|
|
|
|
LOOKUP_UNLOCK();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(addr, resolv->h_addr_list[0], resolv->h_length);
|
|
|
|
|
|
|
|
LOOKUP_UNLOCK();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-02-17 01:32:49 +08:00
|
|
|
/* This routine is so old I can't even remember writing it. But I do
|
|
|
|
* remember that it was an .h file because I didn't know putting code in a
|
|
|
|
* header was bad magic yet. anyway, this routine opens a connection to a
|
|
|
|
* system and returns the fd.
|
2000-09-12 07:56:32 +08:00
|
|
|
* - steve
|
|
|
|
*
|
2000-02-17 01:32:49 +08:00
|
|
|
* Cleaned up some of the code to use memory routines which are now the
|
|
|
|
* default. Also, the routine first checks to see if the address is in
|
2000-09-12 07:56:32 +08:00
|
|
|
* dotted-decimal form before it does a name lookup.
|
2000-02-17 01:32:49 +08:00
|
|
|
* - rjkaes
|
|
|
|
*/
|
2001-11-22 08:19:18 +08:00
|
|
|
int
|
|
|
|
opensock(char *ip_addr, uint16_t port)
|
2000-02-17 01:32:49 +08:00
|
|
|
{
|
2000-09-12 07:56:32 +08:00
|
|
|
int sock_fd;
|
2000-02-17 01:32:49 +08:00
|
|
|
struct sockaddr_in port_info;
|
2002-04-13 13:20:19 +08:00
|
|
|
struct sockaddr_in bind_addr;
|
2000-09-12 07:56:32 +08:00
|
|
|
int ret;
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2001-05-24 02:01:23 +08:00
|
|
|
assert(ip_addr != NULL);
|
|
|
|
assert(port > 0);
|
|
|
|
|
2001-11-22 08:19:18 +08:00
|
|
|
memset((struct sockaddr *) &port_info, 0, sizeof(port_info));
|
2000-02-17 01:32:49 +08:00
|
|
|
|
|
|
|
port_info.sin_family = AF_INET;
|
|
|
|
|
2001-08-29 12:00:22 +08:00
|
|
|
/* Lookup and return the address if possible */
|
2001-12-15 13:58:30 +08:00
|
|
|
ret = lookup_domain(&port_info.sin_addr, ip_addr);
|
2000-09-12 07:56:32 +08:00
|
|
|
|
|
|
|
if (ret < 0) {
|
2001-11-22 08:19:18 +08:00
|
|
|
log_message(LOG_ERR,
|
|
|
|
"opensock: Could not lookup address \"%s\".",
|
|
|
|
ip_addr);
|
2000-02-17 01:32:49 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
port_info.sin_port = htons(port);
|
|
|
|
|
|
|
|
if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
|
2001-11-22 08:19:18 +08:00
|
|
|
log_message(LOG_ERR, "opensock: socket() error \"%s\".",
|
|
|
|
strerror(errno));
|
2000-02-17 01:32:49 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-04-13 13:20:19 +08:00
|
|
|
/* Bind to our listening address*/
|
|
|
|
if (config.ipAddr) {
|
|
|
|
memset(&bind_addr, 0, sizeof(bind_addr));
|
|
|
|
bind_addr.sin_family = AF_INET;
|
|
|
|
bind_addr.sin_addr.s_addr = inet_addr(config.ipAddr);
|
|
|
|
|
|
|
|
ret = bind(sock_fd, (struct sockaddr *)&bind_addr, sizeof(bind_addr));
|
|
|
|
if (ret < 0) {
|
|
|
|
log_message(LOG_ERR, "Could not bind local address \"%\" because of %s",
|
|
|
|
config.ipAddr,
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-23 09:18:43 +08:00
|
|
|
if (connect(sock_fd, (struct sockaddr *) &port_info, sizeof(port_info)) < 0) {
|
2001-11-22 08:19:18 +08:00
|
|
|
log_message(LOG_ERR, "opensock: connect() error \"%s\".",
|
|
|
|
strerror(errno));
|
2000-09-12 07:56:32 +08:00
|
|
|
return -1;
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return sock_fd;
|
|
|
|
}
|
|
|
|
|
2000-09-12 07:56:32 +08:00
|
|
|
/*
|
|
|
|
* Set the socket to non blocking -rjkaes
|
|
|
|
*/
|
2001-11-22 08:19:18 +08:00
|
|
|
int
|
|
|
|
socket_nonblocking(int sock)
|
2000-02-17 01:32:49 +08:00
|
|
|
{
|
2000-09-12 07:56:32 +08:00
|
|
|
int flags;
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2001-05-24 02:01:23 +08:00
|
|
|
assert(sock >= 0);
|
|
|
|
|
2000-09-12 07:56:32 +08:00
|
|
|
flags = fcntl(sock, F_GETFL, 0);
|
|
|
|
return fcntl(sock, F_SETFL, flags | O_NONBLOCK);
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-09-12 07:56:32 +08:00
|
|
|
* Set the socket to blocking -rjkaes
|
2000-02-17 01:32:49 +08:00
|
|
|
*/
|
2001-11-22 08:19:18 +08:00
|
|
|
int
|
|
|
|
socket_blocking(int sock)
|
2000-02-17 01:32:49 +08:00
|
|
|
{
|
2000-09-12 07:56:32 +08:00
|
|
|
int flags;
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2001-05-24 02:01:23 +08:00
|
|
|
assert(sock >= 0);
|
|
|
|
|
2000-09-12 07:56:32 +08:00
|
|
|
flags = fcntl(sock, F_GETFL, 0);
|
|
|
|
return fcntl(sock, F_SETFL, flags & ~O_NONBLOCK);
|
|
|
|
}
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2000-09-12 07:56:32 +08:00
|
|
|
/*
|
|
|
|
* Start listening to a socket. Create a socket with the selected port.
|
|
|
|
* The size of the socket address will be returned to the caller through
|
|
|
|
* the pointer, while the socket is returned as a default return.
|
|
|
|
* - rjkaes
|
|
|
|
*/
|
2001-11-22 08:19:18 +08:00
|
|
|
int
|
|
|
|
listen_sock(uint16_t port, socklen_t * addrlen)
|
2000-09-12 07:56:32 +08:00
|
|
|
{
|
|
|
|
int listenfd;
|
|
|
|
const int on = 1;
|
|
|
|
struct sockaddr_in addr;
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2001-05-24 02:01:23 +08:00
|
|
|
assert(port > 0);
|
|
|
|
assert(addrlen != NULL);
|
|
|
|
|
2000-09-12 07:56:32 +08:00
|
|
|
listenfd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2000-09-12 07:56:32 +08:00
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons(port);
|
2000-02-17 01:32:49 +08:00
|
|
|
|
|
|
|
if (config.ipAddr) {
|
2000-09-12 07:56:32 +08:00
|
|
|
addr.sin_addr.s_addr = inet_addr(config.ipAddr);
|
2000-02-17 01:32:49 +08:00
|
|
|
} else {
|
2000-09-12 07:56:32 +08:00
|
|
|
addr.sin_addr.s_addr = inet_addr("0.0.0.0");
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|
2001-11-22 08:19:18 +08:00
|
|
|
|
|
|
|
bind(listenfd, (struct sockaddr *) &addr, sizeof(addr));
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2000-09-12 07:56:32 +08:00
|
|
|
listen(listenfd, MAXLISTEN);
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2000-09-12 07:56:32 +08:00
|
|
|
*addrlen = sizeof(addr);
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2000-09-12 07:56:32 +08:00
|
|
|
return listenfd;
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Takes a socket descriptor and returns the string contain the peer's
|
|
|
|
* IP address.
|
|
|
|
*/
|
2001-11-22 08:19:18 +08:00
|
|
|
char *
|
|
|
|
getpeer_ip(int fd, char *ipaddr)
|
2000-02-17 01:32:49 +08:00
|
|
|
{
|
|
|
|
struct sockaddr_in name;
|
2001-05-27 10:31:20 +08:00
|
|
|
size_t namelen = sizeof(name);
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2001-05-24 02:01:23 +08:00
|
|
|
assert(fd >= 0);
|
|
|
|
assert(ipaddr != NULL);
|
|
|
|
|
2001-10-23 11:57:34 +08:00
|
|
|
/*
|
|
|
|
* Make sure the user's buffer is initialized to an empty string.
|
|
|
|
*/
|
|
|
|
*ipaddr = '\0';
|
|
|
|
|
2001-11-22 08:19:18 +08:00
|
|
|
if (getpeername(fd, (struct sockaddr *) &name, &namelen) != 0) {
|
|
|
|
log_message(LOG_ERR, "getpeer_ip: getpeername() error \"%s\".",
|
|
|
|
strerror(errno));
|
2000-02-17 01:32:49 +08:00
|
|
|
} else {
|
2000-09-12 07:56:32 +08:00
|
|
|
strlcpy(ipaddr,
|
2001-11-22 08:19:18 +08:00
|
|
|
inet_ntoa(*(struct in_addr *) &name.sin_addr.s_addr),
|
2000-09-12 07:56:32 +08:00
|
|
|
PEER_IP_LENGTH);
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ipaddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Takes a socket descriptor and returns the string containing the peer's
|
|
|
|
* address.
|
|
|
|
*/
|
2001-11-22 08:19:18 +08:00
|
|
|
char *
|
|
|
|
getpeer_string(int fd, char *string)
|
2000-02-17 01:32:49 +08:00
|
|
|
{
|
|
|
|
struct sockaddr_in name;
|
2001-05-27 10:31:20 +08:00
|
|
|
size_t namelen = sizeof(name);
|
2000-02-17 01:32:49 +08:00
|
|
|
struct hostent *peername;
|
|
|
|
|
2001-05-24 02:01:23 +08:00
|
|
|
assert(fd >= 0);
|
|
|
|
assert(string != NULL);
|
|
|
|
|
2001-10-23 11:57:34 +08:00
|
|
|
/*
|
|
|
|
* Make sure the user's buffer is initialized to an empty string.
|
|
|
|
*/
|
|
|
|
*string = '\0';
|
|
|
|
|
2001-11-22 08:19:18 +08:00
|
|
|
if (getpeername(fd, (struct sockaddr *) &name, &namelen) != 0) {
|
|
|
|
log_message(LOG_ERR,
|
|
|
|
"getpeer_string: getpeername() error \"%s\".",
|
|
|
|
strerror(errno));
|
2000-02-17 01:32:49 +08:00
|
|
|
} else {
|
2001-08-29 12:00:22 +08:00
|
|
|
LOCK();
|
2001-11-22 08:19:18 +08:00
|
|
|
peername = gethostbyaddr((char *) &name.sin_addr.s_addr,
|
|
|
|
sizeof(name.sin_addr.s_addr), AF_INET);
|
2001-08-29 12:00:22 +08:00
|
|
|
if (peername)
|
2000-09-12 07:56:32 +08:00
|
|
|
strlcpy(string, peername->h_name, PEER_STRING_LENGTH);
|
2001-10-23 11:57:34 +08:00
|
|
|
else
|
2001-11-22 08:19:18 +08:00
|
|
|
log_message(LOG_ERR,
|
|
|
|
"getpeer_string: gethostbyaddr() error \"%s\".",
|
|
|
|
hstrerror(h_errno));
|
2001-10-23 11:57:34 +08:00
|
|
|
|
2001-08-29 12:00:22 +08:00
|
|
|
UNLOCK();
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2001-09-17 04:11:07 +08:00
|
|
|
/*
|
|
|
|
* Write the buffer to the socket. If an EINTR occurs, pick up and try
|
2001-12-16 04:04:04 +08:00
|
|
|
* again. Keep sending until the buffer has been sent.
|
2001-09-17 04:11:07 +08:00
|
|
|
*/
|
2001-11-22 08:19:18 +08:00
|
|
|
ssize_t
|
2001-12-16 04:04:04 +08:00
|
|
|
safe_write(int fd, const char *buffer, size_t count)
|
2001-09-17 04:11:07 +08:00
|
|
|
{
|
|
|
|
ssize_t len;
|
2001-12-16 04:04:04 +08:00
|
|
|
size_t bytestosend;
|
2001-11-22 08:19:18 +08:00
|
|
|
|
2001-12-17 08:00:24 +08:00
|
|
|
assert(fd >= 0);
|
|
|
|
assert(buffer != NULL);
|
|
|
|
assert(count > 0);
|
|
|
|
|
2001-12-16 04:04:04 +08:00
|
|
|
bytestosend = count;
|
2001-09-17 04:11:07 +08:00
|
|
|
|
2001-12-16 04:04:04 +08:00
|
|
|
while (1) {
|
2001-12-20 04:41:28 +08:00
|
|
|
len = send(fd, buffer, bytestosend, MSG_NOSIGNAL);
|
2001-12-16 04:04:04 +08:00
|
|
|
|
|
|
|
if (len < 0) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len == bytestosend)
|
|
|
|
break;
|
|
|
|
|
|
|
|
buffer += len;
|
|
|
|
bytestosend -= len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
2001-09-17 04:11:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Matched pair for safe_write(). If an EINTR occurs, pick up and try
|
|
|
|
* again.
|
|
|
|
*/
|
2001-11-22 08:19:18 +08:00
|
|
|
ssize_t
|
2001-12-16 04:04:04 +08:00
|
|
|
safe_read(int fd, char *buffer, size_t count)
|
2001-09-17 04:11:07 +08:00
|
|
|
{
|
|
|
|
ssize_t len;
|
|
|
|
|
|
|
|
do {
|
|
|
|
len = read(fd, buffer, count);
|
|
|
|
} while (len < 0 && errno == EINTR);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2001-12-24 08:01:32 +08:00
|
|
|
/*
|
|
|
|
* Send a "message" to the file descriptor provided. This handles the
|
|
|
|
* differences between the various implementations of vsnprintf. This code
|
|
|
|
* was basically stolen from the snprintf() man page of Debian Linux
|
|
|
|
* (although I did fix a memory leak. :)
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
write_message(int fd, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
ssize_t n;
|
|
|
|
size_t size = (1024 * 8); /* start with 8 KB and go from there */
|
|
|
|
char *buf, *tmpbuf;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if ((buf = safemalloc(size)) == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
va_start(ap, fmt);
|
|
|
|
n = vsnprintf(buf, size, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
/* If that worked, break out so we can send the buffer */
|
|
|
|
if (n > -1 && n < size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Else, try again with more space */
|
|
|
|
if (n > -1)
|
|
|
|
/* precisely what is needed (glibc2.1) */
|
|
|
|
size = n + 1;
|
|
|
|
else
|
|
|
|
/* twice the old size (glibc2.0) */
|
|
|
|
size *= 2;
|
|
|
|
|
|
|
|
if ((tmpbuf = saferealloc(buf, size)) == NULL) {
|
|
|
|
safefree(buf);
|
|
|
|
return -1;
|
|
|
|
} else
|
|
|
|
buf = tmpbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (safe_write(fd, buf, n) < 0) {
|
|
|
|
DEBUG2("Error in write_message(): %d", fd);
|
|
|
|
|
|
|
|
safefree(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
safefree(buf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-05-27 10:31:20 +08:00
|
|
|
/*
|
2001-11-22 08:19:18 +08:00
|
|
|
* Read in a "line" from the socket. It might take a few loops through
|
|
|
|
* the read sequence. The full string is allocate off the heap and stored
|
|
|
|
* at the whole_buffer pointer. The caller needs to free the memory when
|
|
|
|
* it is no longer in use. The returned line is NULL terminated.
|
|
|
|
*
|
|
|
|
* Returns the length of the buffer on success (not including the NULL
|
|
|
|
* termination), 0 if the socket was closed, and -1 on all other errors.
|
2001-05-27 10:31:20 +08:00
|
|
|
*/
|
2001-11-22 08:19:18 +08:00
|
|
|
#define SEGMENT_LEN (512)
|
2001-11-25 10:21:46 +08:00
|
|
|
#define MAXIMUM_BUFFER_LENGTH (128 * 1024)
|
2001-11-22 08:19:18 +08:00
|
|
|
ssize_t
|
|
|
|
readline(int fd, char **whole_buffer)
|
2000-02-17 01:32:49 +08:00
|
|
|
{
|
2001-11-22 08:19:18 +08:00
|
|
|
ssize_t whole_buffer_len;
|
|
|
|
char buffer[SEGMENT_LEN];
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
ssize_t ret;
|
|
|
|
ssize_t diff;
|
|
|
|
|
|
|
|
struct read_lines_s {
|
|
|
|
char *data;
|
|
|
|
size_t len;
|
|
|
|
struct read_lines_s *next;
|
|
|
|
};
|
|
|
|
struct read_lines_s *first_line, *line_ptr;
|
|
|
|
|
2001-11-23 09:18:43 +08:00
|
|
|
first_line = safecalloc(sizeof(struct read_lines_s), 1);
|
2001-11-22 08:19:18 +08:00
|
|
|
if (!first_line)
|
|
|
|
return -ENOMEMORY;
|
|
|
|
|
|
|
|
line_ptr = first_line;
|
|
|
|
|
|
|
|
whole_buffer_len = 0;
|
|
|
|
for (;;) {
|
|
|
|
ret = recv(fd, buffer, SEGMENT_LEN, MSG_PEEK);
|
|
|
|
if (ret <= 0)
|
|
|
|
goto CLEANUP;
|
|
|
|
|
|
|
|
ptr = memchr(buffer, '\n', ret);
|
|
|
|
if (ptr)
|
|
|
|
diff = ptr - buffer + 1;
|
|
|
|
else
|
|
|
|
diff = ret;
|
2000-09-12 07:56:32 +08:00
|
|
|
|
2001-11-22 08:19:18 +08:00
|
|
|
whole_buffer_len += diff;
|
|
|
|
|
2001-11-25 10:21:46 +08:00
|
|
|
/*
|
|
|
|
* Don't allow the buffer to grow without bound. If we
|
|
|
|
* get to more than MAXIMUM_BUFFER_LENGTH close.
|
|
|
|
*/
|
|
|
|
if (whole_buffer_len > MAXIMUM_BUFFER_LENGTH) {
|
|
|
|
ret = -EOUTRANGE;
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
|
2001-11-23 09:18:43 +08:00
|
|
|
line_ptr->data = safemalloc(diff);
|
2001-11-22 08:19:18 +08:00
|
|
|
if (!line_ptr->data) {
|
|
|
|
ret = -ENOMEMORY;
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
recv(fd, line_ptr->data, diff, 0);
|
|
|
|
line_ptr->len = diff;
|
|
|
|
|
|
|
|
if (ptr) {
|
|
|
|
line_ptr->next = NULL;
|
|
|
|
break;
|
2000-04-01 04:10:13 +08:00
|
|
|
}
|
2001-11-22 08:19:18 +08:00
|
|
|
|
2001-11-23 09:18:43 +08:00
|
|
|
line_ptr->next = safecalloc(sizeof(struct read_lines_s), 1);
|
2001-11-22 08:19:18 +08:00
|
|
|
if (!line_ptr->next) {
|
|
|
|
ret = -ENOMEMORY;
|
|
|
|
goto CLEANUP;
|
|
|
|
}
|
|
|
|
line_ptr = line_ptr->next;
|
2000-04-01 04:10:13 +08:00
|
|
|
}
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2001-11-23 09:18:43 +08:00
|
|
|
*whole_buffer = safemalloc(whole_buffer_len + 1);
|
2001-11-22 08:19:18 +08:00
|
|
|
if (!*whole_buffer)
|
|
|
|
return -ENOMEMORY;
|
|
|
|
|
|
|
|
*(*whole_buffer + whole_buffer_len) = '\0';
|
|
|
|
|
|
|
|
whole_buffer_len = 0;
|
|
|
|
line_ptr = first_line;
|
|
|
|
while (line_ptr) {
|
|
|
|
memcpy(*whole_buffer + whole_buffer_len, line_ptr->data,
|
|
|
|
line_ptr->len);
|
|
|
|
whole_buffer_len += line_ptr->len;
|
|
|
|
|
|
|
|
line_ptr = line_ptr->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = whole_buffer_len;
|
|
|
|
|
|
|
|
CLEANUP:
|
|
|
|
do {
|
|
|
|
line_ptr = first_line->next;
|
2001-11-23 09:18:43 +08:00
|
|
|
safefree(first_line->data);
|
|
|
|
safefree(first_line);
|
2001-11-22 08:19:18 +08:00
|
|
|
first_line = line_ptr;
|
|
|
|
} while (first_line);
|
2001-10-25 13:10:32 +08:00
|
|
|
|
2001-11-22 08:19:18 +08:00
|
|
|
return ret;
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|