Implement patty_ax25_addr_copy() as a means to copy a patty_ax25_addr object from one location in memory to another, ensuring that the SSID field only contains at minimum the station identifier (0-15) and the reserved bits 0x60
122 lines
2.6 KiB
C
122 lines
2.6 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <errno.h>
|
|
#include <sys/select.h>
|
|
|
|
#include <patty/ax25.h>
|
|
|
|
int patty_ax25_pton(const char *callsign,
|
|
uint8_t ssid,
|
|
patty_ax25_addr *addr) {
|
|
int i,
|
|
end = 0;
|
|
|
|
for (i=0; i<PATTY_AX25_CALLSIGN_LEN; i++) {
|
|
if (callsign[i] == '\0') {
|
|
if (i == 0) {
|
|
errno = EINVAL;
|
|
|
|
goto error_invalid_callsign;
|
|
}
|
|
|
|
end = 1;
|
|
} else {
|
|
if (!PATTY_AX25_ADDR_CHAR_VALID(callsign[i])) {
|
|
errno = EINVAL;
|
|
|
|
goto error_invalid_callsign;
|
|
}
|
|
}
|
|
|
|
addr->callsign[i] = ((end? ' ': callsign[i]) & 0x7f) << 1;
|
|
}
|
|
|
|
addr->ssid = (ssid & 0x0f) << 1;
|
|
|
|
return 0;
|
|
|
|
error_invalid_callsign:
|
|
return -1;
|
|
}
|
|
|
|
int patty_ax25_ntop(const patty_ax25_addr *addr,
|
|
char *dest,
|
|
uint8_t *ssid,
|
|
size_t len) {
|
|
int i, o, space = 0;
|
|
|
|
if (addr == NULL || dest == NULL || ssid == NULL) {
|
|
errno = EINVAL;
|
|
|
|
goto error_invalid_args;
|
|
}
|
|
|
|
for (i=0, o=0; i<PATTY_AX25_CALLSIGN_LEN; i++) {
|
|
uint8_t c;
|
|
|
|
if (o == len) {
|
|
break;
|
|
}
|
|
|
|
if (PATTY_AX25_ADDR_OCTET_LAST(addr->callsign[i])) {
|
|
errno = EINVAL;
|
|
|
|
goto error_invalid_args;
|
|
}
|
|
|
|
c = (addr->callsign[i] & 0xfe) >> 1;
|
|
|
|
if (!PATTY_AX25_ADDR_CHAR_VALID(c)) {
|
|
errno = EINVAL;
|
|
|
|
goto error_invalid_args;
|
|
} else if (c == ' ' && !space) {
|
|
space = 1;
|
|
} else if (c != ' ' && space) {
|
|
errno = EINVAL;
|
|
|
|
goto error_invalid_args;
|
|
}
|
|
|
|
dest[o++] = space? '\0': c;
|
|
}
|
|
|
|
dest[o] = '\0';
|
|
|
|
*ssid = (addr->ssid & 0x1e) >> 1;
|
|
|
|
return 0;
|
|
|
|
error_invalid_args:
|
|
return -1;
|
|
}
|
|
|
|
static inline void hash_byte(uint32_t *hash, uint8_t c) {
|
|
*hash += c;
|
|
*hash += (*hash << 10);
|
|
*hash ^= (*hash >> 6);
|
|
}
|
|
|
|
void patty_ax25_addr_hash(uint32_t *hash, const patty_ax25_addr *addr) {
|
|
size_t i;
|
|
|
|
for (i=0; i<PATTY_AX25_CALLSIGN_LEN; i++) {
|
|
hash_byte(hash, addr->callsign[i] >> 1);
|
|
}
|
|
|
|
hash_byte(hash, PATTY_AX25_ADDR_SSID_NUMBER(addr->ssid));
|
|
}
|
|
|
|
size_t patty_ax25_addr_copy(void *buf,
|
|
patty_ax25_addr *addr,
|
|
uint8_t ssid_flags) {
|
|
size_t encoded = 0;
|
|
|
|
memcpy(buf, addr->callsign, sizeof(addr->callsign));
|
|
|
|
encoded += sizeof(addr->callsign);
|
|
|
|
((uint8_t *)buf)[encoded++] = ssid_flags | 0x60 | (addr->ssid & 0x1e);
|
|
|
|
return encoded;
|
|
}
|