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))
106 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
108 ad.sin_port = htons(clientPort);
110 sock = socket(AF_INET, SOCK_STREAM, 0);
113 if (connect(sock, (struct sockaddr *) &ad, sizeof(ad)) < 0)
124 #if defined(HAVE_STDARG_H)
125 int SockPrintf(int sock, char* format, ...)
128 int SockPrintf(sock,format,va_alist)
137 #if defined(HAVE_STDARG_H)
138 va_start(ap, format) ;
142 #ifdef HAVE_VSNPRINTF
143 vsnprintf(buf, sizeof(buf), format, ap);
145 vsprintf(buf, format, ap);
148 return SockWrite(sock, buf, strlen(buf));
152 int SockWrite(int sock, char *buf, int len)
158 n = write(sock, buf, len);
168 int SockRead(int sock, char *buf, int len)
170 char *newline, *bp = buf;
177 * The reason for these gymnastics is that we want two things:
178 * (1) to read \n-terminated lines,
179 * (2) to return the true length of data read, even if the
180 * data coming in has embedded NULS.
182 if ((n = recv(sock, bp, len, MSG_PEEK)) <= 0)
184 if ((newline = memchr(bp, '\n', n)) != NULL)
185 n = newline - bp + 1;
186 if ((n = read(sock, bp, n)) == -1)
196 int SockPeek(int sock)
197 /* peek at the next socket character without actually reading it */
202 if ((n = recv(sock, &ch, 1, MSG_PEEK)) == -1)
210 * Use the chargen service to test input beuffering directly.
211 * You may have to uncomment the `chargen' service description in your
212 * inetd.conf (and then SIGHUP inetd) for this to work.
216 int sock = SockOpen("localhost", 19);
219 while (SockRead(sock, buf, sizeof(buf)-1))
220 SockWrite(1, buf, strlen(buf));
224 /* socket.c ends here */