xref: /original-bsd/sys/netccitt/pk_acct.c (revision 3b6250d9)
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.8 (Berkeley) 05/14/92
13  */
14 
15 #include "param.h"
16 #include "systm.h"
17 #include "namei.h"
18 #include "proc.h"
19 #include "vnode.h"
20 #include "kernel.h"
21 #include "file.h"
22 #include "socket.h"
23 #include "socketvar.h"
24 
25 #include "../net/if.h"
26 
27 #include "x25.h"
28 #include "pk.h"
29 #include "pk_var.h"
30 #include "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 	USES_VOP_UNLOCK;
42 	register struct vnode *vp = NULL;
43 	struct nameidata nd;
44 	struct vnode *oacctp = pkacctp;
45 	struct proc *p = curproc;
46 	int error;
47 
48 	if (path == 0)
49 		goto close;
50 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, path, p);
51 	if (error = vn_open (&nd, FWRITE, 0644))
52 		return (error);
53 	vp = nd.ni_vp;
54 	VOP_UNLOCK(vp);
55 	if (vp -> v_type != VREG) {
56 		vrele (vp);
57 		return (EACCES);
58 	}
59 	pkacctp = vp;
60 	if (oacctp) {
61 	close:
62 		error = vn_close (oacctp, FWRITE, p -> p_ucred, p);
63 	}
64 	return (error);
65 }
66 
67 /*
68  *  Write a record on the accounting file.
69  */
70 
71 pk_acct (lcp)
72 register struct pklcd *lcp;
73 {
74 	register struct vnode *vp;
75 	register struct sockaddr_x25 *sa;
76 	register char *src, *dst;
77 	register int len;
78 	register long etime;
79 	static struct x25acct acbuf;
80 
81 	if ((vp = pkacctp) == 0)
82 		return;
83 	bzero ((caddr_t)&acbuf, sizeof (acbuf));
84 	if (lcp -> lcd_ceaddr != 0)
85 		sa = lcp -> lcd_ceaddr;
86 	else if (lcp -> lcd_craddr != 0) {
87 		sa = lcp -> lcd_craddr;
88 		acbuf.x25acct_callin = 1;
89 	} else
90 		return;
91 
92 	if (sa -> x25_opts.op_flags & X25_REVERSE_CHARGE)
93 		acbuf.x25acct_revcharge = 1;
94 	acbuf.x25acct_stime = lcp -> lcd_stime;
95 	acbuf.x25acct_etime = time.tv_sec - acbuf.x25acct_stime;
96 	acbuf.x25acct_uid = curproc -> p_cred -> p_ruid;
97 	acbuf.x25acct_psize = sa -> x25_opts.op_psize;
98 	acbuf.x25acct_net = sa -> x25_net;
99 	/*
100 	 * Convert address to bcd
101 	 */
102 	src = sa -> x25_addr;
103 	dst = acbuf.x25acct_addr;
104 	for (len = 0; *src; len++)
105 		if (len & 01)
106 			*dst++ |= *src++ & 0xf;
107 		else
108 			*dst = *src++ << 4;
109 	acbuf.x25acct_addrlen = len;
110 
111 	bcopy (sa -> x25_udata, acbuf.x25acct_udata,
112 		sizeof (acbuf.x25acct_udata));
113 	acbuf.x25acct_txcnt = lcp -> lcd_txcnt;
114 	acbuf.x25acct_rxcnt = lcp -> lcd_rxcnt;
115 
116 	(void) vn_rdwr(UIO_WRITE, vp, (caddr_t)&acbuf, sizeof (acbuf),
117 		(off_t)0, UIO_SYSSPACE, IO_UNIT|IO_APPEND,
118 		curproc -> p_ucred, (int *)0,
119 		(struct proc *)0);
120 }
121