How to send then receive packet from Client-Server in Ada - sockets

I want to create a server-client connection where a client send a request packet to the server then return a value back to the user. I can get the server to read the incoming packet from the client, but when it write back to the client, the client is not receiving the packet.
I have the size of string in Client and Server matches to ensure the read is not waiting for more incoming packet.
Server
Buffer : Ada.Streams.Stream_Element_Array (1 .. 10);
Offset : Ada.Streams.Stream_Element_Offset;
...
GNAT.Sockets.Create_Socket (Socket => Receiver);
GNAT.Sockets.Set_Socket_Option
(Socket => Receiver,
Option => (Name => GNAT.Sockets.Reuse_Address, Enabled => True));
GNAT.Sockets.Bind_Socket
(Socket => Receiver,
Address => (Family => GNAT.Sockets.Family_Inet,
Addr => GNAT.Sockets.Inet_Addr ("127.0.0.1"),
Port => 12321));
GNAT.Sockets.Listen_Socket (Socket => Receiver);
GNAT.Sockets.Accept_Socket
(Server => Receiver,
Socket => Connection,
Address => Client);
Channel := GNAT.Sockets.Stream (Connection);
Ada.Streams.Read(Stream => Channel.all,
Item => Buffer,
Last => Offset);
for J in 1..Offset loop
Ada.Text_IO.Put_Line(Character'Val(Integer (Buffer (J)))'Img);
end loop;
String'Write(GNAT.Sockets.Stream (Connection), "1234567890");
GNAT.Sockets.Close_Socket (Connection);
Client
input : String(1..10);
output : String(1..10);
...
Initialize;
Create_Socket (Socket => Client);
Connect_Socket (Socket => Client,
Server => (Family => Family_Inet,
Addr => Inet_Addr ("127.0.0.1"),
Port => 12321));
String'Write (Stream (Client), Input);
String'Read (Stream (Client), output); --hanging right here
Close_Socket (Client);

For some reason, the client is getting the message now. The code above works as intended

Related

Perl RawIP maximum data size

I'm trying to send some data over TCP using Net::RawIP in Perl. Unfortunately i get the error
sendto() at /usr/lib/x86_64-linus-gnu/perl5/5.24/Net/RawIP.pm line 630
if the TCP data field is bigger than about 1470 characters:
my $n = Net::RawIP->new({
ip => {
saddr => '[src]',
daddr => '[dst]',
},
tcp => {
source => 7777,
dest => 7777,
data => "x" x 150
}
});
$n->send;
works, but
my $n = Net::RawIP->new({
ip => {
saddr => '[src]',
daddr => '[dst]',
},
tcp => {
source => 7777,
dest => 7777,
data => "x" x 1500 # size changed here
}
});
$n->send;
crashes. Any ideas why this happens?
You're building a packet that's too large, so sendto is returning error EMSGSIZE.
EMSGSIZE
The socket type requires that message be sent atomically, and the size of the message to be sent made this impossible.
It's no mystery it starts failing around 1500; that's the maximum an Ethernet frame can carry.
You need to use multiple packets or multiple packet fragments.

How to pass IPv6 parameter for snmp session creation using

I am using SNMP package to create SNMPv3 session. But I am not sure how to pass transport information as UDP6 as I think default behavior is UDP. As far as I know that when use Net::SNMP to create session then we have "domain" parameter to classify between ipv4 and ipv6 address . But the same is not applicable to SNMP::Session. Can you please let me know how to pass transport information?
new SNMP::Session(DestHost => $self->{'ip'},
version => 3,
RemotePort => $self->{'port'},
SecEngineId => ‘0x8888233356',
SecName => $hashVal{'userName'},
AuthProto => $authProto,
AuthPass => $hashVal{'authPW'},
PrivProto => $privProto,
PrivPass => $hashVal{'privPW'},
SecLevel => $hashVal{‘secLevel’');

Cannot broadcast to all clients in play2

Now I'm working on WebSocket using Play2(Scala) and succeessfully communicate with client. However, the broadcast function that play offers seems not to send message to all clients. How can I fix it?
:Controller in server
def ws = WebSocket.using[String] { request =>
val (out, channel) = Concurrent.broadcast[String]
val in = Iteratee.foreach[String] { msg =>
println(msg)
channel push(msg)
}.map{ _ => println("closed") } //when connection close
(in, out)
}
:Client Side
$ ->
ws = new WebSocket("ws://localhost:9000/ws")
ws.onopen = (event) ->
console.log("connected!!")
ws.onmessage = (event) ->
data = event.data.split(",")
infotype = data[0]
if infotype is "noisy"
room = data[1]
alert "noise happen at " + room
else if infotype is "gotIt"
console.log("someone gotIt")
else
console.log(data)
alert("oh arrive")
ws.onerror = (event) ->
console.log("error happned")
$(".rooms").on("click", (event) ->
room = $(this).attr("id")
ws.send("noisy," + room) )
Ideally, when push the button(not button but just rooms class in this case), the client send message to the server(this works correctly) and server should broadcast that message to all clients(it did not work in my code).
As a result, all clients should show the alert message.
However, Only the client which sent message to the server can get the message from server and show the alert.
What is the problem?
It's because you create a new Concurrent.broadcast for every client.
Create it once outside your action, like this:
val (out, channel) = Concurrent.broadcast[String]
def ws = WebSocket.using[String] { request =>
val in = Iteratee.foreach[String] { msg =>
println(msg)
channel push(msg)
}.map{ _ => println("closed") } //when connection close
(in, out)
}

How do I specify packet data in Net::RawIP?

According to the cpan documentation I can create a raw packet with the following code:
use Net::RawIP;
$n = Net::RawIP->new({
ip => {
saddr => 'my.target.lan',
daddr => 'my.target.lan',
},
});
tcp => {
source => 139,
dest => 139,
psh => 1,
syn => 1,
},
});
$n->send;
But where do I declare the data the packet contains?
Can I send the packet with another module?
Since you are sending a tcp packet you need as the documentations says to specify:
$n = Net::RawIP->new({
ip => {
saddr => 'my.target.lan',
daddr => 'my.target.lan',
},
tcp => {
source => 139,
dest => 139,
psh => 1,
syn => 1,
data => $your_data
},
});

How to manipulate and send packets caught with Net::PCAP on windows

I need to change the tcp/ip headers of packets caught with Net::PCAP. I know this is possible with Net::RawIP, but this doesn't work under windows?
Is there a module for this that works with windows? Is there at least to do this in windows with another programming language that I can call in perl, such as C?
To demonstrate what I want to do, here is the code using Net::RawIP, which does not work under windows because I can't install the module:
$n = Net::RawIP->new({
ip => {
saddr => 'my.target.lan',
daddr => 'my.target.lan',
},
tcp => {
source => 139,
dest => 139,
psh => 1,
syn => 1,
data => $your_data
},
});
$n->send();
Try pcap_sendpacket using this per module https://metacpan.org/pod/Net::Pcap