aboutsummaryrefslogtreecommitdiff
path: root/Semestr 4/sieci/pracownia2/router/network_addr.c
blob: a65f48ca94e87feaebd5b863a3312d641b75087d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "network_addr.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct in_addr _get_broadcast_address(struct in_addr addr, uint16_t netmask) {
  struct in_addr result = addr;
  /* bitshift by more than 31 is UB */
  if (netmask < 32) {
    result.s_addr |= ~((1<<netmask) - 1);
  }
  return result;
}

struct in_addr _get_network_address(struct in_addr addr, uint16_t netmask) {
  struct in_addr result = addr;
  if (netmask == 0) {
    addr.s_addr = 0;
  }
  else {
    result.s_addr &= ~((1 << (32 - netmask)) - 1);
  }
  return result;
}

struct in_addr get_broadcast_address(struct network_addr na) {
  return _get_broadcast_address(na.addr, na.netmask);
}

struct in_addr get_network_address(struct network_addr na) {
  return _get_network_address(na.addr, na.netmask);
}

void pretty_print(struct network_addr na) {
  char ip_addr[20];
  inet_ntop(AF_INET, &na.addr, ip_addr, sizeof(ip_addr));
  printf("%s/%d distance %d\n", ip_addr, na.netmask, na.distance);
}

struct network_addr stora(char *str) {
  struct network_addr  result;
  char                addr[20];
  size_t              ip_preffix = strcspn(str, "/");
  
  strncpy(addr, str, strlen(str));
  addr[ip_preffix] = 0;
  inet_pton(AF_INET, addr, &(result.addr));
  result.netmask = atoi(str + ip_preffix + 1);
  return result;
}