]> Pileus Git - ~andy/fetchmail/blob - socket.c
Cameron's speedup diff for socket.c.
[~andy/fetchmail] / socket.c
1 /*
2  * socket.c -- socket library functions
3  *
4  * For license terms, see the file COPYING in this directory.
5  */
6
7 #include <config.h>
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 #include <netdb.h>
16 #if defined(STDC_HEADERS)
17 #include <stdlib.h>
18 #endif
19 #if defined(HAVE_UNISTD_H)
20 #include <unistd.h>
21 #endif
22 #if defined(HAVE_STDARG_H)
23 #include <stdarg.h>
24 #else
25 #include <varargs.h>
26 #endif
27 #include "socket.h"
28
29 #ifndef  INADDR_NONE
30 #ifdef   INADDR_BROADCAST
31 #define  INADDR_NONE    INADDR_BROADCAST
32 #else
33 #define  INADDR_NONE    -1
34 #endif
35 #endif
36
37 /*
38  * There  are, in effect, two different implementations here.  One
39  * uses read(2) and write(2) directly with no buffering, the other
40  * uses stdio with line buffering (for better throughput).  Both
41  * are known to work under Linux.  You get the former by configuring
42  * with --disable-stdio, the latter by configuring with --enable-stdio
43  * or by default.
44  */
45
46 #ifdef USE_STDIO
47 /*
48  * Size of buffer for internal buffering read function 
49  * don't increase beyond the maximum atomic read/write size for
50  * your sockets, or you'll take a potentially huge performance hit
51  */
52 #define  INTERNAL_BUFSIZE       2048
53 #endif /* USE_STDIO */
54
55 FILE *SockOpen(char *host, int clientPort)
56 {
57     int sock;
58     unsigned long inaddr;
59     struct sockaddr_in ad;
60     struct hostent *hp;
61 #ifdef USE_STDIO
62     FILE *fp;
63 #endif /* USE_STDIO */
64
65     memset(&ad, 0, sizeof(ad));
66     ad.sin_family = AF_INET;
67
68     inaddr = inet_addr(host);
69     if (inaddr != INADDR_NONE)
70         memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
71     else
72     {
73         hp = gethostbyname(host);
74         if (hp == NULL)
75             return (FILE *)NULL;
76         memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
77     }
78     ad.sin_port = htons(clientPort);
79     
80     sock = socket(AF_INET, SOCK_STREAM, 0);
81     if (sock < 0)
82         return (FILE *)NULL;
83     if (connect(sock, (struct sockaddr *) &ad, sizeof(ad)) < 0)
84     {
85         close(sock);
86         return (FILE *)NULL;
87     }
88
89 #ifndef USE_STDIO
90     return fdopen(sock, "r+");
91 #else
92     fp = fdopen(sock, "r+");
93
94     setvbuf(fp, NULL, _IOLBF, INTERNAL_BUFSIZE);
95
96     return(fp);
97 #endif /* USE_STDIO */
98 }
99
100
101 #if defined(HAVE_STDARG_H)
102 int SockPrintf(FILE *sockfp, char* format, ...)
103 {
104 #else
105 int SockPrintf(sockfp,format,va_alist)
106 FILE *sockfp;
107 char *format;
108 va_dcl {
109 #endif
110
111     va_list ap;
112     char buf[8192];
113
114 #if defined(HAVE_STDARG_H)
115     va_start(ap, format) ;
116 #else
117     va_start(ap);
118 #endif
119     vsprintf(buf, format, ap);
120     va_end(ap);
121     return SockWrite(buf, 1, strlen(buf), sockfp);
122
123 }
124
125 #ifndef USE_STDIO
126
127 int SockWrite(char *buf, int size, int len, FILE *sockfp)
128 {
129     int n, wrlen = 0;
130
131     len *= size;
132     while (len)
133     {
134         n = write(fileno(sockfp), buf, len);
135         if (n <= 0)
136             return -1;
137         len -= n;
138         wrlen += n;
139         buf += n;
140     }
141     return wrlen;
142 }
143
144 char *SockGets(char *buf, int len, FILE *sockfp)
145 {
146     char *p, *bp = buf;
147     int n;
148
149     if (--len < 1)
150         return NULL;
151     do {
152         if ((n = recv(fileno(sockfp), bp, len, MSG_PEEK)) == -1)
153             return NULL;
154         if ((p = memchr(bp, '\n', n)) != NULL)
155         {
156             if (read(fileno(sockfp), bp, ++p - bp) == -1)
157                 return NULL;
158             *p = '\0';
159             return buf;
160         }
161         if ((n = read(fileno(sockfp), bp, n)) == -1)
162             return NULL;
163         bp += n;
164         len -= n;
165     } while (len);
166     *bp = '\0';
167     return buf;
168 }
169
170 #else
171
172 int SockWrite(char *buf, int size, int len, FILE *sockfp)
173 {
174     int n = fwrite(buf, size, len, sockfp);
175
176     fseek(sockfp, 0L, SEEK_CUR);        /* required by POSIX */
177
178     return(n);
179 }
180
181 char *SockGets(char *buf, int len, FILE *sockfp)
182 {
183     char *in = fgets(buf, len, sockfp);
184
185 #ifndef linux
186     /*
187      * Weirdly, this actually wedges under Linux (2.0.27 with libc 5.3.12-8).
188      * It's apparently good under NEXTSTEP.
189      */
190     fseek(sockfp, 0L, SEEK_CUR);        /* required by POSIX */
191 #endif /* linux */
192
193     return(in);
194 }
195
196 #endif
197
198 #ifdef MAIN
199 /*
200  * Use the chargen service to test input beuffering directly.
201  * You may have to uncomment the `chargen' service description in your
202  * inetd.conf (and then SIGHUP inetd) for this to work. 
203  */
204 main()
205 {
206     FILE        *fp = SockOpen("localhost", 19);
207     char        buf[80];
208
209     while (SockGets(buf, sizeof(buf)-1, fp))
210         SockWrite(buf, 1, strlen(buf), stdout);
211 }
212 #endif /* MAIN */
213
214 /* socket.c ends here */