ajout d'un exemple de systeme d'autoconfiguration via http
[ipphone-tools.git] / ipphone5-debug.c
1 #include <sys/socket.h>
2 #include <netinet/in.h>
3 #include <arpa/inet.h> /* inet_aton,inet_ntoa */
4 #include <signal.h> /* signal,SIG* */
5 #include <errno.h> /* errno,E* */
6 #include <unistd.h> /* read,write,close */
7 #include <string.h> /* memset */
8 #include <stdio.h> /* FILE,fprintf */
9
10 #define UDP_DEBUG_DST_PORT 0x2021
11 #define UDP_DEBUG_SRC_PORT 1025
12
13 #define DEBUG_BUFFER_SIZE 1024 // FIXME: check the real size
14
15 // FIXME: see Palm1DebugConvert.cpp
16
17 void human_debug(const char *buffer, int size) {
18 const unsigned char *p;
19 for (p = (unsigned char *)buffer; *p; p++) {
20 if (32 <= *p && *p <= 127) {
21 fprintf(stdout, "%c", *p);
22 } else if (*p == '\r' && *(p+1) == '\n') {
23 fprintf(stdout, "\\n");
24 p++;
25 } else {
26 fprintf(stdout, "\\%03o", *p);
27 }
28 }
29 }
30
31 int main(int argc, const char *argv[]) {
32 const char *progname;
33 int sd;
34 struct sockaddr_in sin;
35
36 // check arguments count
37 progname = argv[0];
38 if (argc != 1) {
39 fprintf(stderr, "Syntax: %s\n", progname);
40 return 1;
41 }
42
43 // prepare receiving answers (bind debug port)
44 sd = socket(PF_INET, SOCK_DGRAM, 0);
45 if (sd < 0) {
46 fprintf(stderr, "Unable to create a socket!\n");
47 return 3;
48 }
49 memset(&sin, 0, sizeof(sin));
50 sin.sin_family = AF_INET;
51 sin.sin_port = htons(UDP_DEBUG_DST_PORT);
52 sin.sin_addr.s_addr = htonl(INADDR_ANY);
53 if (bind(sd, (struct sockaddr *)&sin, sizeof(sin))) {
54 fprintf(stderr, "Unable to bind!\n");
55 return 4;
56 }
57
58 // wait for answers
59 signal(SIGPIPE, SIG_IGN);
60 while (1) {
61 char buffer[DEBUG_BUFFER_SIZE];
62 struct sockaddr_in sinrecv;
63 socklen_t sinrecv_len = sizeof(sinrecv);
64 int size = recvfrom(sd, buffer, sizeof(buffer), 0, (struct sockaddr *)&sinrecv, &sinrecv_len);
65 if (size < DEBUG_BUFFER_SIZE) {
66 fprintf(stdout, "%s:", inet_ntoa(sinrecv.sin_addr));
67 buffer[size] = '\0'; // FIXME: should be managed below
68 human_debug(buffer, size);
69 fprintf(stdout, "\n");
70 fflush(stdout);
71 } else {
72 fprintf(stderr, "Warning: received an invalid packet size of %d bytes!\n", size);
73 }
74 }
75
76 // cleaning
77 close(sd);
78 return 0;
79 }