-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmtp_utils.h
153 lines (132 loc) · 3.92 KB
/
mtp_utils.h
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
#ifndef MTP_UTILS_H
#define MTP_UTILS_H
/*
* Standard library imports.
*/
#include <stdint.h>
#include <stdio.h>
#include <sys/types.h>
#include <netdb.h>
#include <netinet/if_ether.h>
#include <time.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <ifaddrs.h>
#include <net/ethernet.h>
#include <netinet/ether.h>
#include <linux/if_packet.h>
/*
* Custom MTP imports.
*/
// None
/*****************************************
* CONSTANTS
*****************************************/
#define MTP_TYPE_HELLONR_MSG 1 // Hello message with no repsonse
#define MTP_TYPE_HELLOWR_MSG 2 // Hello message with repsonse
#define MTP_TYPE_JOIN_REQ 3 // Join Request
#define MTP_TYPE_JOIN_RES 4 // Join Response
#define MTP_TYPE_JOIN_ACK 5 // Join ACK
#define MTP_TYPE_KEEP_ALIVE 6
#define MTP_TYPE_DATA_MSG 7
#define MTP_TYPE_FAILURE_UPDATE 8
#define MTP_TYPE_RECOVER_UPDATE 9
#define MTP_TYPE_START_HELLO 10
#define MAX_BUFFER_SIZE 2048
#define ETH_LEN 25 // Ethernet interface naming length (ex: eth1).
#define VID_LEN 64
#define HELLO_TIMER 100 // prior 10 in GENI.
#define DEAD_TIMER 250 // prior 25 in GENI.
#define DETECT_FAIL 1
#define MISS_FAIL 2
#define ETH_MTP_CTRL 0x8850 // MTP Ethertype (custom, it is a currently unregistered value).
#define ETH_IP_CTRL 0x0800 // IPv4 Ethertype (offical registered value).
#define UNREACHABLE_OPTION 1
#define REACHABLE_OPTION 0
#define ADD_OPERATION 1
#define REMOVE_OPERATION 0
/*****************************************
* STRUCTURES
*****************************************/
// None
/*****************************************
* FUNCTION PROTOTYPES
*****************************************/
/**
* @brief Get the all ethernet interface2 object
*
* @param dest destination array to store ethernet interface name
* @return uint8_t total working ethernet interface
*/
uint8_t get_all_ethernet_interface2(char** dest);
/**
* @brief Get the VID by ethernet interface object
*
* @param dest store VID in this pointer
* @param ethernet_interface_name which interface
* @param octet get which byte of IP address as VID, 3rd byte is default
*/
void getRootVID(char *dest, char *ethernet_interface_name, uint8_t octet);
/**
* @brief check whether the provided ethernet interface name is alive
*
* @param port_array ethernet interface list
* @param port_array_size ethernet interface size
* @param port_name provided interface name
* @return int 0 for false, 1 for true
*/
int check_port_is_alive(char** port_array, uint8_t port_array_size,char* port_name);
/**
* @brief Get the tier from hello message payload
*
* @param payload_with_VID_data get tier
* @return uint8_t
*/
uint8_t get_tier_from_hello_message(char *payload_with_VID_data);
/**
* @brief append port number after VID
*
* @param port_name which port want to choose
* @param dest append after this pointer
*/
void append_port_number_after_VID(char *port_name, char *dest);
/**
* @brief extract VIDs from payload
*
* @param VID_array store VIDs in here
* @param recvBuff_start_ptr payload
* @param with_debug
* @return uint16_t
*/
uint16_t extract_VID_from_receive_buff(char **VID_array, char *recvBuff_start_ptr,int with_debug);
/**
* @brief hashing algorithm for load balancing, for more https://en.wikipedia.org/wiki/Jenkins_hash_function
*
* @param key key to be hashed
* @param len length of key
* @return uint32_t a hashcode
*/
uint32_t jenkins_one_at_a_time_hash(uint8_t *key, size_t len);
/**
* @brief convert integer to string
*
* @param dest_storage store string in here
* @param number number to be converted
* @return size_t
*/
size_t int_to_str(char *dest_storage, unsigned int number);
/**
* @brief Get current time in milli second
*
* @param current_time
* @return long long milli second
*/
long long get_milli_sec(struct timeval* current_time);
#endif