Skip to content

Commit

Permalink
1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-starke committed Aug 17, 2018
1 parent e19c1c4 commit 19d93b5
Show file tree
Hide file tree
Showing 12 changed files with 881 additions and 206 deletions.
2 changes: 1 addition & 1 deletion Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ PROJECT_NAME = tr64c
# could be handy for archiving the generated documentation or if some version
# control system is used.

PROJECT_NUMBER = 1.0.0
PROJECT_NUMBER = 1.1.0

# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a
Expand Down
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ Usage
Cache action descriptions of the device in this file.
-f, --format <string>
Defines the output format for queries. Possible values are:
CSV - comma-separated values (default)
TEXT - plain text (default)
CSV - comma-separated values
JSON - JavaScript Object Notation
XML - Extensible Markup Language
-h, --help
Expand Down Expand Up @@ -50,6 +51,23 @@ Usage
--version
Outputs the program version.

Example
=======

Scanning for available devices on local interface with IP address 192.168.178.10:

tr64c -o 192.168.178.10 -l

Listing possible action on FRITZ!Box 7490:

tr64c -o http://192.168.178.1:49000/tr64desc.xml -l

Obtaining some user interface properties from a FRITZ!Box 7490:

tr64c -o http://192.168.178.1:49000/tr64desc.xml -q UserInterface/GetInfo

Check also the binding example for Python [here](etc/tr64c.py).

Building
========

Expand All @@ -74,7 +92,7 @@ Files
|bsearch.* |Binary search algorithm.
|cvutf8.* |UTF-8 conversion functions.
|hmd5.* |MD5 hashing function.
|http.* |HTTP1.x header parser.
|http.* |HTTP/1.x parser.
|mingw-unicode.h|Unicode enabled main() for MinGW targets.
|parser.* |Text parsers and parser helpers.
|sax.* |SAX based XML parser.
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 1.0.0.{build}
version: 1.1.0.{build}

configuration:
- Release
Expand Down
12 changes: 12 additions & 0 deletions doc/Changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,17 @@ M.N.P
| +---- minor: increased if command-line syntax/semantic breaking changes were applied
+------ major: increased if elementary changes (from user's point of view) were made

1.1.0 (2018-08-17)
- added: HTTP common status message texts to error message outputs
- added: Python binding
- added: TEXT output format
- added: CSV, JSON and XML output to scan and list output
- changed: verbosity level of invalid command message in interactive mode from warning to error
- changed: ignore --utf8 for non-Unicode targets
- changed: command output in interactive mode to output an empty line at the end
- fixed: wrong verbosity level for some error and info messages
- fixed: missing flush on output in interactive mode
- fixed: conversion to/from UTF-8 for empty strings

1.0.0 (2018-08-14)
- first release
141 changes: 141 additions & 0 deletions etc/tr64c.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
"""
@file tr64c.py
@author Daniel Starke
@date 2018-08-15
@version 2018-08-17
DISCLAIMER
This file has no copyright assigned and is placed in the Public Domain.
All contributions are also assumed to be in the Public Domain.
Other contributions are not permitted.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
"""

from datetime import datetime
import json, re, subprocess


def quote(value):
""" Quotes and escapes the given value to pass it to tr64c """
esc = {
'\\': "\\\\",
'\n': "\\n",
'\r': "\\r",
'\t': "\\t",
'"' : "\\\"",
'\'': "\\'"
}
res = []
for c in value:
res.append(esc.get(c, c))
return '"' + ''.join(res) + '"'


def scan(app, localIf, timeout = 1000):
""" Scan for compliant devices at the given local network interface """
cmdLine = [app, "-o", localIf, "-t", str(timeout), "--utf8", "-f", "JSON", "-s"]
ctx = subprocess.Popen(cmdLine, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
res = []
while True:
line = ctx.stdout.readline().strip()
if line == "":
break
elif re.match(r"Error:.*", line) != None:
raise RuntimeError(line)
res.append(line)
res = ''.join(res)
if len(res) > 0:
return json.loads(res, encoding="UTF-8")
else:
return {}


def version(app):
""" Returns the version of the application """
cmdLine = [app, "--utf8", "--version"]
ctx = subprocess.Popen(cmdLine, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
line = ctx.stdout.readline().strip()
match = re.match(r"(([0-9]+)\.([0-9]+)\.([0-9]+)) ([^ ]+) (.+)", line)
if match == None:
return {}
return {'Version':match.group(1), 'Major':int(match.group(2)), 'Minor':int(match.group(3)), 'Patch':int(match.group(4)), 'Date':datetime.strptime(match.group(5), "%Y-%m-%d"), 'Backend': match.group(6)}


class Session:
""" TR-064 session instance """

def __init__(self, app, host, timeout = 1000, user = None, password = None, cache = None):
cmdLine = [app, "-o", host, "-t", str(timeout), "-f", "JSON"]
if user != None:
cmdLine.extend(["-u", user])
if password != None:
cmdLine.extend(["-p", password])
if cache != None:
cmdLine.extend(["-c", cache])
cmdLine.extend(["--utf8", "-i"])
self.ctx = subprocess.Popen(cmdLine, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

def __del__(self):
if self.ctx != None:
self.ctx.terminate()

def list(self):
""" Returns a list of possible actions and their arguments. """
self.ctx.stdin.write(b"list\n")
res = []
while self.ctx.poll() == None:
line = self.ctx.stdout.readline().strip()
if line == "":
break
elif re.match(r"Error:.*", line) != None:
raise RuntimeError(line)
res.append(line)
res = ''.join(res)
if len(res) > 0:
return json.loads(res, encoding="UTF-8")
else:
return {}

def query(self, action, args = []):
""" Queries an action and returns its result. """
for i, value in enumerate(args):
args[i] = quote(value)
self.ctx.stdin.write(b"query {} {}\n".format(action, ' '.join(args)))
res = []
while self.ctx.poll() == None:
line = self.ctx.stdout.readline().strip()
if line == "" or re.match(r"Error:.*", line) != None:
break
res.append(line);
res = ''.join(res)
if len(res) > 0:
return json.loads(res, encoding="UTF-8").values()[0]
else:
return {}


if __name__ == '__main__':
import sys, os.path
app = "../bin/tr64c.exe" if os.path.isfile("../bin/tr64c.exe") else "../bin/tr64c"
ver = version(app)
print "Using tr64c version {} with backend {}.".format(ver['Version'], ver['Backend'])
devices = scan(app, "192.168.178.25")
for dev in devices:
print "Found {} at {}.".format(dev['Device'], dev['URL'])
if len(devices) > 0:
session = Session(app, devices[0]['URL'])
record = session.list()
print "Found {} devices in {}.".format(len(record.values()[0]), record.keys()[0])
hostCount = session.query("Hosts/GetHostNumberOfEntries")['HostNumberOfEntries']
fmt = "{:17} {:15} {}"
print fmt.format("MAC", "IP", "Host")
for index in range(1, hostCount):
record = session.query("Hosts/GetGenericHostEntry", ["HostNumberOfEntries=" + str(index)])
print fmt.format(record['MACAddress'], record['IPAddress'], record['HostName'])
8 changes: 7 additions & 1 deletion src/cvutf8.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Daniel Starke
* @see cvutf8.h
* @date 2014-05-03
* @version 2018-07-14
* @version 2018-08-15
*
* DISCLAIMER
* This file has no copyright assigned and is placed in the Public Domain.
Expand Down Expand Up @@ -53,6 +53,9 @@ wchar_t * cvutf8_toUtf16N(const char * utf8, const size_t len) {
if (utf8 == (const char *)NULL) {
return (wchar_t *)NULL;
}
if (len == 0) {
return (wchar_t *)calloc(1, sizeof(wchar_t));
}
#ifdef PCF_IS_WIN
utf16Size = (size_t)MultiByteToWideChar(CP_UTF8, 0, utf8, (int)len, 0, 0);
if (utf16Size == 0xFFFD || utf16Size == 0) {
Expand Down Expand Up @@ -102,6 +105,9 @@ char * cvutf8_fromUtf16N(const wchar_t * utf16, const size_t len) {
if (utf16 == (const wchar_t *)NULL) {
return (char *)NULL;
}
if (len == 0) {
return (char *)calloc(1, sizeof(char));
}
#ifdef PCF_IS_WIN
utf8Size = (size_t)WideCharToMultiByte(CP_UTF8, 0, utf16, (int)len, 0, 0, NULL, NULL);
if (utf8Size == 0xFFFD || utf8Size == 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/getopt.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Daniel Starke
* @see getopt.h
* @date 2017-05-22
* @version 2018-08-13
* @version 2018-08-15
*
* DISCLAIMER
* This file has no copyright assigned and is placed in the Public Domain.
Expand All @@ -20,7 +20,7 @@
*/
#ifndef __LIBPCF_GETOPT_H__
#define __LIBPCF_GETOPT_H__
#define __GETOPT_H__ /* avoid collision with system getopt.h */
#define __GETOPT_H__ /* avoid collision with cygwins getopt.h include in sys/unistd.h */

#include "tchar.h"
#include "target.h"
Expand Down
32 changes: 26 additions & 6 deletions src/tr64c-posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @file tr64c_posix.c
* @author Daniel Starke
* @date 2018-06-21
* @version 2018-08-14
* @version 2018-08-17
*
* DISCLAIMER
* This file has no copyright assigned and is placed in the Public Domain.
Expand Down Expand Up @@ -428,7 +428,14 @@ static int discover(struct tTr64RequestCtx * ctx, const char * localIf, int (* v
case PHRT_SUCCESS:
ctx->status = response.status;
if (response.status != 200) {
if (ctx->verbose > 1) _ftprintf(ferr, MSGT(MSGT_ERR_HTTP_STATUS), (unsigned)response.status);
if (ctx->verbose > 1) {
const tHttpStatusMsg * item = (const tHttpStatusMsg *)bs_staticArray(&(response.status), httpStatMsg, cmpHttpStatusMsg);
if (item != NULL) {
_ftprintf(ferr, MSGT(MSGT_ERR_HTTP_STATUS_STR), (unsigned)response.status, item->string);
} else {
_ftprintf(ferr, MSGT(MSGT_ERR_HTTP_STATUS), (unsigned)response.status);
}
}
break;
} else if (response.content.start != NULL && response.content.start != ctx->buffer && response.content.length > 0) {
ctx->content = (char *)response.content.start;
Expand Down Expand Up @@ -609,6 +616,7 @@ static int request(tTr64RequestCtx * ctx) {
#endif
if (ctx->verbose > 0) _ftprintf(ferr, MSGT(MSGT_ERR_SOCK_SEND_TOUT));
if (ctx->verbose > 1) printLastError(ferr);
ctx->status = 408;
goto onError;
break;
default:
Expand Down Expand Up @@ -701,7 +709,14 @@ static int request(tTr64RequestCtx * ctx) {
httpAuthentication(ctx, &response);
goto onError;
} else if (response.status != 200) {
if (ctx->verbose > 1) _ftprintf(ferr, MSGT(MSGT_ERR_HTTP_STATUS), (unsigned)response.status);
if (ctx->verbose > 1) {
const tHttpStatusMsg * item = (const tHttpStatusMsg *)bs_staticArray(&(response.status), httpStatMsg, cmpHttpStatusMsg);
if (item != NULL) {
_ftprintf(ferr, MSGT(MSGT_ERR_HTTP_STATUS_STR), (unsigned)response.status, item->string);
} else {
_ftprintf(ferr, MSGT(MSGT_ERR_HTTP_STATUS), (unsigned)response.status);
}
}
goto onError;
} else if (response.content.start != NULL && response.content.start != ctx->buffer && response.content.length > 0) {
ctx->content = (char *)response.content.start;
Expand Down Expand Up @@ -732,6 +747,7 @@ static int request(tTr64RequestCtx * ctx) {
if (val > (uint64_t)(ctx->timeout)) {
if (ctx->verbose > 0) _ftprintf(ferr, MSGT(MSGT_ERR_SOCK_RECV_TOUT));
if (ctx->verbose > 1) printLastError(ferr);
ctx->status = 408;
goto onError; /* incomplete */
}
if (signalReceived != 0) goto onError;
Expand Down Expand Up @@ -841,11 +857,14 @@ static void printAddresses(const tTr64RequestCtx * ctx, FILE * fd) {
* Create a new HTTP request context based on the given URL.
*
* @param[in] url - base on this URL
* @param[in] user - user name
* @param[in] pass - password
* @param[in] format - output format type
* @param[in] timeout - network timeouts in milliseconds
* @param[in] verbose - verbosity level
* @return Handle on success, else NULL.
*/
tTr64RequestCtx * newTr64Request(const char * url, const char * user, const char * pass, const size_t timeout, const int verbose) {
tTr64RequestCtx * newTr64Request(const char * url, const char * user, const char * pass, const tFormat format, const size_t timeout, const int verbose) {
if (verbose > 3) _ftprintf(ferr, MSGT(MSGT_DBG_ENTER_NEWTR64REQUEST));
if (url == NULL) return NULL;

Expand All @@ -867,19 +886,20 @@ tTr64RequestCtx * newTr64Request(const char * url, const char * user, const char
if (res->user != NULL) free(res->user);
res->user = strdup(user);
if (res->user == NULL) {
if (verbose > 1) _ftprintf(ferr, MSGT(MSGT_ERR_NO_MEM));
if (verbose > 0) _ftprintf(ferr, MSGT(MSGT_ERR_NO_MEM));
goto onError;
}
}
if (pass != NULL) {
if (res->pass != NULL) free(res->pass);
res->pass = strdup(pass);
if (res->pass == NULL) {
if (verbose > 1) _ftprintf(ferr, MSGT(MSGT_ERR_NO_MEM));
if (verbose > 0) _ftprintf(ferr, MSGT(MSGT_ERR_NO_MEM));
goto onError;
}
}

res->format = format;
res->timeout = timeout;

res->discover = discover;
Expand Down
Loading

0 comments on commit 19d93b5

Please sign in to comment.