From 44cc95036f6c8977ddb01b979d3e66a1db87530b Mon Sep 17 00:00:00 2001 From: Beraldo Leal Date: Sat, 20 Jul 2013 15:21:19 -0400 Subject: [PATCH 1/2] added hability to cat read entire buffer when cat stream file --- inode.c | 57 ++++++++++++++++++++++++++++++++++----------------------- proto.c | 17 ++++++++++++----- proto.h | 1 + 3 files changed, 47 insertions(+), 28 deletions(-) diff --git a/inode.c b/inode.c index f798127..a264272 100644 --- a/inode.c +++ b/inode.c @@ -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 */ diff --git a/proto.c b/proto.c index c4eabce..4db4d50 100644 --- a/proto.c +++ b/proto.c @@ -178,6 +178,14 @@ 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) { @@ -185,13 +193,13 @@ int get_network_string(char *str, struct sk_buff *skb) 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, @@ -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); @@ -385,7 +393,6 @@ static void netsfs_top(struct work_struct *work) } } - if (ret == 0) dev_kfree_skb(netsfsinfo->skb); } diff --git a/proto.h b/proto.h index 5c229e7..b06405d 100644 --- a/proto.h +++ b/proto.h @@ -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 From 6fa8b20e0a3287ad14df3dab74a67365a3fc6398 Mon Sep 17 00:00:00 2001 From: Beraldo Leal Date: Sat, 20 Jul 2013 15:22:53 -0400 Subject: [PATCH 2/2] remove printk debug msgs --- inode.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/inode.c b/inode.c index a264272..6c67374 100644 --- a/inode.c +++ b/inode.c @@ -84,7 +84,6 @@ static ssize_t netsfs_read_stream(struct file *file, char __user *buf, 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); @@ -95,13 +94,9 @@ static ssize_t netsfs_read_stream(struct file *file, char __user *buf, 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);