Skip to content

Commit

Permalink
added hability to cat read entire buffer when cat stream file
Browse files Browse the repository at this point in the history
  • Loading branch information
beraldoleal committed Jul 20, 2013
1 parent 0915302 commit 44cc950
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 28 deletions.
57 changes: 34 additions & 23 deletions inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,37 +71,48 @@ static ssize_t netsfs_read_stream(struct file *file, char __user *buf,

char stream_buf[STREAM_BUF_LEN];
char *mac_string, *network_string;
size_t ret = 0;
size_t ret = 0, size = 0;


/*
if (*ppos != 0)
return 0;
*/

d_private = file->f_dentry->d_parent->d_inode->i_private;
skb = cq_get(&d_private->queue_skbuff);
if (skb) {
mac_string = kmalloc(sizeof(char)*80, GFP_KERNEL);
network_string = kmalloc(sizeof(char)*80, GFP_KERNEL);

if (!mac_string) {
printk("AQUI PAU\n");
return -ENOMEM;
}

get_mac_string(mac_string, skb);
get_network_string(network_string, skb);

ret = sprintf(stream_buf, "%s\n%s\n", mac_string, network_string);

if (ret > 0)
ret = simple_read_from_buffer(buf, count, ppos, stream_buf, ret);
if (kfifo_initialized(&d_private->queue_skbuff)) {
size = cq_howmany(&d_private->queue_skbuff);
if (size > 0) {
printk("size = %ld\n", size); // 32 , 32
skb = cq_get(&d_private->queue_skbuff);
if (skb) {
mac_string = kmalloc(sizeof(char)*80, GFP_KERNEL);
network_string = kmalloc(sizeof(char)*80, GFP_KERNEL);

get_mac_string(mac_string, skb);
get_network_string(network_string, skb);

ret = sprintf(stream_buf, "%s\n%s\n", mac_string, network_string);

printk("primeiro ret = %ld\n", ret); // 148, 147
if (ret > 0) {
printk("ppos antes = %lld\n", *ppos); // 0, 148
*ppos = 0;
ret = simple_read_from_buffer(buf, count, ppos, stream_buf, ret);
printk("segundo ret = %ld\n", ret); // 148, 0
printk("count = %ld, ppos = %lld\n", count, *ppos); // 148, 148
}

dev_kfree_skb(skb);
if (mac_string) kfree(mac_string);
if (network_string) kfree(network_string);
// kfree(dst);
// kfree(network);
dev_kfree_skb(skb);
if (mac_string) kfree(mac_string);
if (network_string) kfree(network_string);
}
return ret;
}
}
return ret;

return 0;
}

/* User space ask to read a read a file */
Expand Down
17 changes: 12 additions & 5 deletions proto.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,20 +178,28 @@ char *get_ip_protocol(struct sk_buff *skb)
return "unknow1";
}

int get_transport_string(char *str, struct sk_buff *skb)
{

sprintf(str, "[TRANSPORT] ************* ");

return 0;
}

int get_network_string(char *str, struct sk_buff *skb)
{

struct iphdr *iphdr;
char *src, *dst;
iphdr = ip_hdr(skb);

src = kmalloc(sizeof(char)*15, GFP_KERNEL);
dst = kmalloc(sizeof(char)*15, GFP_KERNEL);
src = kmalloc(sizeof(char)*35, GFP_KERNEL);
dst = kmalloc(sizeof(char)*35, GFP_KERNEL);

get_ip_address(src, skb, SRC_ADDRESS);
get_ip_address(dst, skb, DST_ADDRESS);

sprintf(str, "[NETWORK] version: %d, tos: %02x, id: %04x, protocol: %s, %s -> %s",
sprintf(str, "[ NETWORK ] version: %d, tos: %02x, id: %04x, protocol: %s, %s -> %s",
iphdr->version,
iphdr->tos,
iphdr->id,
Expand All @@ -206,7 +214,7 @@ int get_network_string(char *str, struct sk_buff *skb)

int get_mac_string(char *str, struct sk_buff *skb)
{
sprintf(str, "[MAC] ts: %llu, dev: %s, len: %d",
sprintf(str, "[ MAC ] ts: %llu, dev: %s, len: %d",
skb->tstamp.tv64,
skb->dev->name,
skb->len);
Expand Down Expand Up @@ -385,7 +393,6 @@ static void netsfs_top(struct work_struct *work)
}
}


if (ret == 0)
dev_kfree_skb(netsfsinfo->skb);
}
Expand Down
1 change: 1 addition & 0 deletions proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extern void netsfs_register_pack(void);
int netsfs_packet_handler(struct sk_buff *skb, struct net_device *dev, struct packet_type *pkt,
struct net_device *dev2);

int get_transport_string(char *str, struct sk_buff *skb);
int get_network_string(char *str, struct sk_buff *skb);
int get_mac_string(char *str, struct sk_buff *skb);
#endif

0 comments on commit 44cc950

Please sign in to comment.