-/* Copyright 1993-95 by Carl Harris, Jr. Copyright 1996 by Eric S. Raymond
- * All rights reserved.
+/*
+ * socket.c -- socket library functions
+ *
* For license terms, see the file COPYING in this directory.
*/
-/***********************************************************************
- module: socket.c
- project: popclient
- programmer: Carl Harris, ceharris@mal.com
- description: socket library functions
-
- ***********************************************************************/
-
-#include <config.h>
-
-#include <fcntl.h>
+#include "config.h"
+#include <stdio.h>
+#include <string.h>
+#ifdef HAVE_MEMORY_H
+#include <memory.h>
+#endif /* HAVE_MEMORY_H */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
-#include <sys/time.h>
-#include <sys/ioctl.h>
#if defined(STDC_HEADERS)
-#include <string.h>
+#include <stdlib.h>
#endif
#if defined(HAVE_UNISTD_H)
#include <unistd.h>
#endif
-#include <stdlib.h>
+#if defined(HAVE_STDARG_H)
+#include <stdarg.h>
+#else
#include <varargs.h>
-#include <errno.h>
-#include "bzero.h"
+#endif
#include "socket.h"
-/* Size of buffer for internal buffering read function
- don't increase beyond the maximum atomic read/write size for
- your sockets, or you'll take a potentially huge performance hit */
+#if NET_SECURITY
+#include <net/security.h>
+#endif /* NET_SECURITY */
-#define INTERNAL_BUFSIZE 2048
+#if INET6
+int SockOpen(const char *host, const char *service, const char *options)
+{
+ int i;
+ struct addrinfo *ai, req;
+#if NET_SECURITY
+ void *request = NULL;
+ int requestlen;
+#endif /* NET_SECURITY */
+
+ memset(&req, 0, sizeof(struct addrinfo));
+ req.ai_socktype = SOCK_STREAM;
+
+ if (i = getaddrinfo(host, service, &req, &ai)) {
+ fprintf(stderr, "fetchmail: getaddrinfo(%s.%s): %s(%d)\n", host, service, gai_strerror(i), i);
+ return -1;
+ };
+#if NET_SECURITY
+ if (!options)
+ requestlen = 0;
+ else
+ if (net_security_strtorequest((char *)options, &request, &requestlen))
+ goto ret;
+
+ i = inner_connect(ai, request, requestlen, NULL,NULL, "fetchmail", NULL);
+ if (request)
+ free(request);
+
+ret:
+#else /* NET_SECURITY */
+ i = inner_connect(ai, NULL, 0, NULL, NULL, "fetchmail", NULL);
+#endif /* NET_SECURITY */
+
+ freeaddrinfo(ai);
+
+ return i;
+};
+#else /* INET6 */
+#ifndef INET_ATON
+#ifndef INADDR_NONE
+#ifdef INADDR_BROADCAST
+#define INADDR_NONE INADDR_BROADCAST
+#else
+#define INADDR_NONE -1
+#endif
+#endif
+#endif /* INET_ATON */
-int Socket(host, clientPort)
-char *host;
-int clientPort;
+int SockOpen(const char *host, int clientPort, const char *options)
{
int sock;
+#ifndef INET_ATON
unsigned long inaddr;
+#endif /* INET_ATON */
struct sockaddr_in ad;
struct hostent *hp;
-
+
memset(&ad, 0, sizeof(ad));
ad.sin_family = AF_INET;
+ /* we'll accept a quad address */
+#ifndef INET_ATON
inaddr = inet_addr(host);
if (inaddr != INADDR_NONE)
memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
else
+#else
+ if (!inet_aton(host, &ad.sin_addr))
+#endif /* INET_ATON */
{
hp = gethostbyname(host);
- if (hp == NULL)
+
+ /*
+ * Add a check to make sure the address has a valid IPv4 or IPv6
+ * length. This prevents buffer spamming by a broken DNS.
+ */
+ if (hp == NULL || (hp->h_length != 4 && hp->h_length != 8))
return -1;
+
+ /*
+ * FIXME: make this work for multihomed hosts.
+ * We're toast if we get back multiple addresses and h_addrs[0]
+ * (aka h_addr) is not one we can actually connect to; this happens
+ * with multi-homed boxen.
+ */
memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
}
ad.sin_port = htons(clientPort);
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
- return sock;
+ return -1;
if (connect(sock, (struct sockaddr *) &ad, sizeof(ad)) < 0)
+ {
+ close(sock);
return -1;
- return sock;
+ }
+
+ return(sock);
}
+#endif /* INET6 */
+
-int SockGets(socket,buf,len)
-int socket;
-char *buf;
-int len;
+#if defined(HAVE_STDARG_H)
+int SockPrintf(int sock, char* format, ...)
{
- int rdlen = 0;
+#else
+int SockPrintf(sock,format,va_alist)
+int sock;
+char *format;
+va_dcl {
+#endif
- while (--len)
- {
- if (SockInternalRead(socket, buf, 1) != 1)
- return -1;
- else
- rdlen++;
- if (*buf == '\n')
- break;
- if (*buf != '\r') /* remove all CRs */
- buf++;
- }
- *buf = 0;
- return rdlen;
-}
+ va_list ap;
+ char buf[8192];
-int SockPuts(socket,buf)
-int socket;
-char *buf;
-{
- int rc;
-
- if (rc = SockWrite(socket, buf, strlen(buf)))
- return rc;
- return SockWrite(socket, "\r\n", 2);
-}
+#if defined(HAVE_STDARG_H)
+ va_start(ap, format) ;
+#else
+ va_start(ap);
+#endif
+#ifdef HAVE_VSNPRINTF
+ vsnprintf(buf, sizeof(buf), format, ap);
+#else
+ vsprintf(buf, format, ap);
+#endif
+ va_end(ap);
+ return SockWrite(sock, buf, strlen(buf));
-int SockWrite(socket,buf,len)
-int socket;
-char *buf;
-int len;
-{
- int n;
-
- while (len)
- {
- n = write(socket, buf, len);
- if (n <= 0)
- return -1;
- len -= n;
- buf += n;
- }
- return 0;
}
-int SockRead(socket,buf,len)
-int socket;
-char *buf;
-int len;
+int SockWrite(int sock, char *buf, int len)
{
- int n;
-
+ int n, wrlen = 0;
+
while (len)
{
- n = SockInternalRead(socket, buf, len);
+ n = write(sock, buf, len);
if (n <= 0)
return -1;
len -= n;
- buf += n;
+ wrlen += n;
+ buf += n;
}
- return 0;
+ return wrlen;
}
-static int sbuflen = 0;
-
-int SockInternalRead (socket,buf,len)
-int socket;
-char *buf;
-int len;
+int SockRead(int sock, char *buf, int len)
{
- static char sbuf [INTERNAL_BUFSIZE];
- static char *bp;
-
- if (sbuflen == 0) {
- /* buffer is empty; refresh. */
- if ((sbuflen = read(socket,sbuf,INTERNAL_BUFSIZE)) < 0) {
- perror("SockInternalRead: read");
- exit(9);
- }
- else
- bp = sbuf;
- }
- else
- ; /* already some data in the buffer */
-
- /* can't get more than we have right now. */
- /* XXX -- should probably try to load any unused part of sbuf
- so that as much of 'len' as possible can be satisfied */
- if (len > sbuflen)
- len = sbuflen;
- else
- ; /* wants no more than we already have */
+ char *newline, *bp = buf;
+ int n;
- /* transfer to caller's buffer */
- if (len == 1) {
- /* special case: if caller only wants one character, it probably
- costs a lot more to call bcopy than to do it ourselves. */
- *buf = *(bp++);
- sbuflen--;
- }
- else {
- bcopy(bp,buf,len);
- sbuflen -= len;
- bp += len;
- }
- return(len);
+ if (--len < 1)
+ return(-1);
+ do {
+ /*
+ * The reason for these gymnastics is that we want two things:
+ * (1) to read \n-terminated lines,
+ * (2) to return the true length of data read, even if the
+ * data coming in has embedded NULS.
+ */
+ if ((n = recv(sock, bp, len, MSG_PEEK)) <= 0)
+ return(-1);
+ if ((newline = memchr(bp, '\n', n)) != NULL)
+ n = newline - bp + 1;
+ if ((n = read(sock, bp, n)) == -1)
+ return(-1);
+ bp += n;
+ len -= n;
+ } while
+ (!newline && len);
+ *bp = '\0';
+ return bp - buf;
}
-/* SockClearHeader: call this procedure in order to kill off any
- forthcoming Header info from the socket that we no longer want.
- */
-int SockClearHeader(socket)
-int socket;
+int SockPeek(int sock)
+/* peek at the next socket character without actually reading it */
{
- char *bufp;
- static char sbuf[INTERNAL_BUFSIZE];
- int nBytes;
- int res;
-
- if ((res = SockDataWaiting(socket)) <= 0)
- return res;
+ int n;
+ char ch;
- while (1)
- {
- if (SockGets(socket,sbuf,INTERNAL_BUFSIZE) < 0)
- return 0;
- bufp = sbuf;
- if (*bufp == '.') {
- bufp++;
- if (*bufp == 0)
- break;
- }
- }
- sbuflen = 0;
- return 0;
+ if ((n = recv(sock, &ch, 1, MSG_PEEK)) == -1)
+ return -1;
+ else
+ return(ch);
}
-
-/* SockDataWaiting: Return a non-zero value if this socket is waiting
- for data. */
-int SockDataWaiting(int socket)
+#ifdef MAIN
+/*
+ * Use the chargen service to test input beuffering directly.
+ * You may have to uncomment the `chargen' service description in your
+ * inetd.conf (and then SIGHUP inetd) for this to work.
+ */
+main()
{
- int flags;
- char sbuf[INTERNAL_BUFSIZE];
- int n;
- int res;
- flags = fcntl(socket,F_GETFL,0);
-
- /* set it to non-block */
- if (fcntl(socket,F_SETFL,flags | O_NONBLOCK) == -1)
- return -1;
+ int sock = SockOpen("localhost", 19, NULL);
+ char buf[80];
- if ((n = recv(socket,sbuf,INTERNAL_BUFSIZE,MSG_PEEK)) == -1)
- {
- /* No data to read. */
- if (errno == EWOULDBLOCK)
- res = 0;
- }
- else
- res = n;
-
- /* reset it to block (or, whatever it was). */
- fcntl(socket,F_SETFL,flags);
- return res;
+ while (SockRead(sock, buf, sizeof(buf)-1))
+ SockWrite(1, buf, strlen(buf));
}
+#endif /* MAIN */
-int SockPrintf(socket,format,va_alist)
-int socket;
-char *format;
-va_dcl {
-
- va_list ap;
- char buf[8192];
-
- va_start(ap);
- vsprintf(buf, format, ap);
- va_end(ap);
- return SockWrite(socket, buf, strlen(buf));
-}
+/* socket.c ends here */