[245] | 1 | #include <stdio.h> |
---|
| 2 | #include <sys/socket.h> |
---|
| 3 | #include <arpa/inet.h> |
---|
| 4 | #include <errno.h> |
---|
| 5 | #include <stdlib.h> |
---|
| 6 | #include <string.h> |
---|
| 7 | #include <fcntl.h> |
---|
| 8 | #include <signal.h> |
---|
| 9 | #include <unistd.h> |
---|
| 10 | #include <netinet/in.h> |
---|
[247] | 11 | #include <time.h> |
---|
[245] | 12 | |
---|
| 13 | void Die(char *mess) |
---|
| 14 | { |
---|
| 15 | perror(mess); |
---|
| 16 | exit(1); |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | int main(int argc, char *argv[]) |
---|
| 20 | { |
---|
| 21 | struct sockaddr_in recv_sockopt; |
---|
| 22 | int recv_sock; |
---|
| 23 | int recv_length = sizeof(recv_sockopt); |
---|
| 24 | int received; |
---|
[246] | 25 | char BLACK_LIST[1024]; |
---|
[245] | 26 | |
---|
| 27 | if (argc <= 1) |
---|
| 28 | { |
---|
| 29 | fprintf(stderr, "USAGE: %s <RECV_PORT>\n", argv[0]); |
---|
[247] | 30 | fprintf(stderr, "Ex: %s 3838\n", argv[0]); |
---|
[245] | 31 | exit(1); |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | /* Create the UDP socket to send response to icas_recv */ |
---|
| 35 | if ((recv_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) |
---|
| 36 | { |
---|
| 37 | Die("Failed to create socket for sending black list."); |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | /* Construct the server sockaddr_in structure */ |
---|
| 41 | memset(&recv_sockopt, 0, sizeof(recv_sockopt)); /* Clear struct */ |
---|
| 42 | memset(&BLACK_LIST, 0, sizeof(BLACK_LIST)); /* Clear string */ |
---|
| 43 | |
---|
| 44 | // socket option of icas_send socket |
---|
| 45 | recv_sockopt.sin_family = AF_INET; /* Internet/IP */ |
---|
| 46 | recv_sockopt.sin_addr.s_addr = htonl(INADDR_ANY); /* Any IP address */ |
---|
| 47 | recv_sockopt.sin_port = htons(atoi(argv[1])); /* server port */ |
---|
| 48 | |
---|
| 49 | /* Bind the socket */ |
---|
| 50 | if (bind(recv_sock, (struct sockaddr *) &recv_sockopt, recv_length) < 0) |
---|
| 51 | { |
---|
| 52 | Die("Failed to bind server socket"); |
---|
| 53 | } |
---|
| 54 | fprintf(stderr,"Finished binding port #%s for icas_recv socket.\n",argv[1]); |
---|
| 55 | |
---|
[246] | 56 | while(1) |
---|
[245] | 57 | { |
---|
[246] | 58 | if ((received = recvfrom(recv_sock, BLACK_LIST, sizeof(BLACK_LIST), 0, |
---|
| 59 | (struct sockaddr *) &recv_sockopt, |
---|
| 60 | (socklen_t *) &recv_length)) < 0) |
---|
| 61 | { |
---|
| 62 | Die("Failed to receive message"); |
---|
| 63 | } |
---|
| 64 | fprintf(stderr,"Received: %s\n", BLACK_LIST); |
---|
[245] | 65 | } |
---|
| 66 | |
---|
| 67 | close(recv_sock); |
---|
| 68 | exit(0); |
---|
| 69 | } |
---|