]> Pileus Git - ~andy/fetchmail/blob - socket.c
16974b6918544ec34e083774407420398c1bf24b
[~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 FILE *SockOpen(char *host, int clientPort)
38 {
39     int sock;
40     unsigned long inaddr;
41     struct sockaddr_in ad;
42     struct hostent *hp;
43     
44     memset(&ad, 0, sizeof(ad));
45     ad.sin_family = AF_INET;
46
47     inaddr = inet_addr(host);
48     if (inaddr != INADDR_NONE)
49         memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
50     else
51     {
52         hp = gethostbyname(host);
53         if (hp == NULL)
54             return (FILE *)NULL;
55         memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
56     }
57     ad.sin_port = htons(clientPort);
58     
59     sock = socket(AF_INET, SOCK_STREAM, 0);
60     if (sock < 0)
61         return (FILE *)NULL;
62     if (connect(sock, (struct sockaddr *) &ad, sizeof(ad)) < 0)
63     {
64         close(sock);
65         return (FILE *)NULL;
66     }
67     return fdopen(sock, "r+");
68 }
69
70
71 #if defined(HAVE_STDARG_H)
72 int SockPrintf(FILE *sockfp, char* format, ...)
73 {
74 #else
75 int SockPrintf(sockfp,format,va_alist)
76 FILE *sockfp;
77 char *format;
78 va_dcl {
79 #endif
80
81     va_list ap;
82     char buf[8192];
83
84 #if defined(HAVE_STDARG_H)
85     va_start(ap, format) ;
86 #else
87     va_start(ap);
88 #endif
89     vsprintf(buf, format, ap);
90     va_end(ap);
91     return SockWrite(buf, 1, strlen(buf), sockfp);
92
93 }
94
95 /*
96  * FIXME: This needs to be recoded to use stdio, if that's possible.
97  *
98  * If you think these functions are too slow and inefficient, you're
99  * absolutely right.  I wish I could figure out what to do about it.
100  * The ancestral popclient used static buffering here to cut down on the
101  * number of read(2) calls, but we can't do that because we can have
102  * two or more sockets open at a time.
103  *
104  * The right thing to do would be to use stdio for internal per-socket
105  * buffering here (which is why Socket() returns a file pointer) but 
106  * this causes mysterious lossage.  In case someone ever finds a way
107  * around this, a note on Carl Harris's original implementation said:
108  *
109  * Size of buffer for internal buffering read function 
110  * don't increase beyond the maximum atomic read/write size for
111  * your sockets, or you'll take a potentially huge performance hit
112  *
113  * #define  INTERNAL_BUFSIZE    2048
114  *
115  */
116
117 int SockWrite(char *buf, int size, int len, FILE *sockfp)
118 {
119     int n, wrlen = 0;
120
121     len *= size;
122     while (len)
123     {
124         n = write(fileno(sockfp), buf, len);
125         if (n <= 0)
126             return -1;
127         len -= n;
128         wrlen += n;
129         buf += n;
130     }
131     return wrlen;
132 }
133
134 char *SockGets(char *buf, int len, FILE *sockfp)
135 {
136     int rdlen = 0;
137     char *cp = buf;
138
139     while (--len)
140     {
141         if (read(fileno(sockfp), cp, 1) != 1)
142             return((char *)NULL);
143         else
144             rdlen++;
145         if (*cp++ == '\n')
146             break;
147     }
148     *cp = 0;
149     return buf;
150 }
151
152 /* socket.c ends here */