Files

332 lines
7.1 KiB
C
Raw Permalink Normal View History

2010-02-02 23:20:29 +00:00
/** \file shared/sockets.c
* Socket functions available to server and clients.
*/
/*-
* This file is part of LCDproc.
*
* Feel free to use this in your own clients... :)
*/
1999-12-09 23:15:14 +00:00
#include <unistd.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
1999-12-09 23:15:14 +00:00
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <stdarg.h>
1999-12-09 23:15:14 +00:00
#include <fcntl.h>
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
2001-11-29 14:09:13 +00:00
#include "report.h"
1999-12-09 23:15:14 +00:00
#include "sockets.h"
// Length of longest transmission allowed at once...
#define MAXMSG 8192
typedef struct sockaddr_in sockaddr_in;
2010-02-02 23:20:29 +00:00
/**
* Tries to resolve a resolve a hostname.
* \param name Pointer to resolves IP-address
* \param hostname Hostname or IP-address (as string)
* \param port Port number
* \return 0 on success, -1 on error.
*/
static int
sock_init_sockaddr (sockaddr_in *name, const char *hostname, unsigned short int port)
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
struct hostent *hostinfo;
memset (name, '\0', sizeof (*name));
2000-04-03 22:13:57 +00:00
name->sin_family = AF_INET;
name->sin_port = htons (port);
hostinfo = gethostbyname (hostname);
if (hostinfo == NULL) {
2001-11-29 14:09:13 +00:00
report (RPT_ERR, "sock_init_sockaddr: Unknown host %s.", hostname);
2000-04-03 22:13:57 +00:00
return -1;
}
name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
2000-04-03 22:13:57 +00:00
return 0;
1999-12-09 23:15:14 +00:00
}
2010-02-02 23:20:29 +00:00
/**
* Connect to server.
* \param host Hostname or IP-address
* \param port Port number
* \return socket file descriptor on success, -1 on error
*/
int
sock_connect (char *host, unsigned short int port)
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
struct sockaddr_in servername;
int sock;
int err = 0;
1999-12-09 23:15:14 +00:00
2001-11-29 14:09:13 +00:00
report (RPT_DEBUG, "sock_connect: Creating socket");
2000-04-03 22:13:57 +00:00
sock = socket (PF_INET, SOCK_STREAM, 0);
if (sock < 0) {
2001-11-29 14:09:13 +00:00
report (RPT_ERR, "sock_connect: Error creating socket");
2000-04-03 22:13:57 +00:00
return sock;
}
2001-11-29 14:09:13 +00:00
debug (RPT_DEBUG, "sock_connect: Created socket (%i)", sock);
if (sock_init_sockaddr (&servername, host, port) < 0)
return -1;
2000-04-03 22:13:57 +00:00
err = connect (sock, (struct sockaddr *) &servername, sizeof (servername));
if (err < 0) {
2001-11-29 14:09:13 +00:00
report (RPT_ERR, "sock_connect: connect failed");
shutdown (sock, SHUT_RDWR);
return -1;
2000-04-03 22:13:57 +00:00
}
2000-04-03 22:13:57 +00:00
fcntl (sock, F_SETFL, O_NONBLOCK);
2000-04-03 22:13:57 +00:00
return sock;
1999-12-09 23:15:14 +00:00
}
2010-02-02 23:20:29 +00:00
/**
* Disconnect from server.
* \param fd Socket file descriptor
* \return 0 on success, -1 on error.
*/
int
sock_close (int fd)
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
int err;
err = shutdown (fd, SHUT_RDWR);
2000-04-03 22:13:57 +00:00
if (!err)
close (fd);
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
return err;
1999-12-09 23:15:14 +00:00
}
2010-02-02 23:20:29 +00:00
/**
* Send printf-like formatted output.
* \param fd Socket file descriptor
* \param format Format string
* \param ... Arguments to the format string
* \return Number of bytes sent.
*/
int
sock_printf(int fd, const char *format, .../*args*/ )
{
char buf[MAXMSG];
va_list ap;
int size = 0;
va_start(ap, format);
size = vsnprintf(buf, sizeof(buf), format, ap);
va_end(ap);
if (size < 0) {
report(RPT_ERR, "sock_printf: vsnprintf failed");
return -1;
}
if (size > sizeof(buf))
report(RPT_WARNING, "sock_printf: vsnprintf truncated message");
return sock_send_string(fd, buf);
}
2010-02-02 23:20:29 +00:00
/**
* Send lines of text.
* \param fd Socket file descriptor
* \param string Pointer to the string to send.
* \return Number of bytes sent.
*/
int
sock_send_string (int fd, char *string)
1999-12-09 23:15:14 +00:00
{
return sock_send(fd, string, strlen(string));
1999-12-09 23:15:14 +00:00
}
2010-02-02 23:20:29 +00:00
/**
* Receive a line of text.
* Recv gives only one line per call...
* \param fd Socket file descriptor
* \param dest Pointer to buffer to store the received data
* \param maxlen Number of bytes to read at most (size of buffer)
* \return Number of bytes received.
*/
int
sock_recv_string (int fd, char *dest, size_t maxlen)
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
char *ptr = dest;
int recvBytes = 0;
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
if (!dest)
return -1;
if (maxlen <= 0)
return 0;
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
while (1) {
int err = read (fd, ptr, 1);
if (err == -1) {
if (errno == EAGAIN) {
if (recvBytes) {
2000-04-03 22:13:57 +00:00
// We've begun to read a string, but no bytes are
// available. Loop.
continue;
}
return 0;
} else {
2001-11-29 14:09:13 +00:00
report (RPT_ERR, "sock_recv_string: socket read error");
2000-04-03 22:13:57 +00:00
return err;
}
} else if (err == 0) {
return recvBytes;
2000-04-03 22:13:57 +00:00
}
2000-02-14 13:38:39 +00:00
recvBytes++;
2000-02-14 13:38:39 +00:00
// stop at max. bytes allowed, at NUL or at LF
if (recvBytes == maxlen || *ptr == '\0' || *ptr == '\n') {
*ptr = '\0';
2000-04-03 22:13:57 +00:00
break;
}
ptr++;
}
2000-02-14 13:38:39 +00:00
// Don't return an empty string
if (recvBytes == 1 && dest[0] == '\0')
2000-04-03 22:13:57 +00:00
return 0;
2000-02-14 13:38:39 +00:00
if (recvBytes < maxlen - 1)
dest[recvBytes] = '\0';
2000-02-14 13:38:39 +00:00
return recvBytes;
1999-12-09 23:15:14 +00:00
}
2010-02-02 23:20:29 +00:00
/**
* Send raw data.
* \param fd Socket file descriptor
* \param src Buffer holding the data to send
* \param size Number of bytes to send at most
* \return Number of bytes sent.
*/
int
sock_send (int fd, void *src, size_t size)
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
int offset = 0;
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
if (!src)
return -1;
2000-02-14 13:38:39 +00:00
2000-04-03 22:13:57 +00:00
while (offset != size) {
// write isn't guaranteed to send the entire string at once,
// so we have to sent it in a loop like this
int sent = write (fd, ((char *) src) + offset, size - offset);
2000-04-03 22:13:57 +00:00
if (sent == -1) {
if (errno != EAGAIN) {
2001-11-29 14:09:13 +00:00
report (RPT_ERR, "sock_send: socket write error");
report (RPT_DEBUG, "Message was: '%.*s'", size-offset, (char *) src);
2000-04-03 22:13:57 +00:00
return sent;
}
continue;
} else if (sent == 0) {
// when this returns zero, it generally means
// we got disconnected
return sent + offset;
}
2000-02-14 13:38:39 +00:00
2000-04-03 22:13:57 +00:00
offset += sent;
}
2000-02-14 13:38:39 +00:00
2000-04-03 22:13:57 +00:00
return offset;
1999-12-09 23:15:14 +00:00
}
2010-02-02 23:20:29 +00:00
/**
* Receive raw data.
* \param fd Socket file descriptor
* \param dest Pointer to buffer to store the received data
* \param maxlen Number of bytes to read at most (size of buffer)
* \return Number of bytes received.
*/
int
sock_recv (int fd, void *dest, size_t maxlen)
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
int err;
2000-04-03 22:13:57 +00:00
if (!dest)
return -1;
if (maxlen <= 0)
return 0;
2000-04-03 22:13:57 +00:00
err = read (fd, dest, maxlen);
if (err < 0) {
2001-11-29 14:09:13 +00:00
//report (RPT_DEBUG,"sock_recv: socket read error");
2000-04-03 22:13:57 +00:00
return err;
}
2001-11-29 14:09:13 +00:00
//debug(RPT_DEBUG, "sock_recv: Got message \"%s\"", (char *)dest);
2000-04-03 22:13:57 +00:00
return err;
1999-12-09 23:15:14 +00:00
}
/*****************************************************************************/
2010-02-02 23:20:29 +00:00
/**
* Return the error message for the last error occured.
* \return Error message string
*/
char*
sock_geterror(void)
{
return strerror(errno);
}
2010-02-02 23:20:29 +00:00
/**
* Send an already formatted error message to the client.
* \param fd socket
* \param message the message to send (without the "huh? ") */
2005-07-17 16:14:07 +00:00
int sock_send_error(int fd, char* message)
{
// simple: performance penalty isn't worth more work...
return sock_printf_error(fd, message);
}
2010-02-02 23:20:29 +00:00
/**
* Print printf-like formatted output to logfile and sends it to the client.
* \note don't add a the "huh? " to the message. This is done by this
2005-10-25 22:38:19 +00:00
* method
2010-02-02 23:20:29 +00:00
* \param fd socket
* \param format a printf format */
2005-07-17 16:14:07 +00:00
int
sock_printf_error(int fd, const char *format, .../*args*/ )
{
static const char huh[] = "huh? ";
2005-07-17 16:14:07 +00:00
char buf[MAXMSG];
va_list ap;
int size = 0;
strncpy(buf, huh, sizeof(huh)); // note: sizeof(huh) < MAXMSG
2005-07-17 16:14:07 +00:00
va_start(ap, format);
size = vsnprintf(buf + (sizeof(huh)-1), sizeof(buf) - (sizeof(huh)-1), format, ap);
buf[sizeof(buf)-1] = '\0';
2005-07-17 16:14:07 +00:00
va_end(ap);
if (size < 0) {
report(RPT_ERR, "sock_printf_error: vsnprintf failed");
return -1;
}
if (size >= sizeof(buf) - (sizeof(huh)-1))
2005-07-17 16:14:07 +00:00
report(RPT_WARNING, "sock_printf_error: vsnprintf truncated message");
report(RPT_ERR, "error: %s", buf);
return sock_send_string(fd, buf);
}