Skip to content

Commit

Permalink
Move to the common framework for sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
tehmaze committed Jan 31, 2016
1 parent 8089de2 commit 1b5c519
Show file tree
Hide file tree
Showing 22 changed files with 346 additions and 73 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "site_wright"]
path = site_wright
url = https://github.com/tehmaze-labs/wright
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ script:
- cat wright.log
- cat Makefile
- make V=1
- make install PREFIX=./dist
- if [ $MAKE_TEST -eq 1 ]; then LD_LIBRARY_PATH=dist/lib make test; fi
- make install DEST=./dist
- find dist -ls
- if [ $MAKE_TEST -eq 1 ]; then LD_LIBRARY_PATH=. make test; fi
1 change: 1 addition & 0 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ NOISEBRIDGE_LIBS += -l{{ opt }}
{% if with('mbelib') %}
NOISEBRIDGE_LIBS += -lportaudio
{% endif %}
NOISEBRIDGE_LIBS += $(COMMON_ARCHIVE) $(COMMON_LIBS)
NOISEBRIDGE_LIBS += {{ lib('pthread', 1) }} {{ lib('bsd', 1) }} {{ lib('m', 1) }} {{ lib('rt', 1) }} {{ lib('dl', 1) }} {{ lib('ws2_32', 1) }}

SERIALDUMP_SOURCES = $(wildcard src/cmd/serialdump/*.c)
Expand Down
5 changes: 3 additions & 2 deletions include/dmr/proto/homebrew.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ typedef struct __attribute__((packed)) {
typedef struct {
dmr_proto_t proto;
int fd;
struct sockaddr_in server, remote;
uint8_t peer_ip[16], bind_ip[16];
uint16_t peer_port, bind_port;
dmr_homebrew_auth_t auth;
dmr_id_t id;
uint8_t buffer[512]; /* receive buffer */
Expand Down Expand Up @@ -115,7 +116,7 @@ typedef struct {
struct timeval last_ping_sent;
} dmr_homebrew_t;

extern dmr_homebrew_t *dmr_homebrew_new(int port, struct in_addr peer);
extern dmr_homebrew_t *dmr_homebrew_new(const uint8_t peer_ip[16], uint16_t peer_port, const uint8_t *bind_ip, uint16_t bind_port);
extern int dmr_homebrew_auth(dmr_homebrew_t *homebrew, const char *secret);
extern void dmr_homebrew_close(dmr_homebrew_t *homebrew);
extern void dmr_homebrew_free(dmr_homebrew_t *homebrew);
Expand Down
1 change: 1 addition & 0 deletions site_wright
Submodule site_wright added at 7cb41c
22 changes: 13 additions & 9 deletions src/cmd/noisebridge/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <dmr/config.h>
#include <dmr/error.h>
#include "config.h"
#include "common/format.h"
#include "common/scan.h"

#if defined(DMR_HAVE_NETDB_H)
#include <netdb.h>
Expand Down Expand Up @@ -108,25 +110,27 @@ int read_config_homebrew(char *line, char *filename, size_t lineno)
else CONFIG_FLOAT(proto, "latitude", proto->instance.homebrew.latitude)
else CONFIG_FLOAT(proto, "longitude", proto->instance.homebrew.longitude)
else if (!strcmp(k, "host")) {
if (proto->instance.homebrew.addr != NULL) {
CONFIG_ERROR("duplicate host");
}
char *host = v, *port = NULL;
host = strtok_r(v, ":", &port);
if (port == NULL) {
proto->instance.homebrew.port = 62030;
proto->instance.homebrew.peer_port = 62030;
} else {
proto->instance.homebrew.port = atoi(port);
proto->instance.homebrew.peer_port = atoi(port);
}
if (proto->instance.homebrew.port == 0) {
proto->instance.homebrew.port = 62030;
if (proto->instance.homebrew.peer_port == 0) {
proto->instance.homebrew.peer_port = 62030;
}
struct hostent *hostent = NULL;
if ((hostent = gethostbyname(host)) == NULL || hostent->h_length == 0) {
CONFIG_ERROR("could not resolve %s: %s", host, strerror(errno));
}
proto->instance.homebrew.addr = (struct in_addr *)hostent->h_addr_list[0];
dmr_log_trace("noisebridge: %s resolved to %s", host, inet_ntoa(*proto->instance.homebrew.addr));
byte_zero(proto->instance.homebrew.peer_ip, sizeof(ip6_t));
byte_copy(proto->instance.homebrew.peer_ip, (void *)ip6mappedv4prefix, sizeof(ip6mappedv4prefix));
byte_copy(proto->instance.homebrew.peer_ip + sizeof(ip6mappedv4prefix), (void *)hostent->h_addr_list[0], 4);
char host_ip[FORMAT_IP6_LEN];
byte_zero(host_ip, sizeof(host_ip));
format_ip6(host_ip, proto->instance.homebrew.peer_ip);
dmr_log_trace("noisebridge: %s resolved to %s", host, host_ip);
} else {
CONFIG_ERROR("unknown key \"%s\"", k);
}
Expand Down
7 changes: 5 additions & 2 deletions src/cmd/noisebridge/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <dmr/proto/mmdvm.h>
#include <dmr/proto/repeater.h>
#include "common/config.h"
#include "common/socket.h"

#define NOISEBRIDGE_MAX_PROTOS 16

Expand All @@ -23,8 +24,10 @@ typedef struct {
void *mem;
union {
struct {
struct in_addr *addr;
int port;
ip6_t peer_ip;
uint16_t peer_port;
ip6_t bind_ip;
uint16_t bind_port;
char *auth;
char *call;
dmr_id_t repeater_id;
Expand Down
45 changes: 45 additions & 0 deletions src/cmd/noisebridge/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <dmr.h>
#include <dmr/packet.h>
#include <dmr/proto/repeater.h>
#include "common/serial.h"
#include "config.h"
#include "repeater.h"
#include "script.h"
Expand All @@ -24,6 +25,48 @@ void usage(const char *program)
fprintf(stderr, "\t-q\t\t\tDecrease verbosity.\n");
}

void show_serial_ports(void)
{
serial_t **ports = talloc_zero(NULL, serial_t *);
size_t i;
if (serial_list(&ports) != 0) {
fprintf(stderr, "can't list serial ports: %s\n", strerror(errno));
exit(1);
}

serial_t *port = NULL;
for (i = 0; ports[i] != NULL; i++) {
port = ports[i];
switch (serial_transport(port)) {
case SERIAL_TRANSPORT_NATIVE:
dmr_log_info("serial port %s: native", serial_name(port));
break;
case SERIAL_TRANSPORT_USB: {
int vid = 0, pid = 0;
serial_usb_vid_pid(port, &vid, &pid);
dmr_log_info("serial port %s: usb id %04X:%04X (%s)",
serial_name(port), vid, pid, serial_usb_product(port));
break;
}
case SERIAL_TRANSPORT_BLUETOOTH: {
char *addr = NULL;
if ((addr = serial_bluetooth_address(port))) {
dmr_log_info("serial port %s: Bluetooth %s",
serial_name(port), addr);
} else {
dmr_log_info("serial port %s: Bluetooth 00:00:00:00:00:00",
serial_name(port));
}
talloc_free(addr);
break;
}
default:
dmr_log_info("serial port %s: unknown", serial_name(port));
break;
}
}
}

int main(int argc, char **argv)
{
int ch, ret = 0;
Expand Down Expand Up @@ -65,6 +108,8 @@ int main(int argc, char **argv)
dmr_log_error("noisebridge: can't set thread name: %s", strerror(ret));
}

show_serial_ports();

if (init_config(filename) != 0) {
exit(1);
return 1;
Expand Down
15 changes: 10 additions & 5 deletions src/cmd/noisebridge/repeater.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <common/config.h>
#include <signal.h>
#include "common/format.h"
#include "common/scan.h"
#include "config.h"
#include "script.h"

Expand Down Expand Up @@ -137,12 +139,15 @@ int init_proto_homebrew(config_t *config, proto_t *proto)
DMR_UNUSED(config);

int ret = 0;
dmr_log_info("noisebridge: connect to BrandMeister homebrew on %s:%d",
inet_ntoa(*proto->instance.homebrew.addr),
proto->instance.homebrew.port);
char host[FORMAT_IP6_LEN];
format_ip6(host, proto->instance.homebrew.peer_ip);
dmr_log_info("noisebridge: connect to BrandMeister homebrew on [%s]:%u",
host, proto->instance.homebrew.peer_port);
dmr_homebrew_t *homebrew = dmr_homebrew_new(
proto->instance.homebrew.port,
*proto->instance.homebrew.addr);
proto->instance.homebrew.peer_ip, proto->instance.homebrew.peer_port,
NULL, 0);
//proto->instance.homebrew.bind_ip,
//proto->instance.homebrew.bind_port);
if (homebrew == NULL) {
return dmr_error(DMR_ENOMEM);
}
Expand Down
10 changes: 7 additions & 3 deletions src/cmd/noisebridge/script.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "common/format.h"
#include "common/scan.h"
#include "config.h"

static void lua_set_fun(lua_State *L, const char *key, void *fun)
{
lua_pushstring(L, key);
lua_pushcfunction(L, fun);
lua_settable(L, -3);
lua_settable(L, -3);
}

static void lua_set_int(lua_State *L, const char *key, int value)
Expand Down Expand Up @@ -222,8 +224,10 @@ int init_script(void)
lua_set_str(L, "name", proto->name);
switch (proto->type) {
case DMR_PROTO_HOMEBREW: {
lua_set_str(L, "host", inet_ntoa(*proto->instance.homebrew.addr));
lua_set_int(L, "port", proto->instance.homebrew.port);
char host[FORMAT_IP6_LEN];
format_ip6(host, proto->instance.homebrew.peer_ip);
lua_set_str(L, "host", host);
lua_set_int(L, "port", proto->instance.homebrew.peer_port);
lua_set_str(L, "call", proto->instance.homebrew.call);
lua_set_int(L, "repeater_id", proto->instance.homebrew.repeater_id);
break;
Expand Down
5 changes: 4 additions & 1 deletion src/common/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
/* #undef {{ key }} */
{% endif %}{% endfor %}

#endif // _COMMON_CONFIG_H
#if !defined(HAVE_INLINE)
#define inline
#endif

#endif // _COMMON_CONFIG_H
14 changes: 14 additions & 0 deletions src/common/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,17 @@ static void debug_handler_default(const char *fmt, ...)
}

void (*debug_handler)(const char *fmt, ...) = debug_handler_default;

char *debugip4(ip4_t ip)
{
static char host[FORMAT_IP4_LEN];
format_ip4(host, ip);
return host;
}

char *debugip6(ip6_t ip)
{
static char host[FORMAT_IP6_LEN];
format_ip6(host, ip);
return host;
}
9 changes: 7 additions & 2 deletions src/common/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <errno.h>
#include <stddef.h>
#include "common/platform.h"
#include "common/format.h"
#include "common/scan.h"
#if defined(PLATFORM_WINDOWS)
#include <windows.h>
#endif
Expand All @@ -28,9 +30,9 @@ extern void (*debug_handler)(const char *fmt, ...);

#define DEBUGF(fmt, ...) do { \
if (debug_handler) \
debug_handler(fmt "\n", __VA_ARGS__); \
debug_handler(fmt "\n", ##__VA_ARGS__); \
} while(0)
#define DEBUG(msg) DEBUGF(msg,NULL)
#define DEBUG(msg) DEBUGF(msg)
#define DEBUG_ERROR(err, msg) DEBUGF("%s returning " #err ": " msg, __func__)
#define RETURN_CODE(x) do { \
DEBUGF("%s returning " #x, __func__); \
Expand Down Expand Up @@ -76,6 +78,9 @@ extern void (*debug_handler)(const char *fmt, ...);
if (retval != 0) RETURN_CODEVAL(retval); \
} while (0)

char *debugip4(ip4_t ip);
char *debugip6(ip6_t ip);

#if defined(__cplusplus)
}
#endif
Expand Down
20 changes: 20 additions & 0 deletions src/common/format.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef _COMMON_FORMAT_H
#define _COMMON_FORMAT_H

#include <stddef.h>
#include "common/socket.h"

#if defined(__cplusplus)
extern "C" {
#endif

unsigned int format_ip4(char *dst, const ip4_t ip);
unsigned int format_ip6(char *dst, const ip6_t ip);
size_t format_ulong(char *dst, unsigned long i);
size_t format_xlong(char *dst, unsigned long i);

#if defined(__cplusplus)
}
#endif

#endif // _COMMON_FORMAT_H
79 changes: 79 additions & 0 deletions src/common/format/ip.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "common/format.h"
#include "common/socket.h"

unsigned int format_ip4(char *dst, const ip4_t ip)
{
unsigned int len;
int i;

len = 0;
for (i=0; i < 4; ++i) {
register unsigned int j;
len += (j = format_ulong(dst, (unsigned long)(unsigned char)ip[i])) + 1;
if (dst && i < 3) {
dst += j;
*dst++ = '.';
}
}
return len - 1;
}

unsigned int format_ip6(char *dst, const ip6_t ip)
{
unsigned long len, temp, k, pos0 = 0, len0 = 0, pos1 = 0, compr = 0;

for (k = 0; k < 16; k += 2) {
if (ip[k] == 0 && ip[k + 1] == 0) {
if (!compr) {
compr = 1;
pos1 = k;
}
if (k == 14) {
k = 16;
goto last;
}
} else if (compr) {
last:
if ((temp = k - pos1) > len0) {
len0 = temp;
pos0 = pos1;
}
compr = 0;
}
}

for (len = 0, k = 0; k < 16; k += 2) {
if (k == 12 && isip4mapped(ip)) {
len += format_ip4(dst, ip+12);
break;
}
if (pos0 == k && len0) {
if (k == 0) {
++len;
if (dst) {
*dst++ = ':';
}
}
++len;
if (dst) {
*dst++ = ':';
}
k += len0-2;
continue;
}
temp = ((unsigned long) (unsigned char) ip[k] << 8) +
(unsigned long) (unsigned char) ip[k + 1];
temp = format_xlong(dst, temp);
len += temp;
if (dst) {
dst += temp;
}
if (k < 14) {
++len;
if (dst) {
*dst++ = ':';
}
}
}
return len;
}
Loading

0 comments on commit 1b5c519

Please sign in to comment.