?? windows internet programming part1.html
字號:
<html>
<body>
<pre>
<font color=red>
_________________
/_ /\
\/ _______ / \
/ / / / /
/ /______/ / /
/ __/ /
/ _______ \ __/
/ / / / \
/ /______/ / /
_/ / /
/______________/ / BLACK SUN RESEARCH FACILITY
\ \ / <a href="http://blacksun.box.sk">HTTP://BLACKSUN.BOX.SK</a>
\______________\/
</font>
WINDOWS INTERNET PROGRAMMING
=================================================
WRITTEN BY [ <a href="mailto:cos125@hotmail.com">cos125@hotmail.com</a> :E-MAIL ]
<a href="mailto:cos125@hotmail.com">BINARY RAPE</a> [ 114603188 :ICQ# ]
[ <a href="http://www.geocities.com/wininetprogram">www.geocities.com/wininetprogram</a> :WEB SITE ]
[ <a href="http://blacksun.box.sk">http://blacksun.box.sk</a> :TURORIALS ]
Thanks to cyberwolf for letting me write this and BSRF for releasing it.
Disclaimer
=======================================
None of the information or code in this tutorial is meant to be used against others
or to purposely damage computer systems or cause any loss of or damage to property.
Further more neither myself or any other contributor to, or member of, the Blacksun
research Facility (BSRF) can be held responsible for damage or loss of property of
computer systems as a result of this tutorial.
In this tutorial the code is provided as a learning aid so you can see how its done
its not meant for you to use against yourself or others.
Also in the Exercises sections you are encouraged to alter the code and improve it.
I say create or build a program to do something not create or build a program to do
something and use it for that purpose.
CONTENTS
=======================================
1. Introduction
2. Different types of Sockets
3. Protocols
3.1 What is TCP
3.2 What is UDP - An alternative
3.3 Introducing IP - The main protocol
3.4 TCP and UDP common functions
3.5 UDP specific functions
3.6 TCP specific functions
3.7 Structures
3.8 Converting
3.9 The application layer
4. Clients and servers
4.1 Bracaman's Server example - Win32
4.2 Bracaman's Client example - Win32
5. Exploring the Winsock
5.1 Looking at inet_addr()
5.2 Looking at htons()
5.3 Exploring Winsock functions
[ EXERCISES ]
6. Common Internet Programs
6.1 DNS
6.2 Port Scan
6.3 Nuker
[ EXERCISES ]
7. E-Mail - SMTP
[ EXERCISES ]
8. WinInet - FTP
[ EXERCISES ]
9. Another Protocol - ICMP
[ EXERCISES ]
10. Other Internet code
10.1 Internet connections
10.2 CGI Programming
[ EXERCISES ]
11. Last Words
Questions And Answers
APPENDIXES
A - The Compiler
B - IP and Port Numbers
C - Servers and Clients
D - Routers and Gateways
E - Further reading
F - Code
________________________________________________________________________________________________________
1.0 INTRODUCTION
=======================================
Welcome to Windows Internet Programming Part 1, the demented ramblings of a drunken Irish man.
Learning Internet programming can be very useful in many areas, not just for programmers that
wish to expand their knowledge but also to network administrators who should be more familiar
with how it all works and students who need to learn quick or they will fail their exams ;).
Even if you just want to learn programming as a hobbie then internet programming is a very easy
way to get more power over connections and to better understand whats really going on underneath
the hood of the internet.
The language of choice in this tutorial is c++ and the compiler is microsoft visual c++. To find
out how to set up your compiler for internet programming skip on ahead to Appendix A - The Compiler.
If you are not familiar with several topics on the internet such as the following:
1. IP address's and Port numbers.
2. Server and client relationships.
3. Routers and Gateways.
Then I suggest you at least read the sections Appendix B, Appendix C and Appendix D,
or goto Appendix E - Further Reading.
The protocols TCP, UDP, ICMP and IP are going to be discussed in the following sections but
they will all be addressed as we meet them so don't worry too much about them right now :).
Most of the following sections have an Exercises section at the end, just some suggestions about what
to do with the code after you read the section.
Youy are free to distribute the code that I write in this tutorial and this tutorial itself but
remember you must keep it intact. Use this file as a reference for anything you want even your own
papers or tutorials but please let me know if you do, id just like to know if this tutorial is of use
to you.
Ive made a website to coincide with this, and future tutorials and it should have a mailing list on it to,
this website will have articles and source code that are like add-on's to the tutorial, even when its finished.
The site should deal with internet technologies and programming in general as well as this tutorial.
The url is available at the top of the page (^ UP THERE ^).
There should be more additions to this tutorial soon as this is only version 1.0.
Feel free to send comments, questions and suggestions to me at my e-mail address above or add me to your
icq list.
Well without further adue Please read on as because im about to send this e-mail onto cyberwolf because
he keeps giving out to me for it being late :).
(Sorry cyber ive just been having alot of sex the last couple of weeks)
________________________________________________________________________________________________________
2.0 DIFFERENT TYPES OF SOCKETS
=======================================
There are 2 different main types of Sockets.
1. Streaming sockets.
2. Datagram sockets.
Programming TCP and UDP from scratch is difficult and involves alot of overhead. For this reason
the idea of sockets were created. The socket paradigm was developed at the University of California
at Berkelely. These sockets were designed for unix and similiar operating systems. In January of 1993
the Winsock was created to keep Bill Gates feet warm.. nnaahhh winsock is short for Windows Sockets
and is an implementation of Berkeley sockets but designed to take advantage of Windows message driven
architecture.
The winsock API was designed quickly in order to get it out as early as possible and therefore the
scope of the architecture mostly focused on TCP/IP but could still implement protocols such as
UDP (thankfully!).
Because Windows Sockets is based on the same sockets as unix, porting is quiet easy between the two,
we'll cover porting and multiple platform support in a later tutorial.
The Winsock is implemented trough the Winsock32.DLL in one of you system folders. It acts as a
layer between you (the programmer) and the hardware level (where packet generation and so on takes place).
You provide some perameters for the winsock such as the contents of the datagram, the target IP
address and the target port number and by calling some functions like sendto(); or recvfrom(); the
winsock creates the neccessary packets and sends or recieves datagrams.
The current version of Winsock is 2.0 and it takes more advantage of windows messaging and implements
better support for protocols other than TCP/IP.
+----------------------+ +-------------------------+ Your Application
| | | |
| Winsock2 Application | | Winsock 1.1 Application |
| | | |
+----------------------+ +-------------------------+
| |
| |
| |
+-----------------------------------------------------+ The Winsock
| Wsock32.dll - The Winsock DLL |
+-----------------------------------------------------+
|
|
|
+-----------------------------------------------------+ The Transport Layer
| The Hardware Layer |
+-----------------------------------------------------+
FIG 1. - How your program and sockets talk.
________________________________________________________________________________________________________
3.0 PROTOCOLS
=======================================
There are several protocols that we use in internet programming.
The first is IP and this is the base layer for Protocol programming.
The next 2 protocols are TCP, which is orientated with streaming sockets,
and then theres UDP which is orientated around datagram sockets.
Using these protocols we can build programs which run on the application layer and form
protocols such as FTP and HTTP.
3.1 WHAT IS TCP?
=======================================
TCP is the Transmission Control Protocol.
TCP is a protocol developed to make sure that packets were not lost on the internet as routers sent them
from computer to computer. You see when you want to send a file across the internet trough TCP you send
the file to the "TCP stack" (which is just another way of saying the Winsock).
TCP then splits up the file into little pieces, each piece is called a datagram, the size of each piece
depends on how good the tcp is on your computer and on the computer your sending the file to.
So if your computer can handle datagrams that are 1 kb large and smaller and your friends computer can
handle datagrams that are 500 bytes large and smaller, your TCP will divide a file into datagrams that
are 500 bytes largeso that both your computer and your friends can handle the size of the datagrams.
Otherwise it would be like trying to push a triangular peg into a round hole, it wouldn't fit.
The same as trying to squeeze digital larceny's mother into a normal bus seat, her ass is just too fat.
Now TCP was designed to keep track of every piece that it sends and to do so it numbers every datagram
that it creates, so we have 3 datagrams from a 1.5 kb file, first datagram is numbered 0, second 500 and
third is 1500.
Thanks to this numbering feature and some built in error checking functions TCP ensures that when you
send the datagrams A, B and C in that order they arrive in the order A B C and not something like C A B.
But how does TCP store these numbers? These numbers along with other information about the datagram,
such as the IP address and port number of where its being sent, is stored in something called a header.
A Datagram is wrapped up in a Header which contains information, this is why TCP was created, to store
and control the safe sending and recieving of Datagrams across the internet.
So with this example in mind say if I wanted to send a 1.5 kb file to a friend. Its a fairly small file
compared to most but still big enough that it needs to be split up by TCP.
+-----------------+ File before being sent
| 1.5 kb file |
+-----------------+
|
+-----+------+
| | |
+----+-----+------+ File split into datagrams on a 500 byte TCP connection
| 0 | 500 | 1500 |
+----+-----+------+
| | |
| | |
+----+-----+------+ TCP headers wrapped around Datagrams
| H | H | H |
+----+-----+------+
| 0 | 500 | 1500 |
+----+-----+------+
FIG 2. - TCP spliting a file into datagrams and wrapping them in their header.
3.2 WHAT IS UDP - AN ALTERNATIVE
=======================================
UDP was designed as an alternative to TCP, only problem is while TCP has built in error checking and
ensures that your file is recieved just the same way as it was sent UDP doesn't it just sends out its
datagrams and lets them find their own way to the host.
With TCP your quarenteed your sending A B C to a person but with UDP it could be more like B A C.
Because of this UDP is considered unreliable and is not counted on for important transfers.
Still UDP is not as bad as every-one says and the error rate is kinda low, still for important transfers
you just can't count on 'kinda' low.
UDP is better used on lan's and ethernet's than the internet because on these smaller networks it is
extremely rare that you would loose a datagram.
So if it is so unreliable why was it created?
Well there is a reason UDP is unreliable, it sacrifices its reliability for speed. There are many cases
where UDP is a definite advantage such as when you only need one datagram sent, then you wouldnt need to
worry whether A B C arrived in order cos' your only sending A.
UDP wraps datagrams in its own header for transmission, these headers are smaller and contain less info
than TCP headers but like I said sometimes UDP is a definite advantage.
3.3 INTRODUCING IP - THE MAIN PROTOCOL
=======================================
IP stands for Internet Protocol and all other protocols such as TCP and UDP ride piggy back on it.
IP doesn't divide files up into datagrams and the like it already has that job done for it by TCP and UDP.
IP is sent the datagram with a header wrapped around it (either a TCP or UDP header it doesn't matter),
and it wraps its own header around it.
Now we have a datagram with 2 seperate headers wrapped around it, the TCP header and the IP header.
This formation is known as an IP Packet. All information, e-mails, web-pages, messages and files are sent
across the internet in IP Packets.
IP headers are used to provide information to routers as opposed to, say for example TCP, whose headers
are meant for servers and clients to tell them how to put datagrams together.
The IP header contains information like the IP# and port number of the host its being sent to, which it
extracts from the TCP or UDP header, so that it can tell the routers where it wants to go.
The TTL is the Time To Live of the packet we are sending. Rather than the time to live specifying the
amount of time in seconds or minutes that the packet exists for it contains a number which states how many
routers the packet can meet before it is destroyed. The TTL is a number specified by you (the programmer).
For example if we set the time to live field as 10 in the IP header then the packet can meet ten
routers before it is destroyed. Each router that the packet meets subtracts 1 from the number so that after
the packet meets the first router the TTL will be equal to 9 and after the second it will be 8.
When the TTL hits 1 the router subtracts 1, gets 0 and throws away the packet, this is to ensure that
if a packet gets lost it wont just wander around the internet forever.
+-----------------------+
| IP Header |
+-----------------------+
|+---------------------+|
|| TCP Header ||
|+---------------------+|
||+-------------------+||
||| |||
||| Datagram |||
||| |||
||| |||
||+-------------------+||
|+---------------------+|
+-----------------------+
Fig 3. - Structure of an IP Packet.
3.4 TCP AND UDP COMMON FUNCTIONS
=======================================
TCP and UDP contain both common and different functions. All of these functions are contained within the
winsock.h header file (which is contained in windows.h).
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -