/** * Event readout routine. * * @param pevent * @param off offset (for subevents) * * @return */ INT read_trigger_event(char *pevent, INT off) { // DWORD *pdata; char bk_name[8]; int status; data_avail = FALSE; /* init bank structure */ bk_init32(pevent); /* create structured bank */ // //sprintf(bk_name,"SI%02i",frontend_index); // Just one VME crate, we don't use frontend_index #if 0 sprintf(bk_name,"FD%02i",frontend_index); bk_create(pevent, bk_name, TID_DWORD, &pdata); // all udp data received. copy data to midas bank /** \todo check data size */ //pthread_mutex_lock( &buf_packet_gl_mutex ); /*TG memcpy(pdata,gpu_data,gpu_data_size); pdata += gpu_data_size/sizeof(pdata[0]); */ int fake_data_size = 12*10*1000; //originally 10*1000 *pdata++ = (DWORD) 0x1CEB00DA; pdata += fake_data_size/sizeof(pdata[0]); bk_close(pevent, pdata); #endif #if 0 // unlock udp threads for (int i=0; i<udp_thread_num; i++) { pthread_mutex_unlock( &udp_thread_info[i].mutex ); } #endif #if 0 // read out tempratures sensord of SIS3350 boards sprintf(bk_name,"ST%02i",frontend_index); WORD *tdata; bk_create(pevent, bk_name, TID_WORD, &tdata); for (int i=0; i<SIS3350_NUM; i++) { if ( ! sis3350_info[i].board.enabled ) continue; unsigned int t; sis3350_ReadTemperature(i, &t); *tdata++ = (WORD)sis3350_info[i].SN; *tdata++ = (WORD)t; short int *ts = (short int*)&t; float tf = *ts; tf /= 4; printf("Board %i temperature: %f\n",sis3350_info[i].SN,tf); } bk_close(pevent, tdata); #endif #if 0 // timing struct timeval t; DWORD *tr_data; sprintf(bk_name,"CS%02i",frontend_index); bk_create(pevent, bk_name, TID_DWORD, &tr_data); status = gettimeofday( &t, NULL); if ( status != 0) { printf("ERROR! gettimeofday() failed\n"); t.tv_sec = 0; t.tv_usec = 0; } trigger_info.time_slave_got_data_s = t.tv_sec; trigger_info.time_slave_got_data_us = t.tv_usec; *tr_data++ = trigger_info.trigger_nr; *tr_data++ = trigger_info.trigger_mask; *tr_data++ = trigger_info.time_master_got_eof_s; *tr_data++ = trigger_info.time_master_got_eof_us; *tr_data++ = trigger_info.time_slave_got_eof_s; *tr_data++ = trigger_info.time_slave_got_eof_us; *tr_data++ = trigger_info.time_tcp_finish_data_read_s; *tr_data++ = trigger_info.time_tcp_finish_data_read_us; bk_close(pevent, tr_data); long int dt_s = trigger_info.time_slave_got_data_s; dt_s -= trigger_info.time_slave_got_eof_s; long int dt_us = trigger_info.time_slave_got_data_us; dt_us -= trigger_info.time_slave_got_eof_us; if ( dt_us < 0 ) { dt_s -= 1; dt_us += 1000000; } printf("Readout: trigger dt = %li s %li us\n", dt_s, dt_us); printf("Bank size: %i\n", bk_size(pevent)); #endif #if 1 struct timeval start_READY_time, end_READY_time; gettimeofday(&start_READY_time, NULL); rpc_g2_ready( frontend_index + frontend_index_offset ); gettimeofday(&end_READY_time, NULL); dt_READY = dt_READY + toddiff(&end_READY_time, &start_READY_time); n_dt_READYs++; printf("n READYs, dt READY average, dt %d %f %f\n", n_dt_READYs, dt_READY/n_dt_READYs, toddiff(&end_READY_time, &start_READY_time)); #endif return bk_size(pevent); }
int main(int argc, char **argv) { int optval = 1; int print_help = 0; int send_packets = 5; int fastmode = 0; int c; struct sockaddr_in si_me; struct mt_packet packet; int i; while (1) { c = getopt(argc, argv, "fs:c:hv?"); if (c == -1) { break; } switch (c) { case 'f': fastmode = 1; break; case 's': ping_size = atoi(optarg) - 18; break; case 'v': print_version(); exit(0); break; case 'c': send_packets = atoi(optarg); break; case 'h': case '?': print_help = 1; break; } } /* We don't want people to use this for the wrong reasons */ if (fastmode && (send_packets == 0 || send_packets > 100)) { fprintf(stderr, "Number of packets to send must be more than 0 and less than 100 in fast mode.\n"); return 1; } if (argc - optind < 1 || print_help) { print_version(); fprintf(stderr, "Usage: %s <MAC> [-h] [-f] [-c <count>] [-s <packet size>]\n", argv[0]); if (print_help) { fprintf(stderr, "\nParameters:\n"); fprintf(stderr, " MAC MAC-Address of the RouterOS/mactelnetd device.\n"); fprintf(stderr, " -f Fast mode, do not wait before sending next ping request.\n"); fprintf(stderr, " -s Specify size of ping packet.\n"); fprintf(stderr, " -c Number of packets to send. (0 = unlimited)\n"); fprintf(stderr, " -h This help.\n"); fprintf(stderr, "\n"); } return 1; } if (ping_size > ETH_FRAME_LEN - 42) { fprintf(stderr, "Packet size must be between 18 and %d\n", ETH_FRAME_LEN - 42 + 18); exit(1); } /* Mikrotik RouterOS does not answer unless the packet has the correct recipient mac-address in * the ethernet frame. Unlike real MacTelnet connections where the OS is ok with it being a * broadcast mac address. */ if (geteuid() != 0) { fprintf(stderr, "You need to have root privileges to use %s.\n", argv[0]); return 1; } /* Get mac-address from string, or check for hostname via mndp */ if (!query_mndp_or_mac(argv[optind], dstmac, 1)) { /* No valid mac address found, abort */ return 1; } sockfd = net_init_raw_socket(); insockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (insockfd < 0) { perror("insockfd"); return 1; } /* Set initialize address/port */ memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(MT_MACTELNET_PORT); si_me.sin_addr.s_addr = htonl(INADDR_ANY); setsockopt(insockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (optval)); /* Bind to specified address/port */ if (bind(insockfd, (struct sockaddr *)&si_me, sizeof(si_me))==-1) { fprintf(stderr, "Error binding to %s:%d\n", inet_ntoa(si_me.sin_addr), MT_MNDP_PORT); return 1; } /* Listen address*/ inet_pton(AF_INET, (char *)"0.0.0.0", &sourceip); /* Set up global info about the connection */ inet_pton(AF_INET, (char *)"255.255.255.255", &destip); srand(time(NULL)); /* Enumerate available interfaces */ net_get_interfaces(interfaces, MAX_INTERFACES); if (ping_size < sizeof(struct timeval)) { ping_size = sizeof(struct timeval); } signal(SIGINT, display_results); for (i = 0; i < send_packets || send_packets == 0; ++i) { fd_set read_fds; static struct timeval lasttimestamp; int reads, result; struct timeval timeout; int ii; int sent = 0; int waitforpacket; struct timeval timestamp; unsigned char pingdata[1500]; gettimeofday(×tamp, NULL); memcpy(pingdata, ×tamp, sizeof(timestamp)); for (ii = sizeof(timestamp); ii < ping_size; ++ii) { pingdata[ii] = rand() % 256; } for (ii = 0; ii < MAX_INTERFACES; ++ii) { struct net_interface *interface = &interfaces[ii]; if (!interface->in_use) { break; } if (!interface->has_mac) { continue; } init_pingpacket(&packet, interface->mac_addr, dstmac); add_packetdata(&packet, pingdata, ping_size); result = net_send_udp(sockfd, interface, interface->mac_addr, dstmac, &sourceip, MT_MACTELNET_PORT, &destip, MT_MACTELNET_PORT, packet.data, packet.size); if (result > 0) { sent++; } } if (sent == 0) { fprintf(stderr, "Error sending packet.\n"); continue; } ping_sent++; FD_ZERO(&read_fds); FD_SET(insockfd, &read_fds); timeout.tv_sec = 1; timeout.tv_usec = 0; waitforpacket = 1; while (waitforpacket) { /* Wait for data or timeout */ reads = select(insockfd+1, &read_fds, NULL, NULL, &timeout); if (reads <= 0) { waitforpacket = 0; fprintf(stderr, "%s ping timeout\n", ether_ntoa((struct ether_addr *)&dstmac)); break; } unsigned char buff[1500]; struct sockaddr_in saddress; unsigned int slen = sizeof(saddress); struct mt_mactelnet_hdr pkthdr; result = recvfrom(insockfd, buff, 1500, 0, (struct sockaddr *)&saddress, &slen); parse_packet(buff, &pkthdr); /* TODO: Check that we are the receiving host */ if (pkthdr.ptype != MT_PTYPE_PONG) { /* Wait for the correct packet */ continue; } struct timeval pongtimestamp; struct timeval nowtimestamp; waitforpacket = 0; gettimeofday(&nowtimestamp, NULL); memcpy(&pongtimestamp, pkthdr.data - 4, sizeof(pongtimestamp)); if (memcmp(pkthdr.data - 4, pingdata, ping_size) == 0) { float diff = toddiff(&nowtimestamp, &pongtimestamp) / 1000.0f; if (diff < min_ms) { min_ms = diff; } if (diff > max_ms) { max_ms = diff; } avg_ms += diff; printf("%s %d byte, ping time %.2f ms%s\n", ether_ntoa((struct ether_addr *)&(pkthdr.srcaddr)), result, diff, (char *)(memcmp(&pongtimestamp,&lasttimestamp,sizeof(lasttimestamp)) == 0 ? " DUP" : "")); } else { printf("%s Reply of %d bytes of unequal data\n", ether_ntoa((struct ether_addr *)&(pkthdr.srcaddr)), result); } pong_received++; memcpy(&lasttimestamp, &pongtimestamp, sizeof(pongtimestamp)); if (!fastmode) { sleep(1); } } } /* Display statistics and exit */ display_results(); return 0; }