changeset 1394:7aacf4954b04

Made dpidc a C language program
author Jorge Arellano Cid <jcid@dillo.org>
date Sun, 01 Nov 2009 16:31:59 -0300
parents dcf1ec78121a
children da16ca154e64
files dpid/Makefile.am dpid/dpidc dpid/dpidc.c
diffstat 3 files changed, 119 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/dpid/Makefile.am	Sun Nov 01 16:31:59 2009 -0300
+++ b/dpid/Makefile.am	Sun Nov 01 16:31:59 2009 -0300
@@ -1,10 +1,10 @@
 AM_CPPFLAGS=-DDPIDRC_SYS='"$(sysconfdir)/dpidrc"'
 
-bin_PROGRAMS = dpid
+bin_PROGRAMS = dpid dpidc
 dpid_LDADD = ../dpip/libDpip.a ../dlib/libDlib.a
+dpidc_LDADD = ../dpip/libDpip.a ../dlib/libDlib.a
 
-EXTRA_DIST = dpidc dpidrc.in
-bin_SCRIPTS = dpidc
+EXTRA_DIST = dpidrc.in
 
 dpid_SOURCES = \
 	dpi.h \
@@ -19,6 +19,8 @@
 	main.c \
 	misc_new.c
 
+dpidc_SOURCES = dpidc.c
+
 sysconf_DATA = dpidrc
 CLEANFILES = $(sysconf_DATA)
 
--- a/dpid/dpidc	Sun Nov 01 16:31:59 2009 -0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,37 +0,0 @@
-#!/usr/bin/perl -w
-# Author: Ferdi Franceschini
-#
-# dpid control program
-# Currently allows
-# register: Tells dpid to register all available dpis
-# stop: Stops dpid.
-
-use strict;
-use IO::Socket::INET;
-
-# Get socket directory name
-#open(DSD, "<$ENV{HOME}/.dillo/dpi_socket_dir");
-#my $dir = <DSD>;
-#close(DSD);
-
-# Get dpid's listening port from saved file
-open(DSD, "<$ENV{HOME}/.dillo/dpid_comm_keys");
-my $port = <DSD>;
-close(DSD);
-print "Got: localhost:$port\n";
-
-my $socket = IO::Socket::INET->new(Peer => "localhost:$port", Type => SOCK_STREAM, Timeout => 1000 ) or die "new: $@";
-
-$socket->autoflush(1);
-
-my %dpi_command = ( 
-        "register" => "<dpi cmd='register_all' '>",
-        "stop" => "<dpi cmd='DpiBye' '>",
-        );
-
-if ( $#ARGV == 0 && exists($dpi_command{$ARGV[0]}) ) {
-        print $socket $dpi_command{$ARGV[0]};
-} else {
-        close($socket);
-        print "Usage: dpidc register|stop\n";
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dpid/dpidc.c	Sun Nov 01 16:31:59 2009 -0300
@@ -0,0 +1,114 @@
+#include <stdio.h>
+#include <stdlib.h>  /* for exit */
+#include <string.h>  /* for bzero */
+#include <unistd.h>  /* for read and write */
+#include <ctype.h>   /* for isxdigit */
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h> 
+#include <errno.h>
+
+#include "../dpip/dpip.h"
+
+#define MSG_ERR(...) printf("** ERROR **: " __VA_ARGS__);
+
+char *CMD_REGISTER = "<cmd='register_all' '>";
+char *CMD_STOP     = "<cmd='DpiBye' '>";
+
+static char SharedKey[32];
+
+
+void error(char *msg)
+{
+    perror(msg);
+    exit(0);
+}
+
+/*
+ * Read dpid's communication keys from its saved file.
+ * Return value: 1 on success, -1 on error.
+ */
+static int Dpi_read_comm_keys(int *port)
+{
+   FILE *In;
+   char *fname, *rcline = NULL, *tail;
+   int i, ret = -1;
+
+   fname = dStrconcat(dGethomedir(), "/.dillo/dpid_comm_keys", NULL);
+   if ((In = fopen(fname, "r")) == NULL) {
+      MSG_ERR("[Dpi_read_comm_keys] %s\n", dStrerror(errno));
+   } else if ((rcline = dGetline(In)) == NULL) {
+      MSG_ERR("[Dpi_read_comm_keys] empty file: %s\n", fname);
+   } else {
+      *port = strtol(rcline, &tail, 10);
+      for (i = 0; *tail && isxdigit(tail[i+1]); ++i)
+         SharedKey[i] = tail[i+1];
+      SharedKey[i] = 0;
+      ret = 1;
+   }
+   dFree(rcline);
+   dFree(fname);
+
+   return ret;
+}
+
+int main(int argc, char *argv[])
+{
+    int sockfd, portno, n;
+    struct sockaddr_in serv_addr;
+    char buffer[256];
+
+    if (argc != 2) {
+       fprintf(stderr,"\nUsage:\n %s {stop|register|chat}\n\n", argv[0]);
+       exit(0);
+    }
+
+    /* Read dpid's port number from saved file */
+    if (Dpi_read_comm_keys(&portno) == -1) {
+       MSG_ERR("main: Can't read dpid's port number\n");
+       exit(1);
+    }
+
+    sockfd = socket(AF_INET, SOCK_STREAM, 0);
+    if (sockfd < 0) 
+        error("ERROR opening socket");
+    bzero((char *) &serv_addr, sizeof(serv_addr));
+    serv_addr.sin_family = AF_INET;
+    serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+
+    serv_addr.sin_port = htons(portno);
+    if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
+        error("ERROR connecting");
+
+    snprintf(buffer, sizeof(buffer), "<cmd='auth' msg='%s' '>", SharedKey);
+    n = write(sockfd, buffer, strlen(buffer));
+    if (n < 0) 
+         error("ERROR writing to socket");
+
+    if (strcmp(argv[1], "stop") == 0) {
+       strcpy(buffer, CMD_STOP);
+    } else if (strcmp(argv[1], "register") == 0) {
+       strcpy(buffer, CMD_REGISTER);
+    } else if (strcmp(argv[1], "chat") == 0) {
+       printf("Please enter the message: ");
+       bzero(buffer,256);
+       fgets(buffer,255,stdin);
+    } else {
+       MSG_ERR("main: Unknown operation '%s'\n", argv[1]);
+       exit(1);
+    }
+
+    n = write(sockfd,buffer,strlen(buffer));
+    if (n < 0) 
+         error("ERROR writing to socket");
+/*
+    bzero(buffer,256);
+    n = read(sockfd,buffer,255);
+    if (n < 0) 
+         error("ERROR reading from socket");
+    printf("%s\n",buffer);
+*/
+    close(sockfd);
+    return 0;
+}