O'reilly Asterisk the Future of Telephony (2Nd Edition).Pdf
Total Page:16
File Type:pdf, Size:1020Kb
SECOND EDITION Asterisk™: The Future of Telephony Jim Van Meggelen, Leif Madsen, and Jared Smith Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo Asterisk™: The Future of Telephony, Second Edition by Jim Van Meggelen, Leif Madsen, and Jared Smith Copyright © 2007, 2005 O’Reilly Media, Inc. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472 O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safari.oreilly.com). For more information, contact our corporate/ institutional sales department: (800) 998-9938 or [email protected]. Editor: Mike Loukides Indexer: Joe Wizda Copy Editor: Sanders Kleinfeld Cover Designer: Karen Montgomery Production Editor: Laurel R.T. Ruma Interior Designer: David Futato Proofreader: Tolman Creek Design Illustrators: Robert Romano and Jessamyn Read Printing History: June 2005: First Edition. August 2007: Second Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Asterisk™: The Future of Telephony, the image of starfish, and related trade dress are trademarks of O’Reilly Media, Inc. Asterisk™ is a trademark of Digium, Inc. Asterisk: The Future of Telephony is published under the Creative Commons “Commons Deed” license (http://creativecommons.org/licenses/by-nc-nd/2.5/ca/). While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tained herein. TM This book uses RepKover™, a durable and flexible lay-flat binding. ISBN-10: 0-596-51048-9 ISBN-13: 978-0-596-51048-0 [M] This book is dedicated to Rich Adamson (1947–2006). Thanks for showing us the meaning of community. Table of Contents Foreword . ...................................................... xi Preface ........................................................ xv 1. A Telephony Revolution .................................... 1 VoIP: Bridging the Gap Between Traditional and Network Telephony 2 Massive Change Requires Flexible Technology 3 Asterisk: The Hacker’s PBX 5 Asterisk: The Professional’s PBX 5 The Asterisk Community 6 The Business Case 8 This Book 9 2. Preparing a System for Asterisk ........................... 11 Server Hardware Selection 13 Environment 21 Telephony Hardware 25 Types of Phones 29 Linux Considerations 34 Conclusion 34 3. Installing Asterisk ......................................... 37 What Packages Do I Need? 38 Obtaining the Source Code 40 Menuselect 42 Compiling Zaptel 43 Compiling libpri 47 Compiling Asterisk 48 Installing Additional Prompts 50 Common Compiling Issues 51 Loading Asterisk and Zaptel Quickly 54 Loading Zaptel Modules Without Scripts 55 vii Loading libpri Without Script 57 Starting Asterisk Without Scripts 57 Directories Used by Asterisk 59 AsteriskNOW™ 62 Conclusion 67 4. Initial Configuration of Asterisk ............................ 69 What Do I Really Need? 70 Working with Interface Configuration Files 71 Setting Up the Dialplan for Some Test Calls 73 FXO and FXS Channels 73 Configuring an FXO Channel for a PSTN Connection 75 Configuring an FXS Channel for an Analog Telephone 79 Configuring SIP Telephones 81 Connecting to a SIP Service Provider 97 Connecting Two Asterisk Boxes Together via SIP 101 Configuring an IAX Softphone 106 Connecting to an IAX Service Provider 110 Connecting Two Asterisk Boxes Together via IAX 111 Using Templates in Your Configuration Files 115 Debugging 116 Conclusion 117 5. Dialplan Basics .......................................... 119 Dialplan Syntax 119 A Simple Dialplan 124 Building an Interactive Dialplan 127 Conclusion 144 6. More Dialplan Concepts ................................. 145 Expressions and Variable Manipulation 145 Dialplan Functions 148 Conditional Branching 149 Voicemail 153 Macros 157 Using the Asterisk Database (AstDB) 160 Handy Asterisk Features 163 Conclusion 165 7. Understanding Telephony ................................ 167 Analog Telephony 167 Digital Telephony 170 The Digital Circuit-Switched Telephone Network 180 viii | Table of Contents Packet-Switched Networks 184 Conclusion 184 8. Protocols for VoIP ....................................... 185 The Need for VoIP Protocols 186 VoIP Protocols 187 Codecs 193 Quality of Service 197 Echo 200 Asterisk and VoIP 202 VoIP Security 204 Conclusion 206 9. The Asterisk Gateway Interface (AGI) .................... 207 Fundamentals of AGI Communication 207 Writing AGI Scripts in Perl 210 Creating AGI Scripts in PHP 214 Writing AGI Scripts in Python 219 Debugging in AGI 223 Conclusion 225 10. Asterisk Manager Interface (AMI) and Adhearsion ........ 227 The Manager Interface 227 The Flash Operator Panel 231 Asterisk Development with Adhearsion 231 11. The Asterisk GUI Framework ............................ 245 Why a GUI for Asterisk? 245 What Is the GUI? 246 Architecture of the Asterisk GUI 248 Installing the Asterisk GUI 249 Developing for the Asterisk GUI 251 12. Relational Database Integration .......................... 263 Introduction 263 Installing the Database 263 Installing and Configuring ODBC 265 Using Realtime 268 Storing Call Detail Records 272 Getting Funky with func_odbc: Hot-Desking 274 ODBC Voicemail 286 Conclusion 291 Table of Contents | ix 13. Managing Your Asterisk System . ........................ 293 Call Detail Recording 293 Managing Logs 295 Running Asterisk As a Non-root User 295 Customizing System Prompts 298 Music on Hold 299 Conclusion 302 14. Potpourri ................................................ 303 Festival 303 Call Files 306 DUNDi 307 Alternative Voicemail Storage Methods 312 Asterisk and Jabber (XMPP) 315 Conclusion 316 15. Asterisk: The Future of Telephony ........................ 317 The Problems with Traditional Telephony 317 Paradigm Shift 320 The Promise of Open Source Telephony 320 The Future of Asterisk 327 A. VoIP Channels .......................................... 337 B. Application Reference ................................... 367 C. AGI Reference ........................................... 449 D. Configuration Files ....................................... 461 E. Asterisk Dialplan Functions .............................. 493 F. Asterisk Manager Interface Actions ....................... 515 G. An Example of func_odbc ................................ 551 Index ........................................................ 557 x | Table of Contents Foreword Once upon a time, there was a boy ...with a computer ...and a phone. This simple beginning begat much trouble! It wasn’t that long ago that telecommunications, both voice and data, as well as soft- ware, were all proprietary products and services, controlled by one select club of companies that created the technologies, and another select club of companies who used the products to provide services. By the late 1990s, data telecommunications had been opened by the expansion of the Internet. Prices plummeted. New and innovative technologies, services, and companies emerged. Meanwhile, the work of free software pioneers like Richard Stallman, Linus Torvalds, and countless others was culminating in the creation of a truly open software platform called Linux (or GNU/Linux). How- ever, voice communications, ubiquitous as they were, remained proprietary. Why? Perhaps it was because voice on the old public telephone network lacked the glamor and promise of the shiny new World Wide Web. Or, perhaps it was because a telephone just wasn’t as effective at supplying adult entertainment. Whatever the reason, one thing was clear. Open source voice communications was about as widespread as open source copy protection software. Necessity (and in some cases simply being cheap) is truly the mother of invention. In 1999, having started Linux Support Services to offer free and commercial technical support for Linux, I found myself in need (or at least in perceived need) of a phone system to assist me in providing 24-hour technical support. The idea was that people would be able to call in, enter their customer identity, and leave a message. The system would in turn page a technician to respond to the customer’s request in short order. Since I had started the company with about $4,000 of capital, I was in no position to be able to afford a phone system of the sort that I needed to implement this scenario. Having already been a Linux user since 1994, and having already gotten my feet wet in open source software development by starting l2tpd, Gaim, and cheops, and in the complete absence of anyone having explained the complexity of such a task, I decided that I would simply make my own phone system using hardware borrowed from xi Adtran, where I had worked as a co-op student. Once I got a call into a PC, I fantasized, I could do anything with it. In fact, it is from this conjecture that the official Asterisk motto (which any sizable, effective project must have) is derived: It’s only software! For better or worse, I rarely think small. Right from the start, it was my intent that Asterisk would do everything related to telephony. The name “Asterisk” was chosen because it was both a key on a standard telephone and also the wildcard symbol in Linux (e.g., rm -rf *). So, in 1999, I had a free telephony platform I’d put out on the Web and I went about my business trying to eke out a living at providing Linux technical