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_MASTER_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/*	... ATTR_SCAN_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
33/* System library. */
34
35#include <sys_defs.h>
36
37/* Utility library. */
38
39#include <attr.h>
40#include <vstring.h>
41#include <msg.h>
42
43/* Global library. */
44
45#include <mail_proto.h>
46#include <msg_stats.h>
47
48 /*
49  * SLMs.
50  */
51#define STR(x) vstring_str(x)
52#define LEN(x) VSTRING_LEN(x)
53
54/* msg_stats_scan - read MSG_STATS from stream */
55
56int     msg_stats_scan(ATTR_SCAN_MASTER_FN scan_fn, VSTREAM *fp,
57		               int flags, void *ptr)
58{
59    MSG_STATS *stats = (MSG_STATS *) ptr;
60    VSTRING *buf = vstring_alloc(sizeof(MSG_STATS) * 2);
61    int     ret;
62
63    /*
64     * Receive the entire structure. This is not only simpler but also likely
65     * to be quicker than having the sender figure out what fields need to be
66     * sent, converting those numbers to string and back, and having the
67     * receiver initialize the unused fields by hand.
68     *
69     * XXX Would be nice if VSTRINGs could import a fixed-size buffer and
70     * gracefully reject attempts to extend it.
71     */
72    ret = scan_fn(fp, flags | ATTR_FLAG_MORE,
73		  ATTR_TYPE_DATA, MAIL_ATTR_TIME, buf,
74		  ATTR_TYPE_END);
75    if (ret == 1) {
76	if (LEN(buf) == sizeof(*stats)) {
77	    memcpy((char *) stats, STR(buf), sizeof(*stats));
78	} else {
79	    msg_warn("msg_stats_scan: size mis-match: %u != %u",
80		     (unsigned) LEN(buf), (unsigned) sizeof(*stats));
81	    ret = (-1);
82	}
83    }
84    vstring_free(buf);
85    return (ret);
86}
87