]> Pileus Git - ~andy/fetchmail/blob - socket.c
Added the `envelope' option.
[~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 *Socket(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, 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 len, FILE *sockfp)
118 {
119     int n, wrlen = 0;
120     
121     while (len)
122     {
123         n = write(fileno(sockfp), buf, len);
124         if (n <= 0)
125             return -1;
126         len -= n;
127         wrlen += n;
128         buf += n;
129     }
130     return wrlen;
131 }
132
133 int SockGets(char *buf, int len, FILE *sockfp)
134 {
135     int rdlen = 0;
136
137     while (--len)
138     {
139         if (read(fileno(sockfp), buf, 1) != 1)
140             return -1;
141         else
142             rdlen++;
143         if (*buf == '\n')
144             break;
145         if (*buf != '\r') /* remove all CRs */
146             buf++;
147     }
148     *buf = 0;
149     return rdlen;
150 }
151
152 /* socket.c ends here */