aboutsummaryrefslogtreecommitdiffstats
path: root/hicn-light/src/utils/interface.c
blob: ab7a88f0f3a673b157dd7f874b755224f45d105f (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <src/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <parc/algol/parc_ArrayList.h>
#include <parc/algol/parc_BufferComposer.h>
#include <parc/algol/parc_Memory.h>
#include <parc/algol/parc_Object.h>
#include <src/utils/addressList.h>
#include <src/utils/interface.h>

#include <parc/assert/parc_Assert.h>
#include <src/utils/commands.h>

struct interface {
  char *name;
  unsigned interfaceIndex;
  bool loopback;
  bool supportMulticast;
  unsigned mtu;

  AddressList *addressList;
};

char *interfaceToString(const Interface *interface) {
  PARCBufferComposer *composer = parcBufferComposer_Create();

  parcBufferComposer_Format(
      composer, "%3u %10s %1s%1s %8u ", interface->interfaceIndex,
      interface->name, interface->loopback ? "l" : " ",
      interface->supportMulticast ? "m" : " ", interface->mtu);

  for (size_t i = 0; i < addressListLength(interface->addressList); i++) {
    addressBuildString(addressListGetItem(interface->addressList, i), composer);
    if (i < (addressListLength(interface->addressList) - 1)) {
      parcBufferComposer_PutStrings(composer, "\n", NULL);
    }
  }

  PARCBuffer *tempBuffer = parcBufferComposer_ProduceBuffer(composer);
  char *result = parcBuffer_ToString(tempBuffer);
  parcBuffer_Release(&tempBuffer);
  parcBufferComposer_Release(&composer);
  return result;
}

Interface *interfaceCreate(const char *name, unsigned interfaceIndex,
                           bool loopback, bool supportMulticast, unsigned mtu) {
  Interface *iface = parcMemory_AllocateAndClear(sizeof(Interface));

  parcAssertNotNull(iface, "parcMemory_AllocateAndClear(%zu) returned NULL",
                    sizeof(Interface));
  iface->name = parcMemory_StringDuplicate(name, 64);
  iface->interfaceIndex = interfaceIndex;
  iface->loopback = loopback;
  iface->supportMulticast = supportMulticast;
  iface->mtu = mtu;
  iface->addressList = addressListCreate();

  return iface;
}

void interfaceDestroy(Interface **interfacePtr) {
  parcAssertNotNull(interfacePtr, "Parameter must be non-null double pointer");
  parcAssertNotNull(*interfacePtr,
                    "Parameter must dereference to non-null pointer");

  Interface *iface = *interfacePtr;
  parcMemory_Deallocate((void **)&iface->name);
  addressListDestroy(&iface->addressList);
  parcMemory_Deallocate((void **)&iface);
  interfacePtr = NULL;
}

void interfaceAddAddress(Interface *iface, Address *address) {
  parcAssertNotNull(iface, "Parameter iface must be non-null");

  size_t length = addressListLength(iface->addressList);
  for (size_t i = 0; i < length; i++) {
    const Address *a = addressListGetItem(iface->addressList, i);
    if (addressEquals(a, address)) {
      return;
    }
  }

  addressListAppend(iface->addressList, address);
}

const AddressList *interfaceGetAddresses(const Interface *iface) {
  parcAssertNotNull(iface, "Parameter iface must be non-null");
  return iface->addressList;
}

unsigned interfaceGetInterfaceIndex(const Interface *iface) {
  parcAssertNotNull(iface, "Parameter iface must be non-null");
  return iface->interfaceIndex;
}

bool interfaceNameEquals(const Interface *iface, const char *name) {
  parcAssertNotNull(iface, "Parameter iface must be non-null");

  if (strcasecmp(iface->name, name) == 0) {
    return true;
  }
  return false;
}

bool interfaceEquals(const Interface *a, const Interface *b) {
  if (a == NULL && b == NULL) {
    return true;
  }

  if (a == NULL || b == NULL) {
    return false;
  }

  if (a->interfaceIndex == b->interfaceIndex) {
    if (a->loopback == b->loopback) {
      if (a->supportMulticast == b->supportMulticast) {
        if (a->mtu == b->mtu) {
          if (strcasecmp(a->name, b->name) == 0) {
            if (addressListEquals(a->addressList, b->addressList)) {
              return true;
            }
          }
        }
      }
    }
  }
  return false;
}

// static const char light_Iface[] = "Interface";
// static const char light_IfName[] = "Name";
// static const char light_IFIDX[] = "Index";
// static const char light_IsLoopback[] = "Loopback";
// static const char light_Multicast[] = "Multicast";
// static const char light_MTU[] = "MTU";

// static const char light_True[] = "true";
// static const char light_False[] = "false";
// static const char light_Addrs[] = "Addrs";

const char *interfaceGetName(const Interface *iface) {
  parcAssertNotNull(iface, "Parameter iface must be non-null");
  return iface->name;
}

unsigned interfaceGetMTU(const Interface *iface) {
  parcAssertNotNull(iface, "Parameter iface must be non-null");
  return iface->mtu;
}