Raspberry Vanilla IPMT

This commit is contained in:
2025-07-22 08:43:20 +07:00
commit 4247fc915d
6460 changed files with 146446 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := main.c data.c ipconfig.c
LOCAL_CFLAGS += -Wall -Wno-unused-parameter -Wextra
LOCAL_SHARED_LIBRARIES := \
libcutils \
liblog \
libandroidfw \
libutils \
libbinder \
libjsoncpp
LOCAL_MODULE := ipconfigstore
include $(BUILD_EXECUTABLE)

View File

@@ -0,0 +1,383 @@
#define _GNU_SOURCE
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "data.h"
uint16_t convertBigEndianUInt16(uint16_t value)
{
union { uint16_t value; unsigned char data[2]; } aux = { 0x4142 };
if (aux.data[0] == 0x41)
{
return value;
}
aux.data[0] = (value >> 8) & 0xff;
aux.data[1] = value & 0xff;
return aux.value;
}
uint32_t convertBigEndianUInt32(uint32_t value)
{
union { uint32_t value; unsigned char data[4]; } aux = { 0x41424344 };
if (aux.data[0] == 0x41)
{
return value;
}
aux.data[0] = (value >> 24) & 0xff;
aux.data[1] = (value >> 16) & 0xff;
aux.data[2] = (value >> 8) & 0xff;
aux.data[3] = value & 0xff;
return aux.value;
}
bool readPackedLink(FILE *stream, struct IPConfigLink *link)
{
char **address = &link->address;
uint32_t *prefix = &link->prefix;
if (!readPackedString(stream, address))
{
return false;
}
if (!readPackedUInt32(stream, prefix))
{
return false;
}
return true;
}
bool readPackedRoute(FILE *stream, struct IPConfigRoute *route)
{
char **destinationAddress = &route->destination.address;
uint32_t *destinationPrefix = &route->destination.prefix;
char **nextHop = &route->nextHop;
uint32_t haveDestination = 0;
uint32_t haveNextHop = 0;
if (!readPackedUInt32(stream, &haveDestination))
{
return false;
}
if (haveDestination)
{
if (!readPackedString(stream, destinationAddress))
{
return false;
}
if (!readPackedUInt32(stream, destinationPrefix))
{
return false;
}
}
if (!readPackedUInt32(stream, &haveNextHop))
{
return false;
}
if (haveNextHop)
{
if (!readPackedString(stream, nextHop))
{
return false;
}
}
return true;
}
bool readPackedString(FILE *stream, char **string)
{
uint16_t length = 0;
if (!readPackedUInt16(stream, &length))
{
return false;
}
while (length == 0)
{
if (fseek(stream, sizeof(uint16_t), SEEK_CUR) == -1)
{
return false;
}
if (!readPackedUInt16(stream, &length))
{
return false;
}
}
*string = calloc(1, length + 1);
if (!*string)
{
return false;
}
if (fread(*string, length, 1, stream) != 1)
{
if (!feof(stream))
{
free(*string);
return false;
}
}
return true;
}
bool readPackedUInt16(FILE *stream, uint16_t *value)
{
uint16_t buffer = 0;
if (fread(&buffer, sizeof buffer, 1, stream) != 1)
{
return false;
}
*value = convertBigEndianUInt16(buffer);
return true;
}
bool readPackedUInt32(FILE *stream, uint32_t *value)
{
uint32_t buffer = 0;
if (fread(&buffer, sizeof buffer, 1, stream) != 1)
{
return false;
}
*value = convertBigEndianUInt32(buffer);
return true;
}
bool writePackedRoute(struct IPConfigRoute *route, FILE *stream)
{
struct IPConfigLink *destination = &route->destination;
if (destination->address && destination->prefix)
{
if (!writePackedUInt32(1, stream))
{
return false;
}
if (!writePackedString(destination->address, stream))
{
return false;
}
if (!writePackedUInt32(destination->prefix, stream))
{
return false;
}
}
else
{
if (!writePackedUInt32(0, stream))
{
return false;
}
}
if (route->nextHop)
{
if (!writePackedUInt32(1, stream))
{
return false;
}
if (!writePackedString(route->nextHop, stream))
{
return false;
}
}
else
{
if (!writePackedUInt32(0, stream))
{
return false;
}
}
return true;
}
bool writePackedLink(struct IPConfigLink *link, FILE *stream)
{
if (!writePackedString(link->address, stream))
{
return false;
}
if (!writePackedUInt32(link->prefix, stream))
{
return false;
}
return true;
}
bool writePackedString(char *string, FILE *stream)
{
size_t stringLength = strlen(string);
if (!writePackedUInt16(stringLength, stream))
{
return false;
}
return fwrite(string, stringLength, 1, stream) == 1;
}
bool writePackedUInt16(uint16_t value, FILE *stream)
{
uint16_t buffer = convertBigEndianUInt16(value);
return fwrite(&buffer, sizeof buffer, 1, stream) == 1;
}
bool writePackedUInt32(uint32_t value, FILE *stream)
{
uint32_t buffer = convertBigEndianUInt32(value);
return fwrite(&buffer, sizeof buffer, 1, stream) == 1;
}
bool readUnpackedLine(FILE *stream, char **line)
{
char *cursor = NULL;
*line = calloc(1, BUFSIZ);
if (!*line)
{
return false;
}
cursor = *line;
while (cursor - *line < BUFSIZ)
{
int character = fgetc(stream);
if (character == EOF || character == '\n')
{
break;
}
*cursor++ = character;
}
return true;
}
bool parseUnpackedPair(char *line, char **key, char **value)
{
char *next = index(line, ':');
if (!next)
{
return false;
}
*key = strndup(line, next - line);
if (!*key)
{
return false;
}
while (*++next && isspace(*next));
*value = strdup(next);
if (!*value)
{
free(*key);
return false;
}
return true;
}
bool parseUnpackedRoute(char *string, struct IPConfigRoute *route)
{
char *next = index(string, ' ');
if (next)
{
*next = 0;
if (!parseUnpackedLink(string, &route->destination))
{
return false;
}
string = ++next;
}
route->nextHop = strdup(string);
if (!route->nextHop)
{
return false;
}
return true;
}
bool parseUnpackedLink(char *string, struct IPConfigLink *link)
{
char *next = index(string, '/');
if (!next)
{
return false;
}
link->address = strndup(string, next - string);
if (!link->address)
{
return false;
}
if (!parseUnpackedUInt32(++next, &link->prefix))
{
free(link->address);
return false;
}
return true;
}
bool parseUnpackedUInt32(char *string, uint32_t *integer)
{
char *terminator = NULL;
unsigned long value = strtoul(string, &terminator, 10);
if (*terminator)
{
return false;
}
*integer = value % UINT32_MAX;
return true;
}

View File

@@ -0,0 +1,31 @@
#ifndef IPCONFIG_DATA_H
#define IPCONFIG_DATA_H
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include "ipconfig.h"
uint16_t convertBigEndianUInt16(uint16_t value);
uint32_t convertBigEndianUInt32(uint32_t value);
bool readPackedRoute(FILE *stream, struct IPConfigRoute *route);
bool readPackedLink(FILE *stream, struct IPConfigLink *link);
bool readPackedString(FILE *stream, char **string);
bool readPackedUInt16(FILE *stream, uint16_t *value);
bool readPackedUInt32(FILE *stream, uint32_t *value);
bool writePackedRoute(struct IPConfigRoute *route, FILE *stream);
bool writePackedLink(struct IPConfigLink *link, FILE *stream);
bool writePackedString(char *string, FILE *stream);
bool writePackedUInt16(uint16_t value, FILE *stream);
bool writePackedUInt32(uint32_t value, FILE *stream);
bool readUnpackedLine(FILE *stream, char **line);
bool parseUnpackedPair(char *line, char **key, char **value);
bool parseUnpackedRoute(char *string, struct IPConfigRoute *route);
bool parseUnpackedLink(char *string, struct IPConfigLink *link);
bool parseUnpackedUInt32(char *string, uint32_t *integer);
#endif

View File

@@ -0,0 +1,14 @@
#ifndef IPCONFIG_ERROR_H
#define IPCONFIG_ERROR_H
#include <string.h>
#include <stdio.h>
#include <errno.h>
#define printError(message) \
fprintf(stderr, "%s: %s\n", __func__, message)
#define printLibraryError(message) \
fprintf(stderr, "%s: %s: %s\n", __func__, message, strerror(errno))
#endif

View File

@@ -0,0 +1,530 @@
#define _DEFAULT_SOURCE
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include "data.h"
#include "ipconfig.h"
#include "error.h"
#define calculateElementCount(array) (sizeof array / sizeof *array)
static const uint32_t IPConfigFileMinimumVersion = 1;
static const uint32_t IPConfigFileMaximumVersion = 3;
static char *IPConfigTerminatorKey = "eos";
static struct IPConfigAttributeKey IPConfigVersion1AttributeKeys[] =
{
{"id", IntegerIPConfigAttributeType},
{"ipAssignment", StringIPConfigAttributeType},
{"linkAddress", LinkIPConfigAttributeType},
{"gateway", StringIPConfigAttributeType},
{"dns", StringIPConfigAttributeType},
{"proxySettings", StringIPConfigAttributeType},
{"proxyHost", StringIPConfigAttributeType},
{"proxyPort", IntegerIPConfigAttributeType},
{"proxyPac", StringIPConfigAttributeType},
{"exclusionList", StringIPConfigAttributeType},
{"eos", TerminalIPConfigAttributeType}
};
static size_t IPConfigVersion1AttributeKeyCount =
calculateElementCount(IPConfigVersion1AttributeKeys);
static struct IPConfigAttributeKey IPConfigVersion2AttributeKeys[] =
{
{"id", IntegerIPConfigAttributeType},
{"ipAssignment", StringIPConfigAttributeType},
{"linkAddress", LinkIPConfigAttributeType},
{"gateway", RouteIPConfigAttributeType},
{"dns", StringIPConfigAttributeType},
{"proxySettings", StringIPConfigAttributeType},
{"proxyHost", StringIPConfigAttributeType},
{"proxyPort", IntegerIPConfigAttributeType},
{"proxyPac", StringIPConfigAttributeType},
{"exclusionList", StringIPConfigAttributeType},
{"eos", TerminalIPConfigAttributeType}
};
static size_t IPConfigVersion2AttributeKeyCount =
calculateElementCount(IPConfigVersion2AttributeKeys);
static struct IPConfigAttributeKey IPConfigVersion3AttributeKeys[] =
{
{"id", StringIPConfigAttributeType},
{"ipAssignment", StringIPConfigAttributeType},
{"linkAddress", LinkIPConfigAttributeType},
{"gateway", RouteIPConfigAttributeType},
{"dns", StringIPConfigAttributeType},
{"proxySettings", StringIPConfigAttributeType},
{"proxyHost", StringIPConfigAttributeType},
{"proxyPort", IntegerIPConfigAttributeType},
{"proxyPac", StringIPConfigAttributeType},
{"exclusionList", StringIPConfigAttributeType},
{"eos", TerminalIPConfigAttributeType}
};
static size_t IPConfigVersion3AttributeKeyCount =
calculateElementCount(IPConfigVersion3AttributeKeys);
static enum IPConfigAttributeType getAttributeType(uint32_t version, char *key)
{
size_t keyLength = strlen(key);
struct IPConfigAttributeKey *keys = NULL;
size_t count = 0;
switch (version)
{
case 1:
keys = IPConfigVersion1AttributeKeys;
count = IPConfigVersion1AttributeKeyCount;
break;
case 2:
keys = IPConfigVersion2AttributeKeys;
count = IPConfigVersion2AttributeKeyCount;
break;
case 3:
keys = IPConfigVersion3AttributeKeys;
count = IPConfigVersion3AttributeKeyCount;
break;
default:
break;
};
for (size_t index = 0; index < count; index++)
{
char *candidate = keys[index].key;
size_t maximumLength = strlen(candidate);
if (keyLength > maximumLength)
{
maximumLength = keyLength;
}
if (!strncmp(candidate, key, maximumLength))
{
return keys[index].type;
}
}
return InvalidIPConfigAttributeType;
}
static void appendAttribute(struct IPConfigAttribute *attribute,
struct IPConfig *config)
{
if (!config->attributes)
{
config->attributes = attribute;
}
else
{
struct IPConfigAttribute *previous = config->attributes;
while (previous->next)
{
previous = previous->next;
}
previous->next = attribute;
}
}
bool readPackedIPConfig(FILE *stream, struct IPConfig *config)
{
if (!readPackedUInt32(stream, &config->version))
{
printError("failed to read file version");
return NULL;
}
if (config->version < IPConfigFileMinimumVersion ||
config->version > IPConfigFileMaximumVersion)
{
printError("unrecognized file version");
return NULL;
}
while (!feof(stream))
{
struct IPConfigAttribute *attribute = NULL;
attribute = calloc(1, sizeof(struct IPConfigAttribute));
if (!attribute)
{
printLibraryError("calloc");
deinitializeIPConfig(config);
return NULL;
}
appendAttribute(attribute, config);
if (!readPackedString(stream, &attribute->key))
{
printError("failed to read attribute key");
deinitializeIPConfig(config);
return NULL;
}
attribute->type = getAttributeType(config->version,
attribute->key);
if (attribute->type == InvalidIPConfigAttributeType)
{
printError("unrecognized attribute key");
deinitializeIPConfig(config);
return NULL;
}
else if (attribute->type == TerminalIPConfigAttributeType)
{
break;
}
else if (attribute->type == IntegerIPConfigAttributeType)
{
uint32_t *integer = &attribute->value.integer;
if (!readPackedUInt32(stream, integer))
{
printError("failed to read integer");
deinitializeIPConfig(config);
return NULL;
}
}
else if (attribute->type == StringIPConfigAttributeType)
{
char **string = &attribute->value.string;
if (!readPackedString(stream, string))
{
printError("failed to read string");
deinitializeIPConfig(config);
return NULL;
}
}
else if (attribute->type == LinkIPConfigAttributeType)
{
if (!readPackedLink(stream, &attribute->value.link))
{
printError("failed to read link");
deinitializeIPConfig(config);
return NULL;
}
}
else if (attribute->type == RouteIPConfigAttributeType)
{
if (!readPackedRoute(stream, &attribute->value.route))
{
printError("failed to read route");
deinitializeIPConfig(config);
return NULL;
}
}
}
return config;
}
void deinitializeIPConfig(struct IPConfig *config)
{
struct IPConfigAttribute *next = NULL;
struct IPConfigAttribute *attribute = config->attributes;
while (attribute)
{
union IPConfigValue *value = &attribute->value;
if (attribute->type == StringIPConfigAttributeType)
{
free(value->string);
}
else if (attribute->type == LinkIPConfigAttributeType)
{
free(value->link.address);
}
else if (attribute->type == RouteIPConfigAttributeType)
{
if (value->route.destination.address)
{
free(value->route.destination.address);
}
if (value->route.nextHop)
{
free(value->route.nextHop);
}
}
next = attribute->next;
free(attribute->key);
free(attribute);
attribute = next;
}
}
bool writePackedIPConfig(struct IPConfig *config, FILE *stream)
{
bool terminated = false;
struct IPConfigAttribute *attribute = config->attributes;
if (!writePackedUInt32(config->version, stream))
{
printError("failed to write file version");
return false;
}
while (attribute)
{
union IPConfigValue *value = &attribute->value;
if (!writePackedString(attribute->key, stream))
{
printError("failed to write key");
return false;
}
if (attribute->type == TerminalIPConfigAttributeType)
{
terminated = true;
break;
}
else if (attribute->type == IntegerIPConfigAttributeType)
{
if (!writePackedUInt32(value->integer, stream))
{
printError("failed to write integer");
return false;
}
}
else if (attribute->type == StringIPConfigAttributeType)
{
if (!writePackedString(value->string, stream))
{
printError("failed to write string");
return false;
}
}
else if (attribute->type == LinkIPConfigAttributeType)
{
if (!writePackedLink(&value->link, stream))
{
printError("failed to write link");
return false;
}
}
else if (attribute->type == RouteIPConfigAttributeType)
{
if (!writePackedRoute(&value->route, stream))
{
printError("failed to write route");
return false;
}
}
attribute = attribute->next;
}
if (!terminated)
{
if (!writePackedString(IPConfigTerminatorKey, stream))
{
printError("failed to write terminator");
return false;
}
}
return true;
}
bool writeUnpackedIPConfig(struct IPConfig *config, FILE *stream)
{
struct IPConfigAttribute *attribute = config->attributes;
while (attribute)
{
if (attribute->type == IntegerIPConfigAttributeType)
{
fprintf(stream, "%s: %" PRIu32 "\n",
attribute->key,
attribute->value.integer);
}
else if (attribute->type == StringIPConfigAttributeType)
{
fprintf(stream, "%s: %s\n",
attribute->key,
attribute->value.string);
}
else if (attribute->type == LinkIPConfigAttributeType)
{
struct IPConfigLink *link = &attribute->value.link;
fprintf(stream, "%s: %s/%" PRIu32 "\n",
attribute->key,
link->address,
link->prefix);
}
else if (attribute->type == RouteIPConfigAttributeType)
{
struct IPConfigRoute *route = &attribute->value.route;
struct IPConfigLink *destination = &route->destination;
if (destination->address && destination->prefix)
{
fprintf(stream, "%s: %s/%" PRIu32 " %s\n",
attribute->key,
destination->address,
destination->prefix,
route->nextHop);
}
else
{
fprintf(stream, "%s: %s\n",
attribute->key,
route->nextHop);
}
}
attribute = attribute->next;
}
return true;
}
bool readUnpackedIPConfig(FILE *stream, struct IPConfig *config)
{
if (config->version < IPConfigFileMinimumVersion ||
config->version > IPConfigFileMaximumVersion)
{
printError("unrecognized file version");
return false;
}
while (!feof(stream))
{
char *line = NULL;
char *value = NULL;
struct IPConfigAttribute *attribute = NULL;
attribute = calloc(1, sizeof(struct IPConfigAttribute));
if (!attribute)
{
printLibraryError("calloc");
deinitializeIPConfig(config);
return false;
}
appendAttribute(attribute, config);
if (!readUnpackedLine(stream, &line))
{
printError("failed to read line");
deinitializeIPConfig(config);
return false;
}
if (strlen(line) == 0)
{
free(line);
if (feof(stream))
{
attribute->key = strdup(IPConfigTerminatorKey);
break;
}
continue;
}
if (!parseUnpackedPair(line, &attribute->key, &value))
{
printError("failed to read pair");
deinitializeIPConfig(config);
free(line);
return false;
}
free(line);
attribute->type = getAttributeType(config->version,
attribute->key);
if (!attribute->type)
{
printError("unrecognized attribute type");
deinitializeIPConfig(config);
free(value);
return false;
}
else if (attribute->type == IntegerIPConfigAttributeType)
{
uint32_t *integer = &attribute->value.integer;
if (!parseUnpackedUInt32(value, integer))
{
printError("failed to read integer");
deinitializeIPConfig(config);
free(value);
return false;
}
free(value);
}
else if (attribute->type == StringIPConfigAttributeType)
{
attribute->value.string = value;
}
else if (attribute->type == LinkIPConfigAttributeType)
{
if (!parseUnpackedLink(value, &attribute->value.link))
{
printError("failed to read link");
deinitializeIPConfig(config);
free(value);
return false;
}
free(value);
}
else if (attribute->type == RouteIPConfigAttributeType)
{
struct IPConfigRoute *route = &attribute->value.route;
if (!parseUnpackedRoute(value, route))
{
printError("failed to read route");
deinitializeIPConfig(config);
free(value);
return false;
}
free(value);
}
}
return true;
}

View File

@@ -0,0 +1,64 @@
#ifndef IPCONFIG_H
#define IPCONFIG_H
#include <stdbool.h>
#include <inttypes.h>
enum IPConfigAttributeType
{
InvalidIPConfigAttributeType = -1,
TerminalIPConfigAttributeType = 0,
IntegerIPConfigAttributeType = 1,
StringIPConfigAttributeType = 2,
LinkIPConfigAttributeType = 3,
RouteIPConfigAttributeType = 4
};
struct IPConfigAttributeKey
{
char *key;
enum IPConfigAttributeType type;
};
struct IPConfigLink
{
char *address;
uint32_t prefix;
};
struct IPConfigRoute
{
struct IPConfigLink destination;
char *nextHop;
};
union IPConfigValue
{
uint32_t integer;
char *string;
struct IPConfigLink link;
struct IPConfigRoute route;
};
struct IPConfigAttribute
{
enum IPConfigAttributeType type;
char *key;
union IPConfigValue value;
struct IPConfigAttribute *next;
};
struct IPConfig
{
uint32_t version;
struct IPConfigAttribute *attributes;
};
bool readPackedIPConfig(FILE *stream, struct IPConfig *config);
bool readUnpackedIPConfig(FILE *stream, struct IPConfig *config);
bool writePackedIPConfig(struct IPConfig *config, FILE *stream);
bool writeUnpackedIPConfig(struct IPConfig *config, FILE *stream);
void deinitializeIPConfig(struct IPConfig *config);
#endif

View File

@@ -0,0 +1,71 @@
#define _XOPEN_SOURCE
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include "ipconfig.h"
#include "error.h"
static void usage(FILE *stream)
{
fprintf(stream, "usage: ipconfigstore OPTION\n");
fprintf(stream, "\n");
fprintf(stream, "Options:\n");
fprintf(stream, " -p VERSION Pack IP configuration\n");
fprintf(stream, " -u Unpack IP configuration\n");
fprintf(stream, "\n");
}
int main(int argc, char *argv[])
{
int option = 0;
struct IPConfig config = {0};
option = getopt(argc, argv, "hp:u");
if (option == 'h')
{
usage(stdout);
return EXIT_SUCCESS;
}
else if (option == 'p')
{
config.version = *optarg - 0x30;
if (!readUnpackedIPConfig(stdin, &config))
{
return EXIT_FAILURE;
}
if (!writePackedIPConfig(&config, stdout))
{
return EXIT_FAILURE;
}
deinitializeIPConfig(&config);
}
else if (option == 'u')
{
if (!readPackedIPConfig(stdin, &config))
{
return EXIT_FAILURE;
}
if (!writeUnpackedIPConfig(&config, stdout))
{
return EXIT_FAILURE;
}
deinitializeIPConfig(&config);
}
else
{
usage(stderr);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}