forked from rumpkernel-attic/rumpuser-linuxkernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo-net.c
85 lines (71 loc) · 1.93 KB
/
demo-net.c
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
/*
* Use the TCP/IP stack in the rump kernel to open a TCP connection
* to vger.kernel.org port 80 and get the main page.
*/
#include <linux/kernel.h>
#include <linux/in.h>
#include <linux/net.h>
#include <linux/slab.h>
#include <linux/socket.h>
#include <rump/rump.h>
#include <rump/rump_syscalls.h>
#include <rump/netconfig.h>
void rumpkern_demo(void);
#define IFNAME "virt0"
#define WANTHTML "GET / HTTP/1.0\n\n"
#define BUFSIZE (64*1024)
/* vger.kernel.org in network byte order ... or did you want me to do DNS? */
#define DESTADDR 0x43b484d1
void
rumpkern_demo(void)
{
struct sockaddr_in sin;
char *buf;
ssize_t nn, off;
int error;
int s;
/* create interface and configure an address for it */
error = rump_pub_netconfig_ifcreate(IFNAME);
printk(KERN_INFO "created %s: %d\n", IFNAME, error);
error = rump_pub_netconfig_dhcp_ipv4_oneshot(IFNAME);
if (error) {
printk(KERN_INFO "failed to configure networking %d\n", error);
return;
}
s = rump_sys_socket(PF_INET, SOCK_STREAM, 0);
if (s == -1) {
printk(KERN_INFO "you're mean! no sucket for you!\n");
return;
}
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(80);
sin.sin_addr.s_addr = DESTADDR;
if (rump_sys_connect(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
printk(KERN_INFO "could not connect\n");
return;
}
nn = rump_sys_write(s, WANTHTML, sizeof(WANTHTML)-1);
printk(KERN_INFO "wrote http request, rv %zd\n", nn);
buf = kmalloc(BUFSIZE, GFP_KERNEL);
off = 0;
do {
nn = rump_sys_read(s, buf+off, (BUFSIZE-off)-1);
off += nn;
BUG_ON(off >= BUFSIZE);
} while (nn > 0);
if (nn == -1) {
printk(KERN_INFO "read failed: %zd\n", nn);
return;
}
printk(KERN_INFO "read %zd bytes\n", off);
/* display last 500 bytes of delicious info */
buf[off] = '\0';
off -= 500;
if (off < 0)
off = 0;
printk(KERN_INFO "that was an educational experience. "
"we learned:\n");
printk(KERN_INFO "%s", &buf[off]);
kfree(buf);
}