Named Pipes - Digiater.nl

Download Report

Transcript Named Pipes - Digiater.nl

Windows NT™ Network
Programming Tools &
Techniques
NT014
Paul Fix
Mentec Inc.
1
Copyright, 1997 ©Mentec Inc
Agenda
• Network API overview
– NAMEPIPES MAILSLOTS NetBios RPC (SOCKETS TDI)
• NET,WNET,WININET
– FEATURES
•
•
•
•
Addressing
Asynchronous - Synchronous
Connection - connectionless
Datagram , Stream ,Packet
– API Basic code overview
– Architecture
– Perfmon , Network Monitor
2
Network Interface API’s
• Application
• RPC , Name Pipes,Mailslots
• Session
• Transport
• MAC
• Sockets,NetBIOS,TDI
• Sockets,TDI
• NDIS
3
Questions
•
What API should I used to find out what user is logged log
on from my program?
•
How can I control security on my NAMEPIPE application?
•
What API should I use to pull files across the network
using FTP with out using window sockets or reading an RFC?
•
What API will allow me to read from my program html
documents without using socket programming?
•
Why can’t I see my LAN Manager based Pathworks servers on
my internal LAN?
4
Questions
•
How do I design a non-transparent task to task AST driven
network server program for Windows NT?
•
When should use a TDI kernel mode driver instead of using
Win sockets?
Why is my RSX decnet system dying on the network after I
added my WNT systems?
•
•
What is the best Synchronization mechanism to use when
developing a High performance server application?
•
How do I write an equivalent to my 802.3,DLX or DLI program
with Windows NT?
5
Named Pipes
• High-level Inter-process communications data
transport API
• Implemented as Windows File system driver NPFS
• API syntax native Win32 file I/O for reads and write.
• Provide Reliable connection oriented data transfer
between two process
• Named pipes are based on OS/2 API
• Additional asynchronous support and increased
security with Windows NT
6
Namepipe features &
techniques
• Additional asynchronous support and increased
security.
– Impersonation: ( feature not available with LAN Manager
2.x Name Pipes)
• Access tokens to that of the Client application process. This allows
the server process to perform local operations based on the client
process privileges based on the current user on the client sides
Windows NT rights and access privileges.
– Overlapped I/O- Support for the NT mechanisms for
asynchronous requests ( AST's )
7
NamedPipe Basic API calls
SERVER
CLIENT
CreateNamedPipe(
\PIPE\<pipename>,,,,,) create a
WaitNamedPipe( server\pipe\<pipe
named pipe
name>)
ConnectNamedPipe(<handle>) wait
CreateFile(server\pipe\<pipe name>)
for client to connect
open a named pipe on server
Writefile(<handle>)
ReadFile(<handle>)
ReadFile(<handle>)
Writefile(<handle>)
CloseHandle(<handle>)
FlushFileBuffers(<handle>)
CloseHandle(<handle>)
8
CreateNamedPipe api call
hPipe = CreateNamedPipe ("\\\\.\\PIPE\\test",//
PIPE_ACCESS_DUPLEX
//
| FILE_FLAG_OVERLAPPED,
PIPE_WAIT
//
| PIPE_READMODE_MESSAGE
| PIPE_TYPE_MESSAGE,
MAX_PIPE_INSTANCES,
//
OUT_BUF_SIZE,
//
IN_BUF_SIZE,
TIME_OUT,
//
&sa);
//
Pipe name = 'test'.
2 way pipe.
Wait on messages.
Maximum instance limit.
Buffer sizes.
Specify time out.
Security attributes.
9
Named Pipes Server
Architecture Overview
Server side user application
Win32 named pipe and file I/O api
kernel32.dll
NT I/O Manager
kernel
NT Server Service
NPFS
Named Pipe File System
TDI interface
NBF
NWlink
Tcp/Ip
10
Named Pipes CLIENT
Architecture Overview
Client side user application
Win32 named pipe and file I/O api
kernel32.dll
NT I/O Manager
MUP
NT LM Redirector
TDI interface
NBF
NWlink
Tcp/Ip
11
Network SMB Server &
Redirector monitoring
demo 1
• Using Performance monitor and excel to
graph SMB traffic
12
Tips and Techniques
• Supported through the InterProcess
Communications shared resource (IPC$)
• Network trace will show SMB packets being
exchanged
• The TransactNamedPipe function combines into a
single network operation r/w
• Named Pipes server can service only one client for
each instance of a pipe.
• Use WaitNamedPipe() on client to block until server
pipe comes available.
13
NamePipe Summary
• Because Named Pipes are implemented by NT file
system drivers the API are highly compatible with
other win32 API's.
• Data transfer between two processes used standard
Windows NT file I/O calls WriteFile() and
ReadFile() .
• Named piped can be opened by DOS, OS/2 , WIN16
or WinNT clients
14
Mailslots
– Subset of those in Microsoft OS/2 LAN
Manager Mailslots
– Windows NT implements only secondclass MailSlots
– Second-class MailSlots provide
connectionless messaging for broadcast
messages
– Delivery of the message is not
guaranteed
•
15
MailSlot Basic API calls
Server
Client
CreateMailSlot(
\server\mailslot\<mailslot name>)
CreateFile
(\server\mailslot\<mailslot
name>)
ReadFile(handle)
WriteFile (handle)
CloseHandle(handle)
CloseHandle(handle)
16
MailSlots Server Architecture
Overview
Server side user application
Win32 MailSlot and file I/O api
kernel32.dll
NT I/O Manager
kernel
NT Server Service
MSFS
MailSlot File System
TDI interface
NBF
NWlink
Tcp/Ip
17
MailSlot CLIENT
Architecture Overview
Client side user application
Win32 MailSlot and file I/O api
kernel32.dll
•MUP - Multiple UNC Provider
NT I/O Manager
MUP
NT LM Redirector
TDI interface
NBF
NWlink
Tcp/Ip
18
MailSlot Summary
– MailSlots like NamePipes are a High-level API
for peer to peer networking. Both are
implemented as file system drivers and use
WriteFile() and ReadFile() for exchanging data.
– Remote access to named pipes and MailSlots,
as with all of the file systems, is provided
through the redirector.
19
Mail Slot Summary
– Second-class MailSlots only
– Second-class MailSlots provide connectionless
messaging for broadcast messages
– not guaranteed
– Second-class MailSlots are most useful for
identifying other computers or services on a
network and for wide-scale notification of a
service.
20
NetBIOS
NETwork Basic Input Output System
21
NetBIOS
• Defacto industry standard for network
application development on MS-DOS
networks.
• Sytex Corp. 1983 for IBM network adapter
ROM
• IBM's LAN Technical Reference / IBM
NETBIOS Application Development Guide
• Session Level Interface
22
NetBIOS features and services
•
•
•
•
•
•
Reliable Connection Oriented data transfer
Unreliable Connectionless datagram
Network Name registration
Session establishment and termination
Adapter Monitoring and management
NBFP NetBIOS Frames Protocol
23
Why use NT NetBIOS
• Existing application
• Industry standard on DOS & OS/2
• Windows Socket interface preferred if there
is a choice and a low level interface is
required.
24
NetBIOS transports
•
•
•
•
NetBEUI
TCP/IP RFC 1001 and RFC 1002
XNS
DECNET object 64
25
NetBIOS over TCP/IP ports
• UDP port 137 (name services)
• UDP port 138 (datagram services)
• TCP port 139 (session services)
26
NetBIOS datagram
TCP/IP
(Ethernet II 08-00 )
Destination Address
= FF-FF-FF-FF-FF-FF (Broadcast)
Source Address
= 08-00-2B-2B-C7-93 (08002B2BC793)
DIX format, Protocol Type
= 08-00
NetBEUI protocol (802.2 SAP F0)
Destination Address
= 03-00-00-00-00-01 (All_NETBIOS)
Source Address
= 08-00-2B-2B-C7-93 (08002B2BC793)
802.2 format
Digital NetBIOS (Ethernet II 80-40)
Destination Address
= 09-00-2B-00-00-07 (DEC_NETBIOS)
Source Address
= AA-00-04-00-BF-72
DIX format, Protocol Type = 80-40
27
NetBIOS TCP/IP datagram
Packet
…..header edited for clarity
DLL: DIX format, Protocol Type = 08-00
IP: Protocol Type
= 17 (UDP)
UDP: Destination port
= 138 (NB Datagram) ¦
RFCNB: Type of request
= 17 (Group)
RFCNB: Source port
= 138 (NB Datagram)
SMB: - - - - - Mailslot Transaction - - - - SMB: Mailslot opcode
= 1 (WriteMS) _
SMB: Message class
= 2 (Unreliable)
SMB: Transaction name or pad
\MAILSLOT\BROWSE"
28
Network Monitor Broadcast
demo 2
29
NetBIOS NT subsystem support
• Win32 and OS/2 Subsystems- just call
win32 NetBIOS function with pointer to
NCB
• No support in POSIX
30
NetBIOS interface
• NetBIOS applications use a NCB structure
to issue NetBIOS commands
– Commands can be categorized as follows:
•
•
•
•
•
•
Name Support
Session Support
Data-Transfer Support
Datagram Support
General Support
Windows NT Extensions
31
NCB
typedef struct _NCB { // ncb
UCHAR ncb_command;
UCHAR ncb_retcode;
UCHAR ncb_lsn;
UCHAR ncb_num;
PUCHAR ncb_buffer;
WORD ncb_length;
UCHAR ncb_callname[NCBNAMSZ];
UCHAR ncb_name[NCBNAMSZ];
UCHAR ncb_rto;
UCHAR ncb_sto;
void (*ncb_post) (struct _NCB *);
UCHAR ncb_lana_num;
UCHAR ncb_cmd_cplt;
UCHAR ncb_reserve[10];
HANDLE ncb_event;
} NCB;
32
NCB_Command
• Example of ncb commands
–
–
–
–
NCBADDNAME - register name
NCBCALL - establish session
NCBSEND - send data
NCBRECV - receive data
33
NetBios function call
UCHAR Netbios(
PNCB pncb // pointer to the network control block
);
34
NetBIOS Basic Architecture
Redirectors,
Servers ,...
Socket
application
NetBIOS
application
Socket
Interface
NetBIOS
Interface
Socket
Eumulator
NetBIOS
Emulator
TDI Clients
TDI Interface
Ndis clients TDI
providers
NetBT
NBF
Appletalk TCP/IP
NDIS NIC Driver
NWlink
other
Transport
Providers
35
NetBIOS summary
– “The Netbios function is provided primarily for
applications that were written for the NetBIOS interface
and need to be ported to Windows. Applications not
requiring compatibility with NetBIOS should use other
interfaces, such as mailslots, named pipes, RPC, or
distributed COM to accomplish tasks similar to those
supported by NetBIOS. These other interfaces are more
flexible and portable. “
» Source: Microsoft SDK help on NetBIOS funtion call
36
Remote Procedure Call
• Originated with Sun Microsystems.
• RPC allows computer to share resources
with other computers on the network
• Resolves data translation problems between
different hosts
• RPC is a popular industry standard
• compatible with OSF/DCE standard RPC
37
RPC Components
–
–
–
–
–
–
–
MIDL Microsoft Interface Definition Language
ACF Attribute configuration file (optional)
RPC Client stub
RPC Server stub
RPC runtime library
RPC services
Remote procedures
38
RPC IPC usage
• RPC uses other IPC mechanisms to
establish communications between the
client and the server.
– Named pipes
– NetBIOS
– Windows Sockets
39
RPC server
• Specify protocol Sequence to use
– RpcServerUsexxx
• register the procedure
– RpcServerRegister
• listen for client requests
– RpcServerListen
40
RPC Architecture Overview
Client Application
Server Application
Client Stub
Server Stub
Client Run Time Library
Server Run Time Library
Transport
Transport
41
RPC Summary
– Can utilize available CPU power across the
network
– Powerful technique for distributed network
application development.
– Can be used on local system using LPC
– Supports many protocols that permit
connectivity to NT, UNIX,NetWare and
DECNET networks.
– Used in conjunction with NT services can be a
powerful set of tools for distributed computing
42
Windows Sockets
• Socket API originally developed at UC
Berkeley BSD Sockets
• Basic design structured like Unix file I/O
– Under the Unix environment the standard file
I/O read and write used for sockets.
• Windows Sockets based on BSD Sockets
4.3 Winsock 2.0 latest version
• low level interface
43
socket
sock = socket( AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET)
MessageBox(hWnd, "socket()failed","Error",MB_OK);
44
Socket families,type, protocol
Family
Type
Protocol
AF_INET
AF_INET
AF_IPX
AF_IPX
AF_IPX
SOCK_DGRAM
SOCK_STREAM
SOCK_DGRAM
SOCK_STREAM
SOCK_SEQPACK
ET
UDP
TCP
IPX
SPX
IPX
….OTHERS
45
Basic API Call example
• Server application
–
–
–
–
–
WSAStartup
socket
bind
listen
accept
– recv
• Client Application
• WSAStartup
– socket
– bind
– connect
– send
46
Socket Architecture
Redirectors,
Servers ,...
Socket
application
NetBIOS
application
Socket
Interface
NetBIOS
Interface
Socket
Eumulator
NetBIOS
Emulator
TDI Clients
TDI Interface
Ndis clients TDI
providers
NetBT
NBF
Appletalk TCP/IP
NDIS NIC Driver
NWlink
other
Transport
Providers
47
Winsock Summary
• Network API of choice for most low level
network applications
• Industry Standard on Unix systems
• easy migration for BSD socket network
applications.
• Create NT services
48
TDI
Transport Driver Interface
• Primarily Microsoft Internal use Not
industry standard
• Most flexible and complex
• kernel mode driver
• Good documentation in DDK and examples
49
Tools for testing and debugging
•
•
•
•
•
Network Monitor
Performance Monitor
TDItrace
Debugger of choice
DEMO……...
50
Other Useful Utilities and tools
for Network Programmers
•
•
•
•
•
•
•
•
NETSTAT
NET CONFIG
WINMSDP /n
GETMAC - get hardware MAC information
NLTEST/NLMON - test NT trust relationships
EXETYPE
EVENT LOG
NETWORK CONTROL PANEL APPLET
51
Programming tools
•
•
•
•
•
•
•
•
•
•
•
Call Attributed Profiler
Win32 API Profiler
Win32 API Logger
File I/O and Synchronization Profiler
Pmon
Working Set Tuner
Virtual Address Dump
The Windows NT symbolic debugger's wt command
Network Monitor
Performance Monitor
TDI trace
52
Other Network API's
• WNET
• NET
• WinInet
53
WNET
• Subset of the Win32 API's to provide
network independence
• Standard API for browsing and connecting
to network providers .
• NET API supplements the generic
networking functions built into WNET with
windows specific API’s.
54
WNET examples
• WNetAddConnection2
• makes a connection to a network resource.
• WNetGetUser
• retrieves the current default user name
• WNetGetConnection
• retrieves the name of the network resource
associated with a local device.
55
NET examples
– NetUseAdd
– establishes a connection between a local device name and
a shared resource
– Win32-based applications should use the
WNetAddConnection2 function.
– NetUserAdd– adds a user account and assigns a password and privilege
level.
– NetUserGetInfo
– -retrieves information about a particular user account
56
Win32 Internet Functions
Wininet
• Wininet.dll
• simplify accessing internet ftp, http, and
gopher
• No need for protocol or need to write
Windows Socket code to directly access
HTTP,FTP and gopher information.
• No need to maintain protocol specific code
to keep up with rapidly changing Internet
57
WinInet API calls
– InternetConnect – Opens an FTP, Gopher, or HTTP session for a given site.
– HttpOpenRequest
– Opens an HTTP request handle.
– InternetReadFile
– -Reads data from a handle opened by the InternetOpenUrl,
FtpOpenFile, GopherOpenFile, or HttpOpenRequest
function.
58
DEMO 3 other tools
59
High level Network Application
programming
• DCOM - Object technology
• N-Tier Client Server applications
– MTS
• INTERNET APPLICATIONS
• ActiveX® , Java, DHTML,scriptlets,vbscripts etc
etc etc. More to come….
• Extensions to IIS … ISAPI
• Internet Client SDK IE4.0
60
Summary
• Windows NT is a Network Operating system that continues to evolve
to expand the Network Programmers design options.
•
When designing or porting an existing network application take time
to evaluate all of the network programming options available. Don’t
forget third party alternatives that could have the exact solution.
61