📄 snap_demux_handler.c
字号:
/* * snap_demux_handler.c : functions for retrieving DEMUXed data */#include <string.h>#include <sys/types.h>#include <netinet/in_systm.h>#include <netinet/in.h>#include <netinet/ip.h>#include <unistd.h>#include <sys/socket.h>#include <sys/un.h>#include <ctype.h> /* for isprint(..) call */#include "../lib/d_printf.h"#include "../lib/io.h"#include <time.h>#include <sys/time.h>#include "snap_demux_handler.h"int socket_unix = -1;int socket_rawip = -1;int socket_udp = -1;int protocols_internal = 0;int max_filedes = -1;fd_set fdset;/* UNIX socket specific code*/void snap_demux_close_unix(){ char filename[PATH_MAX]; /* close the file descriptor*/ close(socket_unix); /* remove the accompanying file */ sprintf(filename, "/tmp/snap%d",receiveport); remove(filename); socket_unix = -1;}int snap_demux_init_unix(){ struct sockaddr_un laddr; if ((socket_unix = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) { perror("demux_init_unix: socket()"); return 1; } laddr.sun_family = AF_UNIX; sprintf(laddr.sun_path, "/tmp/snap%d",receiveport); if (bind(socket_unix, (struct sockaddr *) &laddr, sizeof(laddr)) < 0){ perror("receivesock bind()"); snap_demux_close_unix (); return 1; } return 0;}/* RAW IP socket specific code*/void snap_demux_close_rawip(){ /* close the file descriptor*/ close(socket_rawip); socket_rawip = -1;}int snap_demux_init_rawip(){ struct sockaddr_in bindaddr; if ((socket_rawip = socket(AF_INET, SOCK_RAW, IPPROTO_SNAP)) < 0) { perror("snap_demux_init_rawip: socket()"); return 1; } memset(&bindaddr, 0, sizeof(bindaddr)); bindaddr.sin_family = AF_INET; bindaddr.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(socket_rawip, (struct sockaddr *)&bindaddr,sizeof(bindaddr)) < 0) { perror("snap_demux_init_rawip: bind()"); snap_demux_close_rawip(); return 1; } return 0;}/* UDP socket specific code*/void snap_demux_close_udp(){ /* close the file descriptor*/ close(socket_udp); socket_udp = -1;}int snap_demux_init_udp(){ struct sockaddr_in bindaddr; if ((socket_udp = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("snap_demux_init_udp: socket()"); return 1; } memset(&bindaddr, 0, sizeof(bindaddr)); bindaddr.sin_family = AF_INET; bindaddr.sin_addr.s_addr = htonl(INADDR_ANY); bindaddr.sin_port = htons(receiveport); if (bind(socket_udp, (struct sockaddr *)&bindaddr,sizeof(bindaddr)) < 0) { perror("snap_demux_init_udp: bind()"); snap_demux_close_udp(); return 1; } return 0;}/* buffer handlers*/void snap_demux_buffer_noop(char* pbuf){}void snap_demux_buffer_print_unsafe(char* pbuf){ fprintf(stdout,"DEMUX output = [%s]\n",pbuf);}void snap_demux_buffer_print(char* pbuf){ int i; fprintf(stderr,"DEMUX output = ["); for(i=0; i<strlen(pbuf); i++) { if (pbuf[i] == '\0') { break; } switch(pbuf[i]) { case '"': fprintf(stderr,"\\\""); break; case '\\': fprintf(stderr,"\\"); break; case '\n': fprintf(stderr,"\\n"); break; case '\b': fprintf(stderr,"\\b"); break; case '\r': fprintf(stderr,"\\r"); break; case '\t': fprintf(stderr,"\\t"); break; default: if (isprint(pbuf[i])) { fprintf(stderr,"%c",pbuf[i]); } else { fprintf(stderr,"\\%03o",pbuf[i]); } break; } } fprintf(stderr,"] \n");}/* general code*/void snap_demux_close(){ if (protocols_internal & SNAP_UNIX) snap_demux_close_unix(); if (protocols_internal & SNAP_RAWIP) snap_demux_close_rawip(); if (protocols_internal & SNAP_UDP) snap_demux_close_udp(); /* extend in identical fashion for new protocols */ /* reset vars */ protocols_internal = 0; max_filedes = -1;}int snap_demux_init(int protocols){ /* safety check */ if (protocols_internal){ d_printf(10,"snap_demux_init : WARNING : initializing open connections\n"); snap_demux_close(); } /* initialize vars */ FD_ZERO(&fdset); /* call protocol specfic initilization routines */ if (protocols & SNAP_UNIX) if (!snap_demux_init_unix()) protocols_internal |= SNAP_UNIX; if (protocols & SNAP_RAWIP) if (!snap_demux_init_rawip()) protocols_internal |= SNAP_RAWIP; if (protocols & SNAP_UDP) if (!snap_demux_init_udp()) protocols_internal |= SNAP_UDP; /* extend in identical fashion for new protocols */ /* calculate the maximum filedescriptor that has to be read */ max_filedes = (socket_unix > socket_rawip ? socket_unix : socket_rawip) + 1; max_filedes = (socket_udp >= max_filedes ? socket_udp + 1 : max_filedes); d_printf(80,"fds: unix:%d raw:%d udp:%d max:%d\n",socket_unix,socket_rawip,socket_udp,max_filedes); return protocols_internal;}int snap_demux_receivefrom(int socket_waiting, buffer_handler active_handler){ int length; /*int i;*/ char pbuf[SNAP_BUFLEN]; memset(&pbuf, 0, sizeof(char) * SNAP_BUFLEN); length = recvfrom(socket_waiting, pbuf, sizeof(pbuf), 0, NULL, NULL); /* DEBUG_START */ d_printf_timed(7,"snap_demux_receive : received packet\n"); d_printf(100,"snap_demux_receive: packet is %d bytes, received from filedes %u\n",sizeof(char) * length,(unsigned int) socket_waiting);/* d_printf(100,"snap_demux_receive : received bincode = ["); for (i = 0; i< length ; i++) d_printf(100,"%u ",pbuf[i]); d_printf(100,"] \n");*/ /* DEBUG_END */ if (length < 0){ perror("recvfrom"); return 0; } else{ if (active_handler) active_handler(pbuf); d_printf_timed(7,"snap_demux_receive : received and handled a message\n"); return length; }}int snap_demux_receive(buffer_handler active_handler){ int returnvalue = 0; /* handle messages */ if (protocols_internal & SNAP_UNIX && FD_ISSET(socket_unix,&fdset)) returnvalue += snap_demux_receivefrom(socket_unix, active_handler); if (protocols_internal & SNAP_RAWIP && FD_ISSET(socket_rawip,&fdset)) returnvalue += snap_demux_receivefrom(socket_rawip, active_handler); if (protocols_internal & SNAP_UDP && FD_ISSET(socket_udp,&fdset)) returnvalue += snap_demux_receivefrom(socket_udp, active_handler); if (!returnvalue) d_printf_timed(5,"snap_demux_receive : unable to handle message\n"); /* extend in identical fashion for new protocols */ return returnvalue;}int snap_demux_select(){ struct timeval timeout; d_printf_timed(7,"snap_demux_select : waiting for message\n"); /* initialize the select() vars */ FD_ZERO(&fdset); timeout.tv_sec = 3; /* timeout in 3 secs */ timeout.tv_usec = 0; if (protocols_internal & SNAP_UNIX) FD_SET(socket_unix, &fdset); if (protocols_internal & SNAP_RAWIP) FD_SET(socket_rawip, &fdset); if (protocols_internal & SNAP_UDP) FD_SET(socket_udp, &fdset); /* extend in identical fashion for new protocols */ /* wait for messages */ if (select(max_filedes, &fdset, NULL, NULL, &timeout) < 0) { perror("select"); return 1; } return 0;}int snap_demux_handler(buffer_handler active_handler){ if (!snap_demux_select()) return snap_demux_receive(active_handler); else return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -