- 论坛徽章:
- 0
|
用户空间程序
#include "imp2.h"
#include "GetIp.c"
#define LOG_PATH "/home/log/"
#define FILE_TAIL ".log"
#define INTER_VAL 20
struct msg_to_kernel
{
struct nlmsghdr hdr;
};
struct u_packet_info
{
struct nlmsghdr hdr;
struct packet_info icmp_info;
};
static int skfd;
char *logPath=LOG_PATH;
char fileName[30];
char *fileTail=FILE_TAIL;
char tmpStr[50]={0};
FILE *stream;
char *localName="localhost";
void getCurrentTime(char *str);
static void sig_int(int signo);
int main(){
time_t start,mark,tmp;
struct tm *tim;
struct sockaddr_nl local;
struct sockaddr_nl kpeer;
int kpeerlen;
struct msg_to_kernel message;
struct u_packet_info info;
int sendlen = 0;
int rcvlen = 0;
__u16 ipLength;
__u16 ipID;
char currentTime[30];
struct in_addr saddr,daddr;
skfd = socket(PF_NETLINK, SOCK_RAW, NL_IMP2);
if(skfd < 0)
{
printf("can not create a netlink socket\n");
exit(0);
}
memset(&local, 0, sizeof(local));
local.nl_family = AF_NETLINK;
local.nl_pid = getpid();
local.nl_groups = 0;
if(bind(skfd, (struct sockaddr*)&local, sizeof(local)) != 0)
{
printf("bind() error\n");
return -1;
}
signal(SIGINT, sig_int);
memset(&kpeer, 0, sizeof(kpeer));
kpeer.nl_family = AF_NETLINK;
kpeer.nl_pid = 0;
kpeer.nl_groups = 0;
memset(&message, 0, sizeof(message));
message.hdr.nlmsg_len = NLMSG_LENGTH(0);
message.hdr.nlmsg_flags = 0;
message.hdr.nlmsg_type = IMP2_U_PID;
message.hdr.nlmsg_pid = local.nl_pid;
sendto(skfd, &message, message.hdr.nlmsg_len, 0,
(struct sockaddr*)&kpeer, sizeof(kpeer));
char* localIp=GetIp(localName);
time(&mark);
while(1){
kpeerlen = sizeof(struct sockaddr_nl);
rcvlen = recvfrom(skfd, &info, sizeof(struct u_packet_info),0, (struct sockaddr*)&kpeer, &kpeerlen);
getCurrentTime(currentTime);
ipLength=info.icmp_info.length;
ipID=info.icmp_info.ipid;
saddr.s_addr = info.icmp_info.src;
daddr.s_addr = info.icmp_info.dest;
printf("%s %d %s %s %d %s\n",currentTime,ipID,inet_ntoa(saddr),inet_ntoa(daddr),ipLength,localIp);
}
exit(0);
}
void getCurrentTime(char *str){
time_t now;
struct tm *timenow;
time(&now);
timenow=localtime(&now);
strftime(str,30,"%Y%m%d%H%M%S",timenow);
}
static void sig_int(int signo)
{
struct sockaddr_nl kpeer;
struct msg_to_kernel message;
memset(&kpeer, 0, sizeof(kpeer));
kpeer.nl_family = AF_NETLINK;
kpeer.nl_pid = 0;
kpeer.nl_groups = 0;
memset(&message, 0, sizeof(message));
message.hdr.nlmsg_len = NLMSG_LENGTH(0);
message.hdr.nlmsg_flags = 0;
message.hdr.nlmsg_type = IMP2_CLOSE;
message.hdr.nlmsg_pid = getpid();
sendto(skfd, &message, message.hdr.nlmsg_len, 0, (struct sockaddr *)(&kpeer),
sizeof(kpeer));
close(skfd);
exit(0);
}
|
[ 本帖最后由 lihw_2002 于 2007-12-10 16:08 编辑 ] |
|