Subversion Repositories oidinfo_api

Compare Revisions

Regard whitespace Rev 13 → Rev 14

/trunk/csv_lookup_server_example/oid_lookup_srv
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
/trunk/csv_lookup_server_example/oid_lookup_srv.cpp
1,6 → 1,6
// **************************************************
// ** OID CSV Lookup Server 1.1 **
// ** (c) 2016-2019 ViaThinkSoft, Daniel Marschall **
// ** OID CSV Lookup Server 1.2 **
// ** (c) 2016-2020 ViaThinkSoft, Daniel Marschall **
// **************************************************
 
// todo: log verbosity + datetime
8,6 → 8,7
// todo: server hat sich einfach so beendet... "read: connection reset by peer"
// todo: 2019-02-24 service was booted together with the system, and i got "0 OIDs loaded". why???
// todo: create vnag monitor that checks if this service is OK
// todo: unexplained crash with version 1.1 : Signal "pipefail" (141) received after someone connected?!
 
#include "oid_lookup_srv.h"
 
49,7 → 50,9
char buffer[MAXMSG];
int nbytes;
 
nbytes = read(filedes, buffer, MAXMSG);
nbytes = read(filedes, buffer, sizeof(buffer));
buffer[sizeof(buffer)-1] = 0; // Terminator
 
if (nbytes < 0) {
/* Read error. */
//perror("read");
61,9 → 64,12
} else {
/* Data read. */
 
for (int i=0; i<MAXMSG; ++i) {
if ((buffer[i] == 13) || (buffer[i] == 10)) buffer[i] = 0;
for (uint i=0; i<sizeof(buffer); ++i) {
if ((buffer[i] == 13) || (buffer[i] == 10)) {
buffer[i] = 0; // Terminator
break;
}
}
 
if (strcmp(buffer, "bye") == 0) {
fprintf(stdout, "%s:%d[%d] Client said good bye.\n", inet_ntoa(cons[filedes].clientname.sin_addr), ntohs(cons[filedes].clientname.sin_port), filedes);
71,6 → 77,14
} else {
cons[filedes].queries++;
 
for (uint i=0; i<sizeof(buffer); ++i) {
if (buffer[i] == 0) break;
if (!((buffer[i] >= '0') && (buffer[i] <= '9')) && !(buffer[i] == '.')) {
fprintf(stdout, "%s:%d[%d] Client sent an invalid request.\n", inet_ntoa(cons[filedes].clientname.sin_addr), ntohs(cons[filedes].clientname.sin_port), filedes);
return -1;
}
}
 
// fprintf(stdout, "%s:%d[%d] Query #%d: %s\n", inet_ntoa(cons[filedes].clientname.sin_addr), ntohs(cons[filedes].clientname.sin_port), filedes, cons[filedes].queries, buffer);
 
if (stringAvailable(buffer)) {
139,7 → 153,7
int sock;
fd_set active_fd_set, read_fd_set;
 
fprintf(stdout, "OID CSV Lookup Server 1.0 (c)2016-2019 ViaThinkSoft\n");
fprintf(stdout, "OID CSV Lookup Server 1.2 (c)2016-2020 ViaThinkSoft\n");
fprintf(stdout, "Listening at port: %d\n", PORT);
fprintf(stdout, "Max connections: %d\n", FD_SETSIZE);
 
210,6 → 224,12
fprintf(stdout, "%s:%d[%d] Connected\n", inet_ntoa(clientname.sin_addr), ntohs(clientname.sin_port), new_fd);
}
 
if (new_fd >= FD_SETSIZE) {
fprintf(stderr, "%s:%d[%d] new_fd reached cons[FD_SETSIZE] limit\n", inet_ntoa(clientname.sin_addr), ntohs(clientname.sin_port), new_fd);
close(new_fd);
FD_CLR(new_fd, &active_fd_set);
}
 
cons[new_fd].last_activity = time(NULL);
cons[new_fd].queries = 0;
} else {
/trunk/oid_utils.inc.phps
825,7 → 825,7
// Apply standardized identifiers (case sensitive)
$asn .= '.';
foreach ($standardized as $s => $r) {
$asn = preg_replace("|^$s|", $r, $asn);
$asn = preg_replace("@^".preg_quote($s,"@")."@", $r, $asn);
}
$asn = substr($asn, 0, strlen($asn)-1);
 
/trunk/oidinfo_api.inc.phps
18,7 → 18,7
* limitations under the License.
*/
 
error_reporting(E_ALL | E_NOTICE | E_STRICT | E_DEPRECATED);
// error_reporting(E_ALL | E_NOTICE | E_STRICT | E_DEPRECATED);
 
if(!defined('STDIN')) define('STDIN', fopen('php://stdin', 'rb'));
if(!defined('STDOUT')) define('STDOUT', fopen('php://stdout', 'wb'));
1021,7 → 1021,7
$in = "${oid}\n\0"; // PHP's socket_send() does not send a trailing \n . There needs to be something after the \n ... :(
 
if ($failcount >= self::SPP_MAX_CONNECTION_ATTEMPTS) {
echo "Query $oid: CONNECTION FAILED!\n";
echo "Query $oid: CONNECTION TO SIMPLE PING PROVIDER FAILED!\n";
return null;
}
 
/trunk/uuid_utils.inc.phps
3,7 → 3,7
/*
* UUID utils for PHP
* Copyright 2011-2020 Daniel Marschall, ViaThinkSoft
* Version 2020-09-12
* Version 2020-11-14
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
20,7 → 20,7
 
# This library requires either the GMP extension (or BCMath if gmp_supplement.inc.php is present)
 
if (file_exists(__DIR__ . '/mac_utils.inc.phps')) include_once __DIR__ . '/mac_utils.inc.phps';
if (file_exists(__DIR__ . '/mac_utils.inc.phps')) include_once __DIR__ . '/mac_utils.inc.phps'; // optionally used for uuid_info()
if (file_exists(__DIR__ . '/gmp_supplement.inc.php')) include_once __DIR__ . '/gmp_supplement.inc.php';
 
define('UUID_NAMEBASED_NS_DNS', '6ba7b810-9dad-11d1-80b4-00c04fd430c8');
341,7 → 341,7
}
 
function gen_uuid($prefer_timebased = true) {
if ($prefer_timebased) $uuid = gen_uuid_timebased();
$uuid = $prefer_timebased ? gen_uuid_timebased() : false;
if ($uuid === false) $uuid = gen_uuid_random();
return $uuid;
}
348,7 → 348,7
 
// Version 1 (Time based) UUID
function gen_uuid_timebased() {
# On Debian: aptitude install php-uuid
# On Debian: apt-get install php-uuid
# extension_loaded('uuid')
if (function_exists('uuid_create')) {
# OSSP uuid extension like seen in php5-uuid at Debian 8
363,17 → 363,150
return trim(uuid_create(UUID_TYPE_TIME));
}
 
# On Debian: aptitude install uuid-runtime
# On Debian: apt-get install uuid-runtime
if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
$out = array();
$ec = -1;
exec('uuidgen -t', $out, $ec);
exec('uuidgen -t 2>/dev/null', $out, $ec);
if ($ec == 0) return $out[0];
}
 
# TODO: Implement the time based generation routine ourselves!
# At the moment we cannot determine the time based UUID
# If we hadn't any success yet, then implement the time based generation routine ourselves!
# Based on https://github.com/fredriklindberg/class.uuid.php/blob/master/class.uuid.php
 
$uuid = array(
'time_low' => 0, /* 32-bit */
'time_mid' => 0, /* 16-bit */
'time_hi' => 0, /* 16-bit */
'clock_seq_hi' => 0, /* 8-bit */
'clock_seq_low' => 0, /* 8-bit */
'node' => array() /* 48-bit */
);
 
/*
* Get current time in 100 ns intervals. The magic value
* is the offset between UNIX epoch and the UUID UTC
* time base October 15, 1582.
*/
$tp = gettimeofday();
$time = ($tp['sec'] * 10000000) + ($tp['usec'] * 10) + 0x01B21DD213814000;
 
$uuid['time_low'] = $time & 0xffffffff;
/* Work around PHP 32-bit bit-operation limits */
$high = intval($time / 0xffffffff);
$uuid['time_mid'] = $high & 0xffff;
$uuid['time_hi'] = (($high >> 16) & 0xfff) | (1/*TimeBased*/ << 12);
 
/*
* We don't support saved state information and generate
* a random clock sequence each time.
*/
$uuid['clock_seq_hi'] = 0x80 | mt_rand(0, 64);
$uuid['clock_seq_low'] = mt_rand(0, 255);
 
/*
* Node should be set to the 48-bit IEEE node identifier
*/
$mac = get_mac_address();
if ($mac) {
$node = str_replace(':','',$mac);
for ($i = 0; $i < 6; $i++) {
$uuid['node'][$i] = hexdec(substr($node, $i*2, 2));
}
 
/*
* Now output the UUID
*/
return sprintf(
'%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x',
($uuid['time_low']), ($uuid['time_mid']), ($uuid['time_hi']),
$uuid['clock_seq_hi'], $uuid['clock_seq_low'],
$uuid['node'][0], $uuid['node'][1], $uuid['node'][2],
$uuid['node'][3], $uuid['node'][4], $uuid['node'][5]);
}
 
# We cannot generate the timebased UUID!
return false;
}
 
function get_mac_address() {
static $detected_mac = false;
 
if ($detected_mac !== false) { // false NOT null!
return $detected_mac;
}
 
// TODO: This should actually be part of mac_utils.inc.php, but we need it
// here, and mac_utils.inc.php shall only be optional. What to do?
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
// Windows
$cmds = array(
"ipconfig /all", // faster
"getmac"
);
foreach ($cmds as $cmd) {
$out = array();
$ec = -1;
exec($cmd, $out, $ec);
if ($ec == 0) {
$out = implode("\n",$out);
$m = array();
if (preg_match("/([0-9a-f]{2}\\-[0-9a-f]{2}\\-[0-9a-f]{2}\\-[0-9a-f]{2}\\-[0-9a-f]{2}\\-[0-9a-f]{2})/ismU", $out, $m)) {
$detected_mac = str_replace('-', ':', strtolower($m[1]));
return $detected_mac;
}
}
}
} else if (strtoupper(PHP_OS) == 'DARWIN') {
// Mac OS X
$cmds = array(
"networksetup -listallhardwareports 2>/dev/null",
"netstat -i 2>/dev/null"
);
foreach ($cmds as $cmd) {
$out = array();
$ec = -1;
exec($cmd, $out, $ec);
if ($ec == 0) {
$out = implode("\n",$out);
$m = array();
if (preg_match("/([0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2})/ismU", $out, $m)) {
$detected_mac = $m[1];
return $detected_mac;
}
}
}
} else {
// Linux
foreach (glob('/sys/class/net/'.'*'.'/address') as $x) {
if (!strstr($x,'/lo/')) {
$detected_mac = trim(file_get_contents($x));
return $detected_mac;
}
}
$cmds = array(
"netstat -ie 2>/dev/null",
"ifconfig 2>/dev/null" // only available for root (because it is in sbin)
);
foreach ($cmds as $cmd) {
$out = array();
$ec = -1;
exec($cmd, $out, $ec);
if ($ec == 0) {
$out = implode("\n",$out);
$m = array();
if (preg_match("/([0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2}:[0-9a-f]{2})/ismU", $out, $m)) {
$detected_mac = $m[1];
return $detected_mac;
}
}
}
}
 
$detected_mac = null;
return $detected_mac;
}
 
// Version 2 (DCE Security) UUID
function gen_uuid_dce($domain, $id) {
# Start with a version 1 UUID
411,7 → 544,7
 
// Version 4 (Random) UUID
function gen_uuid_random() {
# On Debian: aptitude install php-uuid
# On Debian: apt-get install php-uuid
# extension_loaded('uuid')
if (function_exists('uuid_create')) {
# OSSP uuid extension like seen in php5-uuid at Debian 8
426,10 → 559,11
return trim(uuid_create(UUID_TYPE_RANDOM));
}
 
# On Debian: aptitude install uuid-runtime
if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
# On Debian: apt-get install uuid-runtime
$out = array();
$ec = -1;
exec('uuidgen -r', $out, $ec);
exec('uuidgen -r 2>/dev/null', $out, $ec);
if ($ec == 0) return $out[0];
 
# On Debian Jessie: UUID V4 (Random)
436,6 → 570,7
if (file_exists('/proc/sys/kernel/random/uuid')) {
return file_get_contents('/proc/sys/kernel/random/uuid');
}
}
 
# Make the UUID by ourselves
# Source: http://rogerstringer.com/2013/11/15/generate-uuids-php