aboutsummaryrefslogtreecommitdiff
path: root/sbin/ipf/libipf/kmem.c
blob: 6ebd0dee6b7246da2ba0a89263400876d8cc3e92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

/*
 * Copyright (C) 2012 by Darren Reed.
 *
 * See the IPFILTER.LICENCE file for details on licencing.
 */
/*
 * kmemcpy() - copies n bytes from kernel memory into user buffer.
 * returns 0 on success, -1 on error.
 */

#include <stdio.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/file.h>
#include <kvm.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <net/if.h>

#include "kmem.h"




static	kvm_t	*kvm_f = NULL;


int
openkmem(char *kern, char *core)
{
	kvm_f = kvm_open(kern, core, NULL, O_RDONLY, NULL);
	if (kvm_f == NULL)
	    {
		perror("openkmem:open");
		return (-1);
	    }
	return (kvm_f != NULL);
}

int
kmemcpy(register char *buf, long pos, register int n)
{
	register int	r;

	if (!n)
		return (0);

	if (kvm_f == NULL)
		if (openkmem(NULL, NULL) == -1)
			return (-1);

	while ((r = kvm_read(kvm_f, pos, buf, n)) < n)
		if (r <= 0)
		    {
			fprintf(stderr, "pos=0x%lx ", (u_long)pos);
			perror("kmemcpy:read");
			return (-1);
		    }
		else
		    {
			buf += r;
			pos += r;
			n -= r;
		    }
	return (0);
}

int
kstrncpy(register char *buf, long pos, register int n)
{
	register int	r;

	if (!n)
		return (0);

	if (kvm_f == NULL)
		if (openkmem(NULL, NULL) == -1)
			return (-1);

	while (n > 0)
	    {
		r = kvm_read(kvm_f, pos, buf, 1);
		if (r <= 0)
		    {
			fprintf(stderr, "pos=0x%lx ", (u_long)pos);
			perror("kmemcpy:read");
			return (-1);
		    }
		else
		    {
			if (*buf == '\0')
				break;
			buf++;
			pos++;
			n--;
		    }
	    }
	return (0);
}