xref: /netbsd-src/external/ibm-public/postfix/dist/src/util/recv_pass_attr.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: recv_pass_attr.c,v 1.1.1.1 2013/09/25 19:06:37 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	recv_pass_attr 3
6 /* SUMMARY
7 /*	predicate if string is all numerical
8 /* SYNOPSIS
9 /*	#include <listen.h>
10 /*
11 /*	int	recv_pass_attr(fd, attr, timeout, bufsize)
12 /*	int	fd;
13 /*	HTABLE	**attr;
14 /*	int	timeout;
15 /*	ssize_t	bufsize;
16 /* DESCRIPTION
17 /*	recv_pass_attr() receives named attributes over the specified
18 /*	The result value is zero for success, -1 for error.
19 /*
20 /*	Arguments:
21 /* .IP fd
22 /*	The file descriptor to read from.
23 /* .IP attr
24 /*	Pointer to attribute list pointer. The target is set to
25 /*	zero on error or when the received attribute list is empty,
26 /*	ohterwise it is assigned a pointer to non-empty attribute
27 /*	list.
28 /* .IP timeout
29 /*	The deadline for receiving all attributes.
30 /* .IP bufsize
31 /*	The read buffer size. Specify 1 to avoid reading past the
32 /*	end of the attribute list.
33 /* LICENSE
34 /* .ad
35 /* .fi
36 /*	The Secure Mailer license must be distributed with this software.
37 /* AUTHOR(S)
38 /*	Wietse Venema
39 /*	IBM T.J. Watson Research
40 /*	P.O. Box 704
41 /*	Yorktown Heights, NY 10598, USA
42 /*--*/
43 
44 /* System library. */
45 
46 #include <sys_defs.h>
47 
48 /* Utility library. */
49 
50 #include <iostuff.h>
51 #include <htable.h>
52 #include <vstream.h>
53 #include <attr.h>
54 #include <mymalloc.h>
55 #include <listen.h>
56 
57 /* recv_pass_attr - receive connection attributes */
58 
59 int     recv_pass_attr(int fd, HTABLE **attr, int timeout, ssize_t bufsize)
60 {
61     VSTREAM *fp;
62     int     stream_err;
63 
64     /*
65      * Set up a temporary VSTREAM to receive the attributes.
66      *
67      * XXX We use one-character reads to simplify the implementation.
68      */
69     fp = vstream_fdopen(fd, O_RDWR);
70     vstream_control(fp,
71 		    VSTREAM_CTL_BUFSIZE, bufsize,
72 		    VSTREAM_CTL_TIMEOUT, timeout,
73 		    VSTREAM_CTL_START_DEADLINE,
74 		    VSTREAM_CTL_END);
75     (void) attr_scan(fp, ATTR_FLAG_NONE,
76 		     ATTR_TYPE_HASH, *attr = htable_create(1),
77 		     ATTR_TYPE_END);
78     stream_err = (vstream_feof(fp) || vstream_ferror(fp));
79     vstream_fdclose(fp);
80 
81     /*
82      * Error reporting and recovery.
83      */
84     if (stream_err) {
85 	htable_free(*attr, myfree);
86 	*attr = 0;
87 	return (-1);
88     } else {
89 	if ((*attr)->used == 0) {
90 	    htable_free(*attr, myfree);
91 	    *attr = 0;
92 	}
93 	return (0);
94     }
95 }
96