Dillo v3.2.0-151-g90488cbf
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 <sys/types.h>
16#include <sys/socket.h>
17#include <netinet/in.h>
18#include <arpa/inet.h>
19#include <netdb.h>
20#include <errno.h>
21
22#include "dlib/dlib.h" /* dIsxdigit */
23#include "dpip/dpip.h"
24
25#define MSG_ERR(...) printf("** ERROR **: " __VA_ARGS__);
26
27char *CMD_REGISTER = "<cmd='register_all' '>";
28char *CMD_STOP = "<cmd='DpiBye' '>";
29
30static char SharedKey[32];
31
32static void print_usage(const char *prgname)
33{
34 fprintf(stderr,"Control program for the Dillo plugin daemon\n"
35 "Usage: %s {stop|register|chat}\n\n", prgname);
36}
37
38static void error(char *msg)
39{
40 perror(msg);
41 exit(1);
42}
43
48static int Dpi_read_comm_keys(int *port)
49{
50 FILE *In;
51 char *fname, *rcline = NULL, *tail;
52 int i, ret = -1;
53
54 fname = dStrconcat(dGethomedir(), "/.dillo/dpid_comm_keys", NULL);
55 if ((In = fopen(fname, "r")) == NULL) {
56 MSG_ERR("[Dpi_read_comm_keys] %s\n", dStrerror(errno));
57 } else if ((rcline = dGetline(In)) == NULL) {
58 MSG_ERR("[Dpi_read_comm_keys] empty file: %s\n", fname);
59 } else {
60 *port = strtol(rcline, &tail, 10);
61 for (i = 0; *tail && dIsxdigit(tail[i+1]); ++i)
62 SharedKey[i] = tail[i+1];
63 SharedKey[i] = 0;
64 ret = 1;
65 }
66 dFree(rcline);
67 dFree(fname);
68
69 return ret;
70}
71
72int main(int argc, char *argv[])
73{
74 int sockfd, portno, n;
75 struct sockaddr_in serv_addr;
76 char buffer[256];
77
78 if (argc != 2) {
79 print_usage(argv[0]);
80 exit(1);
81 }
82
83 /* Read dpid's port number from saved file */
84 if (Dpi_read_comm_keys(&portno) == -1) {
85 MSG_ERR("main: Can't read dpid's port number\n");
86 exit(1);
87 }
88
89 sockfd = socket(AF_INET, SOCK_STREAM, 0);
90 if (sockfd < 0)
91 error("ERROR opening socket");
92 memset(&serv_addr, 0, sizeof(serv_addr));
93 serv_addr.sin_family = AF_INET;
94 serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
95
96 serv_addr.sin_port = htons(portno);
97 if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
98 error("ERROR connecting");
99
100 snprintf(buffer, sizeof(buffer), "<cmd='auth' msg='%s' '>", SharedKey);
101 n = write(sockfd, buffer, strlen(buffer));
102 if (n < 0)
103 error("ERROR writing to socket");
104
105 if (strcmp(argv[1], "stop") == 0) {
106 strcpy(buffer, CMD_STOP);
107 } else if (strcmp(argv[1], "register") == 0) {
108 strcpy(buffer, CMD_REGISTER);
109 } else if (strcmp(argv[1], "chat") == 0) {
110 printf("Please enter the message: ");
111 memset(buffer,0,256);
112 if (fgets(buffer,255,stdin) == NULL)
113 MSG_ERR("dpidc: Can't read the message\n");
114 } else {
115 MSG_ERR("main: Unknown operation '%s'\n", argv[1]);
116 print_usage(argv[0]);
117 exit(1);
118 }
119
120 n = write(sockfd,buffer,strlen(buffer));
121 if (n < 0)
122 error("ERROR writing to socket");
123/*
124 memset(buffer,0,256);
125 n = read(sockfd,buffer,255);
126 if (n < 0)
127 error("ERROR reading from socket");
128 printf("%s\n",buffer);
129*/
130 dClose(sockfd);
131 return 0;
132}
int main(void)
Definition bookmarks.c:1612
char * dGetline(FILE *stream)
Get a line from a FILE stream.
Definition dlib.c:955
char * dStrconcat(const char *s1,...)
Concatenate a NULL-terminated list of strings.
Definition dlib.c:101
void dFree(void *mem)
Definition dlib.c:67
int dClose(int fd)
Close a FD handling EINTR.
Definition dlib.c:978
char * dGethomedir(void)
Return the home directory in a static string (don't free)
Definition dlib.c:933
#define dStrerror
Definition dlib.h:124
static int dIsxdigit(unsigned char c)
Definition dlib.h:54
static char SharedKey[32]
Definition dpidc.c:30
#define MSG_ERR(...)
Definition dpidc.c:25
char * CMD_REGISTER
Definition dpidc.c:27
static int Dpi_read_comm_keys(int *port)
Read dpid's communication keys from its saved file.
Definition dpidc.c:48
static void print_usage(const char *prgname)
Definition dpidc.c:32
static void error(char *msg)
Definition dpidc.c:38
char * CMD_STOP
Definition dpidc.c:28