1 /*++
2 /* NAME
3 /*	msg_stats_scan
4 /* SUMMARY
5 /*	read MSG_STATS from stream
6 /* SYNOPSIS
7 /*	#include <msg_stats.h>
8 /*
9 /*	int	msg_stats_scan(scan_fn, stream, flags, ptr)
10 /*	ATTR_SCAN_COMMON_FN scan_fn;
11 /*	VSTREAM *stream;
12 /*	int	flags;
13 /*	void	*ptr;
14 /* DESCRIPTION
15 /*	msg_stats_scan() reads an MSG_STATS from the named stream
16 /*	using the specified attribute scan routine. msg_stats_scan()
17 /*	is meant to be passed as a call-back to attr_scan(), thusly:
18 /*
19 /*	... RECV_ATTR_FUNC(msg_stats_scan, (void *) &stats), ...
20 /* DIAGNOSTICS
21 /*	Fatal: out of memory.
22 /* LICENSE
23 /* .ad
24 /* .fi
25 /*	The Secure Mailer license must be distributed with this software.
26 /* AUTHOR(S)
27 /*	Wietse Venema
28 /*	IBM T.J. Watson Research
29 /*	P.O. Box 704
30 /*	Yorktown Heights, NY 10598, USA
31 /*
32 /*	Wietse Venema
33 /*	Google, Inc.
34 /*	111 8th Avenue
35 /*	New York, NY 10011, USA
36 /*--*/
37 
38 /* System library. */
39 
40 #include <sys_defs.h>
41 
42 /* Utility library. */
43 
44 #include <attr.h>
45 #include <vstring.h>
46 #include <msg.h>
47 
48 /* Global library. */
49 
50 #include <mail_proto.h>
51 #include <msg_stats.h>
52 
53  /*
54   * SLMs.
55   */
56 #define STR(x) vstring_str(x)
57 #define LEN(x) VSTRING_LEN(x)
58 
59 /* msg_stats_scan - read MSG_STATS from stream */
60 
msg_stats_scan(ATTR_SCAN_COMMON_FN scan_fn,VSTREAM * fp,int flags,void * ptr)61 int     msg_stats_scan(ATTR_SCAN_COMMON_FN scan_fn, VSTREAM *fp,
62 		               int flags, void *ptr)
63 {
64     MSG_STATS *stats = (MSG_STATS *) ptr;
65     VSTRING *buf = vstring_alloc(sizeof(MSG_STATS) * 2);
66     int     ret;
67 
68     /*
69      * Receive the entire structure. This is not only simpler but also likely
70      * to be quicker than having the sender figure out what fields need to be
71      * sent, converting those numbers to string and back, and having the
72      * receiver initialize the unused fields by hand.
73      *
74      * XXX Would be nice if VSTRINGs could import a fixed-size buffer and
75      * gracefully reject attempts to extend it.
76      */
77     ret = scan_fn(fp, flags | ATTR_FLAG_MORE,
78 		  RECV_ATTR_DATA(MAIL_ATTR_TIME, buf),
79 		  ATTR_TYPE_END);
80     if (ret == 1) {
81 	if (LEN(buf) == sizeof(*stats)) {
82 	    memcpy((void *) stats, STR(buf), sizeof(*stats));
83 	} else {
84 	    msg_warn("msg_stats_scan: size mis-match: %u != %u",
85 		     (unsigned) LEN(buf), (unsigned) sizeof(*stats));
86 	    ret = (-1);
87 	}
88     }
89     vstring_free(buf);
90     return (ret);
91 }
92