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)
37 extern int requestlen;
42 int SockOpen(const char *host, const char *service)
45 struct addrinfo *ai, req;
47 memset(&req, 0, sizeof(struct addrinfo));
48 req.ai_socktype = SOCK_STREAM;
50 if (i = getaddrinfo(host, service, &req, &ai)) {
51 fprintf(stderr, "fetchmail: getaddrinfo(%s.%s): %s(%d)\n", host, service, gai_strerror(i), i);
56 i = inner_connect(ai, request, requestlen, NULL, NULL, "fetchmail", NULL);
58 i = inner_connect(ai, NULL, 0, NULL, NULL, "fetchmail", NULL);
67 #ifdef INADDR_BROADCAST
68 #define INADDR_NONE INADDR_BROADCAST
70 #define INADDR_NONE -1
73 #endif /* INET_ATON */
75 int SockOpen(const char *host, int clientPort)
80 #endif /* INET_ATON */
81 struct sockaddr_in ad;
84 memset(&ad, 0, sizeof(ad));
85 ad.sin_family = AF_INET;
87 /* we'll accept a quad address */
89 inaddr = inet_addr(host);
90 if (inaddr != INADDR_NONE)
91 memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
94 if (!inet_aton(host, &ad.sin_addr))
95 #endif /* INET_ATON */
97 hp = gethostbyname(host);
100 * Add a check to make sure the address has a valid IPv4 or IPv6
101 * length. This prevents buffer spamming by a broken DNS.
103 if (hp == NULL || (hp->h_length != 4 && hp->h_length != 8))
107 * FIXME: make this work for multihomed hosts.
108 * We're toast if we get back multiple addresses and h_addrs[0]
109 * (aka h_addr) is not one we can actually connect to; this happens
110 * with multi-homed boxen.
112 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
114 ad.sin_port = htons(clientPort);
116 sock = socket(AF_INET, SOCK_STREAM, 0);
119 if (connect(sock, (struct sockaddr *) &ad, sizeof(ad)) < 0)
130 #if defined(HAVE_STDARG_H)
131 int SockPrintf(int sock, char* format, ...)
134 int SockPrintf(sock,format,va_alist)
143 #if defined(HAVE_STDARG_H)
144 va_start(ap, format) ;
148 #ifdef HAVE_VSNPRINTF
149 vsnprintf(buf, sizeof(buf), format, ap);
151 vsprintf(buf, format, ap);
154 return SockWrite(sock, buf, strlen(buf));
158 int SockWrite(int sock, char *buf, int len)
164 n = write(sock, buf, len);
174 int SockRead(int sock, char *buf, int len)
176 char *newline, *bp = buf;
183 * The reason for these gymnastics is that we want two things:
184 * (1) to read \n-terminated lines,
185 * (2) to return the true length of data read, even if the
186 * data coming in has embedded NULS.
188 if ((n = recv(sock, bp, len, MSG_PEEK)) <= 0)
190 if ((newline = memchr(bp, '\n', n)) != NULL)
191 n = newline - bp + 1;
192 if ((n = read(sock, bp, n)) == -1)
202 int SockPeek(int sock)
203 /* peek at the next socket character without actually reading it */
208 if ((n = recv(sock, &ch, 1, MSG_PEEK)) == -1)
216 * Use the chargen service to test input beuffering directly.
217 * You may have to uncomment the `chargen' service description in your
218 * inetd.conf (and then SIGHUP inetd) for this to work.
222 int sock = SockOpen("localhost", 19);
225 while (SockRead(sock, buf, sizeof(buf)-1))
226 SockWrite(1, buf, strlen(buf));
230 /* socket.c ends here */