]> Pileus Git - ~andy/fetchmail/blob - socket.c
Hacks for avoiding compiler warnings under SunOS.
[~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 #ifdef SUNOS
38 #include <memory.h>
39 #endif
40
41 FILE *SockOpen(char *host, int clientPort)
42 {
43     int sock;
44     unsigned long inaddr;
45     struct sockaddr_in ad;
46     struct hostent *hp;
47
48     memset(&ad, 0, sizeof(ad));
49     ad.sin_family = AF_INET;
50
51     inaddr = inet_addr(host);
52     if (inaddr != INADDR_NONE)
53         memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
54     else
55     {
56         hp = gethostbyname(host);
57         if (hp == NULL)
58             return (FILE *)NULL;
59         memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
60     }
61     ad.sin_port = htons(clientPort);
62     
63     sock = socket(AF_INET, SOCK_STREAM, 0);
64     if (sock < 0)
65         return (FILE *)NULL;
66     if (connect(sock, (struct sockaddr *) &ad, sizeof(ad)) < 0)
67     {
68         close(sock);
69         return (FILE *)NULL;
70     }
71
72     return fdopen(sock, "r+");
73 }
74
75
76 #if defined(HAVE_STDARG_H)
77 int SockPrintf(FILE *sockfp, char* format, ...)
78 {
79 #else
80 int SockPrintf(sockfp,format,va_alist)
81 FILE *sockfp;
82 char *format;
83 va_dcl {
84 #endif
85
86     va_list ap;
87     char buf[8192];
88
89 #if defined(HAVE_STDARG_H)
90     va_start(ap, format) ;
91 #else
92     va_start(ap);
93 #endif
94     vsprintf(buf, format, ap);
95     va_end(ap);
96     return SockWrite(buf, 1, strlen(buf), sockfp);
97
98 }
99
100 int SockWrite(char *buf, int size, int len, FILE *sockfp)
101 {
102     int n, wrlen = 0;
103
104     len *= size;
105     while (len)
106     {
107         n = write(fileno(sockfp), buf, len);
108         if (n <= 0)
109             return -1;
110         len -= n;
111         wrlen += n;
112         buf += n;
113     }
114     return wrlen;
115 }
116
117 char *SockGets(char *buf, int len, FILE *sockfp)
118 {
119     char *p, *bp = buf;
120     int n;
121
122     if (--len < 1)
123         return NULL;
124     do {
125         /* return value of 0 is EOF, < 0 is error */
126         if ((n = recv(fileno(sockfp), bp, len, MSG_PEEK)) <= 0)
127             return NULL;
128         if ((p = memchr(bp, '\n', n)) != NULL)
129         {
130             if (read(fileno(sockfp), bp, ++p - bp) == -1)
131                 return NULL;
132             *p = '\0';
133             return buf;
134         }
135         if ((n = read(fileno(sockfp), bp, n)) == -1)
136             return NULL;
137         bp += n;
138         len -= n;
139     } while (len);
140     *bp = '\0';
141     return buf;
142 }
143
144 int SockPeek(FILE *sockfp)
145 /* peek at the next socket character without actually reading it */
146 {
147     int n;
148     char ch;
149
150     if ((n = recv(fileno(sockfp), &ch, 1, MSG_PEEK)) == -1)
151         return -1;
152     else
153         return(ch);
154 }
155
156 #ifdef MAIN
157 /*
158  * Use the chargen service to test input beuffering directly.
159  * You may have to uncomment the `chargen' service description in your
160  * inetd.conf (and then SIGHUP inetd) for this to work. 
161  */
162 main()
163 {
164     FILE        *fp = SockOpen("localhost", 19);
165     char        buf[80];
166
167     while (SockGets(buf, sizeof(buf)-1, fp))
168         SockWrite(buf, 1, strlen(buf), stdout);
169 }
170 #endif /* MAIN */
171
172 /* socket.c ends here */