lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include "headers.h" |
| 4 | #include "settings.h" |
| 5 | #include "runner.h" |
| 6 | #include "socketHelper.h" |
| 7 | #include "zpingp.h" |
| 8 | #include "softap_log.h" |
| 9 | |
| 10 | |
| 11 | const struct option long_options[] = { |
| 12 | |
| 13 | {"ipv4", no_argument, NULL, '4'}, |
| 14 | //{"ipv6", no_argument, NULL, '6'}, |
| 15 | {"size", required_argument, NULL, 'l'}, |
| 16 | {"pingcount", required_argument, NULL, 'c'}, |
| 17 | {"itemcount", required_argument, NULL, 'n'}, |
| 18 | {"timeout", required_argument, NULL, 'w'}, // wait "w" seconds for each reply |
| 19 | {"interval", required_argument, NULL, 'i'}, // minimum seconds between two requests |
| 20 | {"unstopped", no_argument, NULL, 't'}, |
| 21 | {"printall", no_argument, NULL, 'p'}, |
| 22 | {"lostoutput", no_argument, NULL, 'o'}, |
| 23 | {"maxwaitnum", required_argument, NULL, 'u'}, |
| 24 | {"delaycheck", no_argument, NULL, 'd'}, |
| 25 | {"help", no_argument, NULL, 'h'}, |
| 26 | |
| 27 | {0, 0, 0, 0} |
| 28 | }; |
| 29 | |
| 30 | const char short_options[] = "4l:c:n:w:i:tpou:dh"; |
| 31 | |
| 32 | void sig_handler(int sig) |
| 33 | { |
| 34 | if (sig == SIGINT || sig == SIGTERM) { |
| 35 | exit(0); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | |
| 40 | void init_settings(zping_settings_t *zping_settings) |
| 41 | { |
| 42 | memset(zping_settings, 0, sizeof(zping_settings_t)); |
| 43 | |
| 44 | zping_settings->size = 800; |
| 45 | zping_settings->itemcount = 20; |
| 46 | zping_settings->pingcount = 100; |
| 47 | zping_settings->timeout = 4; |
| 48 | zping_settings->interval = 1; |
| 49 | zping_settings->lostoutput = 1; |
| 50 | zping_settings->unstopped = 1; |
| 51 | } |
| 52 | |
| 53 | int main(int argc, char** argv) |
| 54 | { |
| 55 | int c, rc; |
| 56 | zping_settings_t zping_settings; |
| 57 | |
| 58 | int item_size = 0; |
| 59 | |
| 60 | struct sigaction sigact; |
| 61 | /* // for child pthreads to ignore SIGPIPE |
| 62 | sigset_t signal_mask; |
| 63 | sigemptyset (&signal_mask); |
| 64 | sigaddset (&signal_mask, SIGPIPE); |
| 65 | rc = pthread_sigmask (SIG_BLOCK, &signal_mask, NULL); |
| 66 | if (rc != 0) |
| 67 | { |
| 68 | zping_print(LOG_ALERT, "block sigpipe error\n"); |
| 69 | } |
| 70 | */ |
| 71 | |
| 72 | sigact.sa_handler = sig_handler; |
| 73 | sigact.sa_flags = 0; |
| 74 | sigemptyset(&sigact.sa_mask); |
| 75 | sigaction(SIGINT, &sigact, NULL); |
| 76 | sigaction(SIGTERM, &sigact, NULL); |
| 77 | |
| 78 | /* ignore SIGPIPE */ |
| 79 | sigact.sa_handler = SIG_IGN; |
| 80 | sigact.sa_flags = 0; |
| 81 | sigemptyset(&sigact.sa_mask); |
| 82 | sigaction(SIGPIPE, &sigact, NULL); |
| 83 | |
| 84 | //pthread_t tid = pthread_self(); |
| 85 | //zping_print(LOG_ALERT, "main pthread id:%lu \n",tid); |
| 86 | |
| 87 | //memset(&zperf_settings, 0, sizeof(zperf_settings_t)); |
| 88 | init_settings(&zping_settings); |
| 89 | |
| 90 | while ((c = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) { |
| 91 | //test = 1; |
| 92 | zping_print(LOG_ALERT, "option %c\n", c); |
| 93 | switch (c) { |
| 94 | case '4': |
| 95 | zping_settings.ip_type = 0; |
| 96 | break; |
| 97 | |
| 98 | case 'l': |
| 99 | zping_settings.size = atoi(optarg); |
| 100 | break; |
| 101 | |
| 102 | case 'c': |
| 103 | zping_settings.pingcount = atoi(optarg); |
| 104 | zping_settings.unstopped = 0; |
| 105 | break; |
| 106 | |
| 107 | case 'n': |
| 108 | zping_settings.itemcount = atoi(optarg); |
| 109 | break; |
| 110 | |
| 111 | case 'w': |
| 112 | zping_settings.timeout = atoi(optarg); |
| 113 | break; |
| 114 | |
| 115 | case 'i': |
| 116 | zping_settings.interval = atoi(optarg); |
| 117 | break; |
| 118 | |
| 119 | case 't': |
| 120 | zping_settings.unstopped = 1; |
| 121 | break; |
| 122 | |
| 123 | case 'p': |
| 124 | zping_settings.delayoutput = 1; |
| 125 | break; |
| 126 | |
| 127 | case 'o': |
| 128 | zping_settings.lostoutput = 1; |
| 129 | break; |
| 130 | |
| 131 | case 'u': |
| 132 | zping_settings.maxwaitnum = atoi(optarg); |
| 133 | break; |
| 134 | |
| 135 | case 'd': |
| 136 | //if (atoi(optarg) == 1 || atoi(optarg) == 0) |
| 137 | // zping_settings.set_id = atoi(optarg); |
| 138 | zping_settings.set_id = 1; |
| 139 | zping_settings.delayoutput = 1; |
| 140 | break; |
| 141 | |
| 142 | case 'h': |
| 143 | printf("zping ip_addr [options] \n"); |
| 144 | printf("options: \n"); |
| 145 | printf("-d delay check \n"); |
| 146 | printf("-c num ping num\n"); |
| 147 | printf("-l size ping size\n"); |
| 148 | printf("-p print every packet\n"); |
| 149 | goto out; |
| 150 | |
| 151 | case '?': |
| 152 | break; |
| 153 | |
| 154 | default: |
| 155 | zping_print(LOG_ALERT, "?? getopt returned character code 0%o ??\n", c); |
| 156 | break; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | if (argc <= 1) { |
| 161 | zping_print(LOG_ALERT, "zping -h for usage \n"); |
| 162 | goto out; |
| 163 | } |
| 164 | |
| 165 | strcpy(zping_settings.address_str, argv[optind]); |
| 166 | SockAddr_setHostname(zping_settings.address_str, &zping_settings.zping_sockaddr); |
| 167 | |
| 168 | //zping_print(LOG_ALERT, "zping_settings.address_str:%s, %x\n", zping_settings.address_str, |
| 169 | // ((struct sockaddr_in*)&zping_settings.zping_sockaddr)->sin_addr.s_addr); |
| 170 | |
| 171 | if (zping_settings.size > 1472) { |
| 172 | zping_print(LOG_ALERT, "size should not be greater than 1472\n"); |
| 173 | goto out; |
| 174 | } |
| 175 | |
| 176 | item_size = zping_settings.set_id == 0 ? sizeof(zping_pktlost_item_t) : sizeof(zping_item_t); |
| 177 | if (zping_settings.size < sizeof(zping_hdr_t) + zping_settings.itemcount * item_size) { |
| 178 | zping_print(LOG_ALERT, "%d bytes is not enough to contain %d items, at least %d bytes are needed\n", |
| 179 | zping_settings.size, zping_settings.itemcount, |
| 180 | sizeof(zping_hdr_t) + zping_settings.itemcount * item_size); |
| 181 | goto out; |
| 182 | } |
| 183 | if(zping_settings.set_id) |
| 184 | zping_print(LOG_ALERT, "work type: deley check \n"); |
| 185 | else |
| 186 | zping_print(LOG_ALERT, "work type: loss check \n"); |
| 187 | zping_print(LOG_ALERT, "ping size: %d \n",zping_settings.size); |
| 188 | zping_print(LOG_ALERT, "node num: %d \n",zping_settings.itemcount); |
| 189 | if(zping_settings.unstopped) |
| 190 | zping_print(LOG_ALERT, "ping num: endless \n"); |
| 191 | else |
| 192 | zping_print(LOG_ALERT, "ping num: %d \n",zping_settings.pingcount); |
| 193 | zping_print(LOG_ALERT, "wait time: %d \n",zping_settings.timeout); |
| 194 | |
| 195 | start_run(&zping_settings); |
| 196 | |
| 197 | //thread_start(&zperf_settings); |
| 198 | |
| 199 | //thread_join(&zperf_settings); |
| 200 | |
| 201 | //zping_print(LOG_ALERT, "Hello World\n"); |
| 202 | //getchar(); |
| 203 | //all done ! |
| 204 | out: |
| 205 | return 0; |
| 206 | } |
| 207 | |