libwebsockets fail to recive or send data correctly to hitbox.tv's websocket - sockets

Hello andy well I've been struggling with a slight issue of how should i go about handling web socket connections using libwebsocket. So far it has proved frustrating as it looks like the data is being sent but its not or nto being sent the way they want it and dropped.
So far all the program i made does is connect and ping back and forth and tried so send a json file to their web socket on their server. the issue i am having is do i have everything setup right to be a client and how will i know if i am sending the data correctly or just plain i did something wrong i have no idea. If you could help me that would be great. here is the code in which i am having issues with currently been trying to figure out the issue for over a week now.....
i have changed it several time to try and figure out whats wrong..
http://pastebin.com/jdHbicUi
i have the code pasted in pastebin so its easier for you to view. thank you for your time.

The fix was pretty easy for those who have trouble too. The issue is the data being sent needs to be properly buffered on both ends.
struct per_session_data {
unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 +
LWS_SEND_BUFFER_POST_PADDING];
unsigned int len;
};
pss->len = sprintf((char *)&pss->buf[
LWS_SEND_BUFFER_PRE_PADDING], "%s", data);
n = libwebsocket_write(wsi, &pss->buf[
LWS_SEND_BUFFER_PRE_PADDING], pss->len, LWS_WRITE_TEXT);
This will fix any issue with sending the data.

Related

How do I decode a websocket packet?

I'm using Wireshark packet analyzer & when I filter for all "Websocket" packets I see what I am sending /receiving to the host. When I check individual packets mine always show as [MASKED], but you can 'Umask Payload' which shows the data in clear text that looks like this:
<IC sid="52ccc752-6080-4668-8f55-662020d83979" msqid="120l93l9l114l30l104"/>
However, if I 'Follow TCP stream & look at that same packet, the data shows up as encoded in some way like this:
....K#....../...y#..|...}...f...s...~...}...{G..r...kN.."G..z...r...'...'...z...d.
The problem is all Websocket packets I receive from the host come as encoded, it is NOT SSL & I can't figure out how to decode them, I have no idea what they are even encoded as (but yet my browser can decode it).
I assume that whatever method they are coming back to me as encoded data is the same method that my data is encoded when I use 'Follow TCP stream'.
Can someone please help me figure out how to decode the data the host is sending me? See host data below
~.^jVpZc9y4Ef4ryFQ5+yJpeB+JJJdmNPI6G++mrN249kkFkuChIQmG5Fgj//p0AyAJypzxyi5T6P76
QKPRuHz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Gm5QUubOqcjktRy3kFW98eHzFGP3BVmjGUYVyDMlqxp4ZmQr0iG4ocg9UM1D8V8ShiK95I
sAbCYA3nfA0brhqp0V5jBG8YSgUWArG5mQUPC8JEHaDuQKw15BQIjehoyEEa+suORa+hLE10QBkD
ShFwYau6rKF46lLHc1zeovIgB6VkqdKH69xIbqeTBNaCuOTdFDesB+KNVDWxY6xOol7rGNYFJTWS
wBfxOqCaniY2aA7HlFEE8LXkNBEPWyp5cAnGlCih85OkoRNHGxA13CQVdXPoR3kTdWbqIhMXbdHb
ST90WHcKF3I5FWZhxFV71mVc1IcAKghEJuGTk7hgi/Yggqs0vmNiWikKdLihXffC20QdsnBc9lmL
CTE87kmGr4ZhM9y7gGzLMogAawcaVrOqqIvRh1jNtKfuUCmKmmJP/GXUjYVO0MrxPharXUWPk/NY
4gRh0OwPpkbEvDoooqVU67UCC92I3A/9sUTyPqk6FE2+4N5D44x3N8Y0lk8R73teTQdV2EpHfHMA
4nCX5H6vXisXuE/R2XQ0bs8YCYRNnXVTW+wrh9EaqI7Ym92P7+wAEWcgZ+K18lJCHLzJaqbFMMeT
9CTwyOgcZdpLqEaew3SgFSwAN5wqxyL4bQHwiVHop4RU4vclc3A2Ge11srEIg0nPaJwPQNVc8usV
X0/J2NuO0a5ImI4UVwuPv3z89enu8+ffvpCvFKYgFJq2nXyc2LiG3l7H+R6yH/9PjGGw0LfTyLGN
0Ehdzw4DM6apZaXM8pMA/+NMfLNyXc+IAwcgIaNBEDhmStPYMSPT9izHSMA5QCUA8tMoCFxGHWpa
phn5lhVYNAlTljoueiItwy8ft7f/Bw==
Client to server data is XORed with a mask (included in the dataframe). Some people suggest this is in order to throw off bad caching mechanisms responding to new websocket requests with server messages from older sessions. The masking makes sure that even messages containing identical data will appear differently to applications that do not understand websockets.
Also note that there are many different size options for the headers themselves.
Refer to RFC 6455 Section 5 which defines the masking/unmasking process for payloads sent from the client to the server.
https://www.rfc-editor.org/rfc/rfc6455
If you find any freeware VBA code to do the job of forming packets let me know! :-)

FiddlerScript throws NullReferenceException on headers.ExistsAndContains

This is driving me nuts and it may be something simple, but every time I call the following from Fiddler script is throws a NullReferenceException:
if (oSession.oResponse.headers.ExistsAndContains("Content-Type", "html")){...
This is straight off the Fiddler script knowledge base and I've got the same thing happening on two different Win 8 machines. It seems that oSession.oResponse.headers is null and yes, I am calling this from within OnBeforeRequest!
Same deal with oSession.responseBodyBytes - it looks like the response is null inside OnBeforeRequest. Otherwise Fiddler is behaving normally, traffic proxies through just fine and everything else looks good.
Ideas?
The oResponse.headers and oSession.responseBodyBytes objects don't exist until after the request has been sent to the server and the response has been read back. If you want to interact with the Response headers and body, do so inside the OnBeforeResponse function instead.
The KB was mangled a bit when updated for the new site. I'll file a bug to have this made more explicit.

GCDAsyncSocket write timeout does not work

I am trying to set a timeout on write operations when using GCDAsyncSocket. The code is pretty simple and is the following.
[iAsyncSocket writeData:bytesToSend withTimeout:3.0 tag:0];
Then I disable the Internet connection on my Mac and wait for write timeout to occur, but nothing happens. I don't get a disconnection with a GCDAsyncSocketWriteTimeoutError error as I should.
I have also validated that my server stops, as expected, receiving the messages after I turn off the Internet connection.
I have looked inside the source code and I have found out that the writeTimer, that is responsible for firing a write timeout event, is always cancelled (function endCurrentWrite is called). Tracing back to where the timer is cancelled, I ended up at the following line of code.
ssize_t result = write(socketFD, buffer, (size_t)bytesToWrite);
The write system call always returns the total number of bytes that I am sending, as if the socket manages to send the data although there is no Internet connection. Is this logical?
Has anyone come up with the same problem or seen similar behaviour? Or has anyone managed to set a write timeout for a GCDAsyncSocket?
Thanks a lot.

How to read the whole message with Chilkat socket?

I need to get the whole message(response), but socket.ReceiveBytes(); returns just part of the message. I tried to loop it but it fails on timeout when no bytes to receive.
List<byte> lb = new List<byte>();
byte[] receivedMsg = socket.ReceiveBytes();
while (receivedMsg.Length > 0)
{
lb.AddRange(receivedMsg);
receivedMsg = socket.ReceiveBytes();
}
So, how I can check if there are byte to read? How I can read the whole message?
Since its a Chilkat implementation, you should probably contact the developer. But I found this that could help: http://www.cknotes.com/?p=302
Ultimately, you need to know how much to read from the socket to constitute a whole message. For example, if the overlying protocol is a portmapper, then you know that you are expecting messsages in the format that the RFC specifies (http://tools.ietf.org/html/rfc1833.)
If you are rolling your own protocol over a socket connection, then use the method in the Chilkat blog post about putting the size of the total message in the first 4 bytes.

Sinatra AWS SNS Endpoint - Help needed

Hiya,
Due to the undocumented nature of the Amazon SNS endpoints and how they interact with the user, I am struggling to code an endpoint in Sinatra for my application.
I have some code written in PHP here, but I am struggling to convert it into a Ruby array because request.env["rack.input"].read encapsulates everything into something similar to a PHP array:
{"Message"=>"chapstick", "Subject"=>"cherry", "Signature"=>"QutYUXuqWQqiAY4PUvd+Hq+9KWSvPVwck22oV0pXkgSg0p+kZ/2Q+pS24siDPKxxHB8+ru5q6 QBqQv+RHKWobiLiupEf4UP1MQu3lnP4Z2TYWvbHVjNiO5OutcaivPrn08EIBTOMsWl2i8JmaTBXozOp4r1vheqg6k/wWDoUmd0=", "TopicArn"=>"arn:aws:sns:us-east-1:687180262489:plinklebird", "SignatureVersion"=>"1", "Timestamp"=>"2010-12-03T22:10:01.141Z", "MessageId"=>"dc8a8502-44a8-41ec-9616-1d049f00724d", "UnsubscribeURL"=>"https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-east-1:687180262489:plinklebird:99e83fd5-58a9-41bb-82bd-9920a1ee1a94", "Type"=>"Notification"}
I am not sure how to handle this, can anybody please help?
Thank you very much for your time,
Luke Carpenter
A year and four months later, reflecting on this problem:
It was because I was a complete and utter noob, and I didn't know what a Hash was in Ruby at the time
In your post block, access the contents of the hash like so:
post "/" do
puts params['Message']
halt 200
end
The message SNS sends you will be printed out to STDOUT/your terminal