Skip to content

Instantly share code, notes, and snippets.

@eviltik
Created July 13, 2013 16:04
Show Gist options
  • Save eviltik/5991183 to your computer and use it in GitHub Desktop.
Save eviltik/5991183 to your computer and use it in GitHub Desktop.
Ideas for nodejs raw socket helpers
var socket = raw.createSocket({
protocol: raw.Protocol.TCP
});
/*
* calling new raw.packet will compute automagicaly ip header, pseudo header and tcp header,
* based on tcp headers and ip headers options passed in args
*
* it return an object with functions:
* - send(socket,options,callback) --> see below in this page
* - getBuffer() --> return the computed buffer, usefull for make TESTS !! :)
*/
var packet = new raw.packet({
tcph:{
sport:null, // optional, source port, if null, a random port will be set
dport:81, // mandatory, dst port
seq:null, // optional, sequence number, if null given, a random one will be set
ack_seq:null, // internal (?): next packet sequence number, default 0
doff:0, // internal (?): data offset, can be computed automagicaly ?
flag:raw.Flags.SYN, // mandatory, FIN,SYN,RST,PSH,ACK,URG
window: 1024, // optional, window size, defaut 1024 ?
ptr_urg:0, // optional, default 0
options:{
mss:1460 // optional, default 1460 ?
}
},
iph:{
ihl:0, // internal, header length
checksum:0, // internal, checksum,
protocol:raw.Protocol.TCP, // internal, default is createSocket protocol option ?
frag_off:???, // don't know what it is (fragment offset field)
version:raw.AddressFamily.IPv4, // optional, default IPv4
tos:0, // optional, type of service,
id:0 // optional, random by default
ttl:64 // optional, default 64 ?
saddr:'127.0.0.1', // optional, 127.0.0.1 default ?
daddr:null // optional, if null, take sendPacket first arg
}
});
var options = {
target:'127.0.0.1',
timeout:1000 // how many ms to wait for any the first packet emited by the target
}
var cb = function(err,buffer) {
/* err can be :
- timeout
- bad packet ?
(
should be great to check checksums
before sending the packet, for testing purpose
)
*/
}
var onAck = function(data) {
// data is the received packet
}
var onRst = function(data) {
// data is the received packet
}
// new packet.send function :
packet.send(socket,options,cb);
// theses callbacks should only be fired if
// packets are emited by the target
// and are valids packet (check checksums, size, ...)
packet.on('ack',onAck);
packet.on('rst',onRst);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment