static int l_netutil_is_ip_addr(lua_State *L) { char *addr = (char *)luaL_checkstring(L, 1); lua_pushboolean(L, is_ip_addr(addr)); return 1; }
/** * * * @param argc count of arguments provided to main() * @param argv the arguments * * @return 0 on success, non-zero on invalid arguments */ static int check_input_validity(int argc, char* argv[]) { // An even amount of arguments were provided. // This is invalid, since the first argument in argv[] is ALWAYS going to // be the program name itself (at least when invoked via command line), which // means that an IP Address / Port pair cannot be created for the rest of the arguments if (argc % 2 == 0) { printf("Provide a port number for each IP Address.\n"); return 1; } int i; for (i = 1; i < argc; i++) { if (i % 2) { // check if valid IP address if (!is_ip_addr(argv[i], i)) { return 2; } } else { // check if valid port if (!is_valid_port(argv[i], i)) { return 3; } } } return 0; }
static int cmd_traffic_send_ping(struct sigma_dut *dut, struct sigma_conn *conn, struct sigma_cmd *cmd) { const char *dst, *val; int size, dur, pkts; int id; char resp[100]; float interval; double rate; FILE *f; char buf[100]; int type = 1; int dscp = 0, use_dscp = 0; char extra[100], int_arg[100], intf_arg[100]; val = get_param(cmd, "Type"); if (!val) val = get_param(cmd, "IPType"); if (val) type = atoi(val); if (type != 1 && type != 2) { send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Unsupported address type"); return 0; } dst = get_param(cmd, "destination"); if (dst == NULL || (type == 1 && !is_ip_addr(dst)) || (type == 2 && !is_ipv6_addr(dst))) return -1; val = get_param(cmd, "frameSize"); if (val == NULL) return -1; size = atoi(val); val = get_param(cmd, "frameRate"); if (val == NULL) return -1; rate = atof(val); if (rate <= 0) return -1; val = get_param(cmd, "duration"); if (val == NULL) return -1; dur = atoi(val); if (dur <= 0 || dur > 3600) dur = 3600; pkts = dur * rate; interval = (float) 1 / rate; if (interval > 100000) return -1; val = get_param(cmd, "DSCP"); if (val) { dscp = atoi(val); if (dscp < 0 || dscp > 63) { send_resp(dut, conn, SIGMA_ERROR, "ErrorCode,Invalid DSCP value"); return 0; } use_dscp = 1; } id = dut->next_streamid++; snprintf(buf, sizeof(buf), SIGMA_TMPDIR "/sigma_dut-ping.%d", id); unlink(buf); snprintf(buf, sizeof(buf), SIGMA_TMPDIR "/sigma_dut-ping-pid.%d", id); unlink(buf); sigma_dut_print(dut, DUT_MSG_DEBUG, "Send ping: pkts=%d interval=%f " "streamid=%d", pkts, interval, id); f = fopen(SIGMA_TMPDIR "/sigma_dut-ping.sh", "w"); if (f == NULL) return -2; extra[0] = '\0'; if (use_dscp) { snprintf(extra, sizeof(extra), " -Q 0x%02x", dscp << 2); } int_arg[0] = '\0'; if (rate != 1) snprintf(int_arg, sizeof(int_arg), " -i %f", interval); intf_arg[0] = '\0'; if (type == 2) snprintf(intf_arg, sizeof(intf_arg), " -I %s", get_station_ifname()); fprintf(f, "#!" SHELL "\n" "ping%s -c %d%s -s %d%s -q%s %s > " SIGMA_TMPDIR "/sigma_dut-ping.%d &\n" "echo $! > " SIGMA_TMPDIR "/sigma_dut-ping-pid.%d\n", type == 2 ? "6" : "", pkts, int_arg, size, extra, intf_arg, dst, id, id); fclose(f); if (chmod(SIGMA_TMPDIR "/sigma_dut-ping.sh", S_IRUSR | S_IWUSR | S_IXUSR) < 0) return -2; if (system(SIGMA_TMPDIR "/sigma_dut-ping.sh") != 0) { sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to start ping"); return -2; } unlink(SIGMA_TMPDIR "/sigma_dut-ping.sh"); snprintf(resp, sizeof(resp), "streamID,%d", id); send_resp(dut, conn, SIGMA_COMPLETE, resp); return 0; }
int cmd_traffic_send_ping(struct sigma_dut *dut, tgPingStart_t *staPing, dutCmdResponse_t *spresp) { const char *dst; int val, size, dur, pkts; int id; char resp[100]; float interval, rate; FILE *f; char buf[100]; dst = staPing->dipaddr; if (strcmp(dst, "") == 0 || !is_ip_addr(dst)) return -1; //val = get_param(cmd, "frameSize"); val = staPing->frameSize; if (val == 0) return -1; size = val; //atoi(val); val = staPing->frameRate; if (val == 0) return -1; rate = val; // atof(val); #if 0 if (rate < 1) { return -1; } #endif val = staPing->duration; if (val == 0) return -1; dur = val; // atoi(val); if (dur <= 0) dur = 3600; pkts = dur * rate; interval = (float) 1 / rate; id = dut->next_streamid++; snprintf(buf, sizeof(buf), "/tmp/sigma_dut-ping.%d", id); unlink(buf); snprintf(buf, sizeof(buf), "/tmp/sigma_dut-ping-pid.%d", id); unlink(buf); sigma_dut_print( DUT_MSG_DEBUG, "Send ping: pkts=%d interval=%f " "streamid=%d", pkts, interval, id); f = fopen("/tmp/sigma_dut-ping.sh", "w"); if (f == NULL) return -2; fprintf(f, "#!/bin/sh\n" "ping -c %d -i %f -s %d -q %s > /tmp/sigma_dut-ping.%d &\n" "echo $! > /tmp/sigma_dut-ping-pid.%d\n", pkts, interval, size, dst, id, id); fclose(f); if (chmod("/tmp/sigma_dut-ping.sh", S_IRUSR | S_IWUSR | S_IXUSR) < 0) return -2; if (system("/tmp/sigma_dut-ping.sh") != 0) { sigma_dut_print( DUT_MSG_ERROR, "Failed to start ping"); return -2; } unlink("/tmp/sigma_dut-ping.sh"); snprintf(resp, sizeof(resp), "streamID,%d", id); send_resp(dut, conn, SIGMA_COMPLETE, resp); return 0; }