diff options
author | Jorge Arellano Cid <jcid@dillo.org> | 2009-11-01 16:31:59 -0300 |
---|---|---|
committer | Jorge Arellano Cid <jcid@dillo.org> | 2009-11-01 16:31:59 -0300 |
commit | 0e9c6dd6196594da49bec4a6caea4d0886b54602 (patch) | |
tree | 24a348d46b38e97cb325fc1af1e02ff881db88e3 /dpid/dpidc.c | |
parent | 3640d724b5073ed6ecb10b8c6ba4a68c9d78749a (diff) |
Made dpidc a C language program
Diffstat (limited to 'dpid/dpidc.c')
-rw-r--r-- | dpid/dpidc.c | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/dpid/dpidc.c b/dpid/dpidc.c new file mode 100644 index 00000000..f4accaf2 --- /dev/null +++ b/dpid/dpidc.c @@ -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; +} |