/** * @author Xiaosong Gao * @param [in] t1 T1连接的超时时间,范围是2-16,单位是秒 * @retval NIC_SUCCESS 发送成功 * @retval ERROR_SEND_FAILED 发送失败 * @retval ERROR_NOBUILDLINK 没有建立握手连接,就发出了请求或其他数据请求 * @note START建立T1请求,HMI发送请求信号 */ long Connect::send_start_t1_request(ACE_UINT8 t1) { // 判断设备/连接是否有效 if (!isValid()) { ACE_ERROR((LM_ERROR, "(%P|%t) Connection is not created\n")); return ERROR_NOBUILDLINK; } // 封装数据包头 FD_PACKET packet; ACE_OS::memset(&packet, 0, sizeof(FD_PACKET)); packet.sync_header = SYNC_HEADER; packet.dest_addr = dest_addr_; packet.source_addr = Connect::source_addr_; packet.control_area_1 = T1_TIMEOUT_FRAME | T1_REQUEST_MASK | (t1 << 4); packet.control_area_2 = 0x00; packet.control_area_3 = 0x00; packet.protocol_type = (ACE_UINT8)(ONET_REAL_TRANSFER_PROTOCOL | HIGH_BYTE_ORDER); packet.length_low = 0x00; packet.length_high = 0x00 | (PROTOCOL_VERSION << 4); packet.crc16 = NO_CRC_CHECK; // 产生数据包 char buf[MAX_SIZE_BUF]; long buf_len; generate_packet(packet, 0, buf, buf_len); // 发送数据包 return this->send(buf, buf_len); }
/** * @author Xiaosong Gao * @retval NIC_SUCCESS 发送成功 * @retval ERROR_SEND_FAILED 发送失败 * @retval ERROR_IS_ENABLED 设备没有被启用 * @retval ERROR_NOBUILDLINK 没有建立握手连接,就发出了请求或其他数据请求 * @note START建立链路请求,HMI发送请求信号 */ long Connect::send_start_link_request() { // 判断设备/连接是否有效 if (!isEnabled()) { ACE_ERROR((LM_ERROR, "(%P|%t) Controller is disabled\n")); return ERROR_IS_ENABLED; } // 封装数据包头 FD_PACKET packet; ACE_OS::memset(&packet, 0, sizeof(FD_PACKET)); packet.sync_header = SYNC_HEADER; packet.dest_addr = dest_addr_; packet.source_addr = Connect::source_addr_; packet.control_area_1 = LINK_FRAME | LINK_START_REQUEST; packet.control_area_2 = 0x01 << current_host_; packet.control_area_3 = 0x00; packet.protocol_type = (ACE_UINT8)(ONET_REAL_TRANSFER_PROTOCOL | HIGH_BYTE_ORDER); packet.length_low = 0x00; packet.length_high = 0x00 | (PROTOCOL_VERSION << 4); packet.crc16 = NO_CRC_CHECK; // 产生数据包 char buf[MAX_SIZE_BUF]; long buf_len; generate_packet(packet, 0, buf, buf_len); // 发送数据包 return this->send(buf, buf_len); }
void send_data(){ int sp = FCC; int c, ret, num_groups; int16 mess_type; int endian_mismatch=0; struct packet_structure *p=malloc(sizeof(struct packet_structure)); if (packets_to_send < FCC) sp = packets_to_send; /* printf("Sending %d packets\n", sp); */ for (c=1; c <= sp; c++) { p = generate_packet(); ret= SP_multicast( Mbox, AGREED_MESS, group, 1, sizeof(struct packet_structure), (char *)p ); } packets_to_send = packets_to_send - sp; }
/** * @author Xiaosong Gao * @param [in] description_code 描述码 * - H11_DATA_CALL:子站收到该请求后,将数据发送给HMI主站,一次会话结束 * - H11_DATA_SUBSCRIPTION:子站将数据上送的同时,为该命令开启定时器,将按照设定的节拍主动上送数据 * - H11_CANCAL_DATA_SUBSCRIPTION:子站将数据上送的同时,取消数据订购 * @param [in] time_ms 订阅周期 * @retval NIC_SUCCESS 发送成功 * @retval ERROR_H11_TIMER_OUTRANGE 订购时间超过范围 * @retval ERROR_SEND_FAILED 发送失败 * @retval ERROR_NOBUILDLINK 没有建立握手连接,就发出了请求或其他数据请求 * @note DPU数据总召唤=10H */ long Connect::send_call_io_data(char description_code, long time_ms) { // 判断设备/连接是否有效 if (!isValid()) { ACE_ERROR((LM_ERROR, "(%P|%t) Connection is not created\n")); return ERROR_NOBUILDLINK; } // 封装数据包头 FD_PACKET packet; ACE_OS::memset(&packet, 0, sizeof(FD_PACKET)); packet.sync_header = SYNC_HEADER; packet.dest_addr = dest_addr_; packet.source_addr = Connect::source_addr_; packet.protocol_type = (ACE_UINT8)(ONET_REAL_TRANSFER_PROTOCOL | HIGH_BYTE_ORDER); packet.control_area_1 = GENERAL_DATA_FRAME; packet.control_area_2 = 0x00; packet.control_area_3 = 0x00; ACE_UINT16 datalen = 2; // 数据区长度为信息码和描述码长度之和 packet.length_low = LOW_8_BIT(datalen); packet.length_high = HIGH_8_BIT(datalen) | (PROTOCOL_VERSION << 4); packet.crc16 = NO_CRC_CHECK; // 封装数据包内容 packet.info_code = CALL_FDCS_OBSERVATION_POINT_DATA; packet.description_code = description_code; switch (description_code) { case H11_DATA_CALL: case H11_CANCAL_DATA_SUBSCRIPTION: break; case H11_DATA_SUBSCRIPTION: if ((time_ms < 10 ) || (time_ms > 65535)) { ACE_ERROR((LM_ERROR, "(%P|%t) send_call_io_data time_ms is out of boundary\n")); return ERROR_H11_TIMER_OUTRANGE; } packet.control_area_1 |= (LOW_8_BIT(time_ms) & H11_TIMER_MASK); packet.control_area_2 |= HIGH_8_BIT(time_ms); break; } // 产生数据包的缓冲区 char buf[MAX_SIZE_BUF]; long buf_len; generate_packet(packet, 0, buf, buf_len); // 发送数据包 return this->send(buf, buf_len); }
/** * @author Xiaosong Gao * @param [in] ioaddr IO逻辑地址 * @param [in] groupId 8位组号偏移,0-255 * @param [in] ptId 每位表示一个通道号 * @param [in] type 数据类型长度 * @param [in] vvalue 真实值 * @param [in] vlen 真实值长度 * @retval NIC_SUCCESS 发送成功 * @retval ERROR_SEND_FAILED 发送失败 * @retval ERROR_NOBUILDLINK 没有建立握手连接,就发出了请求或其他数据请求 * @note 设置AO(数据库) */ long Connect::send_control_ao(ACE_UINT8 ioaddr, ACE_UINT8 groupId, ACE_UINT8 ptId, ACE_UINT8 type, ACE_UINT8 * vvalue, ACE_UINT8 vlen) { // 判断设备/连接是否有效 if (!isValid()) { ACE_ERROR((LM_ERROR, "(%P|%t) Connection is not created\n")); return ERROR_NOBUILDLINK; } // 封装数据包头 FD_PACKET packet; ACE_OS::memset(&packet, 0, sizeof(FD_PACKET)); packet.sync_header = SYNC_HEADER; packet.dest_addr = dest_addr_; packet.source_addr = Connect::source_addr_; packet.control_area_1 = GENERAL_DATA_FRAME; packet.control_area_2 = 0x00; packet.control_area_3 = 0x00; packet.protocol_type = (ACE_UINT8)(ONET_REAL_TRANSFER_PROTOCOL | HIGH_BYTE_ORDER); ACE_UINT16 datalen = 7 + vlen; packet.length_low = LOW_8_BIT(datalen); packet.length_high = HIGH_8_BIT(datalen) | (PROTOCOL_VERSION << 4); packet.crc16 = NO_CRC_CHECK; // 封装数据包内容 packet.info_code = DOWN_CONTROL_AO; packet.description_code = 0; packet.data = new ACE_UINT8[5 + vlen]; packet.data[0] = dest_addr_; packet.data[1] = ioaddr; packet.data[2] = groupId; packet.data[3] = ptId; packet.data[4] = type; for (ACE_UINT8 i = 0; i < vlen; i++) packet.data[5 + i] = vvalue[i]; // 产生数据包 char buf[MAX_SIZE_BUF]; long buf_len; generate_packet(packet, 5 + vlen, buf, buf_len); // 发送数据包 return this->send(buf, buf_len); }
int main(int ac, char **av) { int sockfd; char packet[BUFSIZE]; struct timeval time; banner(); if(ac != 2) { usage(*av); } printf("[+] Connecting to target: %s.\n",av[1]); if(connect_afp(av[1],&sockfd)) { printf("[-] An error has occured connecting to the target.\n"); exit(1); } printf("[+] Generating malicious packet.\n"); generate_packet(packet); printf("[+] Sending packet to target.\n"); if(send_packet(packet,&sockfd)) { printf("[-] Error sending packet.\n"); close(sockfd); exit(1); } fd_set mySet; FD_ZERO(&mySet); FD_SET(sockfd, &mySet); time.tv_sec = 0; time.tv_usec = 50; select(sockfd+1, &mySet, NULL, NULL, &time); close(sockfd); return 0; }
/** * @author Xiaosong Gao * @param [in] ioaddr IO逻辑地址 * @param [in] iot 标签点信息 * @retval NIC_SUCCESS 发送成功 * @retval ERROR_SEND_FAILED 发送失败 * @retval ERROR_NOBUILDLINK 没有建立握手连接,就发出了请求或其他数据请求 * @note 设置VPT */ long Connect::send_control_vpt(ACE_UINT8 ioaddr, IO_T & iot) { // 判断设备/连接是否有效 if (!isValid()) { ACE_ERROR((LM_ERROR, "(%P|%t) Connection is not created\n")); return ERROR_NOBUILDLINK; } ACE_DEBUG((LM_DEBUG, "send_control_ao : making packet.\n")); // 封装数据包头 FD_PACKET packet; ACE_OS::memset(&packet, 0, sizeof(FD_PACKET)); packet.sync_header = SYNC_HEADER; packet.dest_addr = dest_addr_; packet.source_addr = Connect::source_addr_; packet.control_area_1 = GENERAL_DATA_FRAME; packet.control_area_2 = 0x00; packet.control_area_3 = 0x00; packet.protocol_type = (ACE_UINT8)(ONET_REAL_TRANSFER_PROTOCOL | HIGH_BYTE_ORDER); // ACE_UINT16 vlen; switch (iot.datatype) { case INT_1_BYTE: case UNSIGNED_INT_1_BYTE: vlen = 1; break; case BOOL_1_BIT: case INT_2_BYTE: case UNSIGNED_INT_2_BYTE: case FLOAT_2_BYTE: vlen = 2; break; case FLOAT_3_BYTE: vlen = 3; case INT_4_BYTE: case UNSIGNED_INT_4_BYTE: case FLOAT_4_BYTE: vlen = 4; break; } ACE_UINT16 datalen = 7+vlen; packet.length_low = LOW_8_BIT(datalen); packet.length_high = HIGH_8_BIT(datalen) | (PROTOCOL_VERSION << 4); packet.crc16 = NO_CRC_CHECK; // 封装数据包内容 packet.info_code = DOWN_CONTROL_VPT; packet.description_code = 0; packet.data = new ACE_UINT8[5 + vlen]; packet.data[0] = dest_addr_; packet.data[1] = ioaddr;//254;//ioaddr; packet.data[2] = HIGH_8_BIT(iot.channel);//offset packet.data[3] = LOW_8_BIT(iot.channel);//若6007 startchannel = 7 // 类型 switch (iot.datatype) { case BOOL_1_BIT: packet.data[4] = 0x1f; break; default: packet.data[4] = 0x32; } // 数值 ACE_UINT8 fbuf[4]; switch (iot.datatype) { // bool case BOOL_1_BIT: packet.data[5] = 0x1 << (iot.subchannel); packet.data[6] = (iot.databuf.b ? 0xff : 0x00); break; // 1 byte case INT_1_BYTE: packet.data[5] = iot.databuf.c; break; case UNSIGNED_INT_1_BYTE: packet.data[5] = iot.databuf.uc; break; // 2 byte case INT_2_BYTE: packet.data[5] = HIGH_8_BIT(iot.databuf.s); packet.data[6] = LOW_8_BIT(iot.databuf.s); break; case UNSIGNED_INT_2_BYTE: packet.data[5] = HIGH_8_BIT(iot.databuf.us); packet.data[6] = LOW_8_BIT(iot.databuf.us); break; case FLOAT_2_BYTE: memcpy(fbuf, &iot.databuf.f, sizeof(float)); packet.data[5] = fbuf[1]; packet.data[6] = fbuf[0]; break; // 3 byte case FLOAT_3_BYTE: memcpy(fbuf, &iot.databuf.f, sizeof(float)); packet.data[5] = fbuf[2]; packet.data[6] = fbuf[1]; packet.data[7] = fbuf[0]; break; // 4 byte case INT_4_BYTE: packet.data[5] = (ACE_UINT8)(((iot.databuf.g) & 0xff000000) >> 24); packet.data[6] = (ACE_UINT8)(((iot.databuf.g) & 0xff0000) >> 16); packet.data[7] = (ACE_UINT8)(((iot.databuf.g) & 0xff00) >> 8); packet.data[8] = (ACE_UINT8)((iot.databuf.g) & 0xff); break; case UNSIGNED_INT_4_BYTE: packet.data[5] = (ACE_UINT8)(((iot.databuf.ug) & 0xff000000) >> 24); packet.data[6] = (ACE_UINT8)(((iot.databuf.ug) & 0xff0000) >> 16); packet.data[7] = (ACE_UINT8)(((iot.databuf.ug) & 0xff00) >> 8); packet.data[8] = (ACE_UINT8)((iot.databuf.ug) & 0xff); break; case FLOAT_4_BYTE: memcpy(fbuf, &iot.databuf.f, sizeof(float)); packet.data[5] = fbuf[3]; packet.data[6] = fbuf[2]; packet.data[7] = fbuf[1]; packet.data[8] = fbuf[0]; break; } // 产生数据包 char buf[MAX_SIZE_BUF]; long buf_len; generate_packet(packet, 5 + 2, buf, buf_len); // 发送数据包 return this->send(buf, buf_len); }
/** * @author Xiaosong Gao * @param [in] ioaddr IO逻辑地址 * @param [in] iotype IO卡类型 * @param [in] srcaddr 源地址 * @param [in] direction 数据流向 * @param [in] priority 优先级 * @param [in] command 命令码 * @param [in] subcommand 子命令码 * @param [in] data 数据区 * @retval NIC_SUCCESS 发送成功 * @retval ERROR_SEND_FAILED 发送失败 * @retval ERROR_NOBUILDLINK 没有建立握手连接,就发出了请求或其他数据请求 * @note HMI与IO板之间的通信,使用透明数据转发 */ long Connect::send_io_transparent_data_down(ACE_UINT8 ioaddr, ACE_UINT8 iotype, ACE_UINT8 srcaddr, ACE_UINT8 direction, ACE_UINT8 priority, ACE_UINT8 command, ACE_UINT8 subcommand, ACE_UINT8 * data) { // 判断设备/连接是否有效 if (!isValid()) { ACE_ERROR((LM_ERROR, "(%P|%t) Connection is not created\n")); return ERROR_NOBUILDLINK; } // 封装数据包头 FD_PACKET packet; ACE_OS::memset(&packet, 0, sizeof(FD_PACKET)); packet.sync_header = SYNC_HEADER; packet.dest_addr = dest_addr_; packet.source_addr = Connect::source_addr_; packet.control_area_1 = GENERAL_DATA_FRAME; packet.control_area_2 = 0x00; packet.control_area_3 = 0x00; packet.protocol_type = (ACE_UINT8)(ONET_REAL_TRANSFER_PROTOCOL | HIGH_BYTE_ORDER); ACE_UINT8 channels = 0; switch (iotype) { case DO_8_1: case AO_8_1: channels = 8; break; case DO_16_1: channels = 16; break; } ACE_UINT16 datalen = 2 + 8 + (3 + 2 * channels); packet.length_low = LOW_8_BIT(datalen); packet.length_high = HIGH_8_BIT(datalen) | (PROTOCOL_VERSION << 4); packet.crc16 = NO_CRC_CHECK; // 封装数据包内容 packet.info_code = HMI_IO_TRANSPARENT_DATA_DOWN; packet.description_code = 0x01; packet.data = new ACE_UINT8[8 + (3 + 2 * channels)]; ACE_UINT16 dl = 3 + 2 * channels; packet.data[0] = HIGH_8_BIT(dl); packet.data[1] = LOW_8_BIT(dl); packet.data[2] = ioaddr; packet.data[3] = srcaddr; packet.data[4] = direction; packet.data[5] = priority; packet.data[6] = command; packet.data[7] = subcommand; packet.data[8] = command; switch (iotype) { case DO_8_1: case DO_16_1: packet.data[9] = BOOL_1_BIT; break; case AO_8_1: packet.data[9] = UNSIGNED_INT_2_BYTE; break; default: packet.data[9] = 0; } packet.data[10] = channels; for (long i = 0; i < 2 * channels; i++) packet.data[11 + i] = data[i]; // 产生数据包 char buf[MAX_SIZE_BUF]; long buf_len; generate_packet(packet, 8 + (3 + 2 * channels), buf, buf_len); // 发送数据包 return this->send(buf, buf_len); }
/** * Traffic generation and experiment management. */ void on_timer_tick(uint _1, uint _2) { // Experiment management if (simulation_warmup && simulation_ticks == 0) { // Start of warmup io_printf(IO_BUF, "Warmup starting...\n"); } // Start of warmup, start of experiment if (simulation_warmup && simulation_ticks >= config_root.warmup_duration) { simulation_ticks = 0u; simulation_warmup = false; io_printf(IO_BUF, "Warmup ended, starting main experiment...\n"); // Reset and enable counters if (leadAp) rtr_unbuf[RTR_DGEN] |= (FWD_CNTR_BIT | DRP_CNTR_BIT) | ((FWD_CNTR_BIT | DRP_CNTR_BIT)<<16) ; } // End of experiment if (!simulation_warmup && simulation_ticks >= config_root.duration) { // Disable counters if (leadAp) rtr_unbuf[RTR_DGEN] &= ~(FWD_CNTR_BIT | DRP_CNTR_BIT); if (config_root.completion_state != COMPLETION_STATE_FAILIURE) config_root.completion_state = COMPLETION_STATE_SUCCESS; spin1_stop(); return; } simulation_ticks ++; // Show current status using LEDs if (leadAp) { // Drive with 1/16% brightness in warmup if (simulation_warmup) spin1_led_control((simulation_ticks%16 == 0) ? LED_ON(BLINK_LED) : LED_OFF(BLINK_LED)); else spin1_led_control(LED_ON(BLINK_LED)); } // Generate traffic for (int i = 0; i < config_root.num_sources; i++) { switch (config_sources[i].temporal_dist) { case TEMPORAL_DIST_BERNOULLI: if (((float)rand() / (float)RAND_MAX) < config_sources[i].temporal_dist_data.bernoulli_packet_prob) { generate_packet(i); } break; default: // Unrecognised temporal distribution do nothing... io_printf(IO_BUF, "Unrecognised traffic distribution '%d' for source with key 0x%08x.\n" , config_sources[i].temporal_dist , config_sources[i].routing_key ); config_root.completion_state = COMPLETION_STATE_FAILIURE; break; } } }
static int measure_roundtrip(struct config *conf, int sd) { unsigned char *buf; struct timeval start_time, end_time, timeout; long sec = 0, usec = 0; long sec_max = 0, usec_max = 0; long sec_min = 2147483647, usec_min = 2147483647; long sum_sec = 0, sum_usec = 0; int i, ret, count; unsigned short seq_num; float rtt_min = 0.0, rtt_avg = 0.0, rtt_max = 0.0; float packet_loss = 100.0; fprintf(stdout, "PING 0x%04x (PAN ID 0x%04x) %i data bytes\n", conf->dst_addr, conf->pan_id, conf->packet_len); buf = (unsigned char *)malloc(MAX_PAYLOAD_LEN); /* 2 seconds packet receive timeout */ timeout.tv_sec = 2; timeout.tv_usec = 0; setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&timeout,sizeof(struct timeval)); count = 0; for (i = 0; i < conf->packets; i++) { generate_packet(buf, conf, i); seq_num = (buf[2] << 8)| buf[3]; send(sd, buf, conf->packet_len, 0); gettimeofday(&start_time, NULL); ret = recv(sd, buf, conf->packet_len, 0); if (seq_num != ((buf[2] << 8)| buf[3])) { printf("Sequenze number did not match\n"); continue; } if (ret > 0) { gettimeofday(&end_time, NULL); count++; sec = end_time.tv_sec - start_time.tv_sec; sum_sec += sec; usec = end_time.tv_usec - start_time.tv_usec; if (usec < 0) { usec += 1000000; sec--; sum_sec--; } sum_usec += usec; if (sec > sec_max) sec_max = sec; else if (sec < sec_min) sec_min = sec; if (usec > usec_max) usec_max = usec; else if (usec < usec_min) usec_min = usec; if (sec > 0) fprintf(stdout, "Warning: packet return time over a second!\n"); fprintf(stdout, "%i bytes from 0x%04x seq=%i time=%.1f ms\n", ret, conf->dst_addr, (int)seq_num, (float)usec/1000); } else fprintf(stderr, "Hit 2s packet timeout\n"); } if (count) packet_loss = 100 - ((100 * count)/conf->packets); if (usec_min) rtt_min = (float)usec_min/1000; if (sum_usec && count) rtt_avg = ((float)sum_usec/(float)count)/1000; if (usec_max) rtt_max = (float)usec_max/1000; fprintf(stdout, "\n--- 0x%04x ping statistics ---\n", conf->dst_addr); fprintf(stdout, "%i packets transmitted, %i received, %.0f%% packet loss\n", conf->packets, count, packet_loss); fprintf(stdout, "rtt min/avg/max = %.3f/%.3f/%.3f ms\n", rtt_min, rtt_avg, rtt_max); free(buf); return 0; }
int main(int argc, char *argv[]){ struct sockaddr_in si_me, si_other; int s, i, slen=sizeof(si_other); int port; char *ep; FILE *fp = fopen(argv[2], "w+"); //check if the arguements take are 3 or not if(argc!=3){ usage(); } //get the port number port = strtoul(argv[1], &ep, 10); if ( ( s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP) ) == -1 ) { printf("Error in creating socket\n"); exit(0); } memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(PORT); si_me.sin_addr.s_addr = htonl(IP); si_other.sin_family = AF_INET; si_other.sin_port = htons(port); si_other.sin_addr.s_addr = htonl(IP); if ( bind(s, (struct sockaddr*)&si_me, sizeof(si_me)) == -1 ) { printf("Error in binding the socket\n"); return 2; } srand(time(0)); (void) signal(SIGINT, sig_handler); int packet_id = 1; //start to listen while(flag){ generate_packet(packet_id); sendto(s, buffer, strlen(buffer) + 1, 0, (struct sockaddr*)&si_other, sizeof(si_other)); packet_id++; //check source ip and destination ip add to counter if (source==0||source==1||source==2){ if(destination==3 ||destination==4){ sendBtoA++; }else{ if(source<=8){ sendBtoC++; }else{ invalied++; } } }else if(source ==3||source==4){ if(destination<=2){ sendAtoB++; }else{ if(source<=8){ sendAtoC++; }else{ invalied++; } } }else{ if(source<=2){ sendCtoB++; }else{ if(source<=4){ sendCtoA++; }else{ invalied++; } } } if (packet_id%2 ==0){ sleep(2); } if(packet_id%20==0){ update_to_file(fp); } } update_to_file(fp); fclose(fp); close(s); return 0; }