How To Create TCP IP Packets Code in C Programming
How To Create TCP IP Packets Code in C Programming
Creating a TCP/IP packet is important when you want your C program to make an active
connection with a remote host to send data back and forth. TCP/IP is a networking
protocol that provides reliable and ordered delivery of packets between two hosts.
The world wide web, email and file transfer applications all use the TCP/IP protocol.
Create a TCP/IP packet by collecting data about a host, making a socket out of that
data and then sending the socket to the remote host
Instructions
1.
o
1
Open your C file in an editor such as VC++.
2
Add the Winsock library to your compiler's project settings so that it will link
properly. In VC++ this is done by clicking the "Project" menu, clicking
"Settings...," clicking "Link" and typing "ws2_32.lib" in the box titled
"Object/library modules." Other possible names for the Winsock library include
"winsock32.lib" and "wsock32.lib."
3
Include the "winsock2" and "ws3tcpip" headers to access the socket functions by adding the
following code at the top of your file:
include <winsock2.h>
include <ws2tcpip.h>
4
Declare the variables needed to create a TCP/IP packet by adding the following code in your
function:
struct addrinfo hints, *res;
int socket_descriptor;
The "addrinfo" structs will store the return value of the "getaddrinfo" function. The
"socket_descriptor" is the integer descriptor that the "socket" function will return.
5
Initialize the variables by adding the following code:
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
The "AF_UNSPEC" value specifies that the address family for the "getaddrinfo" function can be
any valid type, such as IPv4 or IPv6. The "SOCK_STREAM" indicates a TCP stream socket.
6
Call the "getaddrinfo" function by adding the following code:
getaddrinfo("www.server.com", "3490", &hints, &res);
Replace "www.server.com" with the server you want to connect to. Replace "3490" with the port
you will connect to. The "getaddrinfo" function collects protocol-independent information about
an address from its host name, which it returns in the "addrinfo" structs.
7
Create a TCP/IP socket with the "socket" function, by adding the following code:
socket_descriptor = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
The "socket" function uses the information returned from the "getaddrinfo" function to create a
complete socket.
8
Connect to the remote server with the "connect" function by adding the following code:
connect(socket_descriptor, res->ai_addr, res->ai_addrlen);
The "connect" function takes your socket, sends it to the specified address and creates an active
connection with the host.
9
Save the file, compile and execute your program to create the TCP/IP packet.
What you are probably looking for is information on "packet injection" or "packet
crafting". There aren't many legitimate reasons that someone would want to do this
so I can only guess you are interested for reasons of security (or evil).
You should know that this practice is highly complicated. What you are suggesting is
that you want to change a packet that has all ready made it's way onto a network in
route to it's destination. So your goal is to get into the middle of that conversation.
That would mean that you would need to become that users gateway so that you
can intercept, recraft, and resend that information along on it's way. This can be
done by using a tool such as 'ettercap'.
If you are only trying to change your own packets, I suppose that this could be a
little easier. But the way that you are asking this questions suggests that you might
need to study up on networking a bit more before you try something like this.
I would suggest downloading and installing wireshark. Take a look at house things
work and once you really understand matters, you can start looking into security
tools. You might also want to try "backtrack", which is a bootable Linux distribution
that comes pre-loaded with a lot of various security tools.
Source:
http://en.wikipedia.org/wiki/Packet_inje...
http://en.wikipedia.org/wiki/Raw_socket
http://www.wireshark.org/
http://www.backtrack-linux.org/
I'd like to know how one can send a TCP/IP or UDP packet, and then send it.
Preferably in C/C++, if it must be written in a programming language.
do you have a Ham radio set up?
if so you can do pakets with a TNC.
I hook up with the satalites once in a while with my Kantronics packet producing
transmitter with the aid of a TNC on VHF and UHF freqs..
think there's a lot that could have gone wrong at any of the steps (though the previous ones all
seemed to succeed judging by the status codes) so I was hoping that somebody here more
knowledgable than me might be able to see where I'm going wrong or tell me if I'm just doing
the wrong method entirely? I haven't found any examples in the DDK for doing this, though I did
see some for modifying existing packets. I basically just tried this process from reading through
the API and Googling to try to work out how it all appears to link together.
Thanks in advance for any help!
Answer:
Just to let you all know I think I've managed to fix it now!
I changed my function call from NdisAllocateNetBufferAndNetBufferList to
FwpsAllocateNetBufferAndNetBufferList0 (both of which take more or less the same
parameters) and my constructed packet now seems to inject correctly, it shows in
Wireshark on both the source and destination computers. I think that was the only
change I made.
http://jnetpcap.com/node/621
http://search.yahoo.com/search?p=how+to+make+a+tcp+packet