Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

UNIX network programming
PREMIUM
Số trang
1011
Kích thước
29.8 MB
Định dạng
PDF
Lượt xem
1872

UNIX network programming

Nội dung xem thử

Mô tả chi tiết

INPIS: Sockets and XTI 1

Communications I UNlY I

I Networking APls: Sockets and XTI I

Whether you write Web servers, clientlserver applications, or any other network

software, you need to understand networking APls-especially sockets-in greater

detail than ever before. You need UNlX Network Programming, Wume 1,

Seebnd Editfon.

In this book, leading Unvt networking expert. a Richard Stevens, offers unprecedented,

start-to-finish guidance on making the most of s-ockets. the de facto standard for

Unix network programming-as well as extensive coverage of the WOpen Transport

Inte&$e CXTI). 5

Slevrns hclCtns hy tnlrcxlllr.lne, vtrLunlly ('very haslc' repal,~l~Lv <,I T(:l' and 1JI)I' snrkrl

~ncluLling socket funcllons and oplrons. If0 l~iuluplexi~ig. and name and alldress coliver￾smns 111. prvscnL% clrtallrd covcrage OF tho Po%ix.lg ~lt~ndard For sockrts and Posix 1

threads. HP also introduces advanced techniques for. I

lPv4/IPv6 Interupel dblllty * Mult~th~aad~ng

Nonblock~ng I/O Debuggmg techn~ques

Rol ttng mckets Un~x dornam protocols

Broadcastmg and mult~castmg Raw sockets

IP optws ChenWsewer desrr~l alternatwes I

Learn how to choose among today's leadhg clientlserver design approaches, %clud&

TCP iterative. concurrent, preforked and prethreaded servers. Master the VOpen

Transport Interface, including XTI TCP clients and serve;& name and address functions,

options, streams. and additional functions.

The lnternetlintranet revolution has dramatically increased the demand for developers

with a sophisticated understanding of network programming APls, especially

sockets. One book contains all you need to know: UlYaX mmmimk - ~m1,~Editiotr

ABOUT THE AUTHOR qf

W. RICHARD STEVENS is author of UNlX Network Programming. First Edition.

widely recognized as the classic text in Unix networking. He is also the author of

Advanced Progrdmming in thr UNlX Environment and the TCP/iP illustrated Series.

He is an acknowledged Unix and networking expert. sought-after instructor. and

occasional consultant.

PRENTICE HALL

Upper Saddle River, NJ 07458

UNIX Network Programming

Volume 1

Second Edition

Networking APls:

Sockets and XTI

by W. Richard Stevens

To join a Prentice Hall PTR Internet mailing list, point to

http://www.prenhall.com/mail~lists/

ISBN O-L3-4900LZ-X

Prentice Hall PTR

Upper Saddle River, NJ 07458

Library of Congress Cataloging-in-Publica~ion Data

Stevens, W. Richard

UNIX network programming / by W. Richard Stevens. -- 2nd ed.

p. cm.

Includes index.

ISBN 0-13-490012-X

1. UNIX [Computer file) 2. Computer networks. 3. Internet

programming. I. Title.

QA76.76.0635755 1997

005.7'127768--dc21 97-31761

CIP

Editorial/Production Supervision: Eileen Clark

Acquisitions Editor: 1Lfary Franz

Marketing Manager: Miles Williams

Buyer: Alexis R. Heydt

Cover Design: Scott I~Veiss

Cover Design Direction: Jerry Votta

Editorial Assistant: Noreen Regina

O 1998 Prentice Hall PTR

Prentice-Hall, Inc.

A Simon & Schuster Company

Upper Saddle River, NJ 07458

Prentice Hall books are widely used by corporations and government agencies for training, marketing, and

resale. The publisher offers discounts on this book when ordered in bulk quantities.

For more information, contact

Corporate Sales Department,

Phone: 800-382-3419; FAX: 201-236-7141

E-mail (Internet): [email protected]

Or write: Prentice Hall PTR

Corp. Sales Department

One Lake Street

Upper Saddle River, NJ 07458

All rights reserved. No part of this book may be

reproduced, in any form or by any means, without

permission in writing from the publisher.

Printed in the United States of America

ISBN 0- 13-4900 12-X

Prentice-Hall International (UK) Limited, London

Prentice-Hall of Australia Pty. Limited, Sydney

Prentice-Hall Canada Inc., Toronto

Prentice-Hall Hispanoamericana, S.A., Mexico

Prentice-Hall of India Private Limited, New Delhi

Prentice-Hall of Japan, Inc., Tokyo

Simon & Schuster Asia Pte. Ltd., Singapore

Editora Prentice-Hall do Brasil, Ltda., Rio de Janeiro

To Sally, Bill, Ellen, and David.

Aloha nui loa.

Contents

Preface xv

Part 1. Introduction and TCPIIP 1

Chapter 1.

1.1

1.2

1.3

1.4

1.5

1.6

1.7

1.8

1.9

1.10

1.11

1.12

Chapter 2.

2.1

2.2

2.3

2.4

2.5

lntroduction

lntroduction 3

A Simple Daytime Client 6

Protocol Independence 9

Error Handling: Wrapper Functions 11

A Simple Daytime Server 13

Road Map to Client-Server Examples in the Text 16

OSI Model 18

BSD Networking History 19

Test Networks and Hosts 20

Unix Standards 24

64-bit Architectures 27

Summary 28

The Transport Layer: TCP and UDP

lntroduction 29

The Big Picture 30

UDP: User Datagram Protocol 32

TCP: Transmission Control Protocol 32

TCP Connection Establishment and Termination 34

vi UNIX Network Programming Contents

2.6 TIME-WAIT State 40

2.7 Port Numbers 41

2.8 TCP Port Numbers and Concurrent Servers 44

2.9 Buffer Sizes and Limitations 46

2.10 Standard lnternet Services 50

2.11 Protocol Usage by Common Internet Applications 52

2.12 Summary 52

Part 2. Elementarv Sockets

Chapter 3.

3.1

3.2

3.3

3.4

3.5

3.6

3.7

3.8

3.9

3.10

3.1 1

Chapter 4.

4.1

4.2

4.3

4.4

4.5

4.6

4.7

4.8

4.9

4.1 0

4.1 1

Chapter 5.

5.1

5.2

5.3

5.4

5.5

5.6

5.7

5.8

5.9

5.1 0

Sockets Introduction 57

lntroduction 57

Socket Address Structures 57

Value-Result Arguments 63

Byte Ordering Functions 66

Byte Manipulation Functions 69

inet-aton, inet-addr, and inet-ntoa Functions 70

inetston and inet-ntop Functions 72

sock-ntop and Related Functions 75

readn, writen, and readline Functions 77

isfdtype Function 81

Summary 82

Elementary TCP Sockets

lntroduction 85

socket Function 85

connect Function 89

bind Function 91

listen Function 93

accept Function 99

fork and exec Functions 102

Concurrent Servers 104

close Function 107

get sockname and getpeername Functions 107

Summary 1 10

TCP Client-Server Example

lntroduction 11 1

TCP Echo Server: main Function 112

TCP Echo Server: str-echo Function 113

TCP Echo Client: main Function 113

TCP Echo Client: str-cli Function 115

Normal Startup 115

Normal Termination 117

Posix Signal Handling 1 19

Handling SIGCHLD Signals 122

wait and waitpid Functions 124

UNIX Network Programming Contents vii

5.1 1

5.12

5.13

5.14

5.15

5.16

5.1 7

5.18

5.19

Chapter 6.

6.1

6.2

6.3

6.4

6.5

6.6

6.7

6.8

6.9

6.10

6.1 1

6.12

Chapter 7.

7.1

7.2

7.3

7.4

7.5

7.6

7.7

7.8

7.9

7.10

7.1 1

Chapter 8.

8.1

8.2

8.3

8.4

8.5

8.6

8.7

8.8

8.9

8.10

Connection Abort before accept Returns 129

Termination of Server Process 130

SIGPIPE Signal 132

Crashing of Server Host 133

Crashing and Rebooting of Server Host 134

Shutdown of Server Host 135

Summary of TCP Example 135

Data Format 137

Summary 140

I10 Multiplexing: The select and poll Functions

lntroduction 143

It0 Models 144

select Function 150

str-cl i Function (Revisited) 155

Batch Input 157

shutdown Function 160

str-cli Function (Revisited Again) 161

TCP Echo Server (Revisited) 162

pselect Function 168

poll Function 169

TCP Echo Server (Revisited Again) 172

Summary 175

Socket Options

lntroduction 177

getsockopt and setsockopt Functions 178

Checking If an Option Is Supported and Obtaining the Default

Socket States 183

Generic Socket Options 183

IPv4 Socket Options 197

ICMPv6 Socket Option 199

IPv6 Socket Options 199

TCP Socket Options 201

fcntl Function 205

Summary 207

Elementary UDP Sockets

lntroduction 21 1

recvfrom and sendto Functions 212

UDP Echo Server: main Function 213

UDP Echo Server: dg-echo Function 214

UDP Echo Client: main Function 216

UDP Echo Client: dg-cli Function 217

Lost Datagrams 217

Verifying Received Response 21 8

Server Not Running 220

Summary of UDP example 221

viii UNIX Network Programming Contents

8.1 1

8.1 2

8.1 3

8.14

8.15

8.16

Chapter 9.

9.1

9.2

9.3

9.4

9.5

9.6

9.7

9.8

9.9

9.10

9.1 1

connect Function with UDP 224

dg-cli Function (Revisited) 227

Lack of Flow Control with UDP 228

Determining Outgoing Interface with UDP 231

TCP and UDP Echo Server Using select 233

Summary 235

Elementary Name and Address Conversions

lntroduction 237

Domain Name System 237

gethostbyname Function 240

RES-USE-INET6 Resolver Option 245

gethostbyname2 Function and IPv6 Support 246

gethostbyaddr Function 248

uname Function 249

gethostname Function 250

getservbyname and getservbyport Functions 251

Other Networking Information 255

Summary 256

Part 3. Advanced Sockets 259

Chapter 10.

Chapter 11.

IPv4 and IPv6 Interoperability 261

lntroduction 261

IPv4 Client, IPv6 Server 262

IPv6 Client, IPv4 Server 265

IPv6 Address Testing Macros 267

IPV6-ADDRFORM Socket Option 268

Source Code Portability 270

Summary 271

Advanced Name and Address Conversions

lntroduction 273

getaddrinf o Function 273

gai-strerror Function 278

f reeaddrinf o Function 279

getaddrinfo Function: IPv6 and Unix Domain 279

getaddrinfo Function: Examples 282

host-serv Function 284

tcp-connec t Function 285

tcp-1istenFunction 288

udp-client Function 293

udp-connect Function 295

udp-server Function 296

getnameinfo Function 298

Reentrant Functions 300

gethostbyname-r and gethostbyaddr-r Functions 303

UNIX Network Programming Contents ix

Chapter 12.

Chapter 13.

Chapter 14.

Chapter 15.

Chapter 16.

Implementation of getaddrinfo and getnameinfo Functions 305

Summary 328

Daemon Processes and inetd Superserver 33 1

lntroduction 331

syslogd Daemon 332

syslog Function 333

daemon-init Function 335

inetd Daemon 339

daemon-inetd Function 344

Summary 346

Advanced I10 Functions

lntroduction 349

Socket Timeouts 349

recv and send Functions 354

readv and writev Functions 357

recvmsg and sendmsg Functions 358

Ancillary Data 362

How Much Data Is Queued? 365

Sockets and Standard 110 366

TITCP: TCP for Transactions 369

Summary 371

Unix Domain Protocols

lntroduction 373

Unix Domain Socket Address Structure 374

socketpair Function 376

Socket Functions 377

Unix Domain Stream Client-Server 378

Unix Domain Datagram Client-Server 379

Passing Descriptors 381

Receiving Sender Credentials 390

Summary 394

Nonblocking I10

lntroduction 397

Nonblocking Reads and Writes: str-cli Function (Revisited)

Nonblocking connect 409

Nonblocking connect: Daytime Client 410

Nonblocking connect: Web Client 41 3

Nonblocking accept 422

Summary 424

ioc t 1 Operations

lntroduction 425

ioctl Function 426

Socket Operations 426

x UNIX Network Programming Contents

16.4

16.5

16.6

16.7

16.8

16.9

16.10

Chapter 17.

17.1

17.2

17.3

17.4

17.5

17.6

17.7

Chapter 18.

18.1

18.2

18.3

18.4

18.5

18.6

Chapter 19.

19.1

19.2

19.3

19.4

19.5

19.6

19.7

19.8

19.9

19.10

19.11

19.12

Chapter 20.

20.1

20.2

20.3

20.4

20.5

20.6

20.7

20.8

20.9

File Operations 427

lnterface Configuration 428

get-if i-inf o Function 429

lnterface Operations 439

ARP Cache Operations 440

Routing Table Operations 442

Summary 443

Routing Sockets

lntroduction 445

Datalink Socket Address Structure 446

Reading and Writing 447

sysctl Operations 454

get-if i-inf o Function 459

lnterface Name and lndex Functions 463

Summary 467

Broadcasting

lntroduction 469

Broadcast Addresses 470

Unicast versus Broadcast 472

dg-cli Function Using Broadcasting 475

Race Conditions 478

Summary 486

Multicasting

lntroduction 487

Multicast Addresses 487

Multicasting versus Broadcasting on A LAN 490

Multicasting on a WAN 493

Multicast Socket Options 495

mcast-j oin and Related Functions 499

dg-cli Function Using Multicasting 502

Receiving MBone Session Announcements 504

Sending and Receiving 507

SNTP: Simple Network Time Protocol 510

SNTP (Continued) 51 5

Summary 528

Advanced UDP Sockets 531

lntroduction 531

Receiving Flags, Destination IP Address, and lnterface lndex 532

Datagram Truncation 539

When to Use UDP Instead Of TCP 539

Adding Reliability to a UDP Application 542

Binding lnterface Addresses 553

Concurrent UDP Servers 557

IPv6 Packet Information 560

Summary 562

UNIX Network Programming Contents xi

Chapter 21.

Chapter 22.

Chapter 23.

Chapter 24.

Chapter 25.

Out-of-Band Data

lntroduction 565

TCP Out-of-Band Data 565

sockatmark Function 572

TCP Out-of-Band Data Summary 580

Client-Server Heartbeat Functions 581

Summary 586

Signal-Driven 110

lntroduction 589

Signal-Driven I10 for Sockets 590

UDP Echo Server Using SIGIO 592

Summary 598

Threads

Introduction 601

Basic Thread Functions: Creation and Termination 602

str-cli Function Using Threads 605

TCP Echo Server Using Threads 607

Thread-Specific Data 61 1

Web Client and Simultaneous Connections (Continued) 620

Mutexes: Mutual Exclusion 622

Condition Variables 627

Web Client and Simultaneous Connections (Continued) 631

Summary 633

IP Options

lntroduction 635

IPv4 Options 635

IPv4 Source Route Options 637

IPv6 Extension Headers 645

IPv6 Hop-by-Hop Options and Destination Options 645

IPv6 Routing Header 649

IPv6 Sticky Options 653

Summary 654

Raw Sockets

lntroduction 655

Raw Socket Creation 656

Raw Socket Output 657

Raw Socket Input 659

Ping Program 661

Traceroute Program 672

An ICMP Message Daemon 685

Summary 702

xii UNIX Network Programming Contents

Chapter 26.

26.1

26.2

26.3

26.4

26.5

26.6

26.7

Chapter 27.

27.1

27.2

27.3

27.4

27.5

27.6

27.7

27.8

27.9

27.10

27.1 1

27.12

27.13

Datalink Access 703

lntroduction 703

BPF: BSD Packet Filter 704

DLPI: Data Link Provider Interface 706

L~~ux: SOCK-PACKET 707

libpcap: Packet Capture Library 707

Examining the UDP Checksum Field 708

Summary 725

Client-Server Design Alternatives

lntroduction 727

TCP Client Alternatives 730

TCP Test Client 730

TCP Iterative Server 732

TCP Concurrent Server, One Child per Client 732

TCP Preforked Server, No Locking around accept 736

TCP Preforked Server, File Locking around accept 742

TCP Preforked Server, Thread Locking around accept 745

TCP Preforked Server, Descriptor Passing 746

TCP Concurrent Server, One Thread per Client 752

TCP Prethreaded Server, per-Thread accept 754

TCP Prethreaded Server, Main Thread accept 756

Summary 759

Part 4. XTI: WOpen Transport Interface 761

Chapter 28.

Chapter 29.

XTI: TCP Clients

lntroduction 763

t-open Function 764

t-error and t-strerror Functions

netbuf Structures and XTI Structures

t-bind Function 770

t-connect Function 772

t-rcv and t-snd Functions 773

t-look Function 774

t-sndrel and t-rcvrel Functions

t-snddis and t-rcvdis Functions

XTI TCP Daytime Client 778

xti-rdwr Function 781

Summary 782

XTI: Name and Address Functions 783

lntroduction 783

/etc/netconf ig File and netconf ig Functions 784

NETPATH Variable and netpath Functions 785

netdir Functions 786

UNIX Network Programming Contents xiii

Chapter 30.

Chapter 31.

Chapter 32.

Chapter 33.

Chapter 34.

t-alloc and t-free Functions 788

t-getprotaddr Functions 790

xti-ntop Function 791

tcp-connect Function 792

Summary 796

XTI: TCP Servers

lntroduction 797

t-lis ten Function 799

tcp-listen Function 800

t-accept Function 802

xti-accept Function 803

Simple Daytime Server 804

Multiple Pending Connections 806

xti-accept Function (Revisited) 808

Summary 81 6

XTI: UDP Clients and Servers

lntroduction 81 9

t-rcvudata and t-sndudata Functions 81 9

udp-client Function 820

t-rcvuderr Function: Asynchronous Errors 824

udp-server Function 826

Reading a Datagram in Pieces 829

Summary 831

XTI Options

lntroduction 833

t-opthdr Structure 835

XTI Options 837

t-optmgrnt Function 840

Checking If an Option Is Supported and Obtaining the Default

Getting and Setting XTI Options 844

Summary 848

Streams

lntroduction 849

Overview 850

getmsg and putmsg Functions 854

getpmsg and putpmsg Functions 855

ioctl Function 855

TPI: Transport Provider Interface 856

Summary 866

XTI: Additional Functions

lntroduction 867

Nonblocking 110 867

t-rcvconnec t Function 868

xiv UNIX Network Programming Contents

Appendix A.

A. 1

A.2

A.3

A.4

A.5

A.6

Appendix B.

Appendix C.

Appendix D.

D. 1

D. 2

D.3

D.4

Appendix E.

t-getinf o Function 869

t-getstate Function 869

t-sync Function 870

t-unbind Function 872

t-rcvv and t-rcwudata Functions 872

t-sndv and t-sndvudata Functions 873

t-rcvreldata and t-sndreldata Functions 874

Signal-Driven 110 874

Out-of-Band Data 875

Loopback Transport Providers 880

Summary 881

IPv4, IPv6, ICMPv4, and ICMPv6

lntroduction 883

IPv4 Header 883

IPv6 Header 885

IPv4 Addresses 887

IPv6 Addresses 892

ICMPv4 and ICMPv6: lnternet Control Message Protocol

Virtual Networks

lntroduction 899

The MBone 899

The 6bone 901

Debugging Techniques

System Call Tracing 903

Standard lnternet Services 908

sock Program 908

Small Test Programs 91 1

tcpdump Program 913

netstatprogram 914

lsof Program 914

Miscellaneous Source Code

unp. h Header 915

con£ ig . h Header 919

unpxti . h Header 920

Standard Error Functions 922

Solutions to Selected Exercises

Bibliography

Index

Tải ngay đi em, còn do dự, trời tối mất!