tcpflow  1.6.1
About: tcpflow is a TCP/IP packet demultiplexer that captures data transmitted as part of TCP connections (flows), and stores the data in a way that is convenient for protocol analysis and debugging.
  Fossies Dox: tcpflow-1.6.1.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

sbuf_private.h
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 #ifndef SBUF_PRIVATE_H
3 #define SBUF_PRIVATE_H
4 
5 #include <unistd.h>
6 
7 #ifdef HAVE_MMAP
8 #include <sys/mman.h>
9 #endif
10 
11 inline int sbuf_t::memcmp(const uint8_t *cbuf,size_t at,size_t len) const {
12  if(left(at) < len) throw sbuf_t::range_exception_t();
13  return ::memcmp(this->buf+at,cbuf,len);
14 }
15 
16 
17 /**
18  * Unsigned get interfaces
19  */
20 inline uint8_t sbuf_t::get8u(size_t i) const {
21  if(i+1>bufsize) throw sbuf_t::range_exception_t();
22  return this->buf[i];
23 }
24 
25 inline uint16_t sbuf_t::get16u(size_t i) const {
26  if(i+2>bufsize) throw sbuf_t::range_exception_t();
27  return 0
28  | (uint16_t)(this->buf[i+0]<<0)
29  | (uint16_t)(this->buf[i+1]<<8);
30 }
31 
32 inline uint32_t sbuf_t::get32u(size_t i) const {
33  if(i+4>bufsize) throw sbuf_t::range_exception_t();
34  return 0
35  | (uint32_t)(this->buf[i+0]<<0)
36  | (uint32_t)(this->buf[i+1]<<8)
37  | (uint32_t)(this->buf[i+2]<<16)
38  | (uint32_t)(this->buf[i+3]<<24);
39 }
40 
41 inline uint64_t sbuf_t::get64u(size_t i) const {
42  if(i+8>bufsize) throw sbuf_t::range_exception_t();
43  return 0
44  | ((uint64_t)(this->buf[i+0])<<0)
45  | ((uint64_t)(this->buf[i+1])<<8)
46  | ((uint64_t)(this->buf[i+2])<<16)
47  | ((uint64_t)(this->buf[i+3])<<24)
48  | ((uint64_t)(this->buf[i+4])<<32)
49  | ((uint64_t)(this->buf[i+5])<<40)
50  | ((uint64_t)(this->buf[i+6])<<48)
51  | ((uint64_t)(this->buf[i+7])<<56);
52 }
53 
54 inline uint8_t sbuf_t::get8uBE(size_t i) const {
55  if(i+1>bufsize) throw sbuf_t::range_exception_t();
56  return this->buf[i];
57 }
58 
59 inline uint16_t sbuf_t::get16uBE(size_t i) const {
60  if(i+2>bufsize) throw sbuf_t::range_exception_t();
61  return 0
62  | (uint16_t)(this->buf[i+1]<<0)
63  | (uint16_t)(this->buf[i+0]<<8);
64 }
65 
66 inline uint32_t sbuf_t::get32uBE(size_t i) const {
67  if(i+4>bufsize) throw sbuf_t::range_exception_t();
68  return 0
69  | (uint32_t)(this->buf[i+3]<<0)
70  | (uint32_t)(this->buf[i+2]<<8)
71  | (uint32_t)(this->buf[i+1]<<16)
72  | (uint32_t)(this->buf[i+0]<<24);
73 }
74 
75 inline uint64_t sbuf_t::get64uBE(size_t i) const {
76  if(i+8>bufsize) throw sbuf_t::range_exception_t();
77  return 0
78  | ((uint64_t)(this->buf[i+7])<<0)
79  | ((uint64_t)(this->buf[i+6])<<8)
80  | ((uint64_t)(this->buf[i+5])<<16)
81  | ((uint64_t)(this->buf[i+4])<<24)
82  | ((uint64_t)(this->buf[i+3])<<32)
83  | ((uint64_t)(this->buf[i+2])<<40)
84  | ((uint64_t)(this->buf[i+1])<<48)
85  | ((uint64_t)(this->buf[i+0])<<56);
86 }
87 
88 inline uint8_t sbuf_t::get8u(size_t i,sbuf_t::byte_order_t bo) const {
89  return bo==BO_LITTLE_ENDIAN ? get8u(i) : get8uBE(i);
90 }
91 
92 inline uint16_t sbuf_t::get16u(size_t i,sbuf_t::byte_order_t bo) const {
93  return bo==BO_LITTLE_ENDIAN ? get16u(i) : get16uBE(i);
94 }
95 
96 inline uint32_t sbuf_t::get32u(size_t i,sbuf_t::byte_order_t bo) const {
97  return bo==BO_LITTLE_ENDIAN ? get32u(i) : get32uBE(i);
98 }
99 
100 inline uint64_t sbuf_t::get64u(size_t i,sbuf_t::byte_order_t bo) const {
101  return bo==BO_LITTLE_ENDIAN ? get64u(i) : get64uBE(i);
102 }
103 
104 /**
105  * Signed get interfaces simply call the unsigned interfaces and
106  * the return gets cast.
107  */
108 inline int8_t sbuf_t::get8i(size_t i) const { return get8u(i);}
109 inline int16_t sbuf_t::get16i(size_t i) const { return get16u(i);}
110 inline int32_t sbuf_t::get32i(size_t i) const { return get32u(i);}
111 inline int64_t sbuf_t::get64i(size_t i) const { return get64u(i);}
112 inline int8_t sbuf_t::get8iBE(size_t i) const { return get8uBE(i);}
113 inline int16_t sbuf_t::get16iBE(size_t i) const { return get16uBE(i);}
114 inline int32_t sbuf_t::get32iBE(size_t i) const { return get32uBE(i);}
115 inline int64_t sbuf_t::get64iBE(size_t i) const { return get64uBE(i);}
116 
117 inline int8_t sbuf_t::get8i(size_t i,sbuf_t::byte_order_t bo) const {
118  return bo==BO_LITTLE_ENDIAN ? get8u(i) : get8uBE(i);
119 }
120 
121 inline int16_t sbuf_t::get16i(size_t i,sbuf_t::byte_order_t bo) const {
122  return bo==BO_LITTLE_ENDIAN ? get16u(i) : get16uBE(i);
123 }
124 
125 inline int32_t sbuf_t::get32i(size_t i,sbuf_t::byte_order_t bo) const {
126  return bo==BO_LITTLE_ENDIAN ? get32u(i) : get32uBE(i);
127 }
128 
129 inline int64_t sbuf_t::get64i(size_t i,sbuf_t::byte_order_t bo) const {
130  return bo==BO_LITTLE_ENDIAN ? get64u(i) : get64uBE(i);
131 }
132 
133 inline void sbuf_t::release()
134 {
135 #ifdef HAVE_MMAP
136  if(should_unmap && buf){
137  munmap((void *)buf,bufsize);
138  should_unmap = false;
139  buf = 0;
140  }
141 #endif
142  if(should_close && fd>0){
143  ::close(fd);
144  should_close = false;
145  fd=0;
146  }
147  if(should_free && buf){
148  free((void *)buf);
149  should_free = false;
150  buf = 0;
151  }
152  page_number = 0;
153  bufsize = 0;
154  pagesize = 0;
155 }
156 
157 #endif
bool should_close
Definition: sbuf.h:228
int8_t get8i(size_t i) const
Definition: sbuf_private.h:108
size_t bufsize
Definition: sbuf.h:248
int fd
Definition: sbuf.h:224
bool should_unmap
Definition: sbuf.h:225
int32_t get32i(size_t i) const
Definition: sbuf_private.h:110
int memcmp(const uint8_t *cbuf, size_t at, size_t len) const
Definition: sbuf_private.h:11
byte_order_t
Definition: sbuf.h:482
uint8_t get8u(size_t i) const
Definition: sbuf_private.h:20
int page_number
Definition: sbuf.h:234
uint32_t get32u(size_t i) const
Definition: sbuf_private.h:32
uint32_t get32uBE(size_t i) const
Definition: sbuf_private.h:66
size_t pagesize
Definition: sbuf.h:249
size_t left(size_t n) const
Definition: sbuf.h:379
uint64_t get64u(size_t i) const
Definition: sbuf_private.h:41
uint8_t get8uBE(size_t i) const
Definition: sbuf_private.h:54
const uint8_t * buf
Definition: sbuf.h:246
int8_t get8iBE(size_t i) const
Definition: sbuf_private.h:112
uint64_t get64uBE(size_t i) const
Definition: sbuf_private.h:75
bool should_free
Definition: sbuf.h:227
int16_t get16iBE(size_t i) const
Definition: sbuf_private.h:113
uint16_t get16u(size_t i) const
Definition: sbuf_private.h:25
void release()
Definition: sbuf_private.h:133
int64_t get64iBE(size_t i) const
Definition: sbuf_private.h:115
int32_t get32iBE(size_t i) const
Definition: sbuf_private.h:114
int64_t get64i(size_t i) const
Definition: sbuf_private.h:111
int16_t get16i(size_t i) const
Definition: sbuf_private.h:109
uint16_t get16uBE(size_t i) const
Definition: sbuf_private.h:59
@ BO_LITTLE_ENDIAN
Definition: sbuf.h:482
unsigned int uint32_t
Definition: core.h:40
void munmap(void *buf, size_t size)
Definition: util.cpp:238
unsigned short uint16_t
Definition: util.h:7
unsigned char uint8_t
Definition: util.h:6