]> Pileus Git - ~andy/fetchmail/blob - socket.c
Cleaner version of SockRead.
[~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 int 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 -1;
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 -1;
66     if (connect(sock, (struct sockaddr *) &ad, sizeof(ad)) < 0)
67     {
68         close(sock);
69         return -1;
70     }
71
72     return(sock);
73 }
74
75
76 #if defined(HAVE_STDARG_H)
77 int SockPrintf(int sock, char* format, ...)
78 {
79 #else
80 int SockPrintf(sock,format,va_alist)
81 int sock;
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(sock, buf, strlen(buf));
97
98 }
99
100 int SockWrite(int sock, char *buf, int len)
101 {
102     int n, wrlen = 0;
103
104     while (len)
105     {
106         n = write(sock, buf, len);
107         if (n <= 0)
108             return -1;
109         len -= n;
110         wrlen += n;
111         buf += n;
112     }
113     return wrlen;
114 }
115
116 int SockRead(int sock, char *buf, int len)
117 {
118     char *p, *bp = buf;
119     int n, readlen;
120
121     if (--len < 1)
122         return(-1);
123     do {
124         /* 
125          * The reason for these gymnastics is that we want two things:
126          * (1) to read \n-terminated lines,
127          * (2) to return the true length of data read, even if the
128          *     data coming in has embedded NULS.
129          */
130         readlen = 0;
131
132         if ((n = recv(sock, bp, len, MSG_PEEK)) <= 0)
133             return(-1);
134         if ((p = memchr(bp, '\n', n)) != NULL)
135             n = ++p - bp;
136         if ((n = read(sock, bp, n)) == -1)
137             return(-1);
138         readlen += n;
139         bp += n;
140         len -= n;
141         if (p)
142         {
143             *p = '\0';
144             break;
145         }
146     } while 
147             (len);
148     *bp = '\0';
149     return readlen;
150 }
151
152 int SockPeek(int sock)
153 /* peek at the next socket character without actually reading it */
154 {
155     int n;
156     char ch;
157
158     if ((n = recv(sock, &ch, 1, MSG_PEEK)) == -1)
159         return -1;
160     else
161         return(ch);
162 }
163
164 #ifdef MAIN
165 /*
166  * Use the chargen service to test input beuffering directly.
167  * You may have to uncomment the `chargen' service description in your
168  * inetd.conf (and then SIGHUP inetd) for this to work. 
169  */
170 main()
171 {
172     int         sock = SockOpen("localhost", 19);
173     char        buf[80];
174
175     while (SockRead(sock, buf, sizeof(buf)-1))
176         SockWrite(1, buf, strlen(buf));
177 }
178 #endif /* MAIN */
179
180 /* socket.c ends here */