Filter Libcurl Debug Message - c++

Is it possible to filter debug data function text? I want to display commands at one time and full output at the other (I want for example to filter out Adding handle: send: 0). All the time I get a lot of messages. I want something nice like Filezilla Short messages .
Here is my code for debug function and below it a message. I have verbose enabled
int Uploader::DebugDataCallBack(CURL* handle, curl_infotype infotype, char* msg, size_t size, void* f)
{
int level= 1; //debug info 0-None, 1-necessary 2 - All TODO: Should come from config
switch(level) //error level
{
case 0:
{
break; //do nothing
}
case 1:
{
//only necessary, skip headers
if(infotype==CURLINFO_TEXT)
{
static_cast<Uploader*>(f)->SendMessage(wxString(msg));
}
}
default:
{
//full debug messages
static_cast<Uploader*>(f)->SendMessage(wxString(msg));
}
}
return 0;//must return 0
}
----------Thu Dec 26 14:14:40 2013----------
STATE: INIT => CONNECT handle 0x7fffd0001a08; line 998 (connection #-5000)
[14:14:40]
STATE: INIT => CONNECT handle 0x7fffd0001a08; line 998 (connection #-5000)
[14:14:40]
Rebuilt URL to: ftp://ftp.mysite.com/
[14:14:40]
Rebuilt URL to: ftp://ftp.mysite.com/
[14:14:40]
About to connect() to ftp.mysite.com port 21 (#0)
[14:14:40]
About to connect() to ftp.mysite.com port 21 (#0)
[14:14:40]
Trying 31.170.162.203...
[14:14:40]
Trying 31.170.162.203...
[14:14:40]
Adding handle: conn: 0x7fffd0013b48
[14:14:40]
Adding handle: conn: 0x7fffd0013b48
[14:14:40]
Adding handle: send: 0
[14:14:40]
Adding handle: send: 0
[14:14:40]
Adding handle: recv: 0
[14:14:40]
Adding handle: recv: 0
[14:14:40]
Curl_addHandleToPipeline: length: 1
[14:14:40]
Curl_addHandleToPipeline: length: 1
[14:14:40]
0x7fffd0001a08 is at send pipe head!
[14:14:40]
0x7fffd0001a08 is at send pipe head!
[14:14:40]
- Conn 0 (0x7fffd0013b48) send_pipe: 1, recv_pipe: 0
[14:14:40]
- Conn 0 (0x7fffd0013b48) send_pipe: 1, recv_pipe: 0
[14:14:40]
STATE: CONNECT => WAITCONNECT handle 0x7fffd0001a08; line 1045 (connection #0)
[14:14:40]
STATE: CONNECT => WAITCONNECT handle 0x7fffd0001a08; line 1045 (connection #0)
[14:14:40]
Connected to ftp.mysite.com (31.170.162.203) port 21 (#0)
[14:14:40]
Connected to ftp.mysite.com (31.170.162.203) port 21 (#0)
[14:14:40]
FTP 0x7fffd0013fe0 (line 3174) state change from STOP to WAIT220
[14:14:40]
FTP 0x7fffd0013fe0 (line 3174) state change from STOP to WAIT220
[14:14:40]
STATE: WAITCONNECT => PROTOCONNECT handle 0x7fffd0001a08; line 1158 (connection #0)
[14:14:40]
STATE: WAITCONNECT => PROTOCONNECT handle 0x7fffd0001a08; line 1158 (connection #0)
[14:14:40]

First Enable curl by putting verbose to 1L that is CURLOPT_VERBOSE in curl_easy_setopt. Theb set the debug function, that is CURLOPT_DEBUGFUNCTION to receive the debug messages. Then use codes infotypes to filter out what you want. If you want to get command/response like I wanted just take message from Header in/out. Here is piece of code just to show it!
switch(infotype)
{
case CURLINFO_HEADER_OUT:
{
wxString message = _("COMMAND: ")+wxString(msg);
SendMessage(message);
break;
}
case CURLINFO_HEADER_IN:
{
wxString message = _("RESPONSE: ")+wxString(msg);
SendMessage(message);
break;
}
}

Related

SCPing to an EC2 instance is failing

I'm trying to scp a jar file to an EC2 instance that hosts a service I'm working on. I'm able to ssh into that EC2 instance without issue. When I attempt to scp the file with the following command:
scp -vvv foo.jar instance-id:/tmp/
I'm prompted for a password and then get the following output after I authenticate
debug1: Authentication succeeded (password).
Authenticated to <instance id> (via proxy).
...
debug1: Entering interactive session.
debug1: pledge: filesystem full
...
debug1: Sending command: scp -v -t /tmp/
debug2: channel 0: request exec confirm 1
debug2: channel_input_open_confirmation: channel 0: callback done
debug2: channel 0: open confirm rwindow 0 rmax 32768
debug2: channel 0: rcvd adjust 2097152
debug2: channel_input_status_confirm: type 99 id 0
debug2: exec request accepted on channel 0
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: client_input_channel_req: channel 0 rtype eow#openssh.com reply 0
debug2: channel 0: rcvd eow
debug2: chan_shutdown_read: channel 0: (i0 o0 sock -1 wfd 3 efd 7 [write])
debug2: channel 0: input open -> closed
debug2: channel 0: rcvd eof
debug2: channel 0: output open -> drain
debug2: channel 0: obuf empty
debug2: chan_shutdown_write: channel 0: (i3 o1 sock -1 wfd 6 efd 7 [write])
debug2: channel 0: output drain -> closed
debug2: channel 0: rcvd close
debug3: channel 0: will not send data after close
debug2: channel 0: almost dead
debug2: channel 0: gc: notify user
debug2: channel 0: gc: user detached
debug2: channel 0: send close
debug3: send packet: type 97
lost connection
debug2: channel 0: is dead
debug2: channel 0: garbage collecting
debug1: channel 0: free: client-session, nchannels 1
debug3: channel 0: status: The following connections are open:
#0 client-session (t4 r0 i3/0 o3/0 e[write]/0 fd -1/-1/7 sock -1 cc -1)
debug3: send packet: type 1
debug1: fd 0 clearing O_NONBLOCK
debug3: fd 1 is not O_NONBLOCK
Transferred: sent 2752, received 2780 bytes, in 0.3 seconds
Bytes per second: sent 9360.6, received 9455.8
debug1: Exit status 1
As far as I can tell I'm successfully connecting to the host. It seems like the file transfer immediately exits afterwards. I've done some digging on similar questions and responders have said that this can indicate shell problems. I'm not echoing anything in my zshrc which was a commonly indicated issue. Any insight or debugging suggestions would be appreciated!

Unable to receive data from multiple UDP devices

I am using the EIPScanner library to talk to 2 TURCK I/O modules via EthernetIP. The library works fine with one module, but the moment I instantiate the driver for the second one, the first one times out.
I tried to modify the library by passing a port number to listen on, but it didn't seem to make a difference:
In ConnectionManager.cpp
IOConnection::WPtr
ConnectionManager::forwardOpen(const SessionInfoIf::SPtr& si, ConnectionParameters connectionParameters, int port, bool isLarge) {
....
....
....
if (o2tSockAddrInfo != additionalItems.end())
{
Buffer sockAddrBuffer(o2tSockAddrInfo->getData());
sockets::EndPoint endPoint("",0);
sockAddrBuffer >> endPoint;
endPoint.setPort(port)
if (endPoint.getHost() == "0.0.0.0") {
ioConnection->_socket = std::make_unique<UDPSocket>(
si->getRemoteEndPoint().getHost(), endPoint.getPort());
} else {
ioConnection->_socket = std::make_unique<UDPSocket>(endPoint);
}
}
I tried adding a timeout to the recvfrom() function, but that also didn't make a difference.
In UDPSocket.cpp
UDPSocket::UDPSocket(EndPoint endPoint)
: BaseSocket(EndPoint(std::move(endPoint))) {
_sockedFd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (_sockedFd < 0) {
throw std::system_error(BaseSocket::getLastError(), BaseSocket::getErrorCategory());
}
/*struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 1;
setsockopt(_sockedFd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));*/
Logger(LogLevel::DEBUG) << "Opened UDP socket fd=" << _sockedFd;
}
Output:
[DEBUG] Opened TCP socket fd=7
[DEBUG] Connecting to 192.168.1.130:44818
[INFO] Registered session 103
[DEBUG] Opened TCP socket fd=8
[DEBUG] Connecting to 192.168.1.131:44818
[INFO] Registered session 103
2021-06-18 13:03:02,996 Process-1
2021-06-18 13:03:02,997 Process-1 [INFO] Send request: service=0x54 epath=[classId=6 objectId=1]
[INFO] Open IO connection O2T_ID=1781321728 T2O_ID=915406849 SerialNumber 1
[DEBUG] Opened UDP socket fd=15
[INFO] Open UDP socket to send data to 192.168.1.130:2223
[DEBUG] Opened UDP socket fd=16
[INFO] Will look for id: 915406849 from endPoint: 192.168.1.130:2222
[DEBUG] Received data from connection T2O_ID=915406849(host: 192.168.1.130:2223)
[DEBUG] Received: seq=2 data=[0][0][0][0]
[DEBUG] Received data from connection T2O_ID=915406849(host: 192.168.1.130:2223)
[DEBUG] Received: seq=3 data=[0][0][0][0]
2021-06-18 13:03:03,112 Process-2
2021-06-18 13:03:03,113 Process-2
[INFO] Send request: service=0x54 epath=[classId=6 objectId=1]
[INFO] Open IO connection O2T_ID=1782571776 T2O_ID=1911881729 SerialNumber 1
[DEBUG] Received data from connection T2O_ID=1911881729(host: 192.168.1.130:2223)
[DEBUG] Received: seq=1 data=[0][0][4][0]
[DEBUG] Opened UDP socket fd=20
[INFO] Open UDP socket to send data to 192.168.1.131:2224
[DEBUG] Opened UDP socket fd=21
[INFO] Will look for id: 1911881729 from endPoint: 192.168.1.131:2222
[DEBUG] Received data from connection T2O_ID=915406849(host: 192.168.1.131:2224)
[DEBUG] Received: seq=4 data=[0][0][0][0]
[DEBUG] Received data from connection T2O_ID=1911881729(host: 192.168.1.131:2224)
[DEBUG] Received: seq=2 data=[0][0][4][0]
As you can see once the device on 192.168.1.131 starts streaming, the 1.130 device stops. And eventually it times out and shuts down.

How to send JSON data to a REST API?

I'm sending data to a Wordpress site with the WooCommerce plugin installed using libcurl in C++ and the WooCommerce REST API. The data seems to get sent but the expected result is not shown on the website. The purpose of it is to update (modify) the product. My code is based on the WooCommerce documentation, found here.
I have managed to get the CURLOPT_VERBOSE text from the program in a separate txt file.
Here is my C++ code using cURL :
std::string URL = main_domain + "wp-json/wc/v3/products/" + product_id + "?consumer_key=" + consumer_key + "&consumer_secret=" + consumer_secret;
curl_slist* headers = NULL;
headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
headers = curl_slist_append(headers, "Accept:application/json");
headers = curl_slist_append(headers, "Content-Type:application/json");
headers = curl_slist_append(headers, "charsets: utf-8");
// log file
FILE* filep = fopen("logfichier.txt", "w");
std::string toUpdate = "{\"id\":\"" + product_id + ",\"name\":\"" + product_name + "\",\"description\":\"" + product_description + "\",\"price\":\"" + product_price + "\"}";
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl) {
readBuffer = "";
curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, toUpdate.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, toUpdate.length());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
curl_easy_setopt(curl, CURLOPT_STDERR, filep);
res = curl_easy_perform(curl);
// Check for errors
if (res != CURLE_OK) {
// error handling and cleanup
}
else {
// code and cleanup
}
}
else {
// error handling and cleanup
}
I've literally put every header found on the internet that seemed relevant to what i'm trying to acomplish in my code.
Here is the returned debug text :
* STATE: INIT => CONNECT handle 0x10870278; line 1428 (connection #-5000)
* Added connection 0. The cache now contains 1 members
* STATE: CONNECT => WAITRESOLVE handle 0x10870278; line 1464 (connection #0)
* Trying 192.XX.XX.XX...
* TCP_NODELAY set
* STATE: WAITRESOLVE => WAITCONNECT handle 0x10870278; line 1545 (connection #0)
* Connected to mywebsite.com (192.XX.XX.XX) port 443 (#0)
* STATE: WAITCONNECT => SENDPROTOCONNECT handle 0x10870278; line 1599 (connection #0)
* Marked for [keep alive]: HTTP default
* schannel: SSL/TLS connection with mywebsite.com port 443 (step 1/3)
* schannel: checking server certificate revocation
* schannel: sending initial handshake data: sending 176 bytes...
// (here was just a bunch of connexion attemps log text...)
* schannel: SSL/TLS handshake complete
* schannel: SSL/TLS connection with mywebsite.com port 443 (step 3/3)
* schannel: stored credential handle in session cache
* STATE: PROTOCONNECT => DO handle 0x10870278; line 1634 (connection #0)
> PUT /wp-json/wc/v3/products/111867?consumer_key=(the actual key)&consumer_secret=(the actual secret) HTTP/1.1
Host: mywebsite.com
Transfer-Encoding: chunked
Accept:application/json
Content-Type:application/json
charsets: utf-8
4b
* upload completely sent off: 82 out of 75 bytes
* STATE: DO => DO_DONE handle 0x10870278; line 1696 (connection #0)
* STATE: DO_DONE => WAITPERFORM handle 0x10870278; line 1823 (connection #0)
* STATE: WAITPERFORM => PERFORM handle 0x10870278; line 1838 (connection #0)
* schannel: client wants to read 16384 bytes
* schannel: encdata_buffer resized 17408
* schannel: encrypted data buffer: offset 0 length 17408
// (a few decrypting data attempts...)
* schannel: decrypted data returned 536
* schannel: decrypted data buffer: offset 0 length 16384
* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 200 OK
< Date: Tue, 18 Jun 2019 15:27:42 GMT
* Server Apache is not blacklisted
< Server: Apache
< X-Robots-Tag: noindex
< Link: <https://mywebsite.com/wp-json/>; rel="https://api.w.org/"
< X-Content-Type-Options: nosniff
< Access-Control-Expose-Headers: X-WP-Total, X-WP-TotalPages
< Access-Control-Allow-Headers: Authorization, Content-Type
< Expires: Wed, 11 Jan 1984 05:00:00 GMT
< Cache-Control: no-transform, no-cache, must-revalidate, max-age=0
< Allow: GET, POST, PUT, PATCH, DELETE
< Transfer-Encoding: chunked
< Content-Type: application/json; charset=UTF-8
<
* schannel: client wants to read 16384 bytes
* schannel: encrypted data buffer: offset 835 length 17408
// (a few decrypting data attempts...)
* schannel: decrypted data returned 1986
* schannel: decrypted data buffer: offset 0 length 16384
* STATE: PERFORM => DONE handle 0x10870278; line 2011 (connection #0)
* multi_done
* Connection #0 to host axanti.info left intact
I took out a few redundant part from the original text and kept what I think is the main piece of information. It seems that my JSON data is actually sent to the server but the intended result doesn't show up on my website (a product should be modified but it's actually not).
Is there any way this code could be wrong ? Or is the problem on the server side ? Because I litteraly apply the same stuff that is mentioned in the official documentation.
Looks like your payload if off. The id portion is redundant, as you're already specifying the product to update via the URL, so you can drop that. Additionally, you're attempting to set the price incorrectly. Per the REST docs, you need to use the regular_price attribute instead of price (price is read only). The proper payload should look like this:
{
name: 'My product name',
description: 'my product description',
regular_price' : '3.50',
}

Sofia SIP, response to Invite with no SDP

My Sofia SIP application responds to Invite with no SDP like this
nua_respond(_nh, 200, "OK",
SIPTAG_CONTENT_TYPE_STR("application/sdp"),
NUTAG_OFFER_SENT(1),
SOATAG_USER_SDP_STR("m=audio 1 RTP/AVP 0\n"
"a=rtpmap:0 PCMU/8000"),
TAG_NULL());
However, ACK after this one is not processed, and I see "ACK Timeout". Are there any tags I'm missing?

libcurl POST request failing

I am trying to mimic the behavior of a python program using libcurl and getting rejected by the server. As near as I can tell, this is an issue with how the file is being handled. Here is the python code:
import requests
savedTextFile = open('filepath/file.txt', 'rw')
myPostRequest = requests.post("https://mywebserver.com/ws/v1/layers/template/lines2", headers={"API-Key":"source:1111111111"}, files={'file': savedTextFile}, verify=False)
The libcurl/C++ code I have currently is:
CURL* curl; //our curl object
struct curl_httppost* formpost=NULL;
struct curl_httppost* lastptr=NULL;
struct curl_slist* headerlist=NULL;
curl_global_init(CURL_GLOBAL_ALL); //pretty obvious
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "file", // required ?
CURLFORM_FILE, dbFile.c_str(), // makes this part a file upload part
CURLFORM_CONTENTTYPE, "application/octet-stream", // generic binary
CURLFORM_END);
curl = curl_easy_init();
const int MediumBufferSize = 256;
char sApiKeyHeaderLine[MediumBufferSize];
sprintf_s(sApiKeyHeaderLine, MediumBufferSize, "API-Key:%s", api.c_str());
headerlist = curl_slist_append((struct curl_slist*) headerlist, sApiKeyHeaderLine);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_URL, full_url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); //tell curl to output its progress
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl_global_cleanup();
Not sure what im missing.Here is the DOS console output:
* About to connect() to 54.219.209.180 port 443 (#1)
* Trying 54.219.209.180...
* Adding handle: conn: 0x490140
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 1 (0x490140) send_pipe: 1, recv_pipe: 0
* Connected to 54.219.209.180 (54.219.209.180) port 443 (#1)
* schannel: SSL/TLS connection with 54.219.209.180 port 443 (step 1/3)
* schannel: disable server certificate revocation checks
* schannel: using IP address, SNI is being disabled by disabling the servername check against the subject names in server certificates.
* schannel: verifyhost setting prevents Schannel from comparing the supplied tar get name with the subject names in server certificates. Also disables SNI.
* schannel: sending initial handshake data: sending 90 bytes...
* schannel: sent initial handshake data: sent 90 bytes
* schannel: SSL/TLS connection with 54.219.209.180 port 443 (step 2/3)
* schannel: failed to receive handshake, need more data
* schannel: SSL/TLS connection with 54.219.209.180 port 443 (step 2/3)
* schannel: encrypted data buffer: offset 797 length 4096
* schannel: sending next handshake data: sending 326 bytes...
* schannel: SSL/TLS connection with 54.219.209.180 port 443 (step 2/3)
* schannel: encrypted data buffer: offset 59 length 4096
* schannel: SSL/TLS handshake complete
* schannel: SSL/TLS connection with 54.219.209.180 port 443 (step 3/3)
* schannel: incremented credential handle refcount = 1
* schannel: stored credential handle in session cache
> POST /ws/v1/layers/template/lines1 HTTP/1.1 Host: 54.219.209.180 Accept: */* LatLongo-API-Key:source:1234567890
Content-Length: 35023 Expect: 100-continue Content-Type: multipart/form-data; boundary=------------------------3e5310b06e42 8f31
* schannel: client wants to read 16384 bytes
* schannel: encrypted data buffer: offset 0 length 16384
* schannel: encrypted data got 245
* schannel: encrypted data buffer: offset 245 length 16384
* schannel: decrypted data length: 209
* schannel: decrypted data added: 209
* schannel: decrypted data cached: offset 209 length 16384
* schannel: decrypted data buffer: offset 209 length 16384
* schannel: decrypted data returned 209
* schannel: decrypted data buffer: offset 0 length 16384
< HTTP/1.1 400 BAD REQUEST
< Date: Thu, 09 Jan 2014 04:04:59 GMT
< Content-Type: text/html; charset=utf-8
< Transfer-Encoding: chunked
< Connection: keep-alive
* Server gunicorn/18.0 is not blacklisted
< Server: gunicorn/18.0
* HTTP error before end of send, stop sending
< POST form invalid
* Closing connection 1
* schannel: shutting down SSL/TLS connection with 54.219.209.180 port 443
* schannel: clear security context handle
* schannel: decremented credential handle refcount = 0