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 send_sockopt; |
---|
21 | int send_sock; |
---|
22 | int send_length = sizeof(send_sockopt); |
---|
23 | FILE *fd; |
---|
24 | char BLACK_LIST[1024]; |
---|
25 | size_t len = 0; |
---|
26 | ssize_t read; |
---|
27 | |
---|
28 | if (argc <= 3) |
---|
29 | { |
---|
30 | fprintf(stderr, "USAGE: %s <RECV_IP> <RECV_PORT> <BLACK LIST FILE PATH>\n", argv[0]); |
---|
31 | fprintf(stderr, "Ex. %s 127.0.0.1 3838 /opt/icas/report/black/blacklist\n", argv[0]); |
---|
32 | exit(1); |
---|
33 | } |
---|
34 | |
---|
35 | /* open blacklist to read black list data */ |
---|
36 | fd = fopen(argv[3],"r"); |
---|
37 | if (fd == NULL) |
---|
38 | { |
---|
39 | fprintf(stderr, "%s: Couldn't open file %s; %s\n", argv[0], "blacklist", strerror (errno)); |
---|
40 | exit(1); |
---|
41 | } |
---|
42 | |
---|
43 | /* Create the UDP socket to send response to icas_recv */ |
---|
44 | if ((send_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) |
---|
45 | { |
---|
46 | Die("Failed to create socket for sending black list."); |
---|
47 | } |
---|
48 | |
---|
49 | /* Construct the server sockaddr_in structure */ |
---|
50 | memset(&send_sockopt, 0, sizeof(send_sockopt)); /* Clear struct */ |
---|
51 | memset(&BLACK_LIST, 0, sizeof(BLACK_LIST)); /* Clear string */ |
---|
52 | sprintf(BLACK_LIST,"This is a test!"); |
---|
53 | |
---|
54 | // socket option of icas_send socket |
---|
55 | send_sockopt.sin_family = AF_INET; /* Internet/IP */ |
---|
56 | send_sockopt.sin_addr.s_addr = inet_addr(argv[1]); /* IP address */ |
---|
57 | send_sockopt.sin_port = htons(atoi(argv[2])); /* server port */ |
---|
58 | |
---|
59 | while(fgets(BLACK_LIST,sizeof(BLACK_LIST),fd)) |
---|
60 | { |
---|
61 | if (sendto(send_sock, BLACK_LIST, sizeof(BLACK_LIST), 0, |
---|
62 | (struct sockaddr *) &send_sockopt, |
---|
63 | sizeof(send_sockopt)) != sizeof(BLACK_LIST)) |
---|
64 | { |
---|
65 | Die("1: Mismatch in number of sent bytes"); |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | close(send_sock); |
---|
70 | exit(0); |
---|
71 | } |
---|