From bac15e7edf532c4a87f0f219c8f70d93d4c7e752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20Luka=20=C5=A0ijanec?= Date: Tue, 18 Jan 2022 14:05:06 +0100 Subject: dela --- host.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'host.c') diff --git a/host.c b/host.c index b6e68dd..2087761 100644 --- a/host.c +++ b/host.c @@ -21,22 +21,23 @@ struct in_net { POPCNT(32) unsigned int power2[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648 }; /* ah yes, libmath */ /* not needed end */ -struct in_addr host (struct in_net n, int h /* consecutive number of the host in the network */) { +struct in_net host (struct in_net n, unsigned long long int h /* number of host in the network */) { n.addr.s_addr = ntohl(n.addr.s_addr & n.mask.s_addr); n.mask.s_addr = ntohl(n.mask.s_addr); - int c = 1; - int s = 0; - for (int i = 0; i < 32; i++) + unsigned long long int c = 1; + unsigned long int s = 0; + for (unsigned long int i = 0; i < 32; i++) if (1 << i & ~n.mask.s_addr) { if (1 << s++ & h) n.addr.s_addr |= 1 << i; - c = c * 2; - } - if (h >= c) - n.addr.s_addr = 0; + c *= 2; /* if we instead indicated error via addr and */ + } /* returned just addr, it would be impossible to */ + n.mask.s_addr = INADDR_BROADCAST; /* scan 0.0.0.0/0 without immediately detecting */ + if (h >= c) /* this false "error" and address 0.0.0.0 would */ + n.mask.s_addr = 0; /* in fact actually be correct. */ n.addr.s_addr = htonl(n.addr.s_addr); - return n.addr; -} + return n; /* \/= this means host h is outside network */ +} /* returns struct in_net: if .mask is not 255.255.255.255 (INADDR_BROADCAST), .addr is incorrect */ int resolve (const char * d, uint32_t * r) { struct addrinfo hints = { .ai_family = AF_INET, -- cgit v1.2.3