You are on page 1of 6

How to Create TCP/IP Packets Code in C Programming

Read more : http://www.ehow.com/how_11400476_create-tcp-ip-packets-code-cprogramming.html

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.

How to change all incoming udp packets to tcp


packets on the same port using ip tables?
just for security reasons i have to write this rule using IP tables in my rule. u can
give me the answer using mangle tables or other tables.It is network administration
question. Thanks for the help and your time.
Answer:
UDP packets have a different header than TCP packets. Change the header, and
basically, you have changed the packet.
Not only is this impractical.. but if you did succeed, then you need to artificially
create your syn/ack handshake.. UDP doesn't care about handshakes- but your
destination host needs to open a channel to receive TCP packets.
you need to create packet ack/ answers..or at least a sink for them to be routed to..
UDP source doesn't care if you got the packet.. it won't retransmit anyhow.
If an instructor gave me this question, I'd want to clarify the reasons..
using only ip tables, I suppose I could just retransmit the packet.. but it makes no
sense.
Doing this creates more latency; and
if a packet is dropped between your ip tables rule and the destination - then how
will you handle the retransmission request? The destination host is waiting possibly forever?
and
using IP tables / mangle tables or any similar tool is not a complete solution.
I'd suspect this is a question designed to see if I know the protocols (and perhaps
the tools) that I am supposed to know... enough to know this is a silly proposition.
Sure, anything can be done if you try hard enough.. but this is like floating icebergs
to the Sahara. With enough money, it could be done, but is really impractical.

How to edit the network packet ?


how to edit the information in a packet which have information like tcp/ip packets
how to edit their informations ?

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..

How to construct and inject/send a new TCP


packet?
Hi, I was wondering if somebody could help me with the process of constructing from scratch
my own IPv4/TCP header and injecting it at the network send layer?
I've been attempting the following method (though I understand it may be, and in all likelihood
IS, completely wrong)
1. Allocate a data buffer with ExAllocatePoolWithTag , with enough space for a IPv4 header
and a TCP header. (I don't need to send a payload currently) Memset this buffer to 0.
2. Allocate an MDL using NdisAllocateMdl , using as parameters my driver handle, the data
buffer and the size of the data buffer.
3. Allocate a NetBuffer and NBL using NdisAllocateNetBufferAndNetBufferList , using as
parameters my Ndis Pool Handle (which I acquired earlier), 0 for the context size and
back fill, my MDL I just allocated, 0 for the DataOffset and the data buffer size for the
DataLength.
4. Use the NET_BUFFER_LIST_FIRST_NB macro and NdisGetDataBuffer function to get a
pointer to the data buffer (I think I could have used the old data buffer pointer for this, but
I wanted to check that the contiguous space had been allocated to the structure properly
anyway), parameters used for the function was the NetBuffer received from the macro,
the data buffer size, NULL for storage, sizeof(UINT8) for alignment multiple and 0 for
alignment offset (not sure about those last two parameters).
5. Then I fill this buffer with the data I wanted for the IPv4 and TCP header.
6. Then I call FwpsInjectNetworkSendAsync0 , using as parameters my injection handle,
NULL for injection context, 0 for flags, UNSPECIFIED_COMPARTMENT_ID, the
NBL, my completionFn, and a data structure containing pointers so I can clean up
everything afterwards.

On the last step I get back the NTSTATUS code STATUS_FWP_INVALID_PARAMETER, I

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

You might also like