hamnet70/impl/utils/ham64/call2ham64.c

60 lines
1.4 KiB
C
Raw Normal View History

2025-01-04 17:19:16 +01:00
#include <layer2/ham64.h>
#include <logger.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
int main(int argc, char **argv)
{
logger_init();
if(argc < 2) {
LOG(LVL_FATAL, "Not enough arguments!");
LOG(LVL_FATAL, "");
LOG(LVL_FATAL, "usage: %s <callsign>", argv[0]);
return EXIT_FAILURE;
}
const char *callsign = argv[1];
size_t len = strlen(callsign);
if(len > 12) {
LOG(LVL_ERR, "Call sign too long: %zu chars (allowed: 12)", len);
return EXIT_FAILURE;
}
ham64_t ham64;
size_t encoded_len = ham64_encode(callsign, &ham64);
if(encoded_len == 0) {
LOG(LVL_ERR, "Encoding the call sign failed.");
return EXIT_FAILURE;
}
LOG(LVL_INFO, "Call sign encoded successfully: %zu words.", encoded_len);
char formatted[HAM64_FMT_MAX_LEN];
ham64_format(&ham64, formatted);
printf("ham64: %s\n", formatted);
// calculate IPv6 address
struct in6_addr ipv6_addr;
memset(&ipv6_addr, 0, sizeof(struct in6_addr));
// fill the host part from the ham64 address. The bytes are filled in
// reverse order to make the „readable“ IPv6 address as short as possible.
for(uint8_t i = 0; i < ham64.length; i++) {
ipv6_addr.s6_addr[15 - 2*i] = (ham64.addr[i] >> 8) & 0xFF;
ipv6_addr.s6_addr[14 - 2*i] = (ham64.addr[i] >> 0) & 0xFF;
}
// print the address
char ipv6_str[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &ipv6_addr, ipv6_str, sizeof(ipv6_str));
printf("ipv6: %s\n", ipv6_str);
}