82 lines
2.0 KiB
C
82 lines
2.0 KiB
C
#include <stdint.h>
|
|
#include <stddef.h>
|
|
#include <string.h>
|
|
#include <syscall.h>
|
|
|
|
static void print(const char* s) {
|
|
if (s) { SYS_WRITE_CONSOLE_REQUEST req = { SysSvFromCstr(s) }; SysWriteConsole(&req); }
|
|
}
|
|
|
|
static void print_char(char c) {
|
|
SYS_WRITE_CONSOLE_REQUEST req = { SysSvFromParts(&c, 1) };
|
|
SysWriteConsole(&req);
|
|
}
|
|
|
|
static void print_u64(uint64_t v) {
|
|
char buf[32];
|
|
int n = 0;
|
|
if (v == 0) { print("0"); return; }
|
|
while (v && n < (int)sizeof(buf)) {
|
|
buf[n++] = (char)('0' + (v % 10));
|
|
v /= 10;
|
|
}
|
|
while (n > 0) print_char(buf[--n]);
|
|
}
|
|
|
|
static const char* next_token(const char* s, char* out, size_t outSize) {
|
|
if (!s || !out || outSize == 0) return NULL;
|
|
while (*s == ' ' || *s == '\t') s++;
|
|
if (*s == 0) { out[0] = 0; return NULL; }
|
|
|
|
size_t n = 0;
|
|
while (*s && *s != ' ' && *s != '\t') {
|
|
if (n + 1 < outSize) out[n++] = *s;
|
|
s++;
|
|
}
|
|
out[n] = 0;
|
|
return s;
|
|
}
|
|
|
|
static void print_ipv4(const uint8_t ip[4]) {
|
|
print_u64(ip[0]); print("."); print_u64(ip[1]); print(".");
|
|
print_u64(ip[2]); print("."); print_u64(ip[3]);
|
|
}
|
|
|
|
int main(void) {
|
|
char name[256];
|
|
const char* cmd = SysGetPeb()->CommandLine.Data;
|
|
cmd = next_token(cmd, name, sizeof(name)); // program path
|
|
(void)cmd;
|
|
|
|
name[0] = 0;
|
|
if (cmd)
|
|
(void)next_token(cmd, name, sizeof(name));
|
|
if (!name[0]) {
|
|
print("usage: resolve.exe hostname\n");
|
|
SysExitProcess(1);
|
|
}
|
|
|
|
SYS_SOCKET_ADDRESS_IPV4 addr;
|
|
size_t addrSize = sizeof(addr);
|
|
SYS_RESOLVE_HOST_REQUEST req;
|
|
memset(&addr, 0, sizeof(addr));
|
|
memset(&req, 0, sizeof(req));
|
|
req.Name = SysSvFromCstr(name);
|
|
req.Family = SYS_AF_INET;
|
|
req.AddressOut = &addr;
|
|
req.AddressSizeInOut = &addrSize;
|
|
|
|
uint32_t st = SysResolveHost(&req);
|
|
if (st != SYS_STATUS_SUCCESS) {
|
|
print("resolve: SysResolveHost failed "); print_u64(st); print("\n");
|
|
SysExitProcess(1);
|
|
}
|
|
|
|
print(name);
|
|
print(" -> ");
|
|
print_ipv4(addr.Address);
|
|
print("\n");
|
|
SysExitProcess(0);
|
|
return 0;
|
|
}
|