/*
Calisto (c) 1998-1999 Peter Howkins, Matthew Howkins, Simon Howkins
$Id: socks.c,v 1.3 2000/03/02 21:17:59 peter Exp $
$Log: socks.c,v $
Revision 1.3 2000/03/02 21:17:59 peter
Now uses msnprintf()
Revision 1.2 1999/12/16 17:53:13 peter
Fixed bug in logging to file
Revision 1.1 1999/11/29 22:08:22 peter
Initial revision
*/
static char rcsid[] = "$Id: socks.c,v 1.3 2000/03/02 21:17:59 peter Exp $";
/* Ansi Includes */
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
/* Unix includes */
#include "sys/types.h"
#include "sys/socket.h"
#include "netinet/in.h"
#include "unistd.h"
/* Our includes */
#include "log.h"
#include "globals.h"
#include "msnprintf.h"
#include "structs.h"
/* Gets as many characters as possible into the buffer.
There may be nothing, some characters but not a whole line,
possibly a whole line, or several lines including bits.
Returns -1 for problem with socket
0 for no characters available, but connection OK
>0 number of characters placed in buffer - no guarantee of how many lines
*/
int get_some_from_socket(int sfd, char *buffer, size_t bufsize)
{
int size, count = 0;
char c;
while (count < bufsize) {
size = recv(sfd, &c, 1, 0);
switch (size) {
case -1: /* No character read, but connection OK */
return count;
case 0: /* No character read because connection broken */
return -1;
}
/* Character read OK */
*buffer++ = c;
count++;
}
return count;
}
int send_to_socket(int sfd, const char *format, ...) {
va_list ptr;
char gap[OUTPUT_BUFFER];
int check;
va_start(ptr, format);
mvsnprintf(gap, sizeof(gap), format, ptr);
va_end(ptr);
check = send(sfd, gap, strlen(gap),0);
if (check == -1) {
log(usage, "Error writing to socket %d: %s", sfd, strerror(errno));
return 1;
}
return 0;
}