NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
NetSim_utility.h
1/************************************************************************************
2* Copyright (C) 2023 *
3* TETCOS, Bangalore. India *
4* *
5* Tetcos owns the intellectual property rights in the Product and its content. *
6* The copying, redistribution, reselling or publication of any or all of the *
7* Product or its content without express prior written consent of Tetcos is *
8* prohibited. Ownership and / or any other right relating to the software and all *
9* intellectual property rights therein shall remain at all times with Tetcos. *
10* *
11* This source code is licensed per the NetSim license agreement. *
12* *
13* No portion of this source code may be used as the basis for a derivative work, *
14* or used, for any purpose other than its intended use per the NetSim license *
15* agreement. *
16* *
17* This source code and the algorithms contained within it are confidential trade *
18* secrets of TETCOS and may not be used as the basis for any other software, *
19* hardware, product or service. *
20* *
21* Author: Shashi Kant Suman *
22* *
23* ----------------------------------------------------------------------------------*/
24
25
26
27#ifndef _NETSIM_UTILITY_H_
28#define _NETSIM_UTILITY_H_
29#ifndef _CRT_SECURE_NO_DEPRECATE
30#define _CRT_SECURE_NO_DEPRECATE
31#endif
32#ifndef _CRT_SECURE_NO_WARNINGS
33#define _CRT_SECURE_NO_WARNINGS
34#endif
35
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <stdbool.h>
40#ifdef _WIN32
41#include <Windows.h>
42#else
43#include "stdarg.h"
44#include "Linux.h"
45#endif
46#include <time.h>
47#include <stdint.h>
48
49
50#ifdef __cplusplus
51extern "C" {
52#endif
53
54#ifdef _WIN32
55#ifndef _NETSIM_UTILITY_CODE_
56#pragma comment(lib,"NetSim_utility.lib")
57#define NETSIM_UTILITY_EXPORT _declspec(dllimport)
58#else
59#define NETSIM_UTILITY_EXPORT _declspec(dllexport)
60#endif
61#else
62#ifndef _NETSIM_UTILITY_CODE_
63#define NETSIM_UTILITY_EXPORT extern
64#else
65#define NETSIM_UTILITY_EXPORT
66#endif
67#endif
68
69 struct ptrMXArray {
70 size_t dimn;
71 size_t* shape;
72 double* arr;
73 };
74
75 typedef struct stru_pcap_writer
76 {
77 FILE* pFile;
78 time_t start_time;
79 HANDLE hPipe;
80 }PCAP_WRITER, * PPCAP_WRITER;
81
82 typedef struct stru_wireshark_trace
83 {
84 PPCAP_WRITER** pcapWriterlist;
85 void (*convert_sim_to_real_packet)(void* netsim_packet, void* pcap_handle, double dTime);
86 }WIRESHARKTRACE, * PWIRESHARKTRACE;
87
88 NETSIM_UTILITY_EXPORT WIRESHARKTRACE wireshark_trace;
89
90 //Function prototype
91
92 //Used to get path for any file
93 _declspec(dllexport) void fnGetFilePath(char* name, char* path);
94
95 //Used to get options index form command
96 _declspec(dllexport) int fnParseOption(int count, char* arg[], char* opt);
97
98 //Used to get current time in string
99 _declspec(dllexport) void GetTimeStr(char* date, int len);
100
101 //Used to print the log file with thread id and time
102 _declspec(dllexport) FILE* open_log_file(char* filename, char* mode);
103 _declspec(dllexport) int close_log_file(FILE* fp);
104 _declspec(dllexport) int ReportLog(FILE* fp, const char* msg, ...);
105
106 /* Strip whitespace chars off end of given string, in place. Return s. */
107 _declspec(dllexport) char* rstrip(char* s);
108
109 /* Return pointer to first non-whitespace char in given string. */
110 _declspec(dllexport) char* lskip(const char* s);
111
112 /* Return pointer to first char c or ';' comment in given string, or pointer to
113 null at end of string if neither found. ';' must be prefixed by a whitespace
114 character to register as a comment. */
115 _declspec(dllexport) char* find_char_or_comment(const char* s, char c);
116
117 /* Version of strncpy that ensures dest (size bytes) is null-terminated. */
118 _declspec(dllexport) char* strncpy0(char* dest, const char* src, size_t size);
119
120 /* Strip the word out of string*/
121 _declspec(dllexport) char* find_word(char** s);
122
123 /* To check whether a string is start with specified string*/
124 _declspec(dllexport) bool startsWith(const char* str, const char* pre);
125
126 /* Used to init the start_time. Must be called once */
127 _declspec(dllexport) void init_time();
128
129 /* Used to get current time in microsecond */
130 _declspec(dllexport) uint64_t em_current_time();
131
132 /* Used to initialize the start time*/
133 _declspec(dllexport) void em_init_time(void);
134
135 /* Used to get start time */
136 _declspec(dllexport) struct tm get_start_time();
137
138 /* Sleep for µs */
139 _declspec(dllexport) void usleep(__int64 usec);
140
141 /* used to get file name without extension */
142 _declspec(dllexport) char* get_filename_without_extension(char* filename);
143
144 /* Used to prepare filter string for wireshark */
145 _declspec(dllexport) void create_wireshark_filter(__out char* filter, __in int len, __in char* src_ip, __in char* dst_ip);
146
147 /* Call wireshark */
148 _declspec(dllexport) void call_wireshark(int duration, char* filter_string);
149
150 /* open pcap file for writing */
151 _declspec(dllexport) void* open_pcap_file(const char* filename, char* pipename, uint32_t network);
152
153 /* write packet to pcap file */
154 _declspec(dllexport) void write_to_pcap(void* handle, unsigned char* packet, unsigned int len, double* dTime);
155
156 /* close the pcap write */
157 _declspec(dllexport) void close_pcap_file(void* pcap_handle);
158
159 /*Bit wise Operation*/
160#define SET_BIT(val, bitIndex) ((val) |= (1 << (bitIndex)))
161#define SET_BIT_64(val, bitIndex) ((val) |= (1i64 << (bitIndex)))
162#define CLEAR_BIT(val, bitIndex) ((val) &= ~(1 << (bitIndex)))
163#define CLEAR_BIT_64(val, bitIndex) ((val) &= ~(1i64 << (bitIndex)))
164#define TOGGLE_BIT(val, bitIndex) ((val) ^= (1 << (bitIndex)))
165#define TOGGLE_BIT_64(val, bitIndex) ((val) ^= (1i64 << (bitIndex)))
166#define BIT_IS_SET(val, bitIndex) ((val) & (1 << (bitIndex)))
167#define BIT_IS_SET_64(val, bitIndex) ((val) & (1i64 << (bitIndex)))
168#define LSHIFTCHAR(x,eposition) ((UINT8)(x << (8 - eposition)))
169#define RSHIFTCHAR(x,eposition) ((UINT8)(x >> (8 - eposition)))
170
171#define htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
172 _declspec(dllexport) int isBitSet(unsigned int num, int index);
173 _declspec(dllexport) unsigned int setBit(unsigned int num, int index);
174 _declspec(dllexport) unsigned int unsetBit(unsigned int num, int index);
175 _declspec(dllexport) UINT64 hex_to_decimal(char hex[]);
176 _declspec(dllexport) char* hex_to_binary(char* hex, char* bin);
177 _declspec(dllexport) void binary_to_hex(char* bin, char* hex);
178 _declspec(dllexport) UINT64 binary_to_decimal(char bin[]);
179 _declspec(dllexport) UINT8 maskChar(UINT8 sposition, UINT8 eposition);
180
181 _declspec(dllexport) bool find_interaction(double arg1[2],
182 double arg2[2],
183 double intersection[2]);
184
185 //MatLab interface
186 _declspec(dllexport) void netsim_matlab_interface_configure(char* appPath);
187 _declspec(dllexport) void netsim_matlab_interface_close();
188 _declspec(dllexport) void netsim_matlab_send_command(void* command, int len); //Don't use. Under development
189 _declspec(dllexport) void netsim_matlab_send_ascii_command(char* format, ...);
190 _declspec(dllexport) bool netsim_matlab_get_value(char* out, int outLen, char* name, char* type);
191 _declspec(dllexport) struct ptrMXArray* netsim_matlab_get_array(char* name);
192 _declspec(dllexport) void netsim_matlab_send_array(double* arr, size_t* shape, size_t dimn, char* name);
193
194#ifdef __cplusplus
195}
196#endif
197#endif //_NETSIM_UTILITY_H_