summaryrefslogtreecommitdiffstats
path: root/src/common/basic_utils.cpp
blob: fded49ece0e6c5129cabca39e4a6fb79d3681610 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
Copyright (c) 2015-2015 Cisco Systems, Inc.

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 "basic_utils.h"
#include <ctype.h>
#include <stdio.h>
#include <string>
#include <sstream>
#include <sys/resource.h>

#include "pal_utl.h"

int my_inet_pton4(const char *src, unsigned char *dst);

bool utl_is_file_exists (const std::string& name) {
    if (FILE *file = fopen(name.c_str(), "r")) {
        fclose(file);
        return true;
    } else {
        return false;
    }   
}

void utl_k12_pkt_format(FILE* fp,void  * src,  unsigned int size) {
    unsigned int i;
    fprintf(fp,"\n");
    fprintf(fp,"+---------+---------------+----------+\n");
    fprintf(fp,"00:00:00,000,000   ETHER \n");
    fprintf(fp,"|0   |");
    for ( i=0; i<size;i++ ) {
        fprintf(fp,"%02x|",((unsigned char *)src)[i]);
    }
    fprintf(fp,"\n");;
}


void utl_DumpBuffer(FILE* fp,void  * src,  unsigned int size,int offset) {
    unsigned int i;
    for ( i=0; i<size;i++ ) {
        if ( (!(i%16)) && (i!=0) ) fprintf(fp,"]\n");
        if ( !(i%4) && !(i%16) ) fprintf(fp,"[");
        if ( !(i%4) &&  (i%16) ) fprintf(fp,"] [");
        fprintf(fp,"%02x",((unsigned char *)src)[i + offset]);
        if ( (i+1)%4 ) fprintf(fp," ");
    }
    for ( ;i%4;i++ )
        fprintf(fp,"  ");
    fprintf(fp,"]");

    fprintf(fp,"\n");;
}


void utl_DumpChar(FILE* fd, 
                  void  * src,  
                  unsigned int eln, 
                  unsigned int width 
                  ){
    int size=eln*width;
    unsigned char * p=(unsigned char *)src;
    int i;
    fprintf(fd," - ");
    for (i=0; i<size;i++) {
        if ( isprint(*p) ) {
          fprintf(fd,"%c",*p);
        }else{
            fprintf(fd,"%c",'.');
        }
        p++;
    }
}

void utl_DumpBufferLine(FILE* fd, 
                        void  * src,  
                        int     offset,
                        unsigned int eln, 
                        unsigned int width ,
                        unsigned int mask){
    unsigned char * p=(unsigned char *)src;
    uint32 addr;
    
    if ( mask & SHOW_BUFFER_ADDR_EN){
        addr=offset;
        fprintf(fd,"%08x: ",(int)addr);
    }
    int i;
    for (i=0; i<(int)eln; i++) {
        switch (width) {
            case 1:
                fprintf(fd,"%02x ",*p);
                p++;
                break;
            case 2:
                fprintf(fd,"%04x ",*((uint16 *)p));
                p+=2;
                break;
            case 4:
                fprintf(fd,"%08x ",*((int *)p));
                p+=4;
                break;
            case 8:
                fprintf(fd,"%08x",*((int *)p));
                fprintf(fd,"%08x ",*((int *)(p+4)));
                p+=8;
                break;
        }
    }
    if (mask & SHOW_BUFFER_CHAR) {
       utl_DumpChar(fd, src,eln,width);
    }
    fprintf(fd,"\n");
}

void utl_DumpBuffer2(FILE* fd,
                     void  * src,  
                     unsigned int size, //buffer size
                     unsigned int width ,
                     unsigned int width_line ,
                     unsigned int mask
                     ) {
    if (!( (width==1) || (width==2) || (width==4) || (width==8) )){
        width=1;
    }

    int nlen=(size)/(width_line );
    if ( ( (size % width_line))!=0 ) {
        nlen++;
    }
    int i;
    char *p=(char *)src;
    int offset=0;

    if (mask & SHOW_BUFFER_ADDR_EN){
        if (mask & SHOW_BUFFER_ADDR) {
            offset=(int)((uintptr_t)p);
        }else{
            offset=0;
        }
    }
    unsigned int eln_w;  
    int len_exist=size;
    
    for (i=0; i<nlen; i++) {
      if ( len_exist > (int)(width_line /width) ){  
        eln_w=width_line /width;
      }else{
        eln_w=(len_exist+width-1)/width;
      }
      utl_DumpBufferLine(fd, p,offset,eln_w,width,mask);
      p+=width_line;
      offset+=width_line;
      len_exist-=  width_line;
    }
}                              


void TestDump(void){

    char buffer[100];
    int i;
    for (i=0;i<100;i++) {
        buffer[i]=0x61+i;
    }


    utl_DumpBuffer2(stdout,buffer,31,1,4,SHOW_BUFFER_ADDR_EN |SHOW_BUFFER_CHAR);
}

void utl_macaddr_to_str(const uint8_t *macaddr, std::string &output) {
    
    for (int i = 0; i < 6; i++) {
        char formatted[4];

        if (i == 0) {
            snprintf(formatted, sizeof(formatted), "%02x", macaddr[i]);
        } else {
            snprintf(formatted, sizeof(formatted), ":%02x", macaddr[i]);
        }

        output += formatted;
    }

}

std::string utl_macaddr_to_str(const uint8_t *macaddr) {
    std::string tmp;
    utl_macaddr_to_str(macaddr, tmp);
    
    return tmp;
}

bool utl_str_to_macaddr(const std::string &s, uint8_t *mac) {
    int last = -1;
    int rc = sscanf(s.c_str(), "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx%n",
                    mac + 0, mac + 1, mac + 2, mac + 3, mac + 4, mac + 5,
                    &last);

    if ( (rc != 6) || (s.size() != last) ) {
        return false;
    }
    
    return true;
}

/**
 * generate a random connection handler
 * 
 */
std::string 
utl_generate_random_str(unsigned int &seed, int len) {
    std::stringstream ss;

    static const char alphanum[] =
        "0123456789"
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz";

    /* generate 8 bytes of random handler */
    for (int i = 0; i < len; ++i) {
        ss << alphanum[rand_r(&seed) % (sizeof(alphanum) - 1)];
    }

    return (ss.str());
}

/**
 * define the coredump size 
 * allowed when crashing 
 * 
 * @param size - -1 means unlimited
 * @param map_huge_pages - should the core map the huge TLB 
 *                       pages
 */
void utl_set_coredump_size(long size, bool map_huge_pages) {
    int mask;
    struct rlimit core_limits;
    
    if (size == -1) {
        core_limits.rlim_cur = core_limits.rlim_max = RLIM_INFINITY;
    } else {
        core_limits.rlim_cur = core_limits.rlim_max = size;
    }

    setrlimit(RLIMIT_CORE, &core_limits);

    /* set core dump mask */
    FILE *fp = fopen("/proc/self/coredump_filter", "wb");
    if (!fp) {
        printf("failed to open /proc/self/coredump_filter\n");
        exit(-1);
    }

    /* huge pages is the 5th bit */
    if (map_huge_pages) {
        mask = 0x33;
    } else {
        mask = 0x13;
    }
    
    fprintf(fp, "%08x\n", mask);
    fclose(fp);
}

bool utl_ipv4_to_uint32(const char *ipv4_str, uint32_t &ipv4_num) {
    
    uint32_t tmp;
    
    int rc = my_inet_pton4(ipv4_str, (unsigned char *)&tmp);
    if (!rc) {
        return false;
    }
    
    ipv4_num = PAL_NTOHL(tmp);
    
    return true;
}
   
std::string utl_uint32_to_ipv4(uint32_t ipv4_addr) {
    std::stringstream ss;
    ss << ((ipv4_addr >> 24) & 0xff) << "." << ((ipv4_addr >> 16) & 0xff) << "." << ((ipv4_addr >> 8) & 0xff) << "." << (ipv4_addr & 0xff);
    return ss.str();
}