2 * socket.c -- socket library functions
4 * Copyright 1998 by Eric S. Raymond.
5 * For license terms, see the file COPYING in this directory.
12 #include <ctype.h> /* isspace() */
15 #endif /* HAVE_MEMORY_H */
16 #include <sys/types.h>
17 #ifndef HAVE_NET_SOCKET_H
18 #include <sys/socket.h>
20 #include <net/socket.h>
23 #include <netinet/in.h>
24 #ifdef HAVE_ARPA_INET_H
25 #include <arpa/inet.h>
28 #if defined(STDC_HEADERS)
31 #if defined(HAVE_UNISTD_H)
34 #if defined(HAVE_STDARG_H)
41 #include "fetchmail.h"
44 /* Defines to allow BeOS to play nice... */
47 #define fm_close(a) closesocket(a)
48 #define fm_write(a,b,c) send(a,b,c,0)
49 #define fm_peek(a,b,c) recv(a,b,c,0)
50 #define fm_read(a,b,c) recv(a,b,c,0)
52 #define fm_close(a) close(a)
53 #define fm_write(a,b,c) write(a,b,c)
54 #define fm_peek(a,b,c) recv(a,b,c, MSG_PEEK)
55 #define fm_read(a,b,c) read(a,b,c)
58 /* We need to define h_errno only if it is not already */
61 #ifdef HAVE_RES_SEARCH
62 /* some versions of FreeBSD should declare this but don't */
65 /* pretend we have h_errno to avoid some #ifdef's later */
69 #endif /* ndef h_errno */
72 #include <net/security.h>
73 #endif /* NET_SECURITY */
75 #ifdef HAVE_SOCKETPAIR
76 char *const *parse_plugin(const char *plugin, const char *host, const char *service)
77 { const char **argvec;
79 char *cp, *plugin_copy;
80 unsigned int plugin_copy_len;
81 unsigned int plugin_offset = 0, plugin_copy_offset = 0;
82 unsigned int i, s = 2 * sizeof(char*), host_count = 0, service_count = 0;
83 unsigned int plugin_len = strlen(plugin);
84 unsigned int host_len = strlen(host);
85 unsigned int service_len = strlen(service);
87 for (c = p = plugin; *c; c++)
88 { if (isspace(*c) && !isspace(*p))
90 if (*p == '%' && *c == 'h')
92 if (*p == '%' && *c == 'p')
97 plugin_copy_len = plugin_len + host_len * host_count + service_len * service_count;
98 plugin_copy = malloc(plugin_copy_len + 1);
101 report(stderr, GT_("fetchmail: malloc failed\n"));
105 while (plugin_copy_offset < plugin_copy_len)
106 { if ((plugin[plugin_offset] == '%') && (plugin[plugin_offset + 1] == 'h'))
107 { strcpy(plugin_copy + plugin_copy_offset, host);
109 plugin_copy_offset += host_len;
111 else if ((plugin[plugin_offset] == '%') && (plugin[plugin_offset + 1] == 'p'))
112 { strcpy(plugin_copy + plugin_copy_offset, service);
114 plugin_copy_offset += service_len;
117 { plugin_copy[plugin_copy_offset] = plugin[plugin_offset];
119 plugin_copy_offset++;
122 plugin_copy[plugin_copy_len] = 0;
127 report(stderr, GT_("fetchmail: malloc failed\n"));
130 memset(argvec, 0, s);
131 for (c = p = plugin_copy, i = 0; *c; c++)
132 { if ((!isspace(*c)) && (c == p ? 1 : isspace(*p))) {
138 for (cp = plugin_copy; *cp; cp++)
142 return (char *const*)argvec;
145 static int handle_plugin(const char *host,
146 const char *service, const char *plugin)
147 /* get a socket mediated through a given external command */
153 * The author of this code, Felix von Leitner <felix@convergence.de>, says:
154 * he chose socketpair() instead of pipe() because socketpair creates
155 * bidirectional sockets while allegedly some pipe() implementations don't.
157 if (socketpair(AF_UNIX,SOCK_STREAM,0,fds))
159 report(stderr, GT_("fetchmail: socketpair failed\n"));
165 report(stderr, GT_("fetchmail: fork failed\n"));
169 /* fds[1] is the parent's end; close it for proper EOF
171 (void) close(fds[1]);
172 if ( (dup2(fds[0],0) == -1) || (dup2(fds[0],1) == -1) ) {
173 report(stderr, GT_("dup2 failed\n"));
176 /* fds[0] is now connected to 0 and 1; close it */
177 (void) close(fds[0]);
178 if (outlevel >= O_VERBOSE)
179 report(stderr, GT_("running %s (host %s service %s)\n"), plugin, host, service);
180 argvec = parse_plugin(plugin,host,service);
181 execvp(*argvec, argvec);
182 report(stderr, GT_("execvp(%s) failed\n"), *argvec);
185 default: /* parent */
189 /* fds[0] is the child's end; close it for proper EOF detection */
190 (void) close(fds[0]);
193 #endif /* HAVE_SOCKETPAIR */
196 #include <sys/time.h>
198 int SockCheckOpen(int fd)
199 /* poll given socket; is it selectable? */
207 FD_ZERO(&r); FD_ZERO(&w); FD_ZERO(&e);
210 tv.tv_sec = 0; tv.tv_usec = 0;
211 rt = select(fd+1, &r, &w, &e, &tv);
212 if (rt == -1 && (errno != EAGAIN && errno != EINTR))
218 #endif /* __UNUSED__ */
220 int UnixOpen(const char *path)
222 #ifdef HAVE_SIGPROCMASK
224 #endif /* HAVE_SIGPROCMASK */
227 struct sockaddr_un ad;
228 memset(&ad, 0, sizeof(ad));
229 ad.sun_family = AF_UNIX;
230 strncpy(ad.sun_path, path, sizeof(ad.sun_path)-1);
232 sock = socket( AF_UNIX, SOCK_STREAM, 0 );
239 #ifdef HAVE_SIGPROCMASK
240 /* avoid socket leak on alarm signal during connect(2) */
241 sigfillset(&allsigs);
242 sigprocmask(SIG_BLOCK, &allsigs, NULL);
243 #endif /* HAVE_SIGPROCMASK */
245 if (connect(sock, (struct sockaddr *) &ad, sizeof(ad)) < 0)
248 fm_close(sock); /* don't use SockClose, no traffic yet */
254 #ifdef HAVE_SIGPROCMASK
255 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
256 #endif /* HAVE_SIGPROCMASK */
262 int SockOpen(const char *host, const char *service, const char *options,
265 #ifdef HAVE_SIGPROCMASK
267 #endif /* HAVE_SIGPROCMASK */
269 struct addrinfo *ai, *ai0, req;
272 void *request = NULL;
274 #endif /* NET_SECURITY */
276 #ifdef HAVE_SOCKETPAIR
278 return handle_plugin(host,service,plugin);
279 #endif /* HAVE_SOCKETPAIR */
280 memset(&req, 0, sizeof(struct addrinfo));
281 req.ai_socktype = SOCK_STREAM;
283 if (getaddrinfo(host, service, &req, &ai0)) {
284 report(stderr, GT_("fetchmail: getaddrinfo(%s.%s)\n"), host,service);
292 if (net_security_strtorequest((char *)options, &request, &requestlen))
295 i = inner_connect(ai0, request, requestlen, NULL, NULL, "fetchmail", NULL);
300 #else /* NET_SECURITY */
301 #ifdef HAVE_INNER_CONNECT
302 i = inner_connect(ai0, NULL, 0, NULL, NULL, "fetchmail", NULL);
307 #ifdef HAVE_SIGPROCMASK
308 /* avoid socket leak on alarm signal during connect(2) */
309 sigfillset(&allsigs);
310 sigprocmask(SIG_BLOCK, &allsigs, NULL);
311 #endif /* HAVE_SIGPROCMASK */
314 for (ai = ai0; ai; ai = ai->ai_next) {
315 i = socket(ai->ai_family, ai->ai_socktype, 0);
318 if (connect(i, (struct sockaddr *) ai->ai_addr, ai->ai_addrlen) < 0) {
326 #ifdef HAVE_SIGPROCMASK
327 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
328 #endif /* HAVE_SIGPROCMASK */
331 #endif /* NET_SECURITY */
337 #else /* INET6_ENABLE */
338 #ifndef HAVE_INET_ATON
340 #ifdef INADDR_BROADCAST
341 #define INADDR_NONE INADDR_BROADCAST
343 #define INADDR_NONE -1
346 #endif /* HAVE_INET_ATON */
348 int SockOpen(const char *host, int clientPort, const char *options,
351 int sock = -1; /* pacify -Wall */
352 #ifndef HAVE_INET_ATON
353 unsigned long inaddr;
354 #endif /* HAVE_INET_ATON */
355 struct sockaddr_in ad, **pptr;
358 #ifdef HAVE_SOCKETPAIR
362 snprintf(buf, sizeof(buf), /* Yeah, paranoic. So what? :P */
365 #endif /* HAVE_SNPRINTF */
367 return handle_plugin(host,buf,plugin);
369 #endif /* HAVE_SOCKETPAIR */
371 memset(&ad, 0, sizeof(ad));
372 ad.sin_family = AF_INET;
374 /* we'll accept a quad address */
375 #ifndef HAVE_INET_ATON
376 inaddr = inet_addr((char*)host);
377 if (inaddr != INADDR_NONE)
379 memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
381 if (inet_aton(host, &ad.sin_addr))
383 #endif /* HAVE_INET_ATON */
384 ad.sin_port = htons(clientPort);
386 sock = socket(AF_INET, SOCK_STREAM, 0);
392 if (connect(sock, (struct sockaddr *) &ad, sizeof(ad)) < 0)
395 fm_close(sock); /* don't use SockClose, no traffic yet */
400 #ifndef HAVE_INET_ATON
404 #endif /* HAVE_INET_ATON */
406 hp = gethostbyname((char*)host);
414 * Add a check to make sure the address has a valid IPv4 or IPv6
415 * length. This prevents buffer spamming by a broken DNS.
417 if(hp->h_length != 4 && hp->h_length != 8)
421 GT_("fetchmail: illegal address length received for host %s\n"),host);
425 * Try all addresses of a possibly multihomed host until we get
426 * a successful connect or until we run out of addresses.
428 pptr = (struct sockaddr_in **)hp->h_addr_list;
429 for(; *pptr != NULL; pptr++)
431 sock = socket(AF_INET, SOCK_STREAM, 0);
437 ad.sin_port = htons(clientPort);
438 memcpy(&ad.sin_addr, *pptr, sizeof(struct in_addr));
439 if (connect(sock, (struct sockaddr *) &ad, sizeof(ad)) == 0)
441 fm_close(sock); /* don't use SockClose, no traffic yet */
442 memset(&ad, 0, sizeof(ad));
443 ad.sin_family = AF_INET;
448 fm_close(sock); /* don't use SockClose, no traffic yet */
457 #endif /* INET6_ENABLE */
460 #if defined(HAVE_STDARG_H)
461 int SockPrintf(int sock, const char* format, ...)
464 int SockPrintf(sock,format,va_alist)
473 #if defined(HAVE_STDARG_H)
474 va_start(ap, format) ;
478 #ifdef HAVE_VSNPRINTF
479 vsnprintf(buf, sizeof(buf), format, ap);
481 vsprintf(buf, format, ap);
484 return SockWrite(sock, buf, strlen(buf));
489 #include "openssl/ssl.h"
490 #include "openssl/err.h"
491 #include "openssl/pem.h"
492 #include "openssl/x509.h"
494 static SSL_CTX *_ctx = NULL;
495 static SSL *_ssl_context[FD_SETSIZE];
497 SSL *SSLGetContext( int );
498 #endif /* SSL_ENABLE */
500 int SockWrite(int sock, char *buf, int len)
510 if( NULL != ( ssl = SSLGetContext( sock ) ) )
511 n = SSL_write(ssl, buf, len);
513 n = fm_write(sock, buf, len);
515 n = fm_write(sock, buf, len);
526 int SockRead(int sock, char *buf, int len)
528 char *newline, *bp = buf;
546 * The reason for these gymnastics is that we want two things:
547 * (1) to read \n-terminated lines,
548 * (2) to return the true length of data read, even if the
549 * data coming in has embedded NULS.
552 if( NULL != ( ssl = SSLGetContext( sock ) ) ) {
554 /* OK... SSL_peek works a little different from MSG_PEEK
555 Problem is that SSL_peek can return 0 if there
556 is no data currently available. If, on the other
557 hand, we loose the socket, we also get a zero, but
558 the SSL_read then SEGFAULTS! To deal with this,
559 we'll check the error code any time we get a return
560 of zero from SSL_peek. If we have an error, we bail.
561 If we don't, we read one character in SSL_read and
562 loop. This should continue to work even if they
563 later change the behavior of SSL_peek
564 to "fix" this problem... :-( */
565 if ((n = SSL_peek(ssl, bp, len)) < 0) {
566 (void)SSL_get_error(ssl, n);
570 /* SSL_peek says no data... Does he mean no data
571 or did the connection blow up? If we got an error
573 if( 0 != ( n = SSL_get_error(ssl, n) ) ) {
576 /* We didn't get an error so read at least one
577 character at this point and loop */
579 /* Make sure newline start out NULL!
580 * We don't have a string to pass through
581 * the strchr at this point yet */
583 } else if ((newline = memchr(bp, '\n', n)) != NULL)
584 n = newline - bp + 1;
585 /* Matthias Andree: SSL_read can return 0, in that case
586 * we must cal SSL_get_error to figure if there was
587 * an error or just a "no data" condition */
588 if ((n = SSL_read(ssl, bp, n)) <= 0) {
589 if ((n = SSL_get_error(ssl, n))) {
593 /* Check for case where our single character turned out to
594 * be a newline... (It wasn't going to get caught by
595 * the strchr above if it came from the hack... ). */
596 if( NULL == newline && 1 == n && '\n' == *bp ) {
597 /* Got our newline - this will break
598 out of the loop now */
602 if ((n = fm_peek(sock, bp, len)) <= 0)
604 if ((newline = memchr(bp, '\n', n)) != NULL)
605 n = newline - bp + 1;
606 if ((n = fm_read(sock, bp, n)) == -1)
612 if ((n = fm_read(sock, bp, 1)) <= 0)
614 if ((n = fm_peek(sock, bp, len)) <= 0)
617 if ((newline = memchr(bp, '\n', n)) != NULL)
618 n = newline - bp + 1;
620 if ((n = fm_read(sock, bp, n)) == -1)
622 #endif /* __BEOS__ */
632 int SockPeek(int sock)
633 /* peek at the next socket character without actually reading it */
642 if( NULL != ( ssl = SSLGetContext( sock ) ) ) {
643 n = SSL_peek(ssl, &ch, 1);
645 (void)SSL_get_error(ssl, n);
649 /* This code really needs to implement a "hold back"
650 * to simulate a functioning SSL_peek()... sigh...
651 * Has to be coordinated with the read code above.
652 * Next on the list todo... */
654 /* SSL_peek says 0... Does that mean no data
655 or did the connection blow up? If we got an error
657 if( 0 != ( n = SSL_get_error(ssl, n) ) ) {
661 /* Haven't seen this case actually occur, but...
662 if the problem in SockRead can occur, this should
663 be possible... Just not sure what to do here.
664 This should be a safe "punt" the "peek" but don't
665 "punt" the "session"... */
667 return 0; /* Give him a '\0' character */
670 n = fm_peek(sock, &ch, 1);
674 n = fm_peek(sock, &ch, 1);
676 #endif /* SSL_ENABLE */
688 static char *_ssl_server_cname = NULL;
689 static int _check_fp;
690 static char *_check_digest;
691 static char *_server_label;
692 static int _depth0ck;
694 SSL *SSLGetContext( int sock )
696 /* If SSLOpen has never initialized - just return NULL */
700 if( sock < 0 || sock > FD_SETSIZE )
702 return _ssl_context[sock];
706 int SSL_verify_callback( int ok_return, X509_STORE_CTX *ctx, int strict )
711 unsigned char digest[EVP_MAX_MD_SIZE];
712 char text[EVP_MAX_MD_SIZE * 3 + 1], *tp, *te;
714 unsigned int dsz, i, esz;
715 X509_NAME *subj, *issuer;
717 x509_cert = X509_STORE_CTX_get_current_cert(ctx);
718 err = X509_STORE_CTX_get_error(ctx);
719 depth = X509_STORE_CTX_get_error_depth(ctx);
721 subj = X509_get_subject_name(x509_cert);
722 issuer = X509_get_issuer_name(x509_cert);
727 if (outlevel == O_VERBOSE) {
728 if ((i = X509_NAME_get_text_by_NID(issuer, NID_organizationName, buf, sizeof(buf))) != -1) {
729 report(stdout, GT_("Issuer Organization: %s\n"), buf);
730 if (i >= sizeof(buf) - 1)
731 report(stdout, GT_("Warning: Issuer Organization Name too long (possibly truncated).\n"));
733 report(stdout, GT_("Unknown Organization\n"));
734 if ((i = X509_NAME_get_text_by_NID(issuer, NID_commonName, buf, sizeof(buf))) != -1) {
735 report(stdout, GT_("Issuer CommonName: %s\n"), buf);
736 if (i >= sizeof(buf) - 1)
737 report(stdout, GT_("Warning: Issuer CommonName too long (possibly truncated).\n"));
739 report(stdout, GT_("Unknown Issuer CommonName\n"));
741 if ((i = X509_NAME_get_text_by_NID(subj, NID_commonName, buf, sizeof(buf))) != -1) {
742 if (outlevel == O_VERBOSE)
743 report(stdout, GT_("Server CommonName: %s\n"), buf);
744 if (i >= sizeof(buf) - 1) {
745 /* Possible truncation. In this case, this is a DNS name, so this
746 * is really bad. We do not tolerate this even in the non-strict case. */
747 report(stderr, GT_("Bad certificate: Subject CommonName too long!\n"));
750 if (_ssl_server_cname != NULL) {
752 char *p2 = _ssl_server_cname;
757 n = strlen(p2) - strlen(p1);
761 if (0 != strcasecmp(p1, p2)) {
763 GT_("Server CommonName mismatch: %s != %s\n"),
764 buf, _ssl_server_cname );
765 if (ok_return && strict)
768 } else if (ok_return && strict) {
769 report(stderr, GT_("Server name not set, could not verify certificate!\n"));
773 if (outlevel == O_VERBOSE)
774 report(stdout, GT_("Unknown Server CommonName\n"));
775 if (ok_return && strict) {
776 report(stderr, GT_("Server name not specified in certificate!\n"));
780 /* Print the finger print. Note that on errors, we might print it more than once
781 * normally; we kluge around that by using a global variable. */
784 digest_tp = EVP_md5();
785 if (digest_tp == NULL) {
786 report(stderr, GT_("EVP_md5() failed!\n"));
789 if (!X509_digest(x509_cert, digest_tp, digest, &dsz)) {
790 report(stderr, GT_("Out of memory!\n"));
794 te = text + sizeof(text);
795 for (i = 0; i < dsz; i++) {
797 esz = snprintf(tp, te - tp, i > 0 ? ":%02X" : "%02X", digest[i]);
799 esz = sprintf(tp, i > 0 ? ":%02X" : "%02X", digest[i]);
801 if (esz >= te - tp) {
802 report(stderr, GT_("Digest text buffer too small!\n"));
807 if (outlevel > O_SILENT)
808 report(stdout, GT_("%s key fingerprint: %s\n"), _server_label, text);
809 if (_check_digest != NULL) {
810 if (strcmp(text, _check_digest) == 0)
811 if (outlevel > O_SILENT)
812 report(stdout, GT_("%s fingerprints match.\n"), _server_label);
814 if (outlevel > O_SILENT)
815 report(stderr, GT_("%s fingerprints do not match!\n"), _server_label);
822 if (err != X509_V_OK && (strict || outlevel == O_VERBOSE)) {
823 report(strict ? stderr : stdout, GT_("Warning: server certificate verification: %s\n"), X509_verify_cert_error_string(err));
824 /* We gave the error code, but maybe we can add some more details for debugging */
826 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
827 X509_NAME_oneline(issuer, buf, sizeof(buf));
828 buf[sizeof(buf) - 1] = '\0';
829 report(stdout, GT_("unknown issuer (first %d characters): %s\n"), sizeof(buf), buf);
838 int SSL_nock_verify_callback( int ok_return, X509_STORE_CTX *ctx )
840 return SSL_verify_callback(ok_return, ctx, 0);
843 int SSL_ck_verify_callback( int ok_return, X509_STORE_CTX *ctx )
845 return SSL_verify_callback(ok_return, ctx, 1);
848 /* performs initial SSL handshake over the connected socket
849 * uses SSL *ssl global variable, which is currently defined
852 int SSLOpen(int sock, char *mycert, char *mykey, char *myproto, int certck, char *certpath,
853 char *fingerprint, char *servercname, char *label)
857 SSL_load_error_strings();
858 SSLeay_add_ssl_algorithms();
860 if( sock < 0 || sock > FD_SETSIZE ) {
861 report(stderr, GT_("File descriptor out of range for SSL") );
866 /* Be picky and make sure the memory is cleared */
867 memset( _ssl_context, 0, sizeof( _ssl_context ) );
869 if(!strcmp("ssl2",myproto)) {
870 _ctx = SSL_CTX_new(SSLv2_client_method());
871 } else if(!strcmp("ssl3",myproto)) {
872 _ctx = SSL_CTX_new(SSLv3_client_method());
873 } else if(!strcmp("tls1",myproto)) {
874 _ctx = SSL_CTX_new(TLSv1_client_method());
876 fprintf(stderr,GT_("Invalid SSL protocol '%s' specified, using default (SSLv23).\n"), myproto);
881 _ctx = SSL_CTX_new(SSLv23_client_method());
884 ERR_print_errors_fp(stderr);
890 SSL_CTX_set_verify(_ctx, SSL_VERIFY_PEER, SSL_ck_verify_callback);
892 SSL_CTX_load_verify_locations(_ctx, NULL, certpath);
894 /* In this case, we do not fail if verification fails. However,
895 * we provide the callback for output and possible fingerprint checks. */
896 SSL_CTX_set_verify(_ctx, SSL_VERIFY_PEER, SSL_nock_verify_callback);
899 _ssl_context[sock] = SSL_new(_ctx);
901 if(_ssl_context[sock] == NULL) {
902 ERR_print_errors_fp(stderr);
906 /* This static is for the verify callback */
907 _ssl_server_cname = servercname;
908 _server_label = label;
910 _check_digest = fingerprint;
913 if( mycert || mykey ) {
915 /* Ok... He has a certificate file defined, so lets declare it. If
916 * he does NOT have a separate certificate and private key file then
917 * assume that it's a combined key and certificate file.
923 SSL_use_certificate_file(_ssl_context[sock], mycert, SSL_FILETYPE_PEM);
924 SSL_use_RSAPrivateKey_file(_ssl_context[sock], mykey, SSL_FILETYPE_PEM);
927 SSL_set_fd(_ssl_context[sock], sock);
929 if(SSL_connect(_ssl_context[sock]) == -1) {
930 ERR_print_errors_fp(stderr);
934 /* Paranoia: was the callback not called as we expected? */
935 if ((fingerprint != NULL || certck) && !_depth0ck) {
936 report(stderr, GT_("Certificate/fingerprint verification was somehow skipped!\n"));
938 if( NULL != ( ssl = SSLGetContext( sock ) ) ) {
939 /* Clean up the SSL stack */
940 SSL_free( _ssl_context[sock] );
941 _ssl_context[sock] = NULL;
950 int SockClose(int sock)
951 /* close a socket gracefully */
956 if( NULL != ( ssl = SSLGetContext( sock ) ) ) {
957 /* Clean up the SSL stack */
958 SSL_free( _ssl_context[sock] );
959 _ssl_context[sock] = NULL;
965 * This hangs in RedHat 6.2 after fetchmail runs for a while a
966 * FIN_WAIT2 comes up in netstat and fetchmail never returns from
967 * the recv system call. (Reported from jtnews
968 * <jtnews@bellatlantic.net>, Wed, 24 May 2000 21:26:02.)
970 * Half-close the connection first so the other end gets notified.
972 * This stops sends but allows receives (effectively, it sends a
974 if (shutdown(sock, 1) == 0) {
976 /* If there is any data still waiting in the queue, discard it.
977 * Call recv() until either it returns 0 (meaning we received a FIN)
978 * or any error occurs. This makes sure all data sent by the other
979 * side is acknowledged at the TCP level.
981 if (fm_peek(sock, &ch, 1) > 0)
982 while (fm_read(sock, &ch, 1) > 0)
985 #endif /* __UNUSED__ */
987 /* if there's an error closing at this point, not much we can do */
988 return(fm_close(sock)); /* this is guarded */
993 * Use the chargen service to test input buffering directly.
994 * You may have to uncomment the `chargen' service description in your
995 * inetd.conf (and then SIGHUP inetd) for this to work. */
998 int sock = SockOpen("localhost", 19, NULL);
1001 while (SockRead(sock, buf, sizeof(buf)-1))
1002 SockWrite(1, buf, strlen(buf));
1007 /* socket.c ends here */