Programming the Network with Perl Concludes with Chapter 6, Mobile Agents, Which Explores an Area of Computer Networking That Is Generating Considerable Research
Total Page:16
File Type:pdf, Size:1020Kb
Paul Barry Institute of Technology, Carlow, Ireland Copyright ©2002 John Wiley & Sons Ltd Baffins Lane, Chichester, West Sussex PO19 1UD, England National 01243 779777 International (+44) 1243 779777 e-mail (for orders and customer service enquiries): [email protected] Visit our Home Page on http://www.wileyeurope.com or http://www.wiley.com All Rights Reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except under the terms of the Copyright, Designs and Patents Act 1988 or under the terms of a licence issued by the Copyright Licensing Agency Ltd, 90 Tottenham Court Road, London, UK W1P 0LP, without the permission in writing of the Publisher with the exception of any material supplied specifically for the purpose of being entered and executed on a computer system for exclusive use by the purchaser of the publication. Neither the author nor John Wiley & Sons, Ltd accept any responsibility or liability for loss or damage occasioned to any person or property through using the material, instructions, methods or ideas contained herein, or acting or refraining from acting as a result of such use. The author and publisher expressly disclaim all implied warranties, including mer- chantability or fitness for any particular purpose. There will be no duty on the author or publisher to correct any errors or defects in the software. Designations used by companies to distinguish their products are often claimed as trade- marks. In all instances where John Wiley & Sons, Ltd is aware of a claim, the product names appear in capital or all capital letters. Readers, however, should contact the appropriate companies for more complete information regarding trademarks and registration Library of Congress Cataloging-in-Publication Data (applied for) British Library Cataloguing in Publication Data A catalogue record for this book is available from the British Library ISBN 0 471 48670 1 Typeset in 9.5/12.5pt Lucida Bright by T&T Productions Ltd, London. Printed and bound in Great Britain by Biddles Ltd, Guildford and Kings Lynn. This book is printed on acid-free paper responsibly manufactured from sustainable forestry in which at least two trees are planted for each one used for paper production. Dedicated to Deirdre, for continuing to put her ambitions on hold while I pursue mine. Contents Preface xiii 1 Meet Perl 1 1.1 Perl’s Default Behaviour 1 1.1.1 Our first Perl program 2 1.1.2 Perl’s default variable 3 1.1.3 The strange first line explained 3 1.2 Using Variables in Perl 4 1.2.1 One of something: scalars 5 1.2.2 A collection of somethings: arrays and lists 6 1.2.3 Hashes 8 1.2.4 References 9 1.2.5 Built-in variables 12 1.2.6 Scoping with local, my and our 12 1.3 Controlling Flow 13 1.3.1 if 13 1.3.2 The ternary conditional operator 14 1.3.3 while 15 1.3.4 for 15 1.3.5 unless 16 1.3.6 until 16 1.3.7 foreach 16 1.3.8 do 17 1.3.9 eval 17 1.3.10 Statement modifiers 18 1.4 Boolean in Perl 19 1.5 Perl Operators 20 1.6 Subroutines 21 1.6.1 Processing parameters 21 1.6.2 Returning results 22 1.6.3 I want an array 23 1.6.4 In-built subroutines 23 1.6.5 References to subroutines 26 1.7 Perl I/O 26 1.7.1 Variable interpolation 28 1.8 Packages, Modules and Objects 29 1.8.1 Modules 30 1.8.2 Objects 30 1.8.3 The joy of CPAN 31 1.9 More Perl 32 1.10 Where To From Here? 32 viii Contents 1.11 Print Resources 32 1.12 Web Resources 33 2 Snooping 35 2.1 Thank You, Tim Potter 36 2.2 Preparing To Snoop 37 2.2.1 Installing NetPacket::* 37 2.2.2 Installing Net::Pcap 38 2.2.3 Installing Net::PcapUtils 39 2.2.4 Online documentation 39 2.2.5 Configuring your network interface 40 2.3 Building Low-Level Snooping Tools 41 2.3.1 loop = open + next 42 2.3.2 Optional parameters: loop and open 43 2.3.3 Optional parameters: the callback function 45 2.3.4 Ethernet Analysis 45 2.3.5 EtherSnooper (v0.01) 48 2.3.6 EtherSnooper (v0.02) 52 2.3.7 EtherSnooper (v0.03) 55 2.3.8 Displaying IP addresses 58 2.4 Snooping IP Datagrams 63 2.4.1 EtherSnooper (v0.05) 64 2.4.2 EtherSnooper (v0.06) 67 2.5 Transport Snoopers 69 2.5.1 Preparing to snoop UDP 70 2.5.2 Preparing to snoop TCP 70 2.5.3 The TCP and UDP gotcha! 71 2.5.4 Application traffic monitoring 75 2.5.5 EtherSnooper (v0.07) 81 2.6 The Network Debugger 83 2.6.1 Processing command-line parameters 85 2.6.2 Storing captured results 85 2.6.3 The NetDebug source code 86 2.7 Where To From Here? 95 2.8 Print Resources 95 2.9 Web Resources 96 3 Sockets 99 3.1 Clients and Servers 99 3.1.1 Client characteristics 100 3.1.2 Server characteristics 101 3.2 Transport Services 101 3.2.1 Unreliable transport 102 3.2.2 Reliable transport 103 3.3 Introducing the Perl Socket API 104 3.4 Socket Support Subroutines 105 3.4.1 inet_aton and inet_ntoa 105 3.4.2 Socket addresses 105 3.4.3 getservbyname and getservbyport 106 3.4.4 getprotobyname and getprotobynumber 106 3.4.5 gethostbyname and gethostbyaddr 107 3.5 Simple UDP Clients and Servers 108 3.5.1 Testing with localhost 108 3.5.2 The first UDP server 108 3.5.3 The first UDP client 111 Contents ix 3.6 Genericity and Robustness 112 3.7 UDP Is Unreliable 116 3.7.1 No flow control 117 3.8 Sending and Receiving with UDP 118 3.9 Dealing with Deadlock 120 3.9.1 Specifying a time-out 121 3.9.2 Checking for data 123 3.9.3 Spawning a subprocess 125 3.10 TCP Clients and Servers 130 3.10.1 The first TCP server 131 3.10.2 The first TCP client 134 3.11 A Common TCP Gotcha 140 3.12 More TCP Socket Communication 143 3.12.1 The remote syntax checker server 144 3.12.2 The remote syntax checker client 147 3.13 The Concurrent Syntax Checker 150 3.14 Object-Oriented Sockets 153 3.14.1 IO::Socket 154 3.14.2 IO::Socket::INET 154 3.14.3 An object-oriented client and server 156 3.15 Where To From Here? 158 3.16 Print Resources Y 158 3.17 Web Resources L 159 4 ProtocolsF 161 4.1 Gotcha! 161 4.1.1 What’s the deal with newline?M 162 4.2 Working with the Web 164 4.2.1 HTTP requests and responsesA 164 4.3 The World’s Worst Web Browser 165 4.3.1 Embedded graphicsE 168 4.3.2 A persistent wwwb 169 4.3.3 A better get_resource T 172 4.4 HTTP Status Codes 174 4.5 It’s the Gisle and Graham Show! 178 4.5.1 Getting libwww-perl and libnet 179 4.6 The Library for WWW Access in Perl 180 4.6.1 The libwww-perl classes 181 4.7 The LWPwwwb Program 181 4.8 Doing More with LWPwwwb 184 4.8.1 Parsing HTML 185 4.8.2 Some parsewwwb examples 187 4.8.3 The HTML::Parser examples 189 4.9 Building a Custom Web Server 190 4.9.1 The custom Web server source code 190 4.9.2 The custom Web server in action 196 4.10 The libnet Library 197 4.10.1 Working with Usenet 198 4.10.2 The news reading source code 199 4.11 Email Enabling simplehttpd 205 4.11.1 The simple mail transfer protocol 205 4.11.2 The Net::SMTP module 210 4.11.3 Creating simplehttp2d 211 Team-Fly® x Contents 4.12 Other Networking Add-On Modules 213 4.12.1 Installing Net::Telnet 213 4.12.2 A Net::Telnet example 214 4.13 Where To From Here? 217 4.14 Print Resources 217 4.15 Web Resources 217 5 Management 221 5.1 Simple Management with ICMP 222 5.2 Doing the Ping Thing 222 5.2.1 Some ping examples 223 5.3 Doing the Net::Ping Thing 225 5.4 Tracing Routes 227 5.4.1 How traceroute works 228 5.5 Not So Simple Management with SNMP 229 5.5.1 A little SNMP history 229 5.6 The SNMP Management Framework 230 5.7 Managed Data 231 5.7.1 The TCP/IP MIB 231 5.8 The SNMP Protocol 235 5.8.1 SNMP’s operational model 235 5.8.2 A brief tour of SNMPv1, SNMPv2 and SNMPv3 235 5.8.3 SNMP communities 237 5.9 The Net::SNMP Module 237 5.9.1 The Net::SNMP methods 238 5.10 Working With Net::SNMP 240 5.10.1 Working with mnemonic object identifiers 242 5.10.2 The udpstats source code 243 5.10.3 The howlongup program 247 5.11 What’s Up? 249 5.11.1 Being more careful 254 5.12 Setting MIB-II Data 256 5.13 IP Router Mapping 258 5.14 Where To From Here? 266 5.15 Print Resources 266 5.16 Web Resources 267 6 Mobile Agents 269 6.1 What is a Mobile Agent? 269 6.1.1 Mobile agent = code + state 270 6.1.2 What is a mobile-agent environment? 270 6.2 Mobile-Agent Examples 270 6.2.1 Revisiting multiwho 270 6.2.2 Revisiting ipdetermine 271 6.3 Mobile-Agent Advantages/Disadvantages 272 6.4 Perl Agents 274 6.4.1 Preparing Perl for mobile agents 274 6.5 The Agent.pm Module 275 6.6 Ooooh, Objects! 276 6.7 The Default Mobile Agent 276 6.8 A Launching Mobile-Agent Environment 280 6.9 A One-Shot Location 282 Contents xi 6.10 Relocating To Multiple Locations 284 6.10.1 Processing multiple mobile agents 285 6.10.2 Identifying multiple locations 285 6.10.3 A multi-location mobile agent 287 6.11 The Mobile-Agent multiwho 292 6.12 The Mobile-Agent ipdetermine 293 6.13 The Cloning Mobile-Agent ipdetermine 297 6.14 Other Perl Agent Examples 304 6.15 Where To From Here? 305 6.16 Print Resources 305 6.17 Web Resources 305 Appendix A.