求助大神inet_sock问题

hycf_123 2017-06-15 10:26:28
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");
}
...全文
538 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
hycf_123 2017-06-16
  • 打赏
  • 举报
回复
跪求大神给看看。。。

4,436

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 内核源代码研究区
社区管理员
  • 内核源代码研究区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧