premier commit (version 105)
[ipphone-tools.git] / ipphone5-message.c
1 #include <sys/socket.h>
2 #include <netinet/in.h>
3 #include <arpa/inet.h> /* inet_aton,inet_ntoa */
4 #include <unistd.h> /* read,write,close */
5 #include <string.h> /* memset */
6 #include <stdio.h> /* FILE,fprintf */
7
8 #define UDP_DEBUG_SRC_PORT 1025
9 #define REPORT_DST_IPADDR "224.1.2.5"
10
11 int main(int argc, const char *argv[]) {
12 const char *progname, *message, *ipaddr;
13 struct in_addr sin_addr;
14 int sd;
15 struct sockaddr_in sin;
16
17 // check arguments count
18 progname = argv[0];
19 if (argc == 2) { // multicast
20 message = argv[1];
21 ipaddr = REPORT_DST_IPADDR;
22 } else if (argc == 3) { // specific destination
23 message = argv[1];
24 ipaddr = argv[2];
25 } else {
26 fprintf(stderr, "Syntax: %s <message> [<IPv4-address>]\n", progname);
27 return 1;
28 }
29 setbuf(stdout, NULL);
30
31 // check IPv4 address
32 if (!inet_aton(ipaddr, &sin_addr)) {
33 fprintf(stderr, "'%s' is an invalid IPv4 address!\n", ipaddr);
34 return 2;
35 }
36 fprintf(stdout, "IPv4 address: %s\n", inet_ntoa(sin_addr));
37
38 // prepare receiving answers (bind report port)
39 sd = socket(PF_INET, SOCK_DGRAM, 0);
40 if (sd < 0) {
41 fprintf(stderr, "Unable to create a socket!\n");
42 return 3;
43 }
44
45 // send message to display
46 memset(&sin, 0, sizeof(sin));
47 sin.sin_family = AF_INET;
48 sin.sin_port = htons(UDP_DEBUG_SRC_PORT);
49 inet_aton(ipaddr, &sin.sin_addr);
50 if (sendto(sd, message, strlen(message)+1, 0, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
51 fprintf(stderr, "Error sending UDP request!\n");
52 return 4;
53 }
54
55 // cleaning
56 close(sd);
57 return 0;
58 }