9781788624893 Flipbook PDF


18 downloads 105 Views 2MB Size

Recommend Stories


Porque. PDF Created with deskpdf PDF Writer - Trial ::
Porque tu hogar empieza desde adentro. www.avilainteriores.com PDF Created with deskPDF PDF Writer - Trial :: http://www.docudesk.com Avila Interi

EMPRESAS HEADHUNTERS CHILE PDF
Get Instant Access to eBook Empresas Headhunters Chile PDF at Our Huge Library EMPRESAS HEADHUNTERS CHILE PDF ==> Download: EMPRESAS HEADHUNTERS CHIL

Story Transcript

Abhishek Chanda

Network Programming with Rust Build fast and resilient network servers and clients by leveraging Rust's memory-safety and concurrency features

FOR SALE IN INDIA ONLY

Network Programming with Rust

Build fast and resilient network servers and clients by leveraging Rust's memory-safety and concurrency features

Abhishek Chanda

BIRMINGHAM - MUMBAI

Network Programming with Rust Copyright © 2018 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Commissioning Editor: Merint Mathew Acquisition Editor: Chaitanya Nair Content Development Editor: Vikas Tiwari Technical Editor: Diwakar Shukla Copy Editor: Safis Editing Project Coordinator: Ulhas Kambali Proofreader: Safis Editing Indexer: Mariammal Chettiyar Graphics: Tania Dutta Production Coordinator: Shantanu Zagade First published: February 2018 Production reference: 1260218 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78862-489-3

www.packtpub.com

To my wife, Anasua, for being an amazing partner and friend, and also for all the diagrams in this book. To the memory of my mother, Sikha, for her sacrifices and for exemplifying the power of determination.

mapt.io

Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.

Why subscribe? Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Mapt is fully searchable Copy and paste, print, and bookmark content

PacktPub.com Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.

Contributors About the author Abhishek Chanda studied computer science at IIEST Shibpur in India and electrical engineering at Rutgers University. He has lived and worked in multiple countries, working on distributed systems since 2008 at big companies such as Microsoft as well as a number smaller start-ups. Currently, he is working with DataSine in London, where he is responsible for the scalable deployment of infrastructure powering the backend systems of the DataSine platform. He contributes to a number of open source projects, including Rust. I would like to thank Packt and their wonderful team for making this possible, especially Vikas, Chaitanya, Madhunikita, and Diwakar. I am grateful to Pradeep and all the reviewers for their insightful comments. This book would not have been possible without the support of my wife, Anasua.

About the reviewer Pradeep R is a software professional at Gigamon. He is a technology enthusiast passionate about network programing and security, with wide experience in working on leading enterprise network switching and routing solutions and in development and deployment of traditional network security elements. Currently, he is working on next-generation network pervasive visibility solutions. He extensively works with C, C++, Python, JavaScript, Perl, and occasionally with Java, .NET, and Rust. Pradeep has recently reviewed Rust Cookbook. I would like to thank Vigneshwer Dhinakaran, my brother, who has shown me that boundaries exists only in our minds and they are meant to be breached. I would also like to acknowledge with gratitude my mother, Vasanthi, my grandma, Sulochana, and my sister, Deepika, for their constant encouragement and support.

Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.

Table of Contents Preface

1

Chapter 1: Introduction to Client/Server Networking A brief history of networks Layering in networks Addressing in networks How IP routing works How DNS works Common service models

5

Connection-oriented service Connectionless service

6 7 11 13 16 18 18 20

The network programming interface in Linux Summary

20

Chapter 2: Introduction to Rust and its Ecosystem The Rust ecosystem Getting started with Rust Introduction to the borrow checker Generics and the trait system Error handling The macro system

27

Syntactic macros Procedural macros

Functional features in Rust Higher-order functions Iterators

Concurrency primitives Testing Summary Chapter 3: TCP and UDP Using Rust A Simple TCP server and client

26

28 29 33 38 44 48 49 50 51 51 53 55 59 62 63 64

Table of Contents

A Simple UDP server and client

71

UDP multicasting

73

Miscellaneous utilities in std::net Some related crates Summary Chapter 4: Data Serialization, Deserialization, and Parsing Serialization and deserialization using Serde Custom serialization and deserialization

Parsing textual data Parsing binary data Summary

75 77 87 89 90 96 102 109 113

Chapter 5: Application Layer Protocols Introduction to RPC Introduction to SMTP Introduction to FTP and TFTP Summary

115

Chapter 6: Talking HTTP in the Internet Introducing Hyper Introducing Rocket Introducing reqwest Summary

131

Chapter 7: Asynchronous Network Programming Using Tokio Looking into the Future

163

Working with streams and sinks

116 123 126 129

131 139 158 162

164 171

Heading to tokio

178

Socket multiplexing in tokio Writing streaming protocols The larger tokio ecosystem

186 193 203

Conclusion

206

Chapter 8: Security Securing the web

207 207

Letsencrypt using Rust OpenSSL using Rust

210 215

[ ii ]

Table of Contents

Securing tokio applications

220

Cryptography using ring Summary

224 230

Chapter 9: Appendix Introduction to coroutines and generators How May handles coroutines

231 231 235

Awaiting the future Data parallelism Parsing using Pest Miscellaneous utilities Summary

239

Other Books You May Enjoy

259

Index

263

244 249 253 257

[ iii ]

Preface Rust has steadily become one of the most important new programming languages in recent years. Like C or C++, Rust enables the developer to write code that is low-level enough to make Rust code quickly. And since Rust is memory-safe by design, it does not allow code that can crash on a null pointer exception. These properties make it a natural choice for writing low-level networking applications. This book will enable developers to get started with writing networking applications with Rust.

Who this book is for This book's target audience is a software engineer who is interested in writing networking software using Rust.

What this book covers Chapter 1, Introduction to Client/Server Networking, starts the book with a gentle

introduction to computer networking from the ground up. This includes IP addressing, TCP/UDP, and DNS. This forms the basis of our discussions in later chapters. Chapter 2, Introduction to Rust and its Ecosystem, contains an introduction to Rust. This is an

overall introduction that should be good enough to get the reader started. We do assume some familiarity with programming.

Chapter 3, TCP and UDP Using Rust, dives into using Rust for networking. We start with

basic socket programming using the standard library. We then look at some crates from the ecosystem that can be used for network programming. Chapter 4, Data Serialization, Deserialization, and Parsing, explains that an important aspect

of networked computing is handling data. This chapter is an introduction to serializing and deserializing data using Serde. We also look at parsing using nom and other frameworks. Chapter 5, Application Layer Protocols, moves up a layer to look at protocols that operate

above TCP/IP. We look at a few crates to work with, such as RPC, SMTP, FTP, and TFTP.

Preface Chapter 6, Talking HTTP in the Internet, explains that arguably the most common

application of the internet is HTTP. We look at crates such as Hyper and Rocket which are used for writing HTTP servers and clients. Chapter 7, Asynchronous Network Programming Using Tokio, looks at the Tokio stack for

asynchronous programming using futures, streams, and event loops.

Chapter 8, Security, delves into securing the services we have described so far. This is using

certificates and secret keys.

Chapter 9, Appendix, discusses a number of crates have appeared that propose alternate

ways of doing things already covered in this book. This includes the async/await syntax, parsing using Pest, and so on. We will discuss some of these in the appendix.

To get the most out of this book 1. They are either already familiar with Rust or are planning to start learning the language. 2. They have a commercial background in software engineering using other programming languages and are aware about the tradeoffs in developing software using different programming languages. 3. They have a basic familiarity with networking concepts. 4. They can appreciate why distributed systems are important in modern computing.

Download the example code files You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you. You can download the code files by following these steps: 1. 2. 3. 4.

Log in or register at www.packtpub.com. Select the SUPPORT tab. Click on Code Downloads & Errata. Enter the name of the book in the Search box and follow the onscreen instructions.

[2]

Preface

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of: WinRAR/7-Zip for Windows Zipeg/iZip/UnRarX for Mac 7-Zip/PeaZip for Linux The code bundle for the book is also hosted on GitHub at https:/​/​github.​com/ PacktPublishing/​Network-​Programming-​with-​Rust. We also have other code bundles from our rich catalog of books and videos available at https:/​/​github.​com/​PacktPublishing/​. Check them out!

Conventions used There are a number of text conventions used throughout this book. CodeInText: Indicates code words in text, database table names, folder names, filenames,

file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "The target directory contains compilation artifacts." A block of code is set as follows: [package] name = "hello-rust" version = "0.1.0" authors = ["Foo Bar "]

Any command-line input or output is written as follows: # cargo new --bin hello-rust

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "It will not need to call connect for that same connection:" Warnings or important notes appear like this.

[3]

Preface

Tips and tricks appear like this.

Get in touch Feedback from our readers is always welcome. General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected]. Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details. Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material. If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you! For more information about Packt, please visit packtpub.com.

[4]

1

Introduction to Client/Server Networking This book is an introduction to writing networking applications in Rust. This title begs two questions: why should anyone care about networking? And why would anyone want to write networking applications in Rust? We attempt to answer the first question in this chapter. We will introduce Rust and network programming using Rust in subsequent chapters. Firstly, in this chapter, we will start with a bit of history and try to understand how network architecture evolved over the last hundred years. In subsequent sections, we will see how modern networks are layered and addressed. Afterwards, we will describe common service models used in networking. We will end with a summary of networkingrelated programming interfaces that Linux exposes. Note that this book deliberately ignores network programming in other operating systems and focuses only on Linux for the sake of simplicity. While the Rust compiler is platform-agnostic, there can be cases where some things are different in other platforms compared to Linux. We will point out those differences as we progress. In this chapter, we will cover the following topics: History of networking: why and how networks came into use and how the internet evolved Layering in networks: how layering and encapsulation works Addressing: how networks and individual hosts are uniquely identified on the internet How IP routing works How DNS works Service models for data delivery The network programming interface in Linux

Network Programming with Rust Rust is low-level enough to provide fine-grained control over memory while providing safety through compile-time validation. This makes it uniquely suitable for writing low-level networking applications. This book is divided into three main parts that will take you on an exciting journey of building a fully functional web server. The book starts with a solid introduction to Rust and essential networking concepts. This will lay a foundation for, and set the tone of, the entire book. In the second part, we will take an in-depth look at using Rust for networking software. From client-server networking using sockets to IPv4/v6, DNS, TCP, UDP, you will also learn about serializing and deserializing data using serde. The book shows how to communicate with REST servers over HTTP. The final part of the book discusses asynchronous network programming using the Tokio stack. Given the importance of security for modern systems, you will see how Rust supports common primitives such as TLS and public-key cryptography. After reading this book, you will be more than confident enough to use Rust to build effective networking software.

Things you will learn:

• Appreciate why networking is important in implementing distributed systems

• Write a non-asynchronous echo server over TCP that talks to a client over a network

• Parse JSON and binary data using parser combinators such as nom

• Write an HTTP client that talks to the server using reqwest

• Modify an existing Rust HTTTP server and add SSL to it

• Master asynchronous programming support in Rust

• Use external packages in a Rust project

FOR SALE IN INDIA ONLY

www.packtpub.com

Get in touch

Social

© Copyright 2013 - 2024 MYDOKUMENT.COM - All rights reserved.