annotate dpid/dpidc.c @ 2048:5060d415a85a

clickable menu items (even those introducing submenus) MUST have callbacks I clicked on the "Panel size" item itself instead of any of the options in its submenu, and: Segfault!
author corvid <corvid@lavabit.com>
date Thu, 26 May 2011 02:51:18 +0000
parents cf7f2d3312fb
children
rev   line source
1394
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
1 #include <stdio.h>
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
2 #include <stdlib.h> /* for exit */
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
3 #include <string.h> /* for bzero */
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
4 #include <unistd.h> /* for read and write */
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
5 #include <ctype.h> /* for isxdigit */
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
6 #include <sys/types.h>
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
7 #include <sys/socket.h>
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
8 #include <netinet/in.h>
1591
cf7f2d3312fb trim some spaces
corvid <corvid@lavabit.com>
parents: 1545
diff changeset
9 #include <netdb.h>
1394
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
10 #include <errno.h>
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
11
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
12 #include "../dpip/dpip.h"
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
13
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
14 #define MSG_ERR(...) printf("** ERROR **: " __VA_ARGS__);
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
15
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
16 char *CMD_REGISTER = "<cmd='register_all' '>";
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
17 char *CMD_STOP = "<cmd='DpiBye' '>";
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
18
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
19 static char SharedKey[32];
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
20
1537
3fcfd8389ece dpidc usage
corvid <corvid@lavabit.com>
parents: 1535
diff changeset
21 static void print_usage(const char *prgname)
3fcfd8389ece dpidc usage
corvid <corvid@lavabit.com>
parents: 1535
diff changeset
22 {
3fcfd8389ece dpidc usage
corvid <corvid@lavabit.com>
parents: 1535
diff changeset
23 fprintf(stderr,"Control program for the Dillo plugin daemon\n"
3fcfd8389ece dpidc usage
corvid <corvid@lavabit.com>
parents: 1535
diff changeset
24 "Usage: %s {stop|register|chat}\n\n", prgname);
3fcfd8389ece dpidc usage
corvid <corvid@lavabit.com>
parents: 1535
diff changeset
25 }
1394
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
26
1537
3fcfd8389ece dpidc usage
corvid <corvid@lavabit.com>
parents: 1535
diff changeset
27 static void error(char *msg)
1394
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
28 {
1537
3fcfd8389ece dpidc usage
corvid <corvid@lavabit.com>
parents: 1535
diff changeset
29 perror(msg);
3fcfd8389ece dpidc usage
corvid <corvid@lavabit.com>
parents: 1535
diff changeset
30 exit(1);
1394
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
31 }
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
32
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
33 /*
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
34 * Read dpid's communication keys from its saved file.
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
35 * Return value: 1 on success, -1 on error.
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
36 */
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
37 static int Dpi_read_comm_keys(int *port)
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
38 {
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
39 FILE *In;
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
40 char *fname, *rcline = NULL, *tail;
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
41 int i, ret = -1;
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
42
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
43 fname = dStrconcat(dGethomedir(), "/.dillo/dpid_comm_keys", NULL);
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
44 if ((In = fopen(fname, "r")) == NULL) {
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
45 MSG_ERR("[Dpi_read_comm_keys] %s\n", dStrerror(errno));
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
46 } else if ((rcline = dGetline(In)) == NULL) {
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
47 MSG_ERR("[Dpi_read_comm_keys] empty file: %s\n", fname);
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
48 } else {
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
49 *port = strtol(rcline, &tail, 10);
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
50 for (i = 0; *tail && isxdigit(tail[i+1]); ++i)
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
51 SharedKey[i] = tail[i+1];
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
52 SharedKey[i] = 0;
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
53 ret = 1;
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
54 }
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
55 dFree(rcline);
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
56 dFree(fname);
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
57
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
58 return ret;
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
59 }
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
60
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
61 int main(int argc, char *argv[])
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
62 {
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
63 int sockfd, portno, n;
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
64 struct sockaddr_in serv_addr;
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
65 char buffer[256];
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
66
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
67 if (argc != 2) {
1537
3fcfd8389ece dpidc usage
corvid <corvid@lavabit.com>
parents: 1535
diff changeset
68 print_usage(argv[0]);
1535
56cdbdb93af6 dpidc exit status 1 on error
corvid <corvid@lavabit.com>
parents: 1421
diff changeset
69 exit(1);
1394
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
70 }
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
71
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
72 /* Read dpid's port number from saved file */
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
73 if (Dpi_read_comm_keys(&portno) == -1) {
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
74 MSG_ERR("main: Can't read dpid's port number\n");
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
75 exit(1);
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
76 }
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
77
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
78 sockfd = socket(AF_INET, SOCK_STREAM, 0);
1591
cf7f2d3312fb trim some spaces
corvid <corvid@lavabit.com>
parents: 1545
diff changeset
79 if (sockfd < 0)
1394
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
80 error("ERROR opening socket");
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
81 bzero((char *) &serv_addr, sizeof(serv_addr));
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
82 serv_addr.sin_family = AF_INET;
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
83 serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
84
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
85 serv_addr.sin_port = htons(portno);
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
86 if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
87 error("ERROR connecting");
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
88
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
89 snprintf(buffer, sizeof(buffer), "<cmd='auth' msg='%s' '>", SharedKey);
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
90 n = write(sockfd, buffer, strlen(buffer));
1591
cf7f2d3312fb trim some spaces
corvid <corvid@lavabit.com>
parents: 1545
diff changeset
91 if (n < 0)
1394
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
92 error("ERROR writing to socket");
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
93
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
94 if (strcmp(argv[1], "stop") == 0) {
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
95 strcpy(buffer, CMD_STOP);
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
96 } else if (strcmp(argv[1], "register") == 0) {
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
97 strcpy(buffer, CMD_REGISTER);
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
98 } else if (strcmp(argv[1], "chat") == 0) {
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
99 printf("Please enter the message: ");
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
100 bzero(buffer,256);
1545
f02908cec744 check for fgets() return value - avoids a warning
Jorge Arellano Cid <jcid@dillo.org>
parents: 1537
diff changeset
101 if (fgets(buffer,255,stdin) == NULL)
f02908cec744 check for fgets() return value - avoids a warning
Jorge Arellano Cid <jcid@dillo.org>
parents: 1537
diff changeset
102 MSG_ERR("dpidc: Can't read the message\n");
1394
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
103 } else {
1537
3fcfd8389ece dpidc usage
corvid <corvid@lavabit.com>
parents: 1535
diff changeset
104 MSG_ERR("main: Unknown operation '%s'\n", argv[1]);
3fcfd8389ece dpidc usage
corvid <corvid@lavabit.com>
parents: 1535
diff changeset
105 print_usage(argv[0]);
1394
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
106 exit(1);
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
107 }
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
108
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
109 n = write(sockfd,buffer,strlen(buffer));
1591
cf7f2d3312fb trim some spaces
corvid <corvid@lavabit.com>
parents: 1545
diff changeset
110 if (n < 0)
1394
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
111 error("ERROR writing to socket");
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
112 /*
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
113 bzero(buffer,256);
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
114 n = read(sockfd,buffer,255);
1591
cf7f2d3312fb trim some spaces
corvid <corvid@lavabit.com>
parents: 1545
diff changeset
115 if (n < 0)
1394
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
116 error("ERROR reading from socket");
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
117 printf("%s\n",buffer);
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
118 */
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
119 close(sockfd);
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
120 return 0;
7aacf4954b04 Made dpidc a C language program
Jorge Arellano Cid <jcid@dillo.org>
parents:
diff changeset
121 }