The following function returns a char*:
// Returns the pointer to the actual packet data.
// @param pkt_handle The buffer handle.
// @param queue The queue from which the packet is arrived or destined.
// @return The pointer on success, NULL otherwise.
u_char * pfring_zc_pkt_buff_data(
pfring_zc_pkt_buff *pkt_handle,
pfring_zc_queue *queue
);
and the following function takes a string as input.
template<class ...Args>
bool write(Args&&... recordArgs) {
auto const currentWrite = writeIndex_.load(std::memory_order_relaxed);
auto nextRecord = currentWrite + 1;
if (nextRecord == size_) {
nextRecord = 0;
}
if (nextRecord != readIndex_.load(std::memory_order_acquire)) {
new (&records_[currentWrite]) T(std::forward<Args>(recordArgs)...);
writeIndex_.store(nextRecord, std::memory_order_release);
return true;
}
// queue is full
return false;
}
I can't change functions code. The u_char * pfring_zc_pkt_buff_data
returns a pointer to the actual packet captured by pfring_zc_recv_pkt(zq, &buffers[lru], 1)
.
I need to save part of packet to a buffer, So i have to convert u_char* to string* .there are many similar questions like:
How to Convert unsigned char* to std::string in C++?
How to convert a const char * to std::string
How to convert char to string?
I couldn't find proper answer for my question.
here are some of my code.
void run() {
int i=0,n;
char buf[_snapLengthConnection + _ConnectionHeaderSize];
for(;;){
if(likely(pfring_zc_recv_pkt(zq, &buffers[lru], wait_for_packet)>= 0)) {
u_char *pkt_data = pfring_zc_pkt_buff_data(buffers[lru], zq);
pfring_print_pkt(buf, sizeof(buf), pkt_data, buffers[lru]->len,sizeof(buf));
std::string *payload;
//need to save buf to payload
// how do i copy buf to payload?
std::cout<< payload<<"=="<<std::endl;
_activeBuffer->queue->write(payload);
}
}
}
How do i convert u_char* to string?
OS: Ubuntu
gcc version: 4.8.2
std::string
has a constructor for this:
const char *str = "Shravan Kumar";
std::string str(str);
Just make sure that your char *
isn't NULL
, otherwise it will lead to undefined behavior.