[Freeswitch-svn] [commit] r3542 - freeswitch/branches/igorneves/scripts/socket
Freeswitch SVN
igorneves at freeswitch.org
Tue Dec 5 12:41:31 EST 2006
Author: igorneves
Date: Tue Dec 5 12:41:31 2006
New Revision: 3542
Added:
freeswitch/branches/igorneves/scripts/socket/fsconsole.c
Log:
_Vile version of C socket example
Added: freeswitch/branches/igorneves/scripts/socket/fsconsole.c
==============================================================================
--- (empty file)
+++ freeswitch/branches/igorneves/scripts/socket/fsconsole.c Tue Dec 5 12:41:31 2006
@@ -0,0 +1,306 @@
+/*
+ * FreeSWITCH Console
+ */
+
+#include <ctype.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+#define buflen 38
+#define NUL '\0'
+
+char *app = "freeswitch";
+char *version = "0.1";
+char *hostname = "127.0.0.1";
+char *secret = "ClueCon";
+unsigned int port = 8021;
+
+char *intro = "Welcome to FreeSWITCH Console.\n\n";
+
+char buffer[buflen], *command;
+int sd, flag, prompt;
+
+char *trim(char *str);
+void print_tokens(char *line);
+int fs_switch_read();
+
+// Debug level
+int debug = 3;
+
+int main(int argc, char *argv[])
+{
+ if (argv[1] == "--help") {
+ fprintf(stderr,"usage %s hostname port\n", argv[0]);
+ exit(0);
+ }
+
+ /* Hostname and Port defined on command line */
+ if (argc > 1) {
+ strncpy(hostname, argv[1], sizeof(hostname));
+ hostname = argv[1];
+ port = atoi(argv[2]);
+ }
+
+ sd = socket(AF_INET, SOCK_STREAM, 0); /* init socket descriptor */
+ struct sockaddr_in sin;
+ struct hostent *host = gethostbyname(hostname);
+
+ //char buffer[buflen];
+ int len;
+ char *p;
+ char cmd[38];
+ int i, n, out;
+
+ /*** PLACE DATA IN sockaddr_in struct ***/
+ memcpy(&sin.sin_addr.s_addr, host->h_addr, host->h_length);
+ sin.sin_family = AF_INET;
+ sin.sin_port = htons(port);
+
+ /*** CONNECT SOCKET TO THE SERVICE DESCRIBED BY sockaddr_in struct ***/
+ if (connect(sd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
+ {
+ perror("connecting");
+ exit(1);
+ }
+
+ flag = fcntl(sd, F_GETFL, 0);
+ flag |= O_NONBLOCK;
+ fcntl(sd, F_SETFL, flag);
+
+ printf(intro);
+
+ /* Setup the prompt */
+ prompt = 1;
+
+ while(1)
+ {
+
+ /* Prompt */
+ if (prompt)
+ {
+ printf("%s %s> ", app, version);
+ prompt = 0;
+ }
+
+ memset(buffer, 0, sizeof(buffer));
+ memset(cmd, 0, sizeof(cmd));
+
+ /* Setup the first read */
+ fs_switch_read(sd, buffer);
+
+ flag = fcntl(0, F_GETFL, 0);
+ flag |= O_NONBLOCK;
+ fcntl(0, F_SETFL, flag);
+
+ fgets(cmd, 36, stdin);
+ cmd[strlen(cmd) - 1] = 0;
+
+ if (!strcmp(cmd, "exit")) {
+ printf("Exiting.. goodbye\n");
+ exit(0);
+ }
+
+ if (!strcmp(cmd, "version")) {
+ printf("Version %s\n", version);
+ prompt = 1;
+ }
+
+ if (!strcmp(cmd, "\n")) {
+ prompt = 1;
+ }
+
+ if (fs_switch_read() > 0) {
+ // Do nothing
+ } else {
+ // Sleep
+ sleep(1);
+ }
+ }
+
+ close(sd);
+}
+
+
+
+int fs_switch_read()
+ {
+
+ int len;
+ char buf[4096], reply[4096], tmp[255], contents[4096];
+
+ char content[32];
+ char content_type[32];
+ char content_following[32];
+ char content_after[32];
+ char junk[32];
+
+ len = atoi(tmp);
+ if (len <= 0) len += 255;
+ len = recv(sd, buffer, len, 0);
+
+ if (len > 0)
+ {
+ memset(contents, 0, sizeof(contents));
+ strncpy(contents, buffer, len);
+
+ // Print Incoming input to screen
+ if (debug > 0)
+ write(1,contents,len);
+
+ char whitespace[] = "\r";
+ char *token, *token_split, *subtoken_split;
+ char token_key[32], token_val[32], token_contents[4096], subtoken_contents[4096];
+ char subtoken_key[32], subtoken_val[32];
+
+ //print_tokens(contents);
+
+ for (token=strtok(contents,whitespace); token !=NULL; token=strtok(NULL,whitespace))
+ {
+
+ //printf("Next token is %s\n", token);
+
+ sprintf(token_contents, "%s", token);
+
+ token_split = strtok(token_contents, ":");
+ sprintf(token_key, "%s", token_split);
+ token_split = strtok(NULL, "\n");
+ sprintf(token_val, "%s", token_split);
+ token_split = strtok(NULL, "\n");
+
+ token = strtok(NULL, "\n\n");
+
+ trim(token_val);
+
+ if (debug > 1)
+ printf("\nkey: '%s', val: '%s'\n", token_key, token_val);
+
+
+ if (strcmp(token_key, "Content-Type") == 0)
+ {
+
+ if (strcmp(token_val, "auth/request") == 0)
+ {
+ printf("\nAuthenticating...\n");
+
+ command = "auth";
+ sprintf(buf, "auth %s\n\n", secret);
+ write(sd, buf, strlen(buf));
+
+ } else if (strcmp(token_val, "command/reply") == 0) {
+
+ /* Parse the reply */
+ sprintf(subtoken_contents, "%s", token_split);
+
+ subtoken_split = strtok(subtoken_contents, ":");
+ sprintf(subtoken_key, "%s", subtoken_split);
+ subtoken_split = strtok(NULL, "\n");
+ sprintf(subtoken_val, "%s", subtoken_split);
+ subtoken_split = strtok(NULL, "\n");
+
+ trim(subtoken_val);
+
+ if (debug > 1)
+ printf("subkey: '%s', subval: '%s'\n", subtoken_key, subtoken_val);
+
+ if (strcmp(subtoken_key, "Reply-Text") == 0)
+ {
+ if (strcmp(subtoken_val, "+OK accepted") == 0)
+ {
+ if (strcmp(command, "auth") == 0)
+ {
+ printf("\nAuthentication Successful\n");
+ command = "";
+ }
+ }
+ }
+
+ }
+ }
+
+
+ }
+ /* Set the prompt */
+ prompt = 1;
+
+ return 1;
+ } else {
+ return 0;
+ }
+
+}
+
+
+void print_tokens(char *line)
+{
+ char whitespace[] = "\n";
+ char *token;
+
+ for (token=strtok(line,whitespace); token !=NULL; token=strtok(NULL,whitespace))
+ {
+ printf("Next token is %s\n", token);
+ }
+}
+
+
+char *trim(char *str)
+{
+ char *ibuf = str, *obuf = str;
+ int i = 0, cnt = 0;
+
+ /*
+ ** Trap NULL
+ */
+
+ if (str)
+ {
+ /*
+ ** Remove leading spaces (from RMLEAD.C)
+ */
+
+ for (ibuf = str; *ibuf && isspace(*ibuf); ++ibuf)
+ ;
+ if (str != ibuf)
+ memmove(str, ibuf, ibuf - str);
+
+ /*
+ ** Collapse embedded spaces (from LV1WS.C)
+ */
+
+ while (*ibuf)
+ {
+ if (isspace(*ibuf) && cnt)
+ ibuf++;
+ else
+ {
+ if (!isspace(*ibuf))
+ cnt = 0;
+ else
+ {
+ *ibuf = ' ';
+ cnt = 1;
+ }
+ obuf[i++] = *ibuf++;
+ }
+ }
+ obuf[i] = NUL;
+
+ /*
+ ** Remove trailing spaces (from RMTRAIL.C)
+ */
+
+ while (--i >= 0)
+ {
+ if (!isspace(obuf[i]))
+ break;
+ }
+ obuf[++i] = NUL;
+ }
+ return str;
+}
+
+
More information about the Freeswitch-svn
mailing list