xref: /original-bsd/sys/netccitt/pk_acct.c (revision baf24c0d)
1 /*
2  * Copyright (c) University of British Columbia, 1984
3  * Copyright (c) 1990 The Regents of the University of California.
4  * 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	7.3 (Berkeley) 08/30/90
13  */
14 
15 #include "param.h
16 #include "systm.h"
17 #include "time.h"
18 #include "proc.h"
19 #include "user.h"
20 #include "vnode.h"
21 #include "kernel.h"
22 #include "file.h"
23 #include "acct.h"
24 #include "uio.h"
25 #include "socket.h"
26 #include "socketvar.h"
27 
28 #include "../net/if.h"
29 
30 #include "x25.h"
31 #include "pk.h"
32 #include "pk_var.h"
33 #include "x25acct.h"
34 
35 
36 struct	vnode *pkacctp;
37 /*
38  *  Turn on packet accounting
39  */
40 
41 pk_accton (path)
42 	char *path;
43 {
44 	register struct vnode *vp = NULL;
45 	register struct nameidata *ndp = &u.u_nd;
46 	struct vnode *oacctp = pkacctp;
47 	int error;
48 
49 	ndp -> ni_segflg = UIO_USERSPACE;
50 	ndp -> ni_dirp = path;
51 	if (error = vn_open (ndp, FREAD|FWRITE, 0))
52 		return (error);
53 	vp = ndp -> ni_vp;
54 	if (vp -> v_type != VREG) {
55 		vrele (vp);
56 		return (EACCES);
57 	}
58 	pkacctp = vp;
59 	if (oacctp)
60 		vrele (oacctp);
61 	return (0);
62 }
63 
64 /*
65  *  Turn off packet accounting
66  */
67 
68 pk_acctoff ()
69 {
70 	if (pkacctp) {
71 		vrele (pkacctp);
72 		pkacctp = 0;
73 	}
74 }
75 
76 /*
77  *  Write a record on the accounting file.
78  */
79 
80 pk_acct (lcp)
81 register struct pklcd *lcp;
82 {
83 	register struct vnode *vp;
84 	register struct sockaddr_x25 *sa;
85 	register char *src, *dst;
86 	register int len;
87 	register long etime;
88 	static struct x25acct acbuf;
89 
90 	if ((vp = pkacctp) == 0)
91 		return;
92 	bzero ((caddr_t)&acbuf, sizeof (acbuf));
93 	if (lcp -> lcd_ceaddr != 0)
94 		sa = lcp -> lcd_ceaddr;
95 	else if (lcp -> lcd_craddr != 0) {
96 		sa = lcp -> lcd_craddr;
97 		acbuf.x25acct_callin = 1;
98 	} else
99 		return;
100 
101 	if (sa -> x25_opts.op_flags & X25_REVERSE_CHARGE)
102 		acbuf.x25acct_revcharge = 1;
103 	acbuf.x25acct_stime = lcp -> lcd_stime;
104 	acbuf.x25acct_etime = time.tv_sec - acbuf.x25acct_stime;
105 	acbuf.x25acct_uid = u.u_uid;
106 	acbuf.x25acct_psize = sa -> x25_opts.op_psize;
107 	acbuf.x25acct_net = sa -> x25_net;
108 	/*
109 	 * Convert address to bcd
110 	 */
111 	src = sa -> x25_addr;
112 	dst = acbuf.x25acct_addr;
113 	for (len = 0; *src; len++)
114 		if (len & 01)
115 			*dst++ |= *src++ & 0xf;
116 		else
117 			*dst = *src++ << 4;
118 	acbuf.x25acct_addrlen = len;
119 
120 	bcopy (sa -> x25_udata, acbuf.x25acct_udata,
121 		sizeof (acbuf.x25acct_udata));
122 	acbuf.x25acct_txcnt = lcp -> lcd_txcnt;
123 	acbuf.x25acct_rxcnt = lcp -> lcd_rxcnt;
124 
125 	(void) vn_rdwr(UIO_WRITE, vp, (caddr_t)&acbuf, sizeof (acbuf),
126 		(off_t)0, UIO_SYSSPACE, IO_UNIT|IO_APPEND, u.u_cred, (int *)0);
127 }
128