]> Pileus Git - ~andy/fetchmail/blob - socket.c
Give up on trying to use stdio.
[~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
68     return fdopen(sock, "r+");
69 }
70
71
72 #if defined(HAVE_STDARG_H)
73 int SockPrintf(FILE *sockfp, char* format, ...)
74 {
75 #else
76 int SockPrintf(sockfp,format,va_alist)
77 FILE *sockfp;
78 char *format;
79 va_dcl {
80 #endif
81
82     va_list ap;
83     char buf[8192];
84
85 #if defined(HAVE_STDARG_H)
86     va_start(ap, format) ;
87 #else
88     va_start(ap);
89 #endif
90     vsprintf(buf, format, ap);
91     va_end(ap);
92     return SockWrite(buf, 1, strlen(buf), sockfp);
93
94 }
95
96 int SockWrite(char *buf, int size, int len, FILE *sockfp)
97 {
98     int n, wrlen = 0;
99
100     len *= size;
101     while (len)
102     {
103         n = write(fileno(sockfp), buf, len);
104         if (n <= 0)
105             return -1;
106         len -= n;
107         wrlen += n;
108         buf += n;
109     }
110     return wrlen;
111 }
112
113 char *SockGets(char *buf, int len, FILE *sockfp)
114 {
115     char *p, *bp = buf;
116     int n;
117
118     if (--len < 1)
119         return NULL;
120     do {
121         if ((n = recv(fileno(sockfp), bp, len, MSG_PEEK)) == -1)
122             return NULL;
123         if ((p = memchr(bp, '\n', n)) != NULL)
124         {
125             if (read(fileno(sockfp), bp, ++p - bp) == -1)
126                 return NULL;
127             *p = '\0';
128             return buf;
129         }
130         if ((n = read(fileno(sockfp), bp, n)) == -1)
131             return NULL;
132         bp += n;
133         len -= n;
134     } while (len);
135     *bp = '\0';
136     return buf;
137 }
138
139 #ifdef MAIN
140 /*
141  * Use the chargen service to test input beuffering directly.
142  * You may have to uncomment the `chargen' service description in your
143  * inetd.conf (and then SIGHUP inetd) for this to work. 
144  */
145 main()
146 {
147     FILE        *fp = SockOpen("localhost", 19);
148     char        buf[80];
149
150     while (SockGets(buf, sizeof(buf)-1, fp))
151         SockWrite(buf, 1, strlen(buf), stdout);
152 }
153 #endif /* MAIN */
154
155 /* socket.c ends here */