/*
* =====================================================================================
// Last Change: 2010-08-07 20:27:58
*
* Filename: watchrtx.c
*
* Description: watchrtx.c
*
* 本代码仅在ubuntu10.04测试通过!
* 安装编译依赖:
* $sudo apt-get install libgtk2.0-dev libnotify-dev
* 编译:
* $gcc -Wall `pkg-config --cflags gtk+-2.0 glib-2.0` watchrtx.c \
-lnotify -o watchrtx
* 运行示例:
* $sudo ./watchrtx ###ubuntu 需要root才能会显示通知区域
*
* Version: 1.0
* Revision: none
* Compiler: gcc
*
* Author: BaiLiangcn@gmail.com
* Company:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <linux/if_ether.h>
#include <libnotify/notify.h>
#include <time.h>
#include <arpa/inet.h>
#define RTXSIP { 10,63,128,53 }
#define RTXPORT 8000
#define DELAYTIME NOTIFY_EXPIRES_NEVER
#define MAXDATALEN 1000
int main(int argc, char **argv) {
int sock, n;
unsigned char buffer[2048];
unsigned char *iphead;
int sourcePort;
const int rtxip[4]=RTXSIP;
time_t timep;
char date_time[21];
NotifyNotification* nnull;
notify_init("Basics"); //初始化notify
if ( (sock=socket(PF_PACKET, SOCK_RAW,
htons(ETH_P_IP)))<0) {
perror("socket");
exit(1);
} //判断网络是否正常
//显示提示OSD,开始监听RTX
nnull = notify_notification_new("Hi","开始监听RTX",GTK_STOCK_ABOUT,NULL);
notify_notification_set_timeout(nnull,DELAYTIME);
notify_notification_show(nnull,NULL);
while (1) {
n = recvfrom(sock,buffer,2048,0,NULL,NULL); //读取网络数据
if (n<42) {
perror("recvfrom():");
printf("Incomplete packet (errno is %d)\n",
errno);
close(sock);
exit(0);
} //如果网络包不完整,退出
iphead = buffer+14; /* 跳过 Ethernet 包头 */
if (*iphead==0x45) { /* 判断是否是ipv4 */
sourcePort = (iphead[20]<<8)+iphead[21]; //取源端口
//比较RTX服务器地址、端口、包长度
if (iphead[12]==rtxip[0] && iphead[13]==rtxip[1] && iphead[14]==rtxip[2]
&& iphead[15]==rtxip[3] && sourcePort==RTXPORT && n > MAXDATALEN){
time(&timep); //取当前时间,生成时间字符串
strftime(date_time, sizeof(date_time),
"%Y-%m-%d %H:%M:%S\n", localtime(&timep));
//输出信息提示OSD
notify_notification_update(nnull,"RTX",date_time,GTK_STOCK_INFO);
notify_notification_show(nnull,NULL);
}
}
}
}
|