1. Trang chủ
  2. » Công Nghệ Thông Tin

Linux socket programming by example

655 33 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 655
Dung lượng 3,93 MB

Nội dung

AM FL Y TE Team-Fly® Page i Linux Socket Programming by Example Warren W Gay Page ii Linux Socket Programming by Example Copyright â 2000 by Queđ All rights reserved No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher No patent liability is assumed with respect to the use of the information contained herein Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions Nor is any liability assumed for damages resulting from the use of the information contained herein International Standard Book Number: 0-7897-2241-0 Library of Congress Catalog Card Number: 99-66454 Printed in the United States of America First Printing: April 2000 02 01 00 Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized Que cannot attest to the accuracy of this information Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark Linux® is a registered trademark of Linus Torvalds Red Hat ® LinuxTM is a registered trademark of Red Hat Software Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied The information provided is on an "as is" basis The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book Associate Publisher Tracy Dunkelberger Acquisitions Editor Todd Green Development Editor Laura Bulcher Managing Editor Thomas F Hayes Project Editor Karen S Shields Copy Editor Victoria Elzey Indexer Aamir Burki Proofreader Jeanne Clark Technical Editor William Ray Team Coordinator Cindy Teeters Media Developer Jay Payne Interior Designer Karen Ruggles Cover Designer Rader Design Copywriter Eric Borgert Production Lisa England Steve Geiselman Liz Johnston Page iii CONTENTS AT A GLANCE Introduction Part 1: Basic Socket Concepts Introducing Sockets Domains and Address Families 35 Address Conversion Functions 65 Socket Types and Protocols 93 Binding Addresses to a Socket 115 Connectionless-Oriented Protocols 133 Connection-Oriented Protocols for Clients 159 Connection-Oriented Protocols for Servers 183 Hostname and Network Name Lookups 203 Part 2: Advanced Socket Programming 227 10 Using Standard I/O on Sockets 229 11 Concurrent Client Servers 269 12 Socket Options 305 13 329 Broadcasting with UDP 14 Out-of-Band Data 349 15 Using the inetd Daemon 379 16 Network Security Programming 393 17 Passing Credentials and File Descriptors 427 18 A Practical Network Project 473 Appendixes 505 A Socket Function Quick Reference 507 B Socket-Related Structures Reference 519 C Useful Network Tables 525 Glossary 529 Index 537 Page iv TABLE OF CONTENTS Introduction Part Basic Socket Concepts Introducing Sockets A Brief Historical Introduction Understanding Sockets Defining a Socket Using Sockets 10 Referencing Sockets 11 Comparing Sockets to Pipes 12 Creating Sockets 13 Using socketpair(2) in an Example 14 Running the Demonstration Program 16 Performing I/O on Sockets 17 Closing Sockets 21 The shutdown(2) Function 22 Shutting Down Writing to a Socket 22 Dealing with Duplicated Sockets 23 Shutting Down Reading from a Socket 24 Knowing When Not to Use shutdown(2) 24 Writing a Client/Server Example 24 Domains and Address Families Nameless Sockets 35 36 Anonymous Calls 36 Generating Addresses 36 Understanding Domains 36 Forming Socket Addresses 37 Examining the Generic Socket Address Forming Local Addresses 37 38 Forming Traditional Local Addresses 39 Forming Abstract Local Addresses 44 Forming Internet (IPv4) Socket Addresses 47 Understanding Network Byte Order 49 Performing Endian Conversions 50 Page v Initializing a Wild Internet Address 51 Initializing a Specific Internet Address 52 Specifying an X.25 Address 55 Specifying Other Address Families 58 The AF_UNSPEC Address Family 61 Address Conversion Functions Internet IP Numbers 65 66 Internet Address Classes 66 Understanding Netmask Values 67 Allocating IP Addresses 72 Private IP Numbers 72 Reserved IP Numbers 73 Manipulating IP Numbers 73 Using the inet_addr(3) Function 73 The inet_aton(3) Function 77 Using the inet_ntoa(3) Function 80 Using inet_network(3) 83 Using the inet_lnaof(3) Function 85 Using the inet_netof(3) Function 86 Using the inet_makeaddr(3) Function 87 Socket Types and Protocols Specifying the Domain of a Socket 93 94 Choosing PF_INET or AF_INET 94 Using the PF_LOCAL and AF_LOCAL Macros 95 Using the socket(2) Function 96 Choosing a Socket Type 96 Understanding the SOCK_STREAM Socket Type 97 Understanding the SOCK_DGRAM Socket Type 99 Understanding the SOCK_SEQPACKET Socket Type Choosing a Protocol 100 101 Using PF_LOCAL and SOCK_STREAM 102 Using PF_LOCAL and SOCK_DGRAM 103 Using PF_INET and SOCK_STREAM 103 Using PF_INET and SOCK_DGRAM 105 Socket Domain and Type Summary 106 Other Linux-Supported Protocols 107 Researching Other Protocols 109 SIGURG signal, 355 receiving out-of-band data, 359 oobrecv.c, 359-362 sock_addr( ) function, 124 writing, 121 SOCK_DGRAM socket, 99-100 connect(2) function, 180 PF_APPLETALK, 109 PF_AX25, 109 PF_ECONET, 109 PF_INET, 105-108 PF_INET6, 108 AM FL Y PF_IPX, 109 PF_LOCAL, 103, 108 properties, 99 PF_AX25, 108 PF_INET6, 108 TE SOCK_SEQPACKET socket, 100-101 PF_IPX, 109 SOCK_STREAM socket, 97-98 PF_APPLETALK, 109 PF_INET, 103-104, 108 PF_INET6, 108 PF_IPX, 109 PF_LOCAL, 102, 108 properties, 98 sockaddr address structure, 519 sockaddr_atalk address structure, 520 sockaddr_in address structure, 519 Team-Fly® sockaddr_in6 address structure, 520 sockaddr_un address structure, 39 sockaddr_x25 address structures, 520 socket function, 507 Socket Type field (etc/inetd.conf files), 382 socket(2) function, 96 arguments, 96 list of parameters, 108 parameter relationships, 107 socketpair function, 13-15, 507 example, 14-16 writing client/server, 26-32 sockets, 7-10 address structures, 519 addresses binding to, 115-119 examining, 37 forming, 37-44 obtaining, 120-127 addressing, 508 anonymous, 36 applying FILE streams, 243-249, 259 choosing, 96-100 closing, 21-22 comparing to pipes, 12 connecting, 173-174 controlling, 512 creating, 13-17 Page 555 defining, 9-10 domains, 106 specifying, 94-95 duplicating, 23, 234 establishing server/client connections, 380 functions, 507 history, I/O example, 18-21 performing, 17-21 listen(2) function, 190 nameless, 36 options retrieving, 306-310 setting, 310-315 SO_BROADCAST, 324-325 SO_KEEPALIVE, 323-324 SO_LINGER, 320-322 SO_OOBINLINE, 325-326 SO_PASSCRED, 326 SO_PEERCRED, 326 SO_REUSEADDR, 319-320 SO_TYPE, 315-317 reading, 508 shutting down, 24 referencing, 11-12 SOCK_DGRAM, 99-100 connect(2) function, 180 SOCK_SEQPACKET, 100-101 SOCK_STREAM, 97-98 stream, creating, 104 streams associating, 230-232 closing, 232 TCP, name server lookups, 224 types, 93, 106, 112 finding for protocol families, 112 writing, shutting down, 22 writing to, 510 source codes, Web site, 60 SO_BROADCAST socket option code example, 324 setting, 324-325 SO_KEEPALIVE socket option code example, 323 probe messages, 323-324 setting, 323-324 SO_LINGER socket option cautions, 321 code examples, 322 setting, 320-322 SO_OOBINLINE socket option code example, 325 setting, 325-326 SO_PASSCRED socket option, setting, 326 SO_PEERCRED socket option, setting, 326 SO_REUSEADDR socket option, setting, 319-320 SO_TYPE (socket type), retrieving, 315-317 specific addresses, establishing, 53-55 specifying addresses, families, 58-60 backlog argument value, 187-188 domains, 94-95 interfaces, with bind(2) function, 131 X.25 addresses, 55 starting broadcasts, networks, 343-344 datagram server, 151 inetd, 380-381 TCP wrappers, 419-420 stksrv server program, 343 stdio(3) facility, 230 stdio(3) stream, FILE control block, 230 stksrv server program, starting, 343 stksrv.c server program, 332 stock market index broadcasting server, 332-342 stock market quotes broadcast( ) function, 493-494 get_tickinfo( ) function, 484-492 mktwatch program, 495 obtaining, 474-483, 500-503 qserve quotation server, 501 server and clients, 477 Page 556 stream I/O, examples, 97-98 stream sockets, creating, 104 streams closing, 234, 243-249, 259 I/O buffer functions, 241-242 read, 233 creating, 233-234 shutting down, 237 sockets associating, 230-232 closing, 232 write, 233 creating, 233-234 shutting down, 236-238 strerror(3) function, 211 struct protoent program structure, 169 struct servent function, structure, 163 struct utsname, 205 structures ancillary data, 435-436 addresses, 519 cmsqhdr, 523 hostent, 213-214, 522 I/O, 522 linger, 522 msqhdr, 523 protent, 521 servent, 521 sockaddr_un, 39 struct servent, 163 struct cmsghdr, 435-436 struct msghdr, 433-435 timeval, 284, 522 utsname, 522 utsname members, 205 X.25 addresses, 55 support hostname, 515 I/O, 515 supporting protocol families, 110-111 syntax getsockopt(2) function, 306 setsockopt(2) function, 310 T tables amateur radio reserved IP numbers, 525 Internet address classes, 525 Netmask Values by IP class, 525 private IP number allocations, 525 TCP sockets, name server lookups, 224 TCP wrappers, 399 datagram client code analysis, 418 datagram client code example, 414-417 datagram clients, 402-403 denying requests, 421 getpeername(2) function, 402 getsockname(2) function, 402 inetd, starting, 419-420 inetd datagram server code example, 405-408 inetd datagram server code process analysis, 408-409 inetd datagram server timeout code analysis, 409-410 installing, 418-419 log files, 419-422 log.c logging functions code example, 403-405 log.h header files code example, 405 process, 399-401 TCP wrapper program accepting requests, 413 code analysis, 412-414 code example, 410-412 error recovery, 414 rejecting requests, 413 testing, 420-421 installation testing, 419 server timeouts, 421-422 uninstalling, 422 verifying, 399 vulnerabilities, 423 TCP/IP, 36-37 advantages, 161 client programs daytime, 176 writing, 173-180 duplicated packets, 161 flow control, 161 Internet, 162 etc/services file, 162 lost packets, 160 sequencing packets, 161 servers, writing, 190-197 telnet program, 174 tcpd See TCP wrappers Page 557 tcpdump command, 346 tdurg=1 (urgent mode), 369 telnet command, server connections, 317-319 telnet program, TCP/IP testing, 174 termination processing, 282 testing file descriptor sets (FD ISSET macro), 286 for EINTR error, 240 getdomainname(2) function, 208-209 gethostname(2) function, 208-209 inetd servers, 385-389 IP numbers, 153 RPN server functions, 272-273 servers, select(2) function based, 299 TCP wrappers, 420-421 installation testing, 419 server timeouts, 421-422 TCP/IP, telnet program, 174 UDP datagram client, 150-155 UDP datagram server, 150-155 timeouts inetd datagram server timeout code analysis, 409-410 servers, TCP wrappers, 421-422 timeval structure, 284, 522 definition, 284 troubleshooting broadcasts, 346 type domain, 13 U UDP, 135-136 broadcasting, 329 datagram client testing, 150-155 writing, 146-150 datagram server starting, 151 testing, 150-155 writing, 140-145 UDP servers, inetd, 389-390 UDP sockets, creating, 105 uninstalling TCP wrappers, 422 AM FL Y urgent mode (TCP), 366-368 tcp stdurg=1, 369 urgent pointers, 366-369 determining, 370-371 limitations, 375-377 TE examples, 367-36 UserID field (etc/inetd.conf files), 383 users identifying by host / domain name, 396-397 by IP numbers, 397-398 network addresses, 396-398 V values backlog argument, 187-188 flags recvfrom(2) function, 139 sendto(2) function, 137 Team-Fly® inet_lnaof(3) function, 86 inet_netof(3) function examples, 87 netmasks, 67-69 verifying TCP wrappers, 399 W wait flag word, inetd datagram servers, 390-391 wait(2) function, 282 waitpid(2) function, 282 Web sites, source codes, 60 web80 Web server, example, 447-451 wild addresses, replying to, 154-155 wild Internet addresses, creating, 51-52 wild servers examples, 197 running, 196-197 wrappers, TCP, 399 datagram client code analysis, 418 datagram client code example, 414-417 Page 558 datagram clients, 402-403 denying requests, 421 getpeername(2) function, 402 getsockname(2) function, 402 inetd, starting, 419-420 inetd datagram server code example, 405-408 inetd datagram server code process analysis, 408-409 inetd datagram server timeout code analysis, 409-410 installing, 418-419 log files, 419-422 log.c logging functions code example, 403-405 log.h header files code example, 405 process, 399-401 TCP wrapper program accepting requests, 413 code analysis, 412-414 code example, 410-412 error recovery, 414 rejecting requests, 413 testing, 420-421 installation testing, 419 server timeouts, 421-422 uninstalling, 422 verifying, 399 vulnerabilities, 423 write function, 18 write streams, 233 creating, 233-234 shutting down, 236-238 writev function, 510 writing client/server, example, 24-32 shutting down, 22 sock_addr( ) function, 121 TCP/IP client programs, 173-176, 180 TCP/IP server, 190-197 to sockets, 510 UDP datagram client, 146-150 UDP datagram server, 140-145 X X Window, run-levels, 380 X.25 address, 55 establishing, 56-58 Page 559 ...Page i Linux Socket Programming by Example Warren W Gay Page ii Linux Socket Programming by Example Copyright © 2000 by Que® All rights reserved No part of this... Basic Socket Concepts Introducing Sockets A Brief Historical Introduction Understanding Sockets Defining a Socket Using Sockets 10 Referencing Sockets 11 Comparing Sockets to Pipes 12 Creating Sockets... you've learned The by Example Series How does the by Example series make you a better programmer? The by Example series teaches programming using the best method possible— examples The text acts

Ngày đăng: 04/03/2019, 14:14

TỪ KHÓA LIÊN QUAN