xref: /original-bsd/sys/netccitt/pk_acct.c (revision 0ac4996f)
1 /*
2  * Copyright (c) University of British Columbia, 1984
3  * Copyright (c) 1990, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * the Laboratory for Computation Vision and the Computer Science Department
8  * of the University of British Columbia.
9  *
10  * %sccs.include.redist.c%
11  *
12  *	@(#)pk_acct.c	8.2 (Berkeley) 05/14/95
13  */
14 
15 #include <sys/param.h>
16 #include <sys/systm.h>
17 #include <sys/namei.h>
18 #include <sys/proc.h>
19 #include <sys/vnode.h>
20 #include <sys/kernel.h>
21 #include <sys/file.h>
22 #include <sys/socket.h>
23 #include <sys/socketvar.h>
24 
25 #include <net/if.h>
26 
27 #include <netccitt/x25.h>
28 #include <netccitt/pk.h>
29 #include <netccitt/pk_var.h>
30 #include <netccitt/x25acct.h>
31 
32 
33 struct	vnode *pkacctp;
34 /*
35  *  Turn on packet accounting
36  */
37 
38 pk_accton (path)
39 	char *path;
40 {
41 	register struct vnode *vp = NULL;
42 	struct nameidata nd;
43 	struct vnode *oacctp = pkacctp;
44 	struct proc *p = curproc;
45 	int error;
46 
47 	if (path == 0)
48 		goto close;
49 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, path, p);
50 	if (error = vn_open (&nd, FWRITE, 0644))
51 		return (error);
52 	vp = nd.ni_vp;
53 	VOP_UNLOCK(vp, 0, p);
54 	if (vp -> v_type != VREG) {
55 		vrele (vp);
56 		return (EACCES);
57 	}
58 	pkacctp = vp;
59 	if (oacctp) {
60 	close:
61 		error = vn_close (oacctp, FWRITE, p -> p_ucred, p);
62 	}
63 	return (error);
64 }
65 
66 /*
67  *  Write a record on the accounting file.
68  */
69 
70 pk_acct (lcp)
71 register struct pklcd *lcp;
72 {
73 	register struct vnode *vp;
74 	register struct sockaddr_x25 *sa;
75 	register char *src, *dst;
76 	register int len;
77 	register long etime;
78 	static struct x25acct acbuf;
79 
80 	if ((vp = pkacctp) == 0)
81 		return;
82 	bzero ((caddr_t)&acbuf, sizeof (acbuf));
83 	if (lcp -> lcd_ceaddr != 0)
84 		sa = lcp -> lcd_ceaddr;
85 	else if (lcp -> lcd_craddr != 0) {
86 		sa = lcp -> lcd_craddr;
87 		acbuf.x25acct_callin = 1;
88 	} else
89 		return;
90 
91 	if (sa -> x25_opts.op_flags & X25_REVERSE_CHARGE)
92 		acbuf.x25acct_revcharge = 1;
93 	acbuf.x25acct_stime = lcp -> lcd_stime;
94 	acbuf.x25acct_etime = time.tv_sec - acbuf.x25acct_stime;
95 	acbuf.x25acct_uid = curproc -> p_cred -> p_ruid;
96 	acbuf.x25acct_psize = sa -> x25_opts.op_psize;
97 	acbuf.x25acct_net = sa -> x25_net;
98 	/*
99 	 * Convert address to bcd
100 	 */
101 	src = sa -> x25_addr;
102 	dst = acbuf.x25acct_addr;
103 	for (len = 0; *src; len++)
104 		if (len & 01)
105 			*dst++ |= *src++ & 0xf;
106 		else
107 			*dst = *src++ << 4;
108 	acbuf.x25acct_addrlen = len;
109 
110 	bcopy (sa -> x25_udata, acbuf.x25acct_udata,
111 		sizeof (acbuf.x25acct_udata));
112 	acbuf.x25acct_txcnt = lcp -> lcd_txcnt;
113 	acbuf.x25acct_rxcnt = lcp -> lcd_rxcnt;
114 
115 	(void) vn_rdwr(UIO_WRITE, vp, (caddr_t)&acbuf, sizeof (acbuf),
116 		(off_t)0, UIO_SYSSPACE, IO_UNIT|IO_APPEND,
117 		curproc -> p_ucred, (int *)0,
118 		(struct proc *)0);
119 }
120