运用UDP协议的echo中心看护进程-C / C++-优质IT资源分享社区

admin
管理员
管理员
  • UID1
  • 粉丝29
  • 关注4
  • 发帖数581
  • 社区居民
  • 忠实会员
  • 原创写手
阅读:178回复:0

  运用UDP协议的echo中心看护进程

楼主#
更多 发布于:2016-05-21 23:04

昨日花了半天时间写了一个运用UDP协议的echo中心看护进程,请我们纠正

/* Makefile */

all:echod.o

.c.o:

gcc -I/usr/src/linux/include -O2 -c

$Content$lt;

clean:

rm -f *.o

/* echod.c */

#define MODULE

#define __KERNEL__

#include

#if CONFIG_MODVERSIONS

#define MODVERSIONS

#include

#endif

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

struct timer_list tm;

DECLARE_WAIT_QUEUE_HEAD(wq);

DECLARE_WAIT_QUEUE_HEAD(wm);

volatile int finish = 0;

int port = 7;

int init_sock(struct socket **s, int port)

{

struct sockaddr_in sin;

int error = 0;

error = sock_create(PF_INET, SOCK_DGRAM,

IPPROTO_UDP, s);

if (error < 0) {

printk("Error during creation of socketn");

goto out;

}

sin.sin_family = AF_INET;

sin.sin_addr.s_addr = INADDR_ANY;

sin.sin_port = htons((unsigned short)port);

error = (*s) -> ops -> bind(*s, (struct

sockaddr*)&sin, sizeof(sin));

if (error<0) {

printk("Error bindn");

goto out;

}

(*s) -> sk -> reuse = 1;

out:

return error;

}

void doecho(struct socket *s)

{

struct msghdr msg;

struct iovec iov;

int len;

char *buf;

struct sockaddr_in sin;

mm_segment_t oldfs;

if (skb_queue_empty(&(s ->sk ->

receive_queue)))

return;

buf = (char *)kmalloc(4096, GFP_KERNEL);

msg.msg_name = (void *)&sin;

msg.msg_namelen = sizeof(sin);

msg.msg_iov = &iov;

msg.msg_iovlen = 1;

msg.msg_controllen = 0;

msg.msg_flags =0;

msg.msg_iov -> iov_base = buf;

msg.msg_iov -> iov_len = (size_t) 4095;

len = 0;

oldfs = get_fs();

set_fs(get_ds());

len = sock_recvmsg(s, &msg, 4095,

MSG_DONTWAIT);

buf[len] = 0;

/* printk("recv: %s : %dn", buf, len); */

set_fs(oldfs);

if ((len < 0) || (len > 4094))

goto outdo;

oldfs = get_fs();

set_fs(get_ds());

msg.msg_control = NULL;

msg.msg_controllen = 0;

msg.msg_iov -> iov_base = buf;

msg.msg_iov -> iov_len = len;

msg.msg_flags = MSG_NOSIGNAL;

len = sock_sendmsg(s, &msg, len);

set_fs(oldfs);

outdo:

kfree(buf);

}

int echod(void *ptr)

{

struct socket *s = NULL;

sprintf(current -> comm, "echod");

daemonize();

if (init_sock(&s, port))

return -1;

printk("echod beginn");

while (!finish) {

doecho(s);

interruptible_sleep_on_timeout(&wq, 1);

}

sock_release(s);

printk("echod endn");

wake_up_interruptible(&wm);

return 0;

}

void tm_timeout(unsigned long ptr)

{

printk("wait up itn");

finish = 1;

}

MODULE_PARM(port, "i");

int init_module()

{

kernel_thread(echod, NULL, CLONE_FS | CLONE_FILES

| CLONE_SIGHAND);

return 0;

}

void cleanup_module()

{

tm_timeout(0);

interruptible_sleep_on(&wm);

}

优质IT资源分享社区[font=Tahoma  ]为你提供此文。[font=Tahoma  ]

[font=Tahoma  ]本站有大量优质C、C++教程视频,资料等资源,包含C,C++基础教程,高级进阶教程等等,教程视频资源涵盖传智播客,极客学院,达内,北大青鸟,猎豹网校等等IT职业培训机构的培训教学视频,价值巨大。欢迎点击下方链接查看。[font=Tahoma  ]

C、C++教程视频

优质IT资源分享社区(www.itziyuan.top)
一个免费,自由,开放,共享,平等,互助的优质IT资源分享网站。
专注免费分享各大IT培训机构最新培训教学视频,为你的IT学习助力!

!!!回帖受限制请看点击这里!!!
!!!资源失效请在此版块发帖说明!!!

[PS:按 CTRL+D收藏本站网址~]

——“优质IT资源分享社区”管理员专用签名~

本版相似帖子

游客