0%

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