Network Programming with Go

Book description

Go combines the best parts of many other programming languages. It’s fast, scalable, and designed for high-performance networking and multiprocessing—in other words, it’s perfect for network programming.

Network Programming with Go is for developers ready to start leveraging Go’s ease of use for writing secure, readable, production-ready network code. Early chapters establish a foundation of networking and traffic-routing know-how upon which the rest of the book builds. You’ll put that knowledge to use as author Adam Woodbeck guides you through writing programs that communicate using TCP, UDP, Unix sockets, and other features that ensure reliable data transmission. As you progress, you’ll explore higher-level network protocols like HTTP and HTTP/2, then build applications that securely interact with servers, clients, and APIs over a network using TLS.

In addition, Woodbeck shows you how to create a simple messaging protocol, develop tools for monitoring network traffic, craft a custom web server, and implement best practices for interacting with cloud providers using their SDKs. Along the way, you’ll learn:

•IP basics for writing effective network programs, such as IPv4 and IPv6 multicasting, ports, and network address translation
•How to use handlers, middleware, and multiplexers to build capable HTTP-based applications with minimal code
•The OSI and TCP/IP models for layered data architectures
•Methods for reading data from/writing data to a network connection, like the type-length-value encoding scheme
•Tools for incorporating authentication and encryption into your applications using TLS, like mutual authentication
•How to serialize data for storage or transmission in Go-friendly formats like JSON, Gob, XML, and protocol buffers
•How to Leverage Go’s code generation support to efficiently communicate with gRPC-based network services

So get ready to take advantage of Go’s built-in concurrency, rapid compiling, and rich standard library. Because when it comes to writing robust network programs, it’s Go time.

Table of contents

  1. Acknowledgments
  2. Introduction
    1. Who This Book Is For
    2. Installing Go
    3. Recommended Development Environments
    4. What’s in This Book
  3. Part I: Network Architecture
    1. Chapter 1: An Overview of Networked Systems
      1. Choosing a Network Topology
      2. Bandwidth vs. Latency
      3. The Open Systems Interconnection Reference Model
        1. The Hierarchal Layers of the OSI Reference Model
        2. Sending Traffic by Using Data Encapsulation
      4. The TCP/IP Model
        1. The Application Layer
        2. The Transport Layer
        3. The Internet Layer
        4. The Link Layer
      5. What You’ve Learned
    2. Chapter 2: Resource Location and Traffic Routing
      1. The Internet Protocol
      2. IPv4 Addressing
        1. Network and Host IDs
        2. Subdividing IPv4 Addresses into Subnets
        3. Ports and Socket Addresses
        4. Network Address Translation
        5. Unicasting, Multicasting, and Broadcasting
        6. Resolving the MAC Address to a Physical Network Connection
      3. IPv6 Addressing
        1. Writing IPv6 Addresses
        2. IPv6 Address Categories
        3. Advantages of IPv6 over IPv4
      4. The Internet Control Message Protocol
      5. Internet Traffic Routing
      6. Routing Protocols
        1. The Border Gateway Protocol
      7. Name and Address Resolution
        1. Domain Name Resource Records
        2. Multicast DNS
        3. Privacy and Security Considerations of DNS Queries
      8. What You’ve Learned
  4. Part II: Socket-level Programming
    1. Chapter 3: Reliable TCP Data Streams
      1. What Makes TCP Reliable?
      2. Working with TCP Sessions
        1. Establishing a Session with the TCP Handshake
        2. Acknowledging Receipt of Packets by Using Their Sequence Numbers
        3. Receive Buffers and Window Sizes
        4. Gracefully Terminating TCP Sessions
        5. Handling Less Graceful Terminations
      3. Establishing a TCP Connection by Using Go’s Standard Library
        1. Binding, Listening for, and Accepting Connections
        2. Establishing a Connection with a Server
        3. Implementing Deadlines
      4. What You’ve Learned
    2. Chapter 4: Sending TCP Data
      1. Using the net.Conn Interface
      2. Sending and Receiving Data
        1. Reading Data into a Fixed Buffer
        2. Delimited Reading by Using a Scanner
        3. Dynamically Allocating the Buffer Size
        4. Handling Errors While Reading and Writing Data
      3. Creating Robust Network Applications by Using the io Package
        1. Proxying Data Between Connections
        2. Monitoring a Network Connection
        3. Pinging a Host in ICMP-Filtered Environments
      4. Exploring Go’s TCPConn Object
        1. Controlling Keepalive Messages
        2. Handling Pending Data on Close
        3. Overriding Default Receive and Send Buffers
      5. Solving Common Go TCP Network Problems
        1. Zero Window Errors
        2. Sockets Stuck in the CLOSE_WAIT State
      6. What You’ve Learned
    3. Chapter 5: Unreliable UDP Communication
      1. Using UDP: Simple and Unreliable
      2. Sending and Receiving UDP Data
        1. Using a UDP Echo Server
        2. Receiving Data from the Echo Server
        3. Every UDP Connection Is a Listener
        4. Using net.Conn in UDP
      3. Avoiding Fragmentation
      4. What You’ve Learned
    4. Chapter 6: Ensuring UDP Reliability
      1. Reliable File Transfers Using TFTP
      2. TFTP Types
        1. Read Requests
        2. Data Packets
        3. Acknowledgments
        4. Handling Errors
      3. The TFTP Server
        1. Writing the Server Code
        2. Handling Read Requests
        3. Starting the Server
      4. Downloading Files over UDP
      5. What You’ve Learned
    5. Chapter 7: Unix Domain Sockets
      1. What Are Unix Domain Sockets?
      2. Binding to Unix Domain Socket Files
        1. Changing a Socket File’s Ownership and Permissions
        2. Understanding Unix Domain Socket Types
      3. Writing a Service That Authenticates Clients
        1. Requesting Peer Credentials
        2. Writing the Service
        3. Testing the Service with Netcat
      4. What You’ve Learned
  5. Part III: Application-level Programming
    1. Chapter 8: Writing HTTP Clients
      1. Understanding the Basics of HTTP
        1. Uniform Resource Locators
        2. Client Resource Requests
        3. Server Responses
        4. From Request to Rendered Page
      2. Retrieving Web Resources in Go
        1. Using Go’s Default HTTP Client
        2. Closing the Response Body
        3. Implementing Time-outs and Cancellations
        4. Disabling Persistent TCP Connections
      3. Posting Data over HTTP
        1. Posting JSON to a Web Server
        2. Posting a Multipart Form with Attached Files
      4. What You’ve Learned
    2. Chapter 9: Building HTTP Services
      1. The Anatomy of a Go HTTP Server
        1. Clients Don’t Respect Your Time
        2. Adding TLS Support
      2. Handlers
        1. Test Your Handlers with httptest
        2. How You Write the Response Matters
        3. Any Type Can Be a Handler
        4. Injecting Dependencies into Handlers
      3. Middleware
        1. Timing Out Slow Clients
        2. Protecting Sensitive Files
      4. Multiplexers
      5. HTTP/2 Server Pushes
        1. Pushing Resources to the Client
        2. Don’t Be Too Pushy
      6. What You’ve Learned
    3. Chapter 10: Caddy: A Contemporary Web Server
      1. What Is Caddy?
        1. Let’s Encrypt Integration
        2. How Does Caddy Fit into the Equation?
      2. Retrieving Caddy
        1. Downloading Caddy
        2. Building Caddy from Source Code
      3. Running and Configuring Caddy
        1. Modifying Caddy’s Configuration in Real Time
        2. Storing the Configuration in a File
      4. Extending Caddy with Modules and Adapters
        1. Writing a Configuration Adapter
        2. Writing a Restrict Prefix Middleware Module
        3. Injecting Your Module into Caddy
      5. Reverse-Proxying Requests to a Backend Web Service
        1. Creating a Simple Backend Web Service
        2. Setting Up Caddy’s Configuration
        3. Adding a Reverse-Proxy to Your Service
        4. Serving Static Files
        5. Checking Your Work
        6. Adding Automatic HTTPS
      6. What You’ve Learned
    4. Chapter 11: Securing Communications with TLS
      1. A Closer Look at Transport Layer Security
        1. Forward Secrecy
        2. In Certificate Authorities We Trust
        3. How to Compromise TLS
      2. Protecting Data in Transit
        1. Client-side TLS
        2. TLS over TCP
        3. Server-side TLS
        4. Certificate Pinning
      3. Mutual TLS Authentication
        1. Generating Certificates for Authentication
        2. Implementing Mutual TLS
      4. What You’ve Learned
  6. Part IV : Service Architecture
    1. Chapter 12: Data Serialization
      1. Serializing Objects
        1. JSON
        2. Gob
        3. Protocol Buffers
      2. Transmitting Serialized Objects
        1. Connecting Services with gRPC
        2. Creating a TLS-Enabled gRPC Server
        3. Creating a gRPC Client to Test the Server
      3. What You’ve Learned
    2. Chapter 13: Logging and Metrics
      1. Event Logging
        1. The log Package
        2. Leveled Log Entries
        3. Structured Logging
        4. Scaling Up with Wide Event Logging
        5. Log Rotation with Lumberjack
      2. Instrumenting Your Code
        1. Setup
        2. Counters
        3. Gauges
        4. Histograms and Summaries
      3. Instrumenting a Basic HTTP Server
      4. What You’ve Learned
    3. Chapter 14: Moving to the Cloud
      1. Laying Some Groundwork
      2. AWS Lambda
        1. Installing the AWS Command Line Interface
        2. Configuring the CLI
        3. Creating a Role
        4. Defining an AWS Lambda Function
        5. Compiling, Packaging, and Deploying Your Function
        6. Testing Your AWS Lambda Function
      3. Google Cloud Functions
        1. Installing the Google Cloud Software Development Kit
        2. Initializing the Google Cloud SDK
        3. Enable Billing and Cloud Functions
        4. Defining a Cloud Function
        5. Deploying Your Cloud Function
        6. Testing Your Google Cloud Function
      4. Azure Functions
        1. Installing the Azure Command Line Interface
        2. Configuring the Azure CLI
        3. Installing Azure Functions Core Tools
        4. Creating a Custom Handler
        5. Defining a Custom Handler
        6. Locally Testing the Custom Handler
        7. Deploying the Custom Handler
        8. Testing the Custom Handler
      5. What You’ve Learned
  7. Index

Product information

  • Title: Network Programming with Go
  • Author(s): Adam Woodbeck
  • Release date: March 2021
  • Publisher(s): No Starch Press
  • ISBN: 9781718500884