Dillo v3.1.1-98-g318d1f14
Loading...
Searching...
No Matches
dpidc.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2009-2013 Jorge Arellano Cid <jcid@dillo.org>
3 * Copyright (C) 2024 Rodrigo Arias Mallo <rodarima@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 */
10
11#include <stdio.h>
12#include <stdlib.h> /* for exit */
13#include <string.h> /* for memset */
14#include <unistd.h> /* for read and write */
15#include <ctype.h> /* for isxdigit */
16#include <sys/types.h>
17#include <sys/socket.h>
18#include <netinet/in.h>
19#include <arpa/inet.h>
20#include <netdb.h>
21#include <errno.h>
22
23#include "../dlib/dlib.h"
24#include "../dpip/dpip.h"
25
26#define MSG_ERR(...) printf("** ERROR **: " __VA_ARGS__);
27
28char *CMD_REGISTER = "<cmd='register_all' '>";
29char *CMD_STOP = "<cmd='DpiBye' '>";
30
31static char SharedKey[32];
32
33static void print_usage(const char *prgname)
34{
35 fprintf(stderr,"Control program for the Dillo plugin daemon\n"
36 "Usage: %s {stop|register|chat}\n\n", prgname);
37}
38
39static void error(char *msg)
40{
41 perror(msg);
42 exit(1);
43}
44
49static int Dpi_read_comm_keys(int *port)
50{
51 FILE *In;
52 char *fname, *rcline = NULL, *tail;
53 int i, ret = -1;
54
55 fname = dStrconcat(dGethomedir(), "/.dillo/dpid_comm_keys", NULL);
56 if ((In = fopen(fname, "r")) == NULL) {
57 MSG_ERR("[Dpi_read_comm_keys] %s\n", dStrerror(errno));
58 } else if ((rcline = dGetline(In)) == NULL) {
59 MSG_ERR("[Dpi_read_comm_keys] empty file: %s\n", fname);
60 } else {
61 *port = strtol(rcline, &tail, 10);
62 for (i = 0; *tail && isxdigit(tail[i+1]); ++i)
63 SharedKey[i] = tail[i+1];
64 SharedKey[i] = 0;
65 ret = 1;
66 }
67 dFree(rcline);
68 dFree(fname);
69
70 return ret;
71}
72
73int main(int argc, char *argv[])
74{
75 int sockfd, portno, n;
76 struct sockaddr_in serv_addr;
77 char buffer[256];
78
79 if (argc != 2) {
80 print_usage(argv[0]);
81 exit(1);
82 }
83
84 /* Read dpid's port number from saved file */
85 if (Dpi_read_comm_keys(&portno) == -1) {
86 MSG_ERR("main: Can't read dpid's port number\n");
87 exit(1);
88 }
89
90 sockfd = socket(AF_INET, SOCK_STREAM, 0);
91 if (sockfd < 0)
92 error("ERROR opening socket");
93 memset(&serv_addr, 0, sizeof(serv_addr));
94 serv_addr.sin_family = AF_INET;
95 serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
96
97 serv_addr.sin_port = htons(portno);
98 if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
99 error("ERROR connecting");
100
101 snprintf(buffer, sizeof(buffer), "<cmd='auth' msg='%s' '>", SharedKey);
102 n = write(sockfd, buffer, strlen(buffer));
103 if (n < 0)
104 error("ERROR writing to socket");
105
106 if (strcmp(argv[1], "stop") == 0) {
107 strcpy(buffer, CMD_STOP);
108 } else if (strcmp(argv[1], "register") == 0) {
109 strcpy(buffer, CMD_REGISTER);
110 } else if (strcmp(argv[1], "chat") == 0) {
111 printf("Please enter the message: ");
112 memset(buffer,0,256);
113 if (fgets(buffer,255,stdin) == NULL)
114 MSG_ERR("dpidc: Can't read the message\n");
115 } else {
116 MSG_ERR("main: Unknown operation '%s'\n", argv[1]);
117 print_usage(argv[0]);
118 exit(1);
119 }
120
121 n = write(sockfd,buffer,strlen(buffer));
122 if (n < 0)
123 error("ERROR writing to socket");
124/*
125 memset(buffer,0,256);
126 n = read(sockfd,buffer,255);
127 if (n < 0)
128 error("ERROR reading from socket");
129 printf("%s\n",buffer);
130*/
131 dClose(sockfd);
132 return 0;
133}
int main(void)
Definition bookmarks.c:1613
char * dGetline(FILE *stream)
Get a line from a FILE stream.
Definition dlib.c:928
char * dStrconcat(const char *s1,...)
Concatenate a NULL-terminated list of strings.
Definition dlib.c:102
void dFree(void *mem)
Definition dlib.c:68
int dClose(int fd)
Close a FD handling EINTR.
Definition dlib.c:951
char * dGethomedir(void)
Return the home directory in a static string (don't free)
Definition dlib.c:906
#define dStrerror
Definition dlib.h:95
static char SharedKey[32]
Definition dpidc.c:31
#define MSG_ERR(...)
Definition dpidc.c:26
char * CMD_REGISTER
Definition dpidc.c:28
static int Dpi_read_comm_keys(int *port)
Read dpid's communication keys from its saved file.
Definition dpidc.c:49
static void print_usage(const char *prgname)
Definition dpidc.c:33
static void error(char *msg)
Definition dpidc.c:39
char * CMD_STOP
Definition dpidc.c:29