1 #include <stdio.h>
2 #include <stdlib.h> /* for exit */
3 #include <string.h> /* for bzero */
4 #include <unistd.h> /* for read and write */
5 #include <ctype.h> /* for isxdigit */
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <netinet/in.h>
9 #include <netdb.h>
10 #include <errno.h>
11
12 #include "../dlib/dlib.h"
13 #include "../dpip/dpip.h"
14
15 #define MSG_ERR(...) printf("** ERROR **: " __VA_ARGS__);
16
17 char *CMD_REGISTER = "<cmd='register_all' '>";
18 char *CMD_STOP = "<cmd='DpiBye' '>";
19
20 static char SharedKey[32];
21
print_usage(const char * prgname)22 static void print_usage(const char *prgname)
23 {
24 fprintf(stderr,"Control program for the Dillo plugin daemon\n"
25 "Usage: %s {stop|register|chat}\n\n", prgname);
26 }
27
error(char * msg)28 static void error(char *msg)
29 {
30 perror(msg);
31 exit(1);
32 }
33
34 /*
35 * Read dpid's communication keys from its saved file.
36 * Return value: 1 on success, -1 on error.
37 */
Dpi_read_comm_keys(int * port)38 static int Dpi_read_comm_keys(int *port)
39 {
40 FILE *In;
41 char *fname, *rcline = NULL, *tail;
42 int i, ret = -1;
43
44 fname = dStrconcat(dGethomedir(), "/.dillo/dpid_comm_keys", NULL);
45 if ((In = fopen(fname, "r")) == NULL) {
46 MSG_ERR("[Dpi_read_comm_keys] %s\n", dStrerror(errno));
47 } else if ((rcline = dGetline(In)) == NULL) {
48 MSG_ERR("[Dpi_read_comm_keys] empty file: %s\n", fname);
49 } else {
50 *port = strtol(rcline, &tail, 10);
51 for (i = 0; *tail && isxdigit(tail[i+1]); ++i)
52 SharedKey[i] = tail[i+1];
53 SharedKey[i] = 0;
54 ret = 1;
55 }
56 dFree(rcline);
57 dFree(fname);
58
59 return ret;
60 }
61
main(int argc,char * argv[])62 int main(int argc, char *argv[])
63 {
64 int sockfd, portno, n;
65 struct sockaddr_in serv_addr;
66 char buffer[256];
67
68 if (argc != 2) {
69 print_usage(argv[0]);
70 exit(1);
71 }
72
73 /* Read dpid's port number from saved file */
74 if (Dpi_read_comm_keys(&portno) == -1) {
75 MSG_ERR("main: Can't read dpid's port number\n");
76 exit(1);
77 }
78
79 sockfd = socket(AF_INET, SOCK_STREAM, 0);
80 if (sockfd < 0)
81 error("ERROR opening socket");
82 bzero((char *) &serv_addr, sizeof(serv_addr));
83 serv_addr.sin_family = AF_INET;
84 serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
85
86 serv_addr.sin_port = htons(portno);
87 if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
88 error("ERROR connecting");
89
90 snprintf(buffer, sizeof(buffer), "<cmd='auth' msg='%s' '>", SharedKey);
91 n = write(sockfd, buffer, strlen(buffer));
92 if (n < 0)
93 error("ERROR writing to socket");
94
95 if (strcmp(argv[1], "stop") == 0) {
96 strcpy(buffer, CMD_STOP);
97 } else if (strcmp(argv[1], "register") == 0) {
98 strcpy(buffer, CMD_REGISTER);
99 } else if (strcmp(argv[1], "chat") == 0) {
100 printf("Please enter the message: ");
101 bzero(buffer,256);
102 if (fgets(buffer,255,stdin) == NULL)
103 MSG_ERR("dpidc: Can't read the message\n");
104 } else {
105 MSG_ERR("main: Unknown operation '%s'\n", argv[1]);
106 print_usage(argv[0]);
107 exit(1);
108 }
109
110 n = write(sockfd,buffer,strlen(buffer));
111 if (n < 0)
112 error("ERROR writing to socket");
113 /*
114 bzero(buffer,256);
115 n = read(sockfd,buffer,255);
116 if (n < 0)
117 error("ERROR reading from socket");
118 printf("%s\n",buffer);
119 */
120 dClose(sockfd);
121 return 0;
122 }
123