]> Pileus Git - ~andy/fetchmail/blob - socket.c
Include test code.
[~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.
42  */
43 /* #define USE_STDIO */
44
45 #ifdef USE_STDIO
46 /*
47  * Size of buffer for internal buffering read function 
48  * don't increase beyond the maximum atomic read/write size for
49  * your sockets, or you'll take a potentially huge performance hit
50  */
51 #define  INTERNAL_BUFSIZE       2048
52 #endif /* USE_STDIO */
53
54 FILE *SockOpen(char *host, int clientPort)
55 {
56     int sock;
57     unsigned long inaddr;
58     struct sockaddr_in ad;
59     struct hostent *hp;
60 #ifdef USE_STDIO
61     FILE *fp;
62 #endif /* USE_STDIO */
63
64     memset(&ad, 0, sizeof(ad));
65     ad.sin_family = AF_INET;
66
67     inaddr = inet_addr(host);
68     if (inaddr != INADDR_NONE)
69         memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
70     else
71     {
72         hp = gethostbyname(host);
73         if (hp == NULL)
74             return (FILE *)NULL;
75         memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
76     }
77     ad.sin_port = htons(clientPort);
78     
79     sock = socket(AF_INET, SOCK_STREAM, 0);
80     if (sock < 0)
81         return (FILE *)NULL;
82     if (connect(sock, (struct sockaddr *) &ad, sizeof(ad)) < 0)
83     {
84         close(sock);
85         return (FILE *)NULL;
86     }
87
88 #ifndef USE_STDIO
89     return fdopen(sock, "r+");
90 #else
91     fp = fdopen(sock, "r+");
92
93     setvbuf(fp, NULL, _IOLBF, INTERNAL_BUFSIZE);
94
95     return(fp);
96 #endif /* USE_STDIO */
97 }
98
99
100 #if defined(HAVE_STDARG_H)
101 int SockPrintf(FILE *sockfp, char* format, ...)
102 {
103 #else
104 int SockPrintf(sockfp,format,va_alist)
105 FILE *sockfp;
106 char *format;
107 va_dcl {
108 #endif
109
110     va_list ap;
111     char buf[8192];
112
113 #if defined(HAVE_STDARG_H)
114     va_start(ap, format) ;
115 #else
116     va_start(ap);
117 #endif
118     vsprintf(buf, format, ap);
119     va_end(ap);
120     return SockWrite(buf, 1, strlen(buf), sockfp);
121
122 }
123
124 #ifndef USE_STDIO
125
126 int SockWrite(char *buf, int size, int len, FILE *sockfp)
127 {
128     int n, wrlen = 0;
129
130     len *= size;
131     while (len)
132     {
133         n = write(fileno(sockfp), buf, len);
134         if (n <= 0)
135             return -1;
136         len -= n;
137         wrlen += n;
138         buf += n;
139     }
140     return wrlen;
141 }
142
143 char *SockGets(char *buf, int len, FILE *sockfp)
144 {
145     int rdlen = 0;
146     char *cp = buf;
147
148     while (--len)
149     {
150         if (read(fileno(sockfp), cp, 1) != 1)
151             return((char *)NULL);
152         else
153             rdlen++;
154         if (*cp++ == '\n')
155             break;
156     }
157     *cp = 0;
158     return buf;
159 }
160
161 #else
162
163 int SockWrite(char *buf, int size, int len, FILE *sockfp)
164 {
165     return(fwrite(buf, size, len, sockfp));
166 }
167
168 char *SockGets(char *buf, int len, FILE *sockfp)
169 {
170     return(fgets(buf, len, sockfp));
171 }
172
173 #endif
174
175 #ifdef MAIN
176 /*
177  * Use the chargen service to test buffering directly.
178  */
179 main()
180 {
181     FILE        *fp = SockOpen("localhost", 19);
182     char        buf[80];
183
184     while (SockGets(buf, sizeof(buf)-1, fp))
185         SockWrite(buf, 1, strlen(buf), stdout);
186 }
187 #endif /* MAIN */
188
189 /* socket.c ends here */