Postfix3.3.1
recv_pass_attr.c
[詳解]
1 /*++
2 /* NAME
3 /* recv_pass_attr 3
4 /* SUMMARY
5 /* predicate if string is all numerical
6 /* SYNOPSIS
7 /* #include <listen.h>
8 /*
9 /* int recv_pass_attr(fd, attr, timeout, bufsize)
10 /* int fd;
11 /* HTABLE **attr;
12 /* int timeout;
13 /* ssize_t bufsize;
14 /* DESCRIPTION
15 /* recv_pass_attr() receives named attributes over the specified
16 /* descriptor. The result value is zero for success, -1 for error.
17 /*
18 /* Arguments:
19 /* .IP fd
20 /* The file descriptor to read from.
21 /* .IP attr
22 /* Pointer to attribute list pointer. The target is set to
23 /* zero on error or when the received attribute list is empty,
24 /* otherwise it is assigned a pointer to non-empty attribute
25 /* list.
26 /* .IP timeout
27 /* The deadline for receiving all attributes.
28 /* .IP bufsize
29 /* The read buffer size. Specify 1 to avoid reading past the
30 /* end of the attribute list.
31 /* LICENSE
32 /* .ad
33 /* .fi
34 /* The Secure Mailer license must be distributed with this software.
35 /* AUTHOR(S)
36 /* Wietse Venema
37 /* IBM T.J. Watson Research
38 /* P.O. Box 704
39 /* Yorktown Heights, NY 10598, USA
40 /*
41 /* Wietse Venema
42 /* Google, Inc.
43 /* 111 8th Avenue
44 /* New York, NY 10011, USA
45 /*--*/
46 
47 /* System library. */
48 
49 #include <sys_defs.h>
50 
51 /* Utility library. */
52 
53 #include <iostuff.h>
54 #include <htable.h>
55 #include <vstream.h>
56 #include <attr.h>
57 #include <mymalloc.h>
58 #include <listen.h>
59 
60 /* recv_pass_attr - receive connection attributes */
61 
62 int recv_pass_attr(int fd, HTABLE **attr, int timeout, ssize_t bufsize)
63 {
64  VSTREAM *fp;
65  int stream_err;
66 
67  /*
68  * Set up a temporary VSTREAM to receive the attributes.
69  *
70  * XXX We use one-character reads to simplify the implementation.
71  */
72  fp = vstream_fdopen(fd, O_RDWR);
73  vstream_control(fp,
74  CA_VSTREAM_CTL_BUFSIZE(bufsize),
75  CA_VSTREAM_CTL_TIMEOUT(timeout),
78  stream_err = (attr_scan(fp, ATTR_FLAG_NONE,
79  ATTR_TYPE_HASH, *attr = htable_create(1),
80  ATTR_TYPE_END) < 0
81  || vstream_feof(fp) || vstream_ferror(fp));
82  vstream_fdclose(fp);
83 
84  /*
85  * Error reporting and recovery.
86  */
87  if (stream_err) {
88  htable_free(*attr, myfree);
89  *attr = 0;
90  return (-1);
91  } else {
92  if ((*attr)->used == 0) {
93  htable_free(*attr, myfree);
94  *attr = 0;
95  }
96  return (0);
97  }
98 }
void htable_free(HTABLE *table, void(*free_fn)(void *))
Definition: htable.c:287
#define ATTR_FLAG_NONE
Definition: attr.h:98
void myfree(void *ptr)
Definition: mymalloc.c:207
#define CA_VSTREAM_CTL_TIMEOUT(v)
Definition: vstream.h:163
#define ATTR_TYPE_END
Definition: attr.h:39
Definition: htable.h:25
HTABLE * htable_create(ssize_t size)
Definition: htable.c:179
#define CA_VSTREAM_CTL_BUFSIZE(v)
Definition: vstream.h:169
#define ATTR_TYPE_HASH
Definition: attr.h:43
#define CA_VSTREAM_CTL_START_DEADLINE
Definition: vstream.h:171
int recv_pass_attr(int fd, HTABLE **attr, int timeout, ssize_t bufsize)
#define vstream_feof(vp)
Definition: vstream.h:121
int vstream_fdclose(VSTREAM *stream)
Definition: vstream.c:1309
#define CA_VSTREAM_CTL_END
Definition: vstream.h:155
void vstream_control(VSTREAM *stream, int name,...)
Definition: vstream.c:1372
#define attr_scan
Definition: attr.h:111
#define vstream_ferror(vp)
Definition: vstream.h:120
VSTREAM * vstream_fdopen(int fd, int flags)
Definition: vstream.c:1204