2 * socket.c -- socket library functions
4 * For license terms, see the file COPYING in this directory.
12 #endif /* HAVE_MEMORY_H */
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
18 #if defined(STDC_HEADERS)
21 #if defined(HAVE_UNISTD_H)
24 #if defined(HAVE_STDARG_H)
32 #include <net/security.h>
33 #endif /* NET_SECURITY */
36 int SockOpen(const char *host, const char *service, const char *options)
39 struct addrinfo *ai, req;
43 #endif /* NET_SECURITY */
45 memset(&req, 0, sizeof(struct addrinfo));
46 req.ai_socktype = SOCK_STREAM;
48 if (i = getaddrinfo(host, service, &req, &ai)) {
49 fprintf(stderr, "fetchmail: getaddrinfo(%s.%s): %s(%d)\n", host, service, gai_strerror(i), i);
57 if (net_security_strtorequest((char *)options, &request, &requestlen))
60 i = inner_connect(ai, request, requestlen, NULL,NULL, "fetchmail", NULL);
65 #else /* NET_SECURITY */
66 i = inner_connect(ai, NULL, 0, NULL, NULL, "fetchmail", NULL);
67 #endif /* NET_SECURITY */
76 #ifdef INADDR_BROADCAST
77 #define INADDR_NONE INADDR_BROADCAST
79 #define INADDR_NONE -1
82 #endif /* INET_ATON */
84 int SockOpen(const char *host, int clientPort, const char *options)
89 #endif /* INET_ATON */
90 struct sockaddr_in ad;
93 memset(&ad, 0, sizeof(ad));
94 ad.sin_family = AF_INET;
96 /* we'll accept a quad address */
98 inaddr = inet_addr(host);
99 if (inaddr != INADDR_NONE)
100 memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
103 if (!inet_aton(host, &ad.sin_addr))
104 #endif /* INET_ATON */
106 hp = gethostbyname(host);
109 * Add a check to make sure the address has a valid IPv4 or IPv6
110 * length. This prevents buffer spamming by a broken DNS.
112 if (hp == NULL || (hp->h_length != 4 && hp->h_length != 8))
116 * FIXME: make this work for multihomed hosts.
117 * We're toast if we get back multiple addresses and h_addrs[0]
118 * (aka h_addr) is not one we can actually connect to; this happens
119 * with multi-homed boxen.
121 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
123 ad.sin_port = htons(clientPort);
125 sock = socket(AF_INET, SOCK_STREAM, 0);
128 if (connect(sock, (struct sockaddr *) &ad, sizeof(ad)) < 0)
139 #if defined(HAVE_STDARG_H)
140 int SockPrintf(int sock, char* format, ...)
143 int SockPrintf(sock,format,va_alist)
152 #if defined(HAVE_STDARG_H)
153 va_start(ap, format) ;
157 #ifdef HAVE_VSNPRINTF
158 vsnprintf(buf, sizeof(buf), format, ap);
160 vsprintf(buf, format, ap);
163 return SockWrite(sock, buf, strlen(buf));
167 int SockWrite(int sock, char *buf, int len)
173 n = write(sock, buf, len);
183 int SockRead(int sock, char *buf, int len)
185 char *newline, *bp = buf;
192 * The reason for these gymnastics is that we want two things:
193 * (1) to read \n-terminated lines,
194 * (2) to return the true length of data read, even if the
195 * data coming in has embedded NULS.
197 if ((n = recv(sock, bp, len, MSG_PEEK)) <= 0)
199 if ((newline = memchr(bp, '\n', n)) != NULL)
200 n = newline - bp + 1;
201 if ((n = read(sock, bp, n)) == -1)
211 int SockPeek(int sock)
212 /* peek at the next socket character without actually reading it */
217 if ((n = recv(sock, &ch, 1, MSG_PEEK)) == -1)
225 * Use the chargen service to test input beuffering directly.
226 * You may have to uncomment the `chargen' service description in your
227 * inetd.conf (and then SIGHUP inetd) for this to work.
231 int sock = SockOpen("localhost", 19, NULL);
234 while (SockRead(sock, buf, sizeof(buf)-1))
235 SockWrite(1, buf, strlen(buf));
239 /* socket.c ends here */