forked from ooibc88/gam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cc
137 lines (118 loc) · 3.04 KB
/
util.cc
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
// Copyright (c) 2018 The GAM Authors
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include "util.h"
template<>
vector<string>& Split<string>(stringstream& ss, vector<string>& elems,
char delim) {
string item;
while (getline(ss, item, delim)) {
if (!item.empty())
elems.push_back(item);
}
return elems;
}
struct timespec init_time;
void init() __attribute__ ((constructor));
void fini() __attribute__ ((destructor));
void init() {
clock_gettime(CLOCK_REALTIME, &init_time);
}
void fini() {
}
/*
* initial time that is used to avoid long overflow
* return the current time in nanoseconds
*/
long get_time() {
// struct timeval start;
// gettimeofday(&start, NULL);
// return start.tv_sec*1000l*1000+start.tv_usec;
struct timespec start;
clock_gettime(CLOCK_REALTIME, &start);
return (start.tv_sec - init_time.tv_sec) * 1000l * 1000 * 1000
+ (start.tv_nsec - init_time.tv_nsec);;
}
//get the ip address of the first interface
string get_local_ip(const char* iface) {
int MAXINTERFACES = 16;
char *ip = NULL;
int fd, intrface;
struct ifreq buf[MAXINTERFACES];
struct ifconf ifc;
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) {
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = (caddr_t) buf;
if (!ioctl(fd, SIOCGIFCONF, (char *) &ifc)) {
intrface = ifc.ifc_len / sizeof(struct ifreq);
while (intrface-- > 0) {
if (!(ioctl(fd, SIOCGIFADDR, (char *) &buf[intrface]))) {
ip = (inet_ntoa(
((struct sockaddr_in*) (&buf[intrface].ifr_addr))->sin_addr));
if (!iface || strcmp(buf[intrface].ifr_name, iface) == 0) {
break;
}
}
}
}
close(fd);
}
return string(ip);
}
unsigned char* mac_eth(char *eth) {
#define HWADDR_len 6
int s, i;
int mac_len = 17;
unsigned char * MAC_str = (unsigned char*) malloc(sizeof(char) * 18); //13
struct ifreq ifr;
s = socket(AF_INET, SOCK_DGRAM, 0);
strcpy(ifr.ifr_name, eth);
ioctl(s, SIOCGIFHWADDR, &ifr);
for (i = 0; i < HWADDR_len - 1; i++) {
sprintf((char*) &MAC_str[i * 3], "%02x:",
((char*) ifr.ifr_hwaddr.sa_data)[i]);
}
sprintf((char*) &MAC_str[i * 3], "%02x", ((char*) ifr.ifr_hwaddr.sa_data)[i]);
MAC_str[mac_len] = '\0';
return MAC_str;
}
char* get_hostname() {
char *Name = (char*) malloc(150);
memset(Name, 0, 150);
gethostname(Name, 150);
return Name;
}
char* get_ipbyname(char* name) {
struct hostent *h;
char * ip;
/* get the host info */
if ((h = gethostbyname(name)) == NULL) {
herror("gethostbyname(): ");
exit(1);
} else {
ip = inet_ntoa(*((struct in_addr *) h->h_addr));
}
return ip;
}
// Windows
#ifdef _WIN32
#include <intrin.h>
uint64_t rdtsc() {
return __rdtsc();
}
// Linux/GCC
#else
uint64_t rdtsc() {
unsigned int lo, hi;
__asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
return ((uint64_t) hi << 32) | lo;
}
#endif