diff --git a/src/hard_keepalive.c b/src/hard_keepalive.c index 5b90867..ded8991 100644 --- a/src/hard_keepalive.c +++ b/src/hard_keepalive.c @@ -19,7 +19,7 @@ #include "hard_keepalive.h" -int udp_socket_recv(int sockfd, uint32_t *src_ip, uint8_t *buf, uint32_t buf_size) +int udp_socket_recv(int sockfd, uint32_t *src_ip, uint16_t *src_port, uint8_t *buf, uint32_t buf_size) { if (NULL == buf) return -1; @@ -32,7 +32,8 @@ int udp_socket_recv(int sockfd, uint32_t *src_ip, uint8_t *buf, uint32_t buf_siz perror("recvfrom"); return -1; } - *src_ip = ((struct sockaddr_in *)&their_addr)->sin_addr.s_addr; + *src_ip = ((struct sockaddr_in *)&their_addr)->sin_addr.s_addr; + *src_port = ((struct sockaddr_in *)&their_addr)->sin_port; return numbytes; } @@ -92,17 +93,6 @@ int create_recv_udp_socket(uint16_t port) return sockfd; } - -int create_send_udp_socket() -{ - int sockfd = socket(AF_INET, SOCK_DGRAM, 0); - if (-1 == sockfd) - { - return -1; - } - return sockfd; -} - void* thread_hard_keepalive(void *param) { char buf[1500] = {0}; @@ -110,12 +100,12 @@ void* thread_hard_keepalive(void *param) bfd_port_t* bfd_port = (bfd_port_t*)param; int size = 0; uint32_t src_ip = 0; + uint16_t src_port = 0; int send_rec = 0; fd_set rset; int recv_pkt_sd = create_recv_udp_socket(htons(bfd_port->recv_port)); - int send_pkt_sd = create_send_udp_socket(); - if(-1==recv_pkt_sd || -1==send_pkt_sd) + if(-1==recv_pkt_sd) { printf("hard_keepalive create socket error.\n"); return NULL; @@ -131,16 +121,16 @@ void* thread_hard_keepalive(void *param) } if(FD_ISSET(recv_pkt_sd, &rset)) { - size = udp_socket_recv(recv_pkt_sd, &src_ip, (unsigned char*)buf, sizeof(buf)); + size = udp_socket_recv(recv_pkt_sd, &src_ip, &src_port, (unsigned char*)buf, sizeof(buf)); if(size>0) { /*交换discriminator*/ memcpy(discriminator_temp, buf+MY_ID_OFFSET, ID_SIZE); memcpy(buf+MY_ID_OFFSET, buf+YOUR_ID_OFFSET, ID_SIZE); memcpy(buf+YOUR_ID_OFFSET, discriminator_temp, ID_SIZE); - send_rec = udp_socket_send(send_pkt_sd, + send_rec = udp_socket_send(recv_pkt_sd, src_ip, - htons(bfd_port->send_port), + src_port, (char*)buf,size); if(-1==send_rec) { diff --git a/src/main.c b/src/main.c index b4ac64b..066cee5 100644 --- a/src/main.c +++ b/src/main.c @@ -43,7 +43,7 @@ const char* frag_rssb_version_time = "2018-08-13T09:00:00"; const char* frag_rssb_version_des = "MESA@iie rssb_maskey"; int FRAG_RSSB_VERSION_1_0_20181008 = 0; -const char* frag_rssb_version_time_in = "2018-09-27"; +const char* frag_rssb_version_time_in = "2018-10-08"; const char* frag_rssb_version_des_in = "hard balance"; void frag_rssb_history() { @@ -756,7 +756,6 @@ int read_conf_and_init(const char* filename) MESA_load_profile_uint_def(filename, "WLB", "wlb_report_interval", (unsigned int*)&g_frag_cfg.wlb_report_interval,10); MESA_load_profile_uint_def(filename, "WLB", "enable_override", (unsigned int*)&g_frag_cfg.enable_override,0); MESA_load_profile_uint_def(filename, "WLB", "bfd_recv_port", (unsigned int*)&g_frag_cfg.bfd_recv_port,0); - MESA_load_profile_uint_def(filename, "WLB", "bfd_send_port", (unsigned int*)&g_frag_cfg.bfd_send_port,0); /*send bizman :粗拼装往前端发数据,监听响应端口*/ MESA_load_profile_short_def(filename, "NETWORK", "BizmanAckPort", (short*)&g_frag_cfg.bizman_ack_port,22084); @@ -1297,11 +1296,10 @@ int main(int argc, char **argv) } } - if(g_frag_cfg.bfd_recv_port>0 && g_frag_cfg.bfd_send_port>0) + if(g_frag_cfg.bfd_recv_port>0) { bfd_port_t bfd_port; bfd_port.recv_port = g_frag_cfg.bfd_recv_port; - bfd_port.send_port = g_frag_cfg.bfd_send_port; hard_keepalive_run(&bfd_port); } diff --git a/src/main.h b/src/main.h index 0a06c3d..8d25ad6 100644 --- a/src/main.h +++ b/src/main.h @@ -242,7 +242,6 @@ typedef struct frag_rssb_configure_s int16_t save_media; uint32_t bfd_recv_port; - uint32_t bfd_send_port; }frag_rssb_configure_t; typedef struct frag_rssb_status_s diff --git a/src/tool/sendsurvey_k/AV_sendback.h b/src/tool/sendsurvey_k/AV_sendback.h new file mode 100644 index 0000000..e14bb7e --- /dev/null +++ b/src/tool/sendsurvey_k/AV_sendback.h @@ -0,0 +1,198 @@ +#ifndef _AV_SENDBACK_H +#define _AV_SENDBACK_H + +/*魔数*/ +#define AV_MAGIC_VALUE 0x8739 + +/*消息类型*/ +#define AV_TYPE_META 0x01 +#define AV_TYPE_DATA 0x02 +#define AV_TYPE_RESULT 0x11 + +/*协议类型*/ +#define PROTOCOL_DEFAULT 0x00 +#define PROTOCOL_HTTP 0x01 +#define PROTOCOL_SMTP 0x02 +#define PROTOCOL_POP3 0x03 +#define PROTOCOL_IMAP 0x04 +#define PROTOCOL_FTP 0x05 +#define PROTOCOL_HTTP_PIC 0x06 +#define PROTOCOL_RTSP_RDT 0x07 +#define PROTOCOL_RTSP_RTP 0x08 +#define PROTOCOL_MMS 0x09 +#define PROTOCOL_RTMP 0x0A +#define PROTOCOL_SIP 0x0B + +/*媒体类型*/ +#define MEDIA_TYPE_UNKNOWN 0x00 +/*媒体类型:视频支持的媒体类型*/ +#define MEDIA_TYPE_VIDEO 0x01 +#define MEDIA_TYPE_WMV 0x02 +#define MEDIA_TYPE_MPG 0x03 +#define MEDIA_TYPE_FLV 0x04 +#define MEDIA_TYPE_RMFF 0x05 +#define MEDIA_TYPE_AVI 0x06 +#define MEDIA_TYPE_SWF 0x07 +#define MEDIA_TYPE_MPG4 0x08 +#define MEDIA_TYPE_AIFF 0x09 +#define MEDIA_TYPE_OGG 0x0A +#define MEDIA_TYPE_DRC 0x0B +#define MEDIA_TYPE_DIRECTSHOW 0x0C +#define MEDIA_TYPE_FLIC 0x0E +#define MEDIA_TYPE_INDEO 0x0F +#define MEDIA_TYPE_MKV 0x10 +/*媒体类型:音频支持的媒体类型*/ +#define MEDIA_TYPE_AUDIO 0x20 +#define MEDIA_TYPE_MP3 0x21 +/*媒体类型:图片支持的媒体类型*/ +#define MEDIA_TYPE_IMAGE 0x30 +#define MEDIA_TYPE_JPG 0x31 +#define MEDIA_TYPE_BMP 0x32 +#define MEDIA_TYPE_GIF 0x33 +/*媒体类型:实时流媒体支持的媒体类型*/ +#define MEDIA_TYPE_MMS 0x40 +#define MEDIA_TYPE_RTSP_RDT 0x41 +#define MEDIA_TYPE_RTSP_RTP 0x42 +/*媒体类型:碎片化音视频支持的媒体类型*/ +#define MEDIA_TYPE_OSMF 0x50 +#define MEDIA_TYPE_HLS 0x51 +/*媒体类型:VOIP支持的媒体类型,亦即编码类型*/ +#define MEDIA_TYPE_AUDIO_UNKNOWN 0x60 +#define MEDIA_TYPE_AUDIO_G711_ULAW 0x61 +#define MEDIA_TYPE_AUDIO_G711_ALAW 0x62 +#define MEDIA_TYPE_AUDIO_G722 0x63 +#define MEDIA_TYPE_AUDIO_G723 0x64 +#define MEDIA_TYPE_AUDIO_G726_40 0x65 +#define MEDIA_TYPE_AUDIO_G726_32 0x66 +#define MEDIA_TYPE_AUDIO_G726_24 0x67 +#define MEDIA_TYPE_AUDIO_G726_16 0x68 +#define MEDIA_TYPE_AUDIO_AAL2_G726_40 0x69 +#define MEDIA_TYPE_AUDIO_AAL2_G726_32 0x6A +#define MEDIA_TYPE_AUDIO_AAL2_G726_24 0x6B +#define MEDIA_TYPE_AUDIO_AAL2_G726_16 0x6C +#define MEDIA_TYPE_AUDIO_G728 0x6D +#define MEDIA_TYPE_AUDIO_G729D 0x6E +#define MEDIA_TYPE_AUDIO_G729E 0x6F +#define MEDIA_TYPE_AUDIO_GSM 0x70 +#define MEDIA_TYPE_AUDIO_GSM_EFR 0x71 +#define MEDIA_TYPE_AUDIO_ILBC 0x72 +#define MEDIA_TYPE_AUDIO_AMR 0x73 +#define MEDIA_TYPE_AUDIO_AMR_WB 0x74 +#define MEDIA_TYPE_AUDIO_SILK 0x75 +#define MEDIA_TYPE_AUDIO_LPC 0x76 +#define MEDIA_TYPE_AUDIO_LPC1016 0x77 +#define MEDIA_TYPE_AUDIO_LPC1015 0x78 +#define MEDIA_TYPE_AUDIO_L16 0x79 +#define MEDIA_TYPE_AUDIO_SPEEX 0x7A +#define MEDIA_TYPE_AUDIO_L8 0x7B +#define MEDIA_TYPE_AUDIO_MPA 0x7C +#define MEDIA_TYPE_AUDIO_DVI4 0x7D +#define MEDIA_TYPE_AUDIO_VDVI 0x7E +#define MEDIA_TYPE_AUDIO_CN 0x7F +#define MEDIA_TYPE_AUDIO_RED 0x80 +#define MEDIA_TYPE_AUDIO_QCELP 0x81 +#define MEDIA_TYPE_AUDIO_EVRC0 0x82 +#define MEDIA_TYPE_AUDIO_EVRCB0 0x83 +#define MEDIA_TYPE_AUDIO_G729 0x84 +#define MEDIA_TYPE_AUDIO_VIVOX 0x85 + +/*指导消息体选项类型*/ +/*指导消息体选项类型-VOIP选项类型*/ +#define OPT_SIP_SEND_RATE_INFO 0x71 /*VOIP编码参数信息*/ + +/*==================================消息头部==============================================*/ +typedef struct msg_head_s +{ + uint16_t magic; + uint16_t m_type; + uint32_t c_len; //消息体长度(不包括消息头部) +}msg_head_t; + +/*==================================指导消息体==============================================*/ +typedef struct msg_meta_s +{ + char pid[8]; //ID,由数据获取节点生成 + uint64_t proglen; //流文件的总字节长度,为0表示长度未知 + uint32_t capip; // 获取节点的IP + uint8_t protocol; // 承载协议类型 + uint8_t mediatype; // 可能的媒体类型 + uint8_t data_flag; // 数据标志 + uint8_t opt_num; // 选项个数 +}msg_meta_t; + +/*已经在其他文件里面定义过 +struct opt_unit_t +{ + uint32_t opt_len; //包括opt_len、opt_type、opt_value + uint8_t opt_type; + char* opt_value; +}; +*/ + +/*==================================流数据消息体==============================================*/ +typedef struct av_data_s +{ + char pid[8]; // 流文件的ID,由数据获取节点生成 + uint16_t frag_seq; // 音视频碎片:片段序号 VOIP:主被叫 + int64_t offset:48; // 本次数据在整个流文件中的偏移位置 + //char* avs; // 多媒体流数据 +}av_data_t; + +/*VOIP多媒体流数据格式*/ +struct voip_header_t +{ + uint32_t time_stamp; // 语音帧的时间戳, 网络序。 + uint32_t sequence; + //char* data; //VOIP的语音数据 +}; + +/*RTSP/RTMP多媒体流数据格式*/ +struct sdp_data_packet +{ + char sdp[8]; // 恒为"RTSP_SDP" + uint32_t sdp_len; // 为sdp的数据长度 + //char * data; // sdp数据内容 +}; + +/*RTSP sdp数据内容格式*/ +struct rtsp_data_packet +{ + uint32_t flag; // RTSP数据头开头标志 0x46464646 "FFFF" + uint8_t type; // 数据类型,1 for RDT,2 for RTP + uint8_t channel; // 数据的通道号 + uint16_t len; // 数据长度 + //char * data; // 数据内容 +}; + +/*RTMP sdp数据内容格式*/ +struct rtmp_data_packet +{ + uint32_t flag; // RTMP数据头开头标志 0x52544D50"RTMP" + uint32_t av_type:8; // 音视频类型,8 for Audio, 9 for Video + uint32_t cs_id:24; // Chunk Stream ID + uint32_t timestamp; // 时间戳 + uint32_t len; // 总数据长度(包括本头部字段长度,20字节) + uint32_t stream_id; // 流ID + //char * data; // 数据内容 +}; + +/*MMS多媒体流数据格式*/ +struct mms_data_packet +{ + uint32_t frame_flag; // 恒为0x46464646 "FFFF" + uint32_t data_len; // 为数据长度 + //char * data; // 数据内容H包数据内容或者D包数据内容 +}; + +/*==================================结果消息体==================================================*/ +typedef struct msg_result_s +{ + char pid[8]; // 本流文件的ID,由数据获取节点生成 + int servicetype; // 业务类型 + uint32_t cfgid; // 命中的配置ID,值为0表示命中未知配置 + int8_t level; // 检测结果置信度, [0, 100] + uint8_t pad[7]; // 预留,必须填0值 + //char* logindex; // 访问现场数据文件的存储路径URL,以"/0"结束 +}msg_result_t; + +#endif diff --git a/src/tool/sendsurvey_k/Makefile b/src/tool/sendsurvey_k/Makefile new file mode 100644 index 0000000..4ccb8b2 --- /dev/null +++ b/src/tool/sendsurvey_k/Makefile @@ -0,0 +1,41 @@ +# CREATED by Guo Le, 08/23/2010 +# +CC = gcc +PROGRAM = sendsurvey +CFLAGS = -g -Wall +INCLUDE = -I. -I./common +LDFLAGS = -lpthread -Wall +OBJS = backend.o +DEPLOY_DIR = /home/ictsoft/fake_backend +BUILD_DIR = ./debug + +.PHONY : all clean +all: $(PROGRAM) + + +sendsurvey: sendsurvey.c + gcc $(INCLUDE) $(LDFLAGS) -g -Wall sendsurvey.c common.a -o sendsurvey + +$(OBJS): %.o: %.c + $(CC) -c $(CFLAGS) $(INCLUDE) $< -o $@ + + +clean: + $(RM) *.o $(PROGRAM) + +localdeploy: $(PROGRAM) + @echo "- Kill previous process ---------------------------------------------" + @-killall -vq backend_video backend_audio; + @echo "---------------------------------------------------------------------" + @echo "- Check deploy dir --------------------------------------------------" + @if [ ! -d "$(DEPLOY_DIR)" ]; \ + then echo "DEPLOY_DIR doesn't exist!"; mkdir -p $(DEPLOY_DIR); \ + else echo "DEPLOY_DIR exist, remove all"; fi + @-rm -rfv $(DEPLOY_DIR)/* + @echo "---------------------------------------------------------------------" + @echo "- Deploy ------------------------------------------------------------" + @cp -rv $(BUILD_DIR)/* $(DEPLOY_DIR) + @echo "---------------------------------------------------------------------" + @echo "- Run ---------------------------------------------------------------" + @cd $(DEPLOY_DIR); ./r2; + @ps au | grep -e "backend" | grep -v grep diff --git a/src/tool/sendsurvey_k/common.a b/src/tool/sendsurvey_k/common.a new file mode 100644 index 0000000..3549d78 Binary files /dev/null and b/src/tool/sendsurvey_k/common.a differ diff --git a/src/tool/sendsurvey_k/common/Makefile b/src/tool/sendsurvey_k/common/Makefile new file mode 100644 index 0000000..dcb5fc0 --- /dev/null +++ b/src/tool/sendsurvey_k/common/Makefile @@ -0,0 +1,24 @@ +# CREATED by Guo Le, 08/23/2010 +# +CC = gcc +PROGRAM = +CFLAGS = -g -Wall -D_DEBUG_ + +LDFLAGS = +OBJS = input.o output.o loadprof.o log.o error.o ioctl_op.o + +.PHONY : all clean +all: common.a + +$(OBJS): %.o: %.c + $(CC) -c $(CFLAGS) $< -o $@ + +common.a: $(OBJS) + ar -r $@ $^ + cp $@ ../preprocessor/lib/libcommon.a + cp $@ ../pktgen + +clean: + $(RM) *.o common.a + + diff --git a/src/tool/sendsurvey_k/common/debug.h b/src/tool/sendsurvey_k/common/debug.h new file mode 100644 index 0000000..f0b5d3b --- /dev/null +++ b/src/tool/sendsurvey_k/common/debug.h @@ -0,0 +1,25 @@ +#ifndef _DEBUG_H_ +#define _DEBUG_H_ + +#include +#include +#include + +#ifdef _DEBUG_ +#define DEBUG_MESSAGE(msg) \ + fprintf(stderr, "[Debug]: %s(%d) " msg "\n", __FILE__, __LINE__) +#define DEBUG_PRINTF(fmt,...) \ + fprintf(stderr, "[Debug]: %s(%d) " fmt, __FILE__, __LINE__, __VA_ARGS__) +#else +#define DEBUG_MESSAGE(msg) +#define DEBUG_PRINTF(fmt,...) +#endif /* _DEBUG_ */ + +#ifdef _DEBUG_VERBOSE_ +#define DEBUG_WRAP(msg) msg +#else +#define DEBUG_WRAP(msg) +#endif /* _DEBUG_VERBOSE_ */ + + +#endif // #define _DEBUG_H_ diff --git a/src/tool/sendsurvey_k/common/error.c b/src/tool/sendsurvey_k/common/error.c new file mode 100644 index 0000000..c0bd4f1 --- /dev/null +++ b/src/tool/sendsurvey_k/common/error.c @@ -0,0 +1,25 @@ +// error.c +// +// +// +// MODIFIED by +// CREATED by Guo Le, 09/16/2010 + +#include "error.h" + +int error_flags = 0; + +void setabort() +{ + error_flags |= ERROR_FLAG_ABORT; +} +void unsetabort() +{ + error_flags &= ~ERROR_FLAG_ABORT; +} +int isabort() +{ + return error_flags & ERROR_FLAG_ABORT ? 1 : 0; +} + + diff --git a/src/tool/sendsurvey_k/common/error.h b/src/tool/sendsurvey_k/common/error.h new file mode 100644 index 0000000..cd7469a --- /dev/null +++ b/src/tool/sendsurvey_k/common/error.h @@ -0,0 +1,17 @@ +#ifndef _ERROR_H_ +#define _ERROR_H_ +#include +#include +#include + +#define ERROR_FLAG_ABORT 0x1 + +#define ERROR_MESSAGE(msg) \ + fprintf(stderr, "[Error]: " msg "\n") + +void setabort(); +void unsetabort(); +int isabort(); + + +#endif // #define _DEBUG_H_ diff --git a/src/tool/sendsurvey_k/common/input.c b/src/tool/sendsurvey_k/common/input.c new file mode 100644 index 0000000..e697371 --- /dev/null +++ b/src/tool/sendsurvey_k/common/input.c @@ -0,0 +1,114 @@ +// input.c +// +// +// +// MODIFIED by +// CREATED by Guo Le, 09/11/2010 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include //offsetof +#include "input.h" + + +int input_udp_init(uint16_t port) +{ + int sockfd = socket(AF_INET, SOCK_DGRAM, 0); + if (-1 == sockfd) { + //DEBUG_PRINTF("listener: socket %s", strerror()); + perror("listener: socket"); + return -1; + } + struct sockaddr_in my_addr; /* my address information */ + my_addr.sin_family = AF_INET; /* host byte order */ + my_addr.sin_port = port; /* short, network byte order */ + my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */ + bzero(&(my_addr.sin_zero), 8); /* zero the rest of the struct */ + + if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { + //DEBUG_PRINTF("listener: bind %s", strerror()); + perror("listener: bind"); + close(sockfd); + return -1; + } + /* + if(fcntl(sockfd, F_SETFL, O_NONBLOCK) == -1) { + close(sockfd); + perror("fcntl"); + return -1; + } + */ + return sockfd; +} + +void input_udp_clear(int sockfd) +{ + close(sockfd); +} + + +int input_udp_recv(int sockfd, uint32_t *src_ip, uint8_t *buf, uint32_t buf_size) +{ + if (NULL == buf) + return -1; + + int numbytes; + struct sockaddr_storage their_addr; + socklen_t addr_len = sizeof their_addr; + //printf("listener: waiting to recvfrom...\n"); + if ((numbytes = recvfrom(sockfd, buf, buf_size , 0, + (struct sockaddr *)&their_addr, &addr_len)) == -1) { + perror("recvfrom"); + return -1; + } + *src_ip = ((struct sockaddr_in *)&their_addr)->sin_addr.s_addr; + return numbytes; +} +int init_input_un_socket(const char* file_name) +{ + int sd=-1; + int len; + struct sockaddr_un un; + if(strlen(file_name)+8>sizeof(un.sun_path)) + { + printf("Unix-domain socket file name too long.\n"); + return -1; + } + sd=socket(AF_UNIX,SOCK_DGRAM,0); + if(sd<0) + { + printf("Get Unix-domain socket error:%s\n",strerror(errno)); + return -1; + } + memset(&un,0,sizeof(un)); + un.sun_family=AF_UNIX; + + snprintf(un.sun_path,sizeof(un.sun_path),"%s",file_name); + len=offsetof(struct sockaddr_un,sun_path)+strlen(un.sun_path); + unlink(un.sun_path); + if(0>bind(sd,(struct sockaddr*)&un,len)) + { + printf("Bind Unix-domain socket error:%s\n",strerror(errno)); + return -1; + } + return sd; +} +int named_unix_domain_socket_recv(int un_sd,char*buf,int buf_len) +{ + int recv_len=0; + struct sockaddr_storage sa; + socklen_t sa_len; + recv_len=recvfrom(un_sd,buf,buf_len,0,(struct sockaddr*)&sa,&sa_len); + return recv_len; +} diff --git a/src/tool/sendsurvey_k/common/input.h b/src/tool/sendsurvey_k/common/input.h new file mode 100644 index 0000000..e3934df --- /dev/null +++ b/src/tool/sendsurvey_k/common/input.h @@ -0,0 +1,13 @@ +#ifndef _INPUT_H_ +#define _INPUT_H_ + +#include + +int input_udp_init(uint16_t port); + +void input_udp_clear(int sockfd); + +int input_udp_recv(int sockfd, uint32_t *src_ip, uint8_t *buf, uint32_t buf_size); +int init_input_un_socket(const char* file_name); +int named_unix_domain_socket_recv(int un_sd,char*buf,int buf_len); +#endif /* _INPUT_H_ */ diff --git a/src/tool/sendsurvey_k/common/ioctl_op.c b/src/tool/sendsurvey_k/common/ioctl_op.c new file mode 100644 index 0000000..8fc3917 --- /dev/null +++ b/src/tool/sendsurvey_k/common/ioctl_op.c @@ -0,0 +1,45 @@ +// ioctl_op.c +// +// +// +// MODIFIED by +// CREATED by Guo Le, 11/29/2010 + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "debug.h" + +unsigned int getipbyifname(const char *ifname) +{ + int sockfd; + struct ifreq ifr; + unsigned int ip; + + sockfd = socket(AF_INET, SOCK_DGRAM, 0); + if (-1 == sockfd) { + perror("socket"); + goto error; + } + + strcpy(ifr.ifr_name,ifname); + if (ioctl(sockfd, SIOCGIFADDR, &ifr) < 0) { + perror("ioctl"); + goto error; + } + + ip = ((struct sockaddr_in*)&(ifr.ifr_addr))->sin_addr.s_addr; + close(sockfd); + return ip; + +error: + close(sockfd); + return INADDR_NONE; +} diff --git a/src/tool/sendsurvey_k/common/ioctl_op.h b/src/tool/sendsurvey_k/common/ioctl_op.h new file mode 100644 index 0000000..568d21e --- /dev/null +++ b/src/tool/sendsurvey_k/common/ioctl_op.h @@ -0,0 +1,6 @@ +#ifndef _IOCTL_OP_H_ +#define _IOCTL_OP_H_ + +unsigned int getipbyifname(const char *ifname); + +#endif /*_IOCTL_OP_H_*/ diff --git a/src/tool/sendsurvey_k/common/loadprof.c b/src/tool/sendsurvey_k/common/loadprof.c new file mode 100644 index 0000000..7449215 --- /dev/null +++ b/src/tool/sendsurvey_k/common/loadprof.c @@ -0,0 +1,452 @@ +// loadprof.c +// +// general routines to read in profile +// +// CREATED by Guo Le, 2010-12-15 + + +#include +#include +#include +#include +#include + +#include "loadprof.h" + +#ifdef WIN32 +#define strncasecmp(x,y,z) memicmp(x,y,z) +#endif + + +// seek specified section in initialization file, +// file read pointer at beginning of next line +// +// Return: +// >= 0 : success +// < 0 : error +static int seek_section( + FILE *fp, // [IN] initialization file descriptor + const char *section) // [IN] section name in initialization file +{ + char line[MAX_PROF_LINE]; + unsigned int i, j; + char *pc; + + i = strlen(section); + while (NULL != fgets(line, MAX_PROF_LINE, fp)) { + pc = line; + while (' ' == *pc || '\t' == *pc) + pc++; // cut prefixal space chars + if (SECTION_PRE != *pc) + continue; + pc ++; + j = strlen(pc); + if (j < (i + 1) || SECTION_POST != pc[i]) + continue; + if (strncasecmp(section, pc, i)) + continue; + return 0; + } + return -1; +} + + + +// seek specified key in initialization file +// +// Return: +// >= 0 : success +// < 0 : error +static int seek_key( + FILE *fp, // [IN] initialization file descriptor + const char *key, // [IN] key name in initialization file + char *line) // [OUT] key value string +{ + unsigned int i, j; + char *pc; + + i = strlen(key); + *line = '\0'; + while (NULL != fgets(line, MAX_PROF_LINE, fp)) { + pc = line; + while (' ' == *pc || '\t' == *pc) + pc++; // cut prefixal space chars + if (SECTION_PRE == *pc) // next section + return -1; + j = strlen(pc); + if (j < i) + continue; + if (strncasecmp(key, pc, i)) + continue; + pc += i; + while (' ' == *pc || '\t' == *pc) + pc++; + if (VALUE_SET != *pc) + continue; + pc++; + j = strlen(pc); + if (j < 1) + return 0; + memmove(line, pc, j); + line[j] = '\0'; + return 1; + } + return -1; +} + + + +// Read in specified integer value +// +// Return: +// >= 0 : success +// < 0 : error, val is set to default +int profile_read_int( + const char *file, // [IN] initialization file path + const char *section, // [IN] section name in initialization file + const char *key, // [IN] keyword name in initialization file + int *val, // [OUT] returned value + int dval) // [IN] default value +{ + char line[MAX_PROF_LINE]; + FILE *fp; + + *val = dval; + fp = fopen(file, "r"); + if (NULL == fp) + return -1; + if (seek_section(fp, section) < 0) { + fclose(fp); + return -2; + } + if (seek_key(fp, key, line) < 0) { + fclose(fp); + return -3; + } + *val = strtol(line, NULL, 0); + return 0; +} + + + +// Read in specified unsigned integer value +// +// Return: +// >= 0 : success +// < 0 : error, val is set to default +int profile_read_uint( + const char *file, // [IN] initialization file path + const char *section, // [IN] section name in initialization file + const char *key, // [IN] keyword name in initialization file + unsigned int *val, // [OUT] returned value + unsigned int dval) // [IN] default value +{ + char line[MAX_PROF_LINE]; + FILE *fp; + + *val = dval; + fp = fopen(file, "r"); + if (NULL == fp) + return -1; + if (seek_section(fp, section) < 0) { + fclose(fp); + return -2; + } + if (seek_key(fp, key, line) < 0) { + fclose(fp); + return -3; + } + *val = strtoul(line, NULL, 0); + return 0; +} + + + +// Read in specified string value, +// if value string is too long to return, extra chars truncated. +// prefix/postfix space chars cutted, +// space chars: ' ', '\t' +// +// Return: +// >= 0 : length of val +// < 0 : error, str is set to default +int profile_read_string( + const char *file, // [IN] initialization file path + const char *section, // [IN] section name in initialization file + const char *key, // [IN] keyword name in initialization file + char *str, // [OUT] returned string + size_t size, // [IN] buffer size(bytes) + const char *dstr) // [IN] default string +{ + char line[MAX_PROF_LINE]; + FILE *fp; + size_t i; + char *pc; + + if (0 == size) { + *str = '\0'; + return 0; + } + i = strlen(dstr); + if (i > size - 1) { + return -1; + } + strcpy(str, dstr); + fp = fopen(file, "r"); + if (NULL == fp) + return -1; + if (seek_section(fp, section) < 0) { + fclose(fp); + return -2; + } + if (seek_key(fp, key, line) < 0) { + fclose(fp); + return -3; + } + fclose(fp); + pc = line; + while (' ' == *pc || '\t' == *pc) + pc++; // cut prefixal space chars + i = strlen(pc); + if (i < 1) + return 0; + if ('\n' == pc[i - 1]) + i --; // cut pending '\n' + while (' ' == pc[i - 1] || '\t' == pc[i - 1]) { + i --; // cut pending space chars + if (i < 1) { + *str = '\0'; + return 0; + } + } + if (i > size - 1) + i = size - 1; // truncate extra chars + memcpy(str, pc, i); + str[i] = '\0'; + return i; +} + + +int profile_read_nbyte_hex( + const char *file, // [IN] initialization file path + const char *section, // [IN] section name in initialization file + const char *key, // [IN] keyword name in initialization file + unsigned char *bytes, + size_t size) // [IN] buffer size(bytes) +{ + char str[MAX_PROF_LINE]; + if (profile_read_string(file, section, key, str, MAX_PROF_LINE, "") < 0) + return 0; + + char *p = str; + uint8_t num; + size_t count = 0; + while (0 != *p) { + if (strncasecmp("0x", p, 2)) break; + p += 2; + num = 0; + while((*p >= '0' && *p <= '9') || + (*p >= 'a' && *p <= 'f') || + (*p >= 'A' && *p <= 'F')) { + if (*p >= 'a' && *p <= 'f') + num = num * 16 + (*p - 'a' + 10); + else if (*p >= 'A' && *p <= 'F') + num = num * 16 + (*p - 'A' + 10); + else + num = num * 16 + (*p - '0'); + p++; + } + bytes[count++] = num; + if (count == size) break; + while (' ' == *p || '\t' == *p || SEP_ITEM == *p) p++; + } + return count; +} + + +// +// Return: +// >= 0 : num of successfully converted network address. +// < 0 : error, +int profile_read_naddr( + const char *file, // [IN] initialization file path + const char *section, // [IN] section name in initialization file + const char *key, // [IN] keyword name in initialization file + addr4_t *addrs, // [OUT] returned string + size_t size) // [IN] buffer size(num of addr4_t) +{ + char str[MAX_PROF_LINE]; + if (profile_read_string(file, section, key, str, MAX_PROF_LINE, "") < 0) + return 0; + + char *s, *e; + char buf[20]; + in_addr_t ip; + e = str; + size_t count = 0; + while ('\0' != *e) { + s = e; + while((*e >= '0' && *e <= '9') || '.' == *e) e++; + strncpy(buf, s, e-s); + buf[e-s] = 0; + if (inet_pton(AF_INET, buf, &ip) <= 0) + break; + addrs[count].s_addr = ip; + + while (' ' == *e || '\t' == *e || SEP_IP_PORT == *e) e++; + s = e; + while(*e >= '0' && *e <= '9') e++; + strncpy(buf, s, e-s); + buf[e-s] = 0; + addrs[count].sin_port = htons(atoi(buf)); + if (++count == size) + break; + while (' ' == *e || '\t' == *e || SEP_ITEM == *e) e++; + } + return count; + +} + +int profile_read_naddrdev( + const char *file, // [IN] initialization file path + const char *section, // [IN] section name in initialization file + const char *key, // [IN] keyword name in initialization file + addr4dev_t *addrdevs, // [OUT] returned string + size_t size) // [IN] buffer size(num of addr4_t) +{ + char str[MAX_PROF_LINE]; + if (profile_read_string(file, section, key, str, MAX_PROF_LINE, "") < 0) + return 0; + + char *s, *e; + char buf[20]; + in_addr_t ip; + e = str; + size_t count = 0; + while ('\0' != *e) { + s = e; + while((*e >= '0' && *e <= '9') || '.' == *e) e++; + strncpy(buf, s, e-s); + buf[e-s] = 0; + if (inet_pton(AF_INET, buf, &ip) <= 0) + break; + addrdevs[count].s_addr = ip; + + while (' ' == *e || '\t' == *e || SEP_IP_PORT == *e) e++; + s = e; + while(*e >= '0' && *e <= '9') e++; + strncpy(buf, s, e-s); + buf[e-s] = 0; + addrdevs[count].sin_port = htons(atoi(buf)); + + while (' ' == *e || '\t' == *e || SEP_IP_PORT == *e) e++; + s = e; + while(!isspace(*e) && SEP_ITEM != *e) e++; + strncpy(buf, s, e-s); + buf[e-s] = 0; + strcpy(addrdevs[count].dev, buf); + + if (++count == size) + break; + while (' ' == *e || '\t' == *e || SEP_ITEM == *e) e++; + } + return count; + +} +int profile_read_nstring( + const char *file, // [IN] initialization file path + const char *section, // [IN] section name in initialization file + const char *key, // [IN] keyword name in initialization file + char *strings, // [OUT] returned string + size_t string_size, + size_t string_num) // [IN] buffer size(num of strigns) +{ + int count=0; + char str[MAX_PROF_LINE]; + if (profile_read_string(file, section, key, str, MAX_PROF_LINE, "") < 0) + return 0; + char* string_begin=str; + char* string_end=strchr(string_begin,','); + while(string_end!=NULL&&string_num>count) + { + memcpy(strings+count*string_size,string_begin,string_end-string_begin); + count++; + string_begin=string_end+1; + string_end=strchr(string_end+1,','); + } + memcpy(strings+count*string_size,string_begin,strlen(str)-(string_begin-str)); + count++; + return count; + +} + +// +// Return: +// >= 0 : num of successfully converted network address. +// < 0 : error, +int profile_read_nip( + const char *file, // [IN] initialization file path + const char *section, // [IN] section name in initialization file + const char *key, // [IN] keyword name in initialization file + in_addr_t *ips, // [OUT] returned string + size_t size)// [IN] buffer size(num of in_addr_t) + +{ + char str[MAX_PROF_LINE]; + if (profile_read_string(file, section, key, str, MAX_PROF_LINE, "") < 0) + return 0; + + char *s, *e; + char buf[20]; + in_addr_t ip; + e = str; + size_t count = 0; + while ('\0' != *e) { + s = e; + while((*e >= '0' && *e <= '9') || '.' == *e) e++; + strncpy(buf, s, e-s); + buf[e-s] = 0; + if (inet_pton(AF_INET, buf, &ip) <= 0) + break; + ips[count] = ip; + + if (++count == size) + break; + while (' ' == *e || '\t' == *e || SEP_ITEM == *e) e++; + } + return count; +} + +// +// Return: +// >= 0 : num of successfully converted network address. +// < 0 : error, +int profile_read_nport( + const char *file, // [IN] initialization file path + const char *section, // [IN] section name in initialization file + const char *key, // [IN] keyword name in initialization file + in_port_t *ports, // [OUT] + size_t size)// [IN] buffer size + +{ + char str[MAX_PROF_LINE]; + if (profile_read_string(file, section, key, str, MAX_PROF_LINE, "") < 0) + return 0; + + char *s, *e; + char buf[20]; + e = str; + size_t count = 0; + while ('\0' != *e) { + s = e; + while((*e >= '0' && *e <= '9') || '.' == *e) e++; + strncpy(buf, s, e-s); + buf[e-s] = 0; + ports[count] = htons(atoi(buf)); + + if (++count == size) + break; + while (' ' == *e || '\t' == *e || SEP_ITEM == *e) e++; + } + return count; +} diff --git a/src/tool/sendsurvey_k/common/loadprof.h b/src/tool/sendsurvey_k/common/loadprof.h new file mode 100644 index 0000000..bd6c332 --- /dev/null +++ b/src/tool/sendsurvey_k/common/loadprof.h @@ -0,0 +1,120 @@ +#ifndef SLIB_LOADPROF_H +#define SLIB_LOADPROF_H + +#include + +#define MAX_PROF_LINE 1024 /* MAX bytes in a single line */ +#define COMMENT_CHAR '#' /* comments line prefix */ +#define SECTION_PRE '[' /* section name prefix */ +#define SECTION_POST ']' /* section name postfix */ +#define VALUE_SET '=' /* set key value operator */ + +#define SEP_ITEM ',' +#define SEP_IP_PORT ':' + +typedef struct addr4 { + in_addr_t s_addr; + in_port_t sin_port; + in_port_t __align; +} addr4_t; + + +#define DEV_NAME_MAX 10 +typedef struct addr4dev { + in_addr_t s_addr; + in_port_t sin_port; + char dev[DEV_NAME_MAX]; +} addr4dev_t; + + + + + +// Read in specified integer value +// +// Return: +// >= 0 : success +// < 0 : error, val is set to default +int profile_read_int( + const char *file, // [IN] initialization file path + const char *section, // [IN] section name in initialization file + const char *key, // [IN] keyword name in initialization file + int *val, // [OUT] returned value + int dval); // [IN] default value + +// Read in specified unsigned integer value +// +// Return: +// >= 0 : success +// < 0 : error, val is set to default +int profile_read_uint( + const char *file, // [IN] initialization file path + const char *section, // [IN] section name in initialization file + const char *key, // [IN] keyword name in initialization file + unsigned int *val, // [OUT] returned value + unsigned int dval); // [IN] default value + +// Read in specified string value, +// if value string is too long to return, extra chars truncated. +// prefix/postfix space chars cutted, +// space chars: ' ', '\t' +// +// Return: +// >= 0 : length of val +// < 0 : error, str is set to default +int profile_read_string( + const char *file, // [IN] initialization file path + const char *section, // [IN] section name in initialization file + const char *key, // [IN] keyword name in initialization file + char *str, // [OUT] returned string + size_t size, // [IN] buffer size(bytes) + const char *dstr); // [IN] default string + + + +int profile_read_nbyte_hex( + const char *file, // [IN] initialization file path + const char *section, // [IN] section name in initialization file + const char *key, // [IN] keyword name in initialization file + unsigned char *bytes, + size_t size); // [IN] buffer size(bytes) + +int profile_read_naddr( + const char *file, // [IN] initialization file path + const char *section, // [IN] section name in initialization file + const char *key, // [IN] keyword name in initialization file + addr4_t *addrs, // [OUT] returned string + size_t size); // [IN] buffer size(num of addr4_t) + +int profile_read_naddrdev( + const char *file, // [IN] initialization file path + const char *section, // [IN] section name in initialization file + const char *key, // [IN] keyword name in initialization file + addr4dev_t *addrdevs, // [OUT] returned string + size_t size); // [IN] buffer size(num of addr4_t) + + +int profile_read_nip( + const char *file, // [IN] initialization file path + const char *section, // [IN] section name in initialization file + const char *key, // [IN] keyword name in initialization file + in_addr_t *ips, // [OUT] returned string + size_t size); // [IN] buffer size(num of in_addr_t) + +// Return: +// >= 0 : num of successfully converted network address. +// < 0 : error, +int profile_read_nport( + const char *file, // [IN] initialization file path + const char *section, // [IN] section name in initialization file + const char *key, // [IN] keyword name in initialization file + in_port_t *ports, // [OUT] + size_t size);// [IN] buffer size +int profile_read_nstring( + const char *file, // [IN] initialization file path + const char *section, // [IN] section name in initialization file + const char *key, // [IN] keyword name in initialization file + char *strings, // [OUT] returned string + size_t string_size, + size_t string_num); +#endif /* #ifndef SLIB_LOADPROF_H */ diff --git a/src/tool/sendsurvey_k/common/log.c b/src/tool/sendsurvey_k/common/log.c new file mode 100644 index 0000000..8400187 --- /dev/null +++ b/src/tool/sendsurvey_k/common/log.c @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include +#include "log.h" + +FILE *log_init(const char *fname) +{ + FILE *fp = fopen(fname,"ab+"); + if (!fp) { + fprintf(stderr, "log init error: %s\n", strerror(errno)); + exit(0); + } + return fp; +} + +void log_clear(FILE *fp) +{ + if (fp) + fclose(fp); +} + +void write_local_log(FILE * fp,const char *fmt,...) +{ + if(!fp) return; + struct tm * now; + time_t currTime; + time(&currTime); + char strTime[32]; + now = localtime(&currTime); + + if(!fp) return ; + memset(strTime,0,sizeof(strTime)); + strftime(strTime,sizeof(strTime),"%Y-%m-%d %H:%M:%S",now); + fprintf(fp,"%s\t",strTime); + + va_list ap; + + va_start(ap,fmt); + vfprintf(fp,fmt,ap); + va_end(ap); + + fputc('\n',fp); + fflush(fp); + +}// end of WriteLocalLog + diff --git a/src/tool/sendsurvey_k/common/log.h b/src/tool/sendsurvey_k/common/log.h new file mode 100644 index 0000000..767f646 --- /dev/null +++ b/src/tool/sendsurvey_k/common/log.h @@ -0,0 +1,12 @@ +#ifndef _LOG_H_ +#define _LOG_H_ + +#include +#include +#include + +FILE *log_init(const char *fname); +void log_clear(FILE *fp); +void write_local_log(FILE * fp,const char *fmt,...); + +#endif /*_LOG_H_ */ diff --git a/src/tool/sendsurvey_k/common/output.c b/src/tool/sendsurvey_k/common/output.c new file mode 100644 index 0000000..c992a91 --- /dev/null +++ b/src/tool/sendsurvey_k/common/output.c @@ -0,0 +1,206 @@ +// output.c +// +// +// +// MODIFIED by +// CREATED BY Guo Le, 08/24/2010 +// use sendnto instead of sendto, zhengchao@iie.ac.cn 2012-05-30 +// add Unix-domain sokcet,zhengchao@iie.ac.cn 2012-06-04 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include //offsetof +#include "debug.h" + +#include "output.h" + +int output_udp_init() +{ + int sockfd = socket(AF_INET, SOCK_DGRAM, 0); + if (-1 == sockfd) { + DEBUG_PRINTF("udpout_init: socket %s\n", strerror(errno)); + return -1; + } + return sockfd; +} + + +void output_udp_clear(int sockfd) +{ + close(sockfd); +} + +// send udp packet +int output_udp_send(int sockfd, uint32_t addr, uint16_t port, uint8_t *data, int datalen) +{ + struct sockaddr_in dst_addr; /* connector's address information */ + dst_addr.sin_family = AF_INET; /* host byte order */ + dst_addr.sin_port = port; /* short, network byte order */ + dst_addr.sin_addr.s_addr = addr; + bzero(&(dst_addr.sin_zero), 8); /* zero the rest of the struct */ + int to_send_len=datalen; + int already_sended_len=0; + while(to_send_len>0) + { + already_sended_len=sendto(sockfd,data, + to_send_len-already_sended_len, + 0, + (struct sockaddr *)&(dst_addr), + sizeof(dst_addr)); + if(already_sended_len==-1) + { + if((EAGAIN == errno)||( EINTR == errno )|| (EWOULDBLOCK==errno)) + { + continue; + } + else + { + DEBUG_MESSAGE("udpout_send: loss data"); + return -1; + } + } + to_send_len-=already_sended_len; + } + return already_sended_len; +} + + +int output_udp_init_connect(uint32_t addr, uint16_t port, char *dev) +{ + int sockfd = socket(AF_INET, SOCK_DGRAM, 0); + if (-1 == sockfd) { + DEBUG_PRINTF("udpout_init_connect: socket %s\n", strerror(errno)); + return -1; + } + struct sockaddr_in dst_addr; /* connector's address information */ + dst_addr.sin_family = AF_INET; /* host byte order */ + dst_addr.sin_port = port; /* short, network byte order */ + dst_addr.sin_addr.s_addr = addr; + bzero(&(dst_addr.sin_zero), 8); /* zero the rest of the struct */ + + if (-1 == connect(sockfd, (struct sockaddr*)&dst_addr, sizeof(dst_addr))) + { + DEBUG_PRINTF("udpout_init_connect: error in connect %s\n", strerror(errno)); + return -1; + } + + if (dev) { + struct ifreq interface; + strncpy(interface.ifr_ifrn.ifrn_name, dev, IFNAMSIZ); + if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, (char *)&interface, sizeof(interface)) < 0) { + DEBUG_PRINTF("SO_BINDTODEVICE failed %s\n", strerror(errno)); + return -1; + } + } + + return sockfd; +} + +void output_udp_clear_connect(int sockfd) +{ + close(sockfd); +} + +// send udp packet +int output_udp_send_connect(int sockfd, uint8_t *data, int datalen) +{ + int to_send_len=datalen; + int already_sended_len=0; + while(to_send_len>0) + { + already_sended_len= send(sockfd, data, datalen, 0); + if(already_sended_len==-1) + { + if((EAGAIN == errno)||( EINTR == errno )|| (EWOULDBLOCK==errno)) + { + continue; + } + else + { + printf("udpout_send_connect: loss data %s\n",strerror(errno)); + DEBUG_MESSAGE("udpout_send_connect: loss data"); + return -1; + } + } + to_send_len-=already_sended_len; + } + return already_sended_len; +} +int init_output_un_socket(const char* file_name) +{ + int sd=-1; + int len; + struct sockaddr_un un; + struct timeval send_timeout; + int send_buf_size=1024*1024*4; + if(strlen(file_name)+8>sizeof(un.sun_path)) + { + printf("Unix-domain socket file name too long.\n"); + return -1; + } + sd=socket(AF_UNIX,SOCK_DGRAM,0); + if(sd<0) + { + printf("Get Unix-domain socket error:%s\n",strerror(errno)); + return -1; + }/* + flags=fcntl(sd,F_GETFL); + flags|=O_NONBLOCK; + if(fcntl(sd,F_SETFL,flags)==-1) + { + printf("Set Unix-domain socket error:%s",strerror(errno)); + return -1; + }*/ + send_timeout.tv_sec=0; + send_timeout.tv_usec=100000; + memset(&un,0,sizeof(un)); + un.sun_family=AF_UNIX; + if(-1==setsockopt(sd,SOL_SOCKET,SO_SNDTIMEO,&send_timeout,sizeof(send_timeout))) + { + printf("Set Unix-domain socket error:%s\n",strerror(errno)); + return -1; + } + if(-1==setsockopt(sd,SOL_SOCKET,SO_SNDBUF,&send_buf_size,sizeof(send_buf_size))) + { + printf("Set Unix-domain socket error:%s\n",strerror(errno)); + return -1; + } + snprintf(un.sun_path,sizeof(un.sun_path),"%s",file_name); + len=offsetof(struct sockaddr_un,sun_path)+strlen(un.sun_path); + unlink(un.sun_path); + if(0>bind(sd,(struct sockaddr*)&un,len)) + { + printf("Bind Unix-domain socket error:%s\n",strerror(errno)); + return -1; + } + return sd; +} +int named_unix_domain_socket_send(int un_sd,struct sockaddr_un *dest_un,const char*data,int datalen) +{ + int to_send_len=datalen; + int already_sended_len=0; + int sock_addr_len=offsetof(struct sockaddr_un,sun_path)+strlen(dest_un->sun_path); + while(to_send_len>0) + { + already_sended_len= sendto(un_sd, data, datalen,0,(struct sockaddr*)dest_un, sock_addr_len); + if(already_sended_len==-1) + { + + //DEBUG_PRINTF("Unix-domain send data: loss data %s\n",strerror(errno)); + return -1; + + } + to_send_len-=already_sended_len; + } + return already_sended_len; +} diff --git a/src/tool/sendsurvey_k/common/output.h b/src/tool/sendsurvey_k/common/output.h new file mode 100644 index 0000000..2da9fc2 --- /dev/null +++ b/src/tool/sendsurvey_k/common/output.h @@ -0,0 +1,18 @@ +#ifndef _OUTPUT_H_ +#define _OUTPUT_H_ + +#include +#include +// send udp packet + +int output_udp_init(); +void output_udp_clear(int sockfd); +int output_udp_send(int sockfd, uint32_t addr, uint16_t port, uint8_t *data, int datalen); + + +int output_udp_init_connect(uint32_t addr, uint16_t port, char *dev); +void output_udp_clear_connect(int sockfd); +int output_udp_send_connect(int sockfd, uint8_t *data, int datalen); +int init_output_un_socket(const char* file_name); +int named_unix_domain_socket_send(int un_sd,struct sockaddr_un *dest_un,const char*data,int datalen); +#endif /* _OUTPUT_H_ */ diff --git a/src/tool/sendsurvey_k/common/un_server b/src/tool/sendsurvey_k/common/un_server new file mode 100644 index 0000000..4bc61e4 Binary files /dev/null and b/src/tool/sendsurvey_k/common/un_server differ diff --git a/src/tool/sendsurvey_k/common/un_server.c b/src/tool/sendsurvey_k/common/un_server.c new file mode 100644 index 0000000..a815c84 --- /dev/null +++ b/src/tool/sendsurvey_k/common/un_server.c @@ -0,0 +1,90 @@ +//gcc -g -Wall un_server.c -o un_server +#include +#include +#include +#include +#include +#include +#include +/* + memset(&(g_pproc->un_backend_addr),0,sizeof(g_pproc->un_backend_addr)); + strcpy(g_pproc->un_backend_addr[i].sun_path,back_end_file_name); + g_pproc->un_backend_addr[i].sun_family=AF_UNIX; + +*/ +int init_named_unix_domain_socket(const char* file_name) +{ + int sd=-1; + int len; + struct sockaddr_un un; + if(strlen(file_name)+8>sizeof(un.sun_path)) + { + printf("Unix-domain socket file name too long.\n"); + return -1; + } + sd=socket(AF_UNIX,SOCK_DGRAM,0); + if(sd<0) + { + printf("Get Unix-domain socket error:%s\n",strerror(errno)); + return -1; + } + + memset(&un,0,sizeof(un)); + un.sun_family=AF_UNIX; + + snprintf(un.sun_path,sizeof(un.sun_path),"%s",file_name); + len=offsetof(struct sockaddr_un,sun_path)+strlen(un.sun_path); + unlink(un.sun_path); + if(0>bind(sd,(struct sockaddr*)&un,len)) + { + printf("Bind Unix-domain socket error:%s\n",strerror(errno)); + return -1; + } + return sd; +} +int named_unix_domain_socket_send(int un_sd,struct sockaddr_un *dest_un,const char*data,int datalen) +{ + int to_send_len=datalen; + int already_sended_len=0; + int sock_addr_len=offsetof(struct sockaddr_un,sun_path)+strlen(dest_un->sun_path); + while(to_send_len>0) + { + already_sended_len= sendto(un_sd, data, datalen,0,(struct sockaddr*)dest_un, sock_addr_len); + if(already_sended_len==-1) + { + + printf("Unix-domain send data: loss data %s\n",strerror(errno)); + return -1; + + } + to_send_len-=already_sended_len; + } + return already_sended_len; +} +int main() +{ + int sd=-1; + char buf[2048]; + int recv_len=0; + struct sockaddr_storage sa; + socklen_t sa_len; + char* recv_file_name="/home/zhengchao/recv_preproc"; + unsigned long long stat_recv_bytes=0,stat_recv_pkts=0; + unsigned long long mult_tmp=0; + int i=0; + sd=init_named_unix_domain_socket(recv_file_name); + while(1) + { + recv_len=recvfrom(sd,buf,sizeof(buf),0,(struct sockaddr*)&sa,&sa_len); + stat_recv_bytes+=recv_len; + stat_recv_pkts++; + for(i=1;i +#include +#include +#include +#include +#include +#include + +#include "./common/output.h" +#include "AV_sendback.h" + + +#define BUF_SIZE 1024 +#define OPT_IMG_FP 0x10 +#define OPT_IMG_LEN 0x14 + + +struct image_fp_t +{ + unsigned int offset; + unsigned char fingerprint[128]; +}; +/*********************************************** +*Function: Add option to buf,return actual add size +*Note:This function is used for 505 sendback option create. +*Author:zhengchao@iie.ac.cn +*Date:2012-11-03 +***********************************************/ +int add_opt_505(char *buf,unsigned int buf_len, unsigned char opt_type,const char*opt_cont,unsigned int opt_cont_len) +{ + char *ptr; + unsigned int this_opt_len=sizeof(opt_type)+sizeof(unsigned int)+opt_cont_len; + if(buf==NULL||buf_len<=0) + return 0; + if(this_opt_len>buf_len) + { + return 0; + } + ptr=buf; + *(unsigned int *)ptr = opt_cont_len+sizeof(opt_type)+sizeof(opt_cont_len); + ptr += sizeof(unsigned int); + *(unsigned char *)ptr = opt_type; + ptr += sizeof(unsigned char); + memcpy(ptr, opt_cont, opt_cont_len); + return this_opt_len; +} +int main (int argc, char *argv[]) +{ + if(argc!=6 && argc!=7) + { + printf("input error\n"); + return 0; + } + char buf_survey[BUF_SIZE] = {0}; + int w_offset=sizeof(msg_head_t)+sizeof(msg_result_t); + msg_head_t *mh_survey = (msg_head_t*)buf_survey; + msg_result_t *msi_survey = (msg_result_t *)(buf_survey + sizeof(msg_head_t)); + struct sockaddr_un un_backend_addr; + memset(buf_survey, 0, BUF_SIZE); + mh_survey->magic = AV_MAGIC_VALUE; + mh_survey->m_type = AV_TYPE_RESULT; + mh_survey->c_len = sizeof(msg_result_t); + + *((uint64_t *)msi_survey->pid) = strtoull(argv[2], NULL, 0); + msi_survey->servicetype = atoi(argv[3]); + msi_survey->cfgid = atoi(argv[4]); + msi_survey->level = atoi(argv[5]); + + int fd_survey = init_output_un_socket("./un_tmp"); + if (fd_survey < 0) { + printf("socket error!"); + exit(1); + } + + memset(&(un_backend_addr),0,sizeof(un_backend_addr)); + strcpy(un_backend_addr.sun_path,argv[1]); + un_backend_addr.sun_family=AF_UNIX; + printf("param num: %d\n", argc); + if(argc==7) + { + w_offset += 1024-strlen(argv[6]) + sprintf(buf_survey + w_offset, argv[6]); + } + else + { + w_offset += 1024; + } + printf("pid:%lu, service:%d, configID:%u, level:%d, logindex:%s\n", *((uint64_t *)msi_survey->pid), msi_survey->servicetype, msi_survey->cfgid, msi_survey->level, argv[6]); + mh_survey->c_len = w_offset-sizeof(msg_head_t); + named_unix_domain_socket_send(fd_survey,&un_backend_addr, buf_survey, w_offset); + + return 0; +} +