summaryrefslogtreecommitdiff
path: root/common/src/utils.cpp
blob: 8fb944a5397556fef699ab17b28e52e75bbf2758 (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
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <net/if.h>
#include <netinet/ip.h>
#include <netinet/ether.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>

#include "utils.h"
#include "log.h"

/******************************************************************************
 * uuid_array
 ******************************************************************************/

void uuid_array_init(struct uuid_array *array)
{
    memset(array, 0, sizeof(uuid_array));
    array->num = 0;
    array->size = sizeof(array->uuids) / sizeof(array->uuids[0]);
}

void uuid_array_append(struct uuid_array *array, uuid_t uuid)
{
    if (array->num < array->size)
    {
        uuid_copy(array->uuids[array->num], uuid);
        array->num++;
    }
    else
    {
        LOG_ERROR("%s: uuid array add elem too much !!!", LOG_TAG_UTILS);
    }
}

void uuid_array_remove(struct uuid_array *array, uuid_t uuid)
{
    for (int i = 0; i < array->num; i++)
    {
        if (uuid_compare(array->uuids[i], uuid) == 0)
        {
            if (i + 1 != array->size)
            {
                memmove(&(array->uuids[i]), &(array->uuids[i + 1]), sizeof(array->uuids[0]) * (array->num - i - 1));
            }
            i--;
            array->num--;
        }
    }
}

int uuid_array_contains(struct uuid_array *array, uuid_t uuid)
{
    for (int i = 0; i < array->num; i++)
    {
        if (uuid_compare(array->uuids[i], uuid) == 0)
        {
            return 1;
        }
    }

    return 0;
}

int uuid_array_is_full(struct uuid_array *array)
{
    if (array->num == array->size)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int uuid_array_get_count(struct uuid_array *array)
{
    if (array)
    {
        return array->num;
    }
    else
    {
        return 0;
    }
}

uuid_t *uuid_array_get_at(struct uuid_array *array, int index)
{
    if (index >= array->num)
    {
        assert(0);
        return NULL;
    }
    else
    {
        return &(array->uuids[index]);
    }
}

/******************************************************************************
 * device
 ******************************************************************************/

int get_ip_by_device_name(const char *dev_name, char *ip_str)
{
    int fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (fd == -1)
    {
        return -1;
    }

    struct ifreq ifr;
    memset(&ifr, 0, sizeof(struct ifreq));
    ifr.ifr_addr.sa_family = AF_INET;
    strcpy(ifr.ifr_name, dev_name);
    if (ioctl(fd, SIOCGIFADDR, &ifr) != 0)
    {
        close(fd);
        return -1;
    }

    strcpy(ip_str, inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));
    close(fd);

    return 0;
}

int get_mac_by_device_name(const char *dev_name, char *mac_str)
{
    int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
    if (fd == -1)
    {
        return -1;
    }

    struct ifreq ifr;
    memset(&ifr, 0, sizeof(struct ifreq));
    strcpy(ifr.ifr_name, dev_name);
    if (ioctl(fd, SIOCGIFHWADDR, &ifr) != 0)
    {
        close(fd);
        return -1;
    }

    u_char *mac = (u_char *)ifr.ifr_hwaddr.sa_data;
    sprintf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
    close(fd);

    return 0;
}

int str_to_mac(const char *mac_str, u_char mac[])
{
    if (sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", &(mac[0]), &(mac[1]), &(mac[2]), &(mac[3]), &(mac[4]), &(mac[5])) == 6)
    {
        return 0;
    }
    else
    {
        return -1;
    }
}