Raw sockets and the type IPConn

This section covers advanced material which most programmers are unlikely to need. it deals with raw sockets, which allow the programmer to build their own IP protocols, or use protocols other than TCP or UDP

TCP and UDP are not the only protocols built above the IP layer. The site lists about 140 of them (this list is often available on Unix systems in the file /etc/protocols). TCP and UDP are only numbers 6 and 17 respectively on this list.

Go allows you to build so-called raw sockets, to enable you to communicate using one of these other protocols, or even to build your own.
But it gives minimal support: it will connect hosts, and write and read packets between the hosts.
In the next chapter we will look at designing and implementing your own protocols above TCP; this section considers the same type of problem, but at the IP layer.

To keep things simple, we shall use almost the simplest possible example: how to send a ping message to a host. Ping uses the “echo” command from the ICMP protocol. This is a byte-oriented protocol, in which the client sends a stream of bytes to another host, and the host replies. the format is:

  • The first byte is 8, standing for the echo message
  • The second byte is zero
  • The third and fourth bytes are a checksum on the entire message
  • The fifth and sixth bytes are an arbitrary identifier
  • The seventh and eight bytes are an arbitrary sequence number
  • The rest of the packet is user data

The following program will prepare an IP connection, send a ping request to a host and get a reply. You may need to have root access in order to run it successfully.

  1. /* Ping
  2. */
  3. package main
  4. import (
  5. "bytes"
  6. "fmt"
  7. "io"
  8. "net"
  9. "os"
  10. )
  11. func main() {
  12. if len(os.Args) != 2 {
  13. fmt.Println("Usage: ", os.Args[0], "host")
  14. os.Exit(1)
  15. }
  16. addr, err := net.ResolveIPAddr("ip", os.Args[1])
  17. if err != nil {
  18. fmt.Println("Resolution error", err.Error())
  19. os.Exit(1)
  20. }
  21. conn, err := net.DialIP("ip4:icmp", addr, addr)
  22. checkError(err)
  23. var msg [512]byte
  24. msg[0] = 8 // echo
  25. msg[1] = 0 // code 0
  26. msg[2] = 0 // checksum, fix later
  27. msg[3] = 0 // checksum, fix later
  28. msg[4] = 0 // identifier[0]
  29. msg[5] = 13 //identifier[1]
  30. msg[6] = 0 // sequence[0]
  31. msg[7] = 37 // sequence[1]
  32. len := 8
  33. check := checkSum(msg[0:len])
  34. msg[2] = byte(check >> 8)
  35. msg[3] = byte(check & 255)
  36. _, err = conn.Write(msg[0:len])
  37. checkError(err)
  38. _, err = conn.Read(msg[0:])
  39. checkError(err)
  40. fmt.Println("Got response")
  41. if msg[5] == 13 {
  42. fmt.Println("identifier matches")
  43. }
  44. if msg[7] == 37 {
  45. fmt.Println("Sequence matches")
  46. }
  47. os.Exit(0)
  48. }
  49. func checkSum(msg []byte) uint16 {
  50. sum := 0
  51. // assume even for now
  52. for n := 1; n < len(msg)-1; n += 2 {
  53. sum += int(msg[n])*256 + int(msg[n+1])
  54. }
  55. sum = (sum >> 16) + (sum & 0xffff)
  56. sum += (sum >> 16)
  57. var answer uint16 = uint16(^sum)
  58. return answer
  59. }
  60. func checkError(err error) {
  61. if err != nil {
  62. fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
  63. os.Exit(1)
  64. }
  65. }

Conclusion

This chapter has considered programming at the IP, TCP and UDP levels.
This is often necessary if you wish to implement your own protocol, or build a client or server for an existing protocol.