xref: /netbsd-src/external/ibm-public/postfix/dist/src/util/recv_pass_attr.c (revision 33881f779a77dce6440bdc44610d94de75bebefe)
1 /*	$NetBSD: recv_pass_attr.c,v 1.3 2020/03/18 19:05:22 christos 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 /*	descriptor. 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 /*	otherwise 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 /*	Wietse Venema
44 /*	Google, Inc.
45 /*	111 8th Avenue
46 /*	New York, NY 10011, USA
47 /*--*/
48 
49 /* System library. */
50 
51 #include <sys_defs.h>
52 
53 /* Utility library. */
54 
55 #include <iostuff.h>
56 #include <htable.h>
57 #include <vstream.h>
58 #include <attr.h>
59 #include <mymalloc.h>
60 #include <listen.h>
61 
62 /* recv_pass_attr - receive connection attributes */
63 
recv_pass_attr(int fd,HTABLE ** attr,int timeout,ssize_t bufsize)64 int     recv_pass_attr(int fd, HTABLE **attr, int timeout, ssize_t bufsize)
65 {
66     VSTREAM *fp;
67     int     stream_err;
68 
69     /*
70      * Set up a temporary VSTREAM to receive the attributes.
71      *
72      * XXX We use one-character reads to simplify the implementation.
73      */
74     fp = vstream_fdopen(fd, O_RDWR);
75     vstream_control(fp,
76 		    CA_VSTREAM_CTL_BUFSIZE(bufsize),
77 		    CA_VSTREAM_CTL_TIMEOUT(timeout),
78 		    CA_VSTREAM_CTL_START_DEADLINE,
79 		    CA_VSTREAM_CTL_END);
80     stream_err = (attr_scan(fp, ATTR_FLAG_NONE,
81 			    ATTR_TYPE_HASH, *attr = htable_create(1),
82 			    ATTR_TYPE_END) < 0
83 		  || vstream_feof(fp) || vstream_ferror(fp));
84     vstream_fdclose(fp);
85 
86     /*
87      * Error reporting and recovery.
88      */
89     if (stream_err) {
90 	htable_free(*attr, myfree);
91 	*attr = 0;
92 	return (-1);
93     } else {
94 	if ((*attr)->used == 0) {
95 	    htable_free(*attr, myfree);
96 	    *attr = 0;
97 	}
98 	return (0);
99     }
100 }
101