What the first line is doing is shifting the left half of the sourcePort to the right so that it fits into the 8bit packet. And the second line is erasing the left half of the sourcepPort so that the right half of the sourcePort fits in the second part of the packet. Is this right?
I also enjoyed the post! Really cool and help me learn something :)
Exactly! There are several ways to do this step, finally I used this way to use both, bit shift and logic gate operation.
According to the example, for the port 80 (bits in green are the insert):
packet[0] = sourcePort >> 8;
packet[1] = sourcePort & 0xFF;
and, for the sourcePort 3258: packet[2] = destinationPort >> 8;
packet[3] = destinationPort & 0xFF;
I hope I have clarified the insertion more, although you had deduced it well!
Of course this can be simplified somewhat by using the header structures that are already defined, e.g struct tcphdr in netinet/tcp.h, there are also headers defined for UDP, IPv4, IPv6, ethernet etc...
Simplified example, also note the use of htons(3) to take care of endianess...
#include <netinet/tcp.h>
#include <arpa/inet.h>
intmain(void){structtcphdrtcphdr={0};/* source port */tcphdr.th_sport=htons(80);/* destination port */tcphdr.th_dport=htons(3258);/* etc ... see struct tcphdr in /usr/include/netinet/tcp.h */return0;}
Hopefully you'll agree that's cleaner, more readable and maintainable code...
Totally agree, my code was originally part of a university work and I thought to extend it to a didactic project to better understand the structure of the packages (at a lower level). I will continue to publish on the subject, so I would like you to review it when I do (you have much more knowledge than me (without doubts) on the subject, so I'm sure I learn from you 😋).
Thx for reading and aport!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Just so I understand this part of the code
What the first line is doing is shifting the left half of the sourcePort to the right so that it fits into the 8bit packet. And the second line is erasing the left half of the sourcepPort so that the right half of the sourcePort fits in the second part of the packet. Is this right?
I also enjoyed the post! Really cool and help me learn something :)
Exactly! There are several ways to do this step, finally I used this way to use both, bit shift and logic gate operation.
According to the example, for the port 80 (bits in green are the insert):
packet[0] = sourcePort >> 8;packet[1] = sourcePort & 0xFF;and, for the sourcePort 3258:
packet[2] = destinationPort >> 8;packet[3] = destinationPort & 0xFF;I hope I have clarified the insertion more, although you had deduced it well!
Thx for reading and commenting!
Thanks for the deeper explanation!
Of course this can be simplified somewhat by using the header structures that are already defined, e.g struct tcphdr in netinet/tcp.h, there are also headers defined for UDP, IPv4, IPv6, ethernet etc...
Simplified example, also note the use of htons(3) to take care of endianess...
Hopefully you'll agree that's cleaner, more readable and maintainable code...
Cheers,
Andrew
Totally agree, my code was originally part of a university work and I thought to extend it to a didactic project to better understand the structure of the packages (at a lower level). I will continue to publish on the subject, so I would like you to review it when I do (you have much more knowledge than me (without doubts) on the subject, so I'm sure I learn from you 😋).
Thx for reading and aport!