]> Pileus Git - ~andy/fetchmail/blob - socket.c
A version of setvbuf that works.
[~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  * Size of buffer for internal buffering read function 
39  * don't increase beyond the maximum atomic read/write size for
40  * your sockets, or you'll take a potentially huge performance hit
41  */
42 #define  INTERNAL_BUFSIZE       2048
43
44 FILE *sockopen(char *host, int clientPort)
45 {
46     int sock;
47     unsigned long inaddr;
48     struct sockaddr_in ad;
49     struct hostent *hp;
50     FILE *fp;
51
52     memset(&ad, 0, sizeof(ad));
53     ad.sin_family = AF_INET;
54
55     inaddr = inet_addr(host);
56     if (inaddr != INADDR_NONE)
57         memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
58     else
59     {
60         hp = gethostbyname(host);
61         if (hp == NULL)
62             return (FILE *)NULL;
63         memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
64     }
65     ad.sin_port = htons(clientPort);
66     
67     sock = socket(AF_INET, SOCK_STREAM, 0);
68     if (sock < 0)
69         return (FILE *)NULL;
70     if (connect(sock, (struct sockaddr *) &ad, sizeof(ad)) < 0)
71     {
72         close(sock);
73         return (FILE *)NULL;
74     }
75     fp = fdopen(sock, "r+");
76
77     /* the point of all this mishigoss ... dynamic per-stream buffering */
78     setvbuf(fp, NULL, _IOLBF, INTERNAL_BUFSIZE);
79
80     return(fp);
81 }
82
83 #ifdef MAIN
84 /*
85  * Use the chargen service to test buffering directly.
86  */
87 main()
88 {
89     FILE        *fp = sockopen("localhost", 19);
90     char        buf[80];
91
92     while (fgets(buf, sizeof(buf)-1, fp))
93         fputs(buf, stdout);
94 }
95 #endif /* MAIN */
96
97 /* socket.c ends here */