/* 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 #include #include #include #include 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 (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; } } /** * 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); }