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