求助大神inet_sock问题
kernel版本为2.6.32,从inet_sock结构体中取出来的daddr dport saddr sport四个成员的数据是错误的。。。
从task_struct中获取进程开启的文件,判断如果是个socket文件,就打印出daddr dport saddr sport这些成员的值。
但是实际情况出现偏差:
Process:sshd [3140]
-- /dev/null
-- /dev/null
-- /dev/null
Net: 0.0.0.0:0 ----> 0.0.0.0:0
Net: 0.0.0.0:0 ----> 0.0.0.0:0
-- pipe:[19769]
-- pipe:[19769]
-- /dev/ptmx
-- /dev/ptmx
-- /dev/ptmx
Process:sshd [3142]
-- /dev/null
-- /dev/null
-- /dev/null
Net: 0.0.0.0:0 ----> 0.0.0.0:0
Net: 0.0.0.0:0 ----> 0.0.0.0:0
-- pipe:[19771]
-- pipe:[19771]
-- /dev/ptmx
-- /dev/ptmx
-- /dev/ptmx
Process:sshd [3145]
-- /dev/null
-- /dev/null
-- /dev/null
Net: 0.136.255.255:24722 ----> 88.176.148.57:63719
Net: 107.107.107.107:27499 ----> 107.107.107.107:27499
-- pipe:[19770]
-- pipe:[19770]
-- /dev/ptmx
-- /dev/ptmx
-- /dev/ptmx
跪求大神给看看哪儿的问题。。。
代码如下:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/file.h>
#include <linux/dcache.h>
#include <linux/fdtable.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/limits.h>
#include <linux/string.h>
#include <linux/net.h>
#include <net/inet_sock.h>
static int __init myfile_init(void);
static void __exit myfile_exit(void);
module_init(myfile_init);
module_exit(myfile_exit);
MODULE_LICENSE("GPL");
void get_socket(struct file *f){
struct socket *st;
struct inet_sock *is;
st = list_entry(f, struct socket, file);
is = inet_sk(st->sk);
printk(KERN_ALERT"Net: %d.%d.%d.%d:%u ----> %d.%d.%d.%d:%u\n",NIPQUAD(is->saddr),ntohs(is->sport),NIPQUAD(is->daddr),ntohs(is->dport));
}
char* get_path(struct path p) {
char *buff = NULL;
char *path = NULL;
buff = kmalloc(PATH_MAX,GFP_KERNEL);
if (!buff)
return NULL;
path = d_path(&p,buff,PATH_MAX);
if (path == strstr(path,"socket"))
return "socket";
else
return path;
}
static int __init myfile_init(void) {
struct task_struct *t;
struct files_struct *f;
struct file *file;
struct fdtable *fdt;
struct path p;
char *msg;
int i;
list_for_each_entry(t,¤t->tasks,tasks) {
printk(KERN_ALERT"Process:%s [%d]\n", t->comm, t->pid);
task_lock(t);
f = t->files;
if(NULL != f){
fdt = f->fdt;
for (i = 0;i<NR_OPEN_DEFAULT;i++) {
if (fdt != NULL) {
file = fdt->fd[i];
if(file != NULL && file->f_path.dentry != NULL) {
p = file->f_path;
msg = get_path(p);
if (msg != NULL && msg != "socket")
printk(KERN_ALERT"-- %s\n",msg);
if (msg == "socket")
get_socket(file);
}
}
}
}
task_unlock(t);
}
return 0;
}
static void __exit myfile_exit(void) {
printk(KERN_ALERT"Remove Module!\n");
}