-
Notifications
You must be signed in to change notification settings - Fork 56
/
netcat_main.c
141 lines (115 loc) · 3.19 KB
/
netcat_main.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#define pr_fmt(fmt) "[" KBUILD_MODNAME "]: " fmt
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/miscdevice.h>
#include <linux/slab.h>
#include "netcat.h"
struct netcat {
char *msg;
bool first_time;
int current_track;
};
#define DEVICE_NAME "netcat" /* Dev name as it appears in /proc/devices */
extern struct netcat_track netcat_cpi_trk1;
extern struct netcat_track netcat_cpi_trk2;
extern struct netcat_track netcat_cpi_trk3;
extern struct netcat_track netcat_cpi_trk4;
extern struct netcat_track netcat_cpi_trk5;
extern struct netcat_track netcat_cpi_trk6;
static struct netcat_track *tracks[] = {&netcat_cpi_trk1,
&netcat_cpi_trk2,
&netcat_cpi_trk3,
&netcat_cpi_trk4,
&netcat_cpi_trk5,
&netcat_cpi_trk6};
static int device_open(struct inode *inode, struct file *file)
{
struct netcat *netcat;
netcat = kzalloc(sizeof(*netcat), GFP_KERNEL);
if (!netcat)
return -ENOMEM;
netcat->first_time = true;
netcat->msg = tracks[0]->data; /* track 1 */
file->private_data = netcat;
return 0;
}
static int device_release(struct inode *inode, struct file *file)
{
struct netcat *netcat = file->private_data;
kfree(netcat);
return 0;
}
static ssize_t device_read(struct file *file,
char *buffer,
size_t length,
loff_t *offset)
{
struct netcat *netcat = file->private_data;
int current_track = netcat->current_track;
int bytes_read = 0;
if (netcat->first_time == true) {
pr_info("Now playing track %d - %s\n",
current_track + 1, tracks[current_track]->name);
netcat->first_time = false;
}
if (netcat->msg - tracks[current_track]->data >=
tracks[current_track]->len) {
/* End of Track. Skip to next track, or finish if it's the last track */
current_track++;
if (current_track >= ARRAY_SIZE(tracks))
current_track = 0;
pr_info("Now playing track %d - %s\n",
current_track + 1, tracks[current_track]->name);
netcat->msg = tracks[current_track]->data;
netcat->current_track = current_track;
}
while (length &&
(netcat->msg - tracks[current_track]->data) <
tracks[current_track]->len) {
put_user(*(netcat->msg++), buffer++);
length--;
bytes_read++;
}
return bytes_read;
}
static ssize_t
device_write(struct file *filp, const char *buff, size_t len, loff_t *off)
{
pr_err("Writing to netcat - Cycles Per Instruction isn't supported.\n");
return -EINVAL;
}
static const struct file_operations fops = {
.owner = THIS_MODULE,
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release
};
static struct miscdevice netcat_dev = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &fops,
.mode = S_IRUGO,
};
static int netcat_init(void)
{
int ret;
ret = misc_register(&netcat_dev);
if (ret) {
pr_err("misc device register failed\n");
return ret;
}
pr_info("netcat - Cycles Per Instruction - Kernel Module Edition - 2014\n");
pr_info("netcat is Brandon Lucia, Andrew Olmstead, and David Balatero\n");
pr_info("On the web at http://netcat.co\n");
pr_info("'ogg123 - < /dev/netcat' to play.\n");
return 0;
}
static void netcat_exit(void)
{
misc_deregister(&netcat_dev);
}
module_init(netcat_init);
module_exit(netcat_exit);