Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
johandc committed May 1, 2022
2 parents 50c3c2f + adbccad commit 6f81c3d
Show file tree
Hide file tree
Showing 22 changed files with 109 additions and 46 deletions.
8 changes: 0 additions & 8 deletions include/csp/csp.h
Expand Up @@ -35,7 +35,6 @@ enum csp_dedup_types {
*/
typedef struct csp_conf_s {
uint8_t version; /**< Protocol version to use (either 1 or 2) */
uint16_t address; /**< CSP address of the system */
const char *hostname; /**< Host name, returned by the #CSP_CMP_IDENT request */
const char *model; /**< Model, returned by the #CSP_CMP_IDENT request */
const char *revision; /**< Revision, returned by the #CSP_CMP_IDENT request */
Expand Down Expand Up @@ -63,13 +62,6 @@ void csp_free_resources(void);
*/
const csp_conf_t * csp_get_conf(void);

/**
Get the system's own address.
@return system's own address
*/
uint16_t csp_get_address(void);


/**
* Copy csp id fields from source to target object
*/
Expand Down
1 change: 0 additions & 1 deletion include/csp/csp_debug.h
Expand Up @@ -30,7 +30,6 @@ extern uint8_t csp_dbg_errno;
#define CSP_DBG_ERR_MTU_EXCEEDED 2
#define CSP_DBG_ERR_ALREADY_FREE 3
#define CSP_DBG_ERR_REFCOUNT 4
#define CSP_DBG_ERR_INVALID_CAN_CONFIG 5
#define CSP_DBG_ERR_INVALID_RTABLE_ENTRY 6
#define CSP_DBG_ERR_UNSUPPORTED 7
#define CSP_DBG_ERR_INVALID_BIND_PORT 8
Expand Down
3 changes: 1 addition & 2 deletions include/csp/csp_id.h
Expand Up @@ -15,5 +15,4 @@ unsigned int csp_id_get_host_bits(void);
unsigned int csp_id_get_max_nodeid(void);
unsigned int csp_id_get_max_port(void);

int csp_id_is_broadcast(uint16_t addr, uint16_t netmask);

int csp_id_is_broadcast(uint16_t addr, csp_iface_t * iface);
4 changes: 4 additions & 0 deletions include/csp/csp_iflist.h
Expand Up @@ -13,10 +13,14 @@ int csp_iflist_add(csp_iface_t * iface);
csp_iface_t * csp_iflist_get_by_name(const char *name);
csp_iface_t * csp_iflist_get_by_addr(uint16_t addr);
csp_iface_t * csp_iflist_get_by_subnet(uint16_t addr, csp_iface_t * from);
csp_iface_t * csp_iflist_get_by_index(int idx);
int csp_iflist_is_within_subnet(uint16_t addr, csp_iface_t * ifc);

csp_iface_t * csp_iflist_get(void);

void csp_iflist_set_default(csp_iface_t * interface);
csp_iface_t * csp_iflist_get_default(void);

/* Convert bytes to readable string */
unsigned long csp_bytesize(unsigned long bytes, char *postfix);

Expand Down
2 changes: 1 addition & 1 deletion include/csp/csp_interface.h
Expand Up @@ -8,7 +8,7 @@
* Interface Tx function.
* @return #CSP_ERR_NONE on success, otherwise an error code.
*/
typedef int (*nexthop_t)(csp_iface_t * iface, uint16_t via, csp_packet_t * packet);
typedef int (*nexthop_t)(csp_iface_t * iface, uint16_t via, csp_packet_t * packet, int from_me);

/* This struct is referenced in documentation. Update doc when you change this. */
struct csp_iface_s {
Expand Down
2 changes: 1 addition & 1 deletion include/csp/interfaces/csp_if_can.h
Expand Up @@ -106,7 +106,7 @@
#define CFP2_DST_OFFSET 13

#define CFP2_SENDER_SIZE 6
#define CFP2_SENDER_MASK 0x1F
#define CFP2_SENDER_MASK 0x3F
#define CFP2_SENDER_OFFSET 7

#define CFP2_SC_MASK 0x3
Expand Down
2 changes: 1 addition & 1 deletion include/csp/interfaces/csp_if_i2c.h
Expand Up @@ -52,7 +52,7 @@ int csp_i2c_add_interface(csp_iface_t * iface);
@return #CSP_ERR_NONE on success, otherwise an error code.
*/
int csp_i2c_tx(csp_iface_t * iface, uint16_t via, csp_packet_t * packet);
int csp_i2c_tx(csp_iface_t * iface, uint16_t via, csp_packet_t * packet, int from_me);

/**
Process received I2C frame.
Expand Down
2 changes: 1 addition & 1 deletion include/csp/interfaces/csp_if_kiss.h
Expand Up @@ -68,7 +68,7 @@ int csp_kiss_add_interface(csp_iface_t * iface);
@return #CSP_ERR_NONE on success, otherwise an error code.
*/
int csp_kiss_tx(csp_iface_t * iface, uint16_t via, csp_packet_t * packet);
int csp_kiss_tx(csp_iface_t * iface, uint16_t via, csp_packet_t * packet, int from_me);

/**
Process received CAN frame.
Expand Down
12 changes: 6 additions & 6 deletions src/bindings/python/pycsp.c
Expand Up @@ -73,7 +73,7 @@ static void pycsp_free_csp_conn(PyObject * obj) {
}

static void pycsp_free_csp_socket(PyObject * obj) {
csp_conn_t * socket = get_obj_as_socket(obj, true);
csp_conn_t * socket = (csp_conn_t *)get_obj_as_socket(obj, true);
if (socket) {
csp_close(socket);
}
Expand Down Expand Up @@ -129,11 +129,10 @@ static PyObject * pycsp_socket(PyObject * self, PyObject * args) {
return NULL; // TypeError is thrown
}

csp_socket_t * socket = csp_socket(opts);
if (socket == NULL) {
return PyErr_Error("csp_socket() - no free sockets/connections", CSP_ERR_NOBUFS);
}
return PyCapsule_New(socket, SOCKET_CAPSULE, pycsp_free_csp_socket);
csp_socket_t socket = {0};
socket.opts = opts;

return PyCapsule_New((void *)&socket, SOCKET_CAPSULE, pycsp_free_csp_socket);
}

static PyObject * pycsp_accept(PyObject * self, PyObject * args) {
Expand Down Expand Up @@ -480,6 +479,7 @@ static int csp_route_start_task(void) {
pthread_attr_destroy(&attributes);

if (ret != 0) {
printf("Failed to start router task, error: %d", ret);
return ret;
}

Expand Down
11 changes: 8 additions & 3 deletions src/csp_id.c
Expand Up @@ -228,9 +228,14 @@ unsigned int csp_id_get_max_port(void) {
return ((1 << (CSP_ID2_PORT_SIZE)) - 1);
}

int csp_id_is_broadcast(uint16_t addr, uint16_t netmask) {
uint16_t hostmask = (1 << (csp_id_get_host_bits() - netmask)) - 1;
if ((addr & hostmask) == hostmask) {
int csp_id_is_broadcast(uint16_t addr, csp_iface_t * iface) {
uint16_t hostmask = (1 << (csp_id_get_host_bits() - iface->netmask)) - 1;
uint16_t netmask = (1 << csp_id_get_host_bits()) - 1 - hostmask;
if (((addr & hostmask) == hostmask) && ((addr & netmask) == (iface->addr & netmask))) {
return 1;
}

if (addr == csp_id_get_max_nodeid()) {
return 1;
}
return 0;
Expand Down
18 changes: 18 additions & 0 deletions src/csp_iflist.c
Expand Up @@ -11,6 +11,16 @@
/* Interfaces are stored in a linked list */
static csp_iface_t * interfaces = NULL;

static csp_iface_t * dfl_if = NULL;

void csp_iflist_set_default(csp_iface_t * interface) {
dfl_if = interface;
}

csp_iface_t * csp_iflist_get_default(void) {
return dfl_if;
}

int csp_iflist_is_within_subnet(uint16_t addr, csp_iface_t * ifc) {

if (ifc == NULL) {
Expand Down Expand Up @@ -84,6 +94,14 @@ csp_iface_t * csp_iflist_get_by_name(const char * name) {
return NULL;
}

csp_iface_t * csp_iflist_get_by_index(int idx) {
csp_iface_t * ifc = interfaces;
while(ifc && idx--) {
ifc = ifc->next;
}
return ifc;
}

int csp_iflist_add(csp_iface_t * ifc) {

ifc->next = NULL;
Expand Down
16 changes: 10 additions & 6 deletions src/csp_init.c
Expand Up @@ -11,19 +11,24 @@

csp_conf_t csp_conf = {
.version = 2,
.address = 0,
.hostname = "",
.model = "",
.revision = "",
.conn_dfl_so = CSP_O_NONE,
.dedup = CSP_DEDUP_OFF};

uint16_t csp_get_address(void) {
return csp_conf.address;
}

void csp_init(void) {

/* Validation of version */
if ((csp_conf.version == 0) || (csp_conf.version > 2)) {
csp_conf.version = 2;
}

/* Validation of dedup */
if (csp_conf.dedup > CSP_DEDUP_ALL) {
csp_conf.dedup = CSP_DEDUP_OFF;
}

csp_buffer_init();
csp_conn_init();
csp_qfifo_init();
Expand All @@ -38,7 +43,6 @@ void csp_init(void) {
}

void csp_free_resources(void) {

csp_rtable_free();
}

Expand Down
50 changes: 44 additions & 6 deletions src/csp_io.c
Expand Up @@ -14,13 +14,15 @@
#include <csp/arch/csp_queue.h>
#include <csp/arch/csp_time.h>
#include <csp/crypto/csp_hmac.h>
#include <csp/csp_id.h>

#include "csp_port.h"
#include "csp_conn.h"
#include "csp_promisc.h"
#include "csp_qfifo.h"
#include "csp_rdp.h"


#if (CSP_USE_PROMISC)
extern csp_queue_handle_t csp_promisc_queue;
#endif
Expand Down Expand Up @@ -91,6 +93,8 @@ void csp_send_direct(csp_id_t* idout, csp_packet_t * packet, csp_iface_t * route

while ((iface = csp_iflist_get_by_subnet(idout->dst, iface)) != NULL) {

local_found = 1;

/* Do not send back to same inteface (split horizon)
* This check is is similar to that below, but faster */
if (iface == routed_from) {
Expand All @@ -103,17 +107,21 @@ void csp_send_direct(csp_id_t* idout, csp_packet_t * packet, csp_iface_t * route
}

/* Apply outgoing interface address to packet */
idout->src = iface->addr;

if ((from_me) && (idout->src == 0)) {
idout->src = iface->addr;
}

/* Rewrite routed brodcast (L3) to local (L2) when arriving at the interface */
if (csp_id_is_broadcast(idout->dst, iface)) {
idout->dst = csp_id_get_max_nodeid();
}

/* Todo: Find an elegant way to avoid making a copy when only a single destination interface
* is found. But without looping the list twice. And without using stack memory.
* Is this even possible? */
copy = csp_buffer_clone(packet);
csp_send_direct_iface(idout, copy, iface, via, from_me);

local_found = 1;

}

/* If the above worked, we don't want to look at the routing table */
Expand All @@ -125,13 +133,43 @@ void csp_send_direct(csp_id_t* idout, csp_packet_t * packet, csp_iface_t * route
/* Try to send via routing table */
csp_route_t * route = csp_rtable_find_route(idout->dst);
if (route != NULL) {
if (idout->src == 0) {

/* Do not send back to same inteface (split horizon)
* This check is is similar to that below, but faster */
if (route->iface == routed_from) {
csp_buffer_free(packet);
return;
}

/* Apply outgoing interface address to packet */
if ((from_me) && (idout->src == 0)) {
idout->src = route->iface->addr;
}

csp_send_direct_iface(idout, packet, route->iface, route->via, from_me);
return;
}

/* Try to send via default interface */
csp_iface_t * dfl_if = csp_iflist_get_default();
if (dfl_if) {

/* Do not send back to same inteface (split horizon)
* This check is is similar to that below, but faster */
if (dfl_if == routed_from) {
csp_buffer_free(packet);
return;
}

/* Apply outgoing interface address to packet */
if ((from_me) && (idout->src == 0)) {
idout->src = dfl_if->addr;
}

csp_send_direct_iface(idout, packet, dfl_if, CSP_NO_VIA_ADDRESS, from_me);
return;
}

csp_buffer_free(packet);

}
Expand Down Expand Up @@ -193,7 +231,7 @@ void csp_send_direct_iface(csp_id_t* idout, csp_packet_t * packet, csp_iface_t *
if (mtu > 0 && bytes > mtu)
goto tx_err;

if ((*iface->nexthop)(iface, via, packet) != CSP_ERR_NONE)
if ((*iface->nexthop)(iface, via, packet, from_me) != CSP_ERR_NONE)
goto tx_err;

iface->tx++;
Expand Down
2 changes: 1 addition & 1 deletion src/csp_route.c
Expand Up @@ -140,7 +140,7 @@ int csp_route_work(void) {

/* The packet is to me, if the address matches that of the incoming interface,
* or the address matches the broadcast address of the incoming interface */
int is_to_me = ((input.iface->addr == packet->id.dst) || (csp_id_is_broadcast(packet->id.dst, input.iface->netmask)));
int is_to_me = ((input.iface->addr == packet->id.dst) || (csp_id_is_broadcast(packet->id.dst, input.iface)));

/* Deduplication */
if ((csp_conf.dedup == CSP_DEDUP_ALL) ||
Expand Down
6 changes: 5 additions & 1 deletion src/csp_yaml.c
Expand Up @@ -161,7 +161,11 @@ static void csp_yaml_end_if(struct data_s * data, unsigned int * dfl_addr) {
iface->netmask = atoi(data->netmask);
iface->name = strdup(data->name);

csp_print(" %s addr: %u netmask %u\n", iface->name, iface->addr, iface->netmask);
if (data->is_dfl) {
csp_iflist_set_default(iface);
}

csp_print(" %s addr: %u netmask %u %s\n", iface->name, iface->addr, iface->netmask, (data->is_dfl) ? "DFL" : "");

}

Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/csp_if_can.c
Expand Up @@ -163,7 +163,7 @@ int csp_can1_rx(csp_iface_t * iface, uint32_t id, const uint8_t * data, uint8_t
return CSP_ERR_NONE;
}

int csp_can1_tx(csp_iface_t * iface, uint16_t via, csp_packet_t * packet) {
int csp_can1_tx(csp_iface_t * iface, uint16_t via, csp_packet_t * packet, int from_me) {

/* Loopback */
if (packet->id.dst == iface->addr) {
Expand Down Expand Up @@ -368,7 +368,7 @@ int csp_can2_rx(csp_iface_t * iface, uint32_t id, const uint8_t * data, uint8_t
return CSP_ERR_NONE;
}

int csp_can2_tx(csp_iface_t * iface, uint16_t via, csp_packet_t * packet) {
int csp_can2_tx(csp_iface_t * iface, uint16_t via, csp_packet_t * packet, int from_me) {

/* Loopback */
if (packet->id.dst == iface->addr) {
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/csp_if_i2c.c
Expand Up @@ -4,7 +4,7 @@
#include <csp/csp_id.h>
#include <csp/csp.h>

int csp_i2c_tx(csp_iface_t * iface, uint16_t via, csp_packet_t * packet) {
int csp_i2c_tx(csp_iface_t * iface, uint16_t via, csp_packet_t * packet, int from_me) {

/* Loopback */
if (packet->id.dst == iface->addr) {
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/csp_if_kiss.c
Expand Up @@ -20,7 +20,7 @@
#define TFESC 0xDD
#define TNC_DATA 0x00

int csp_kiss_tx(csp_iface_t * iface, uint16_t via, csp_packet_t * packet) {
int csp_kiss_tx(csp_iface_t * iface, uint16_t via, csp_packet_t * packet, int from_me) {

csp_kiss_interface_data_t * ifdata = iface->interface_data;
void * driver = iface->driver_data;
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/csp_if_lo.c
Expand Up @@ -8,7 +8,7 @@
* @param packet Packet to transmit
* @return 1 if packet was successfully transmitted, 0 on error
*/
static int csp_lo_tx(csp_iface_t * iface, uint16_t via, csp_packet_t * packet) {
static int csp_lo_tx(csp_iface_t * iface, uint16_t via, csp_packet_t * packet, int from_me) {

/* Send back into CSP, notice calling from task so last argument must be NULL! */
csp_qfifo_write(packet, &csp_if_lo, NULL);
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/csp_if_tun.c
Expand Up @@ -11,7 +11,7 @@ __attribute__((weak)) int csp_crypto_encrypt(uint8_t * msg_begin, uint8_t msg_le
return -1;
}

static int csp_if_tun_tx(csp_iface_t * iface, uint16_t via, csp_packet_t * packet) {
static int csp_if_tun_tx(csp_iface_t * iface, uint16_t via, csp_packet_t * packet, int from_me) {

csp_if_tun_conf_t * ifconf = iface->driver_data;

Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/csp_if_udp.c
Expand Up @@ -14,7 +14,7 @@
#define MSG_CONFIRM (0)
#endif

static int csp_if_udp_tx(csp_iface_t * iface, uint16_t via, csp_packet_t * packet) {
static int csp_if_udp_tx(csp_iface_t * iface, uint16_t via, csp_packet_t * packet, int from_me) {

csp_if_udp_conf_t * ifconf = iface->driver_data;

Expand Down

0 comments on commit 6f81c3d

Please sign in to comment.