pysnmp sending bytes as response to get - snmp

I've been tasked with creating a custom SNMP agent, and one of the things I need to do is manually encrypt the response I'm sending back.
To do so, I'd ideally like to send back a custom set of varBinds based on the get command, like so:
response = [(varBinds[0][0], v2c.OctetString(b'127.0.0.4')),
(varBinds[0][0], v2c.OctetString((sal+signature+iv+encryptor.tag+cipher_text)))]
Where I'd send the salt, signature, IV, tag and ciphered text (all in bytes) and could manage to
decrypt on the other side (a symmetric key is used), but I can't seem to obtain the same result when on the other side I use the following:
msg = bytes(vars[1][1])
How can I get back exactly the bytes I sent in the OctetString from the agent? Something does misformat in the way I'm decoding, because the bytes I get back are not the same I sent.
Edit: the bytestring does work flawlessly, the bytes on the second variable don't.
Thank you very much for any help :)

Related

How can I return an array of integers from Solana rust program function to front end?

I don't know how can I write a smart contract in Solana that after executing the logic, returns an array of integers, strings, ... to the client, and how can I fetch it using Web3?
There's a syscall available to on-chain programs called set_return_data, which puts data into a buffer that can be read by the higher-level programs using get_return_data. This all mediated through opaque byte buffers, so you'll need to know how to decode the response.
If you want to fetch the data from the client side, you can simulate the transaction and read the data back from the return_data field in the response: https://edge.docs.solana.com/developing/clients/jsonrpc-api#results-50
The RPC support in simulated transactions is very new in version 1.11, but the return data is available in earlier versions.
Source code for set_return_data at https://github.com/solana-labs/solana/blob/658752cda710cb358d7ccbbc2cee06bf8009c2d4/sdk/program/src/program.rs#L102
Source code for get_return_data at https://github.com/solana-labs/solana/blob/658752cda710cb358d7ccbbc2cee06bf8009c2d4/sdk/program/src/program.rs#L117
So, programs do not return data (other than success or failure).
However; most programs write data to a program owned account's data field and this could be read from client apps (Rust, Python, TS/JS, etc.).
If using the Solana web3 library, you can call getAccountInfo on the Connection object. This will return the byte array of the account. You will then need to deserialize that data. You have to know how the program serializes the data to reverse it successfully.
Check the Solana Cookbook for overview using borsh https://solanacookbook.com/guides/serialization.html#how-to-deserialize-account-data-on-the-client

Trying to send a FIX api message to ctrader server using Ruby but receiving no response

Trying to see if I can get a response from ctrader server.
Getting no response and seems to hang at "s.recv(1024)". So not sure what could be going wrong here. I have limited experience with sockets and network coding.
I have checked my login credentials and all seems ok.
Note: I am aware of many FIX engines that are available for this purpose but wanted to
try this on my own.
ctrader FIX guides
require 'socket'
hostname = "h51.p.ctrader.com"
port = 5201
#constructing a fix message to see what ctrader server returns
#8=FIX.4.4|9=123|35=A|49=demo.ctrader.*******|56=cServer|57=QUOTE|50=QUOTE|34=1|52=20220127-16:49:31|98=0|108=30|553=********|554=*******|10=155|
fix_message = "8=FIX.4.4|9=#{bodylengthsum}|" + bodylength + "10=#{checksumcalc}|"
s = TCPSocket.new(hostname, port)
s.send(fix_message.force_encoding("ASCII"),0)
print fix_message
puts s.recv(1024)
s.close
Sockets are by default blocking on read. When you call recv that call will block if no data is available.
The fact that your recv call is not returning anything, would be an indication that the server did not send you any reply at all; the call is blocking waiting for incoming data.
If you would use read instead, then the call will block until all the requested data has been received.
So calling recv(1024) will block until 1 or more bytes are available.
Calling read(1024) will block until all 1024 bytes have been received.
Note that you cannot rely on a single recv call to return a full message, even if the sender sent you everything you need. Multiple recv calls may be required to construct the full message.
Also note that the FIX protocol gives the msg length at the start of each message. So after you get enough data to see the msg length, you could call read to ensure you get the rest.
If you do not want your recv or read calls to block when no data (or incomplete data) is available, then you need to use non-blocking IO instead for your reads. This is complex topic, which you need to research, but often used when you don't want to block and need to read arbitary length messages. You can look here for some tips.
Another option would be to use something like EventMachine instead, which makes it easier to deal with sockets in situations like this, without having to worry about blocking in your code.

How to store receiving SMS in GSM Module memory (SM or ME)?

I am using AI Thinker A7 GSM/GPRS/GPS Module demo v7.1.
I am trying to send and receive SMS through AI-Thinker Serial Tools V1.2.3.0
This module is working fine with making a call and receiving a call, sending and receiving an SMS. They work exactly fine.
The problem is that it does not store SMS in the memory?
I have done a lot of things to make it work, but there is no progress.
For instance, my AT commands and their responses are here,
AT+CPMS=?
+CPMS: ("ME","SM"),("ME","SM"),("ME","SM")
and
AT+CPMS="SM"
+CPMS: 0,35,0,35,0,25
and
AT+CPMS="ME"
+CPMS: 0,25,0,35,0,25
And I have tried CNMI settings like these,
AT+CNMI=1,1,0,0,0
OK
After these, I send msgs, and then tried to check through AT+CPMS and AT+CMGL, but there are no messages stored.
then I tried
AT +CNMI = 2,1,0,0,0
OK
and the same with
AT +CNMI = 2,2,0,0,0
OK
similarly
AT +CNMI = 0,1,0,0,0
OK
But still, I have not succeeded in getting message stored in memories.
Please help, if there are any other settings I need to do or my commands are wrong or my module doesn't support storing SMS (i highly doubt it since it has memory spaces which are showing that 25 SMS in SM, 35 in ME can be stored)??
If I understand your problem correctly, you are trying to read incoming SMS but can't list them.
Well, you need to understand that your GSM module doesn't store any incoming SMS by default. You have to command your GSM Module to store it. As you mentioned, you were very close to command for that. you need to command it this way:
AT+CPMS=mem1,mem2,mem3
AT+CPMS="SM","SM","SM"
As per documentation, you can mention three memories as the parameter for different operations like:
mem1 (in this example SM) - a memory from which messages are read and delete
mem2 (in this example SM; but could be ME) - a memory from which writing and sending operations are made
mem3 (in this example SM; but could be ME) - memory to which received messages are preferred to be stored
Now your GSM module will store all incoming messages into the SIM card and you'll be able to list all the messages with AT+CMGL=ALL
This way it worked for me.

Message formatting of OSC for MIDI messages

I'm using the github.com/hypebeast/go-osc/osc package to send OSC messages to an OSC server. For this I'm using OSCulator so that I can route the data as MIDI to Abelton Live.
The problem I'm having is I can not find any information on message formatting for things like note on, note off, duration etc. I found a guide on the OSCulator website that's a little helpful, but it doesn't go into much detail on messaging: http://s3.amazonaws.com/osculator/doc/OSCulator+2.12+Manual.pdf
For example, the following function works just fine, but I have no idea what the message is really doing:
func note(pitch float32 , velocity float32) {
// TODO: Pass client into function. Find out it's type.
client := osc.NewClient("localhost", 8765)
noteMsg := osc.NewMessage("/4/toggle2")
client.Send(noteMsg)
msg := osc.NewMessage("/4/xy")
msg.Append(pitch)
msg.Append(velocity)
client.Send(msg)
}
I mean, what purpose does the 4 play in this, and what is xy? Also, what other messages are available apart from toggle2? I thought there would be some sort of documentation online that has all the different types of messages available for MIDI type applications.
Your question seems to be more related to OSC itself.
OSC works like this:
You send a message to a server. A message is composed by an adress and some values.
In this case, /4/xy is the address. The 4 and the slashes you define what will be. When you receive it on the other side you will know what you want to receive, which means the address you're sending. So you will configure the server or the receiver to do something when it receives a message from a specific adress.
In the same way, you are appending values to the message. The quantity of values you already know, so you just have to do what you want with them when you receive.
Basically, if you decide to have a keyboard sending notes, you would use something like /keyboard/note as adress and send one value at a time, so you would read this value and do something with it.

Ruby TCPSocket Send/Receive binary data -> array hash etc

I'm trying to create a custom game server protocol in Ruby but i'm failing to understand how i can/should do a couple things:
Q->1#
Server sends an array to client using TCPServer/TCPSocket. but i can't use JSON as the transfer needs to be binary.. How can i convert a ruby array into binary which can then be transformed back into an array in client side?
This is my server:
#!/usr/bin/env ruby
require 'socket'
server = TCPServer.new 5020
loop do
Thread.start(server.accept) { |c|
print "new Client connected"
a = ["Hello, there"]
payload_with_header = CustomProtocolModule.constructMethod(a) # what method do i use to convert a (array -> binary representation? don't really understand .unpack, .pack, what template would i use if i did use String#Unpack? will String#Unpack work for what i need?
client.write payload_with_header
client.close
}
end
(Above is my first question)
Q->2#
Next since i know i need to use some sort of termination to check the end of message or a way to determine if the client/server has received the full message could i set the first two bytes of the payload to contain the size of the message? How would i do this server-side & client-side? The receiving party would have to loop TCPSocket.recv ? until it receives the first two bytes (the header), then read up the size the first two bytes contains? Something like this is what i had in mind:
| Payload Header (2 bytes) | array (contains cmd, message etc) |
If someone could help guide me in the right direction and/or provide pseudo code that can help. It would be greatly appreciated. Specifically i'm interested in how to loop for header, then read the payload back into an array and constructing the payload header.
Thanks!
payload_with_header = CustomProtocolModule.constructMethod(a)
You could serialize with
payload = Marshal.dump(a)
Then you can use
[payload.length].pack('l>') # to create a big endian signed long integer size for the packet
On the other side you can read an integer from the socket, then read the binary and deserialize with
array= Marshal.load(read_binary)
This will work for almost every data structure. Proc instances are an exception.
Ruby also supports remote objects and procedure calls so you can have interprocess communication, even between different ruby implementations (JRuby and MRI for instance)

Resources