1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
| #incnclude <stdio.h> #include <getopt.h> #include <stdbool.h> #incle <string.h> #include <stdlib.h> #include <pthread.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <fcntl.h> #include <unistd.h> #include <sys/time.h> #include <time.h> char *l_opt_arg; char* const short_options = "c:l:i:srhp:a:y:";
struct option long_options[] = { { "count", 1, NULL, 'c' }, { "length", 1, NULL, 'l' }, { "interval", 1, NULL, 'i' }, { "port", 1, NULL, 'p' }, { "ip", 1, NULL, 'a' }, { "cycles", 1, NULL, 'y' }, { "send", 0, NULL, 's' }, { "receive", 0, NULL, 'r' }, { "help", 0, NULL, 'h' }, { 0, 0, 0, 0} }; void printHelp(); struct ControlArguments{ bool isSend; bool isReceive; int SendCount; int PacketLength; int Interval; int Port; int Cycles; char IP[15]; }; void sendUDP(struct ControlArguments * const); void receiveUDP(struct ControlArguments * const); void initControlArguments(struct ControlArguments * const controlArguments){ controlArguments->isSend = false; controlArguments->isReceive = false; controlArguments->SendCount = 1; controlArguments->PacketLength = 100; controlArguments->Interval = 20; controlArguments->Port = 0; controlArguments->Cycles = 10; controlArguments->IP[0] = '\0'; } int charPoint2Int(char const * string){ return atoi(string); } int main(int argc, char *argv[]) { int c; int index=0; bool argumentOK = true; static struct ControlArguments controlArguments; initControlArguments(&controlArguments); while((c = getopt_long (argc, argv, short_options, long_options, &index)) != -1) { switch (c) { case 's': printf("Send\n"); controlArguments.isSend = true; break; case 'r': printf("Receive\n"); controlArguments.isReceive = true; break; case 'c': l_opt_arg = optarg; printf("Send Count : %s\n", l_opt_arg); controlArguments.SendCount = charPoint2Int(l_opt_arg); break; case 'y': l_opt_arg = optarg; printf("Send Cycles : %s\n", l_opt_arg); controlArguments.Cycles = charPoint2Int(l_opt_arg); break; case 'l': l_opt_arg = optarg; printf("Packet Length : %s\n",l_opt_arg); controlArguments.PacketLength = charPoint2Int(l_opt_arg); break; case 'i': l_opt_arg = optarg; printf("Interval Time : %s\n",l_opt_arg); controlArguments.Interval = charPoint2Int(l_opt_arg); break; case 'h': printHelp(); break; case 'p': l_opt_arg = optarg; printf("Chosen Port : %s\n",l_opt_arg); controlArguments.Port = charPoint2Int(l_opt_arg); break; case 'a': l_opt_arg = optarg; printf("Target IP : %s\n",l_opt_arg); strncpy(controlArguments.IP,l_opt_arg,15); break; default: printf("Do not match!!!\n"); argumentOK = false; } } if(argumentOK){ if(controlArguments.isSend && !controlArguments.isReceive){ printf("choosing send\n"); sendUDP(&controlArguments); }else if(controlArguments.isReceive && !controlArguments.isSend){ printf("choosing receive\n"); receiveUDP(&controlArguments); }else{ printf("**************************\n"); printf("Arguments Error! both send and receive.\n"); } }else{ printf("************************************************\n"); printHelp(); } return 0; } void printHelp(){ printf("Help Menu:\n Argument reference list:\n"); printf(" -s :send UDP packets.\n"); printf(" -r :receive UDP packets.\n"); printf(" -c --count :packets to send one cycle.default is 1.\n"); printf(" -y --cycles :send cycles.default is 10.\n"); printf(" -l --length :data length in udp packet.default is 100bytes.\n"); printf(" -i --interval :milliseconds between send options.default is 20ms.\n"); printf(" -a --ip :ip address to operate.\n"); printf(" -p --port :port used for UDP socket.\n"); printf(" -h --help :command help.\n"); }
bool IPFormatCheck(char * const IP){ printf("IP check\n"); int ip_length = strlen(IP); bool checkError = false; int count_s = 0; for(int i = 0,interval = 0 ; i < ip_length ; ++ i){ if(IP[i] == '.'){ if(interval == 0 || interval > 3){ checkError = true; break; }else{ ++ count_s; interval = 0; } }else if(IP[i] >= '0' && IP[i] <= '9'){ ++ interval; }else{ checkError = true; break; } } if(count_s != 3){ checkError = true; } return (!checkError); }
bool checkSendConfig(struct ControlArguments * const controlArguments){ printf("checking arguments.\n"); bool returnVaule; char * IP; IP = malloc(sizeof(strlen(controlArguments->IP)) + 1); strncpy(IP,controlArguments->IP,15); if(strlen(IP) != 0){ if(IPFormatCheck(IP)){ if((controlArguments->Port == 0) || (controlArguments->PacketLength == 0)){ printf("Port:%d Length:%d\n",controlArguments->Port,controlArguments->PacketLength); printf("[Error] : Port or PacketLength Error.\n"); returnVaule = false; }else{ returnVaule = true; } }else{ printf("[Error] : IP Error\n"); returnVaule = false; } }else{ printf("[Error] : IP length = 0!\n"); returnVaule = false; } free(IP); return returnVaule; } void threadSend(struct ControlArguments * const controlArguments){ printf("****************************************\nSending Thread Start\n"); int sock_cli = socket(AF_INET,SOCK_DGRAM, 0); struct sockaddr_in servaddr; memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(controlArguments->Port); servaddr.sin_addr.s_addr = inet_addr(controlArguments->IP); struct timeval time; if (connect(sock_clli, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0){ perror("connect"); }else{ char *sendbuf = malloc(controlArguments->PacketLength); for(int i = 0 ; i < controlArguments->PacketLength - 1 ; i++){ gettimeofday(&time,NULL); int seed = time.tv_usec + i; sendbuf[i] = rand_r(&seed) % 93 + 33; } sendbuf[controlArguments->PacketLength - 1] = '\0'; printf("buf : %s\n",sendbuf); useconds_t sleep_us = (unsigned int)controlArguments->Interval * 1000; int pack_seq = 0; for(int i = 0 ; i < controlArguments->Cycles ; ++ i){ for(int j = 0 ; j < controlArguments->SendCount ; j++){ char *realSend = malloc((strlen(sendbuf) + 20)); if(realSend){ gettimeofday(&time,NULL); sprintf(realSend,"[%4ld.%06ld][%4d]:%s",time.tv_sec,time.tv_usec,++pack_seq,sendbuf); if(send(sock_cli, realSend, strlen(realSend),0) > 0){ printf("Send Successful\n"); } free(realSend); } } usleep(sleep_us); } free(sendbuf);
} close(sock_cli); printf("Sending Thread Finish\n****************************************\n"); } void sendUDP(struct ControlArguments * const controlArguments){ if(checkSendConfig(controlArguments)){
printf("Before Send Thread\n");
threadSend(controlArguments); printf("After Send Thread\n"); }else{ ; } } bool checkReceiveConfig(struct ControlArguments * const controlArguments){ if(controlArguments->Port == 0){ printf("[Error] : Please set Listening port.\n"); return false; }else{ return true; } } void threadReceive(struct ControlArguments * const controlArguments){ printf("****************************************\nReceiving Thread Start\n"); int server_sockfd = socket(AF_INET,SOCK_DGRAM, 0); static int recive_serial = 0; if(server_sockfd < 0){ perror("socket"); }else{ struct sockaddr_in server_sockaddr; struct sockaddr_in client_addr; memset(&server_sockaddr,0,sizeof(struct sockaddr_in)); server_sockaddr.sin_family = AF_INET; server_sockaddr.sin_port = htons(controlArguments->Port); server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY); if(bind(server_sockfd,(struct sockaddr *)&server_sockaddr,sizeof(server_sockaddr))==-1){ perror("bind"); }else{ char buffer[1024]; socklen_t length = sizeof(client_addr); int recv_len = 0; struct timeval time; while(1){ recv_len = recvfrom(server_sockfd,buffer,sizeof(buffer),0,(struct sockaddr *)&client_addr,&length); gettimeofday(&time,NULL); if(recv_len >= 0){ ++ recive_serial; printf("[%8ld.%06ld]Get[%d][l:%d] : %s\n",time.tv_sec,time.tv_usec,recive_serial,recv_len,buffer); } memset(&buffer,0,sizeof(buffer)); } } } close(server_sockfd); printf("Receiving Thread Finish\n****************************************\n"); } void receiveUDP(struct ControlArguments * const controlArguments){ if(checkReceiveConfig(controlArguments)){
printf("Before Thread\n");
threadReceive(controlArguments); printf("After Thread\n"); }else{ ; } }
|