| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 // | |
| 5 // Some socket related helper methods for quic. | |
| 6 | |
| 7 #ifndef NET_TOOLS_QUIC_QUIC_SOCKET_UTILS_H_ | |
| 8 #define NET_TOOLS_QUIC_QUIC_SOCKET_UTILS_H_ | |
| 9 | |
| 10 #include <netinet/in.h> | |
| 11 #include <stddef.h> | |
| 12 #include <sys/socket.h> | |
| 13 | |
| 14 #include <string> | |
| 15 | |
| 16 #include "base/macros.h" | |
| 17 #include "net/base/ip_address.h" | |
| 18 #include "net/base/ip_endpoint.h" | |
| 19 #include "net/quic/core/quic_bandwidth.h" | |
| 20 #include "net/quic/core/quic_types.h" | |
| 21 | |
| 22 namespace net { | |
| 23 class QuicIpAddress; | |
| 24 class QuicSocketAddress; | |
| 25 | |
| 26 // This is the structure that SO_TIMESTAMPING fills into the cmsg header. It is | |
| 27 // well-defined, but does not have a definition in a public header. See | |
| 28 // https://www.kernel.org/doc/Documentation/networking/timestamping.txt for more | |
| 29 // information. | |
| 30 struct LinuxTimestamping { | |
| 31 // The converted system time of the timestamp. | |
| 32 struct timespec systime; | |
| 33 // Deprecated; serves only as padding. | |
| 34 struct timespec hwtimetrans; | |
| 35 // The raw hardware timestamp. | |
| 36 struct timespec hwtimeraw; | |
| 37 }; | |
| 38 | |
| 39 class QuicSocketUtils { | |
| 40 public: | |
| 41 // The first integer is for overflow. The in6_pktinfo is the larger of the | |
| 42 // address structures present. LinuxTimestamping is present for socket | |
| 43 // timestamping. The subsequent int is for ttl. | |
| 44 // The final int is a sentinel so the msg_controllen feedback | |
| 45 // can be used to detect larger control messages than there is space for. | |
| 46 static const int kSpaceForCmsg = | |
| 47 CMSG_SPACE(CMSG_LEN(sizeof(int)) + CMSG_LEN(sizeof(in6_pktinfo)) + | |
| 48 CMSG_LEN(sizeof(LinuxTimestamping)) + | |
| 49 CMSG_LEN(sizeof(int)) + | |
| 50 CMSG_LEN(sizeof(int))); | |
| 51 | |
| 52 // Fills in |address| if |hdr| contains IP_PKTINFO or IPV6_PKTINFO. Fills in | |
| 53 // |timestamp| if |hdr| contains |SO_TIMESTAMPING|. |address| and |timestamp| | |
| 54 // must not be null. | |
| 55 static void GetAddressAndTimestampFromMsghdr(struct msghdr* hdr, | |
| 56 QuicIpAddress* address, | |
| 57 QuicWallTime* walltimestamp); | |
| 58 | |
| 59 // If the msghdr contains an SO_RXQ_OVFL entry, this will set dropped_packets | |
| 60 // to the correct value and return true. Otherwise it will return false. | |
| 61 static bool GetOverflowFromMsghdr(struct msghdr* hdr, | |
| 62 QuicPacketCount* dropped_packets); | |
| 63 | |
| 64 // If the msghdr contains an IP_TTL entry, this will set ttl to the correct | |
| 65 // value and return true. Otherwise it will return false. | |
| 66 static bool GetTtlFromMsghdr(struct msghdr* hdr, int* ttl); | |
| 67 | |
| 68 // Sets either IP_PKTINFO or IPV6_PKTINFO on the socket, based on | |
| 69 // address_family. Returns the return code from setsockopt. | |
| 70 static int SetGetAddressInfo(int fd, int address_family); | |
| 71 | |
| 72 // Sets SO_TIMESTAMPING on the socket for software receive timestamping. | |
| 73 // Returns the return code from setsockopt. | |
| 74 static int SetGetSoftwareReceiveTimestamp(int fd); | |
| 75 | |
| 76 // Sets the send buffer size to |size| and returns false if it fails. | |
| 77 static bool SetSendBufferSize(int fd, size_t size); | |
| 78 | |
| 79 // Sets the receive buffer size to |size| and returns false if it fails. | |
| 80 static bool SetReceiveBufferSize(int fd, size_t size); | |
| 81 | |
| 82 // Reads buf_len from the socket. If reading is successful, returns bytes | |
| 83 // read and sets peer_address to the peer address. Otherwise returns -1. | |
| 84 // | |
| 85 // If dropped_packets is non-null, it will be set to the number of packets | |
| 86 // dropped on the socket since the socket was created, assuming the kernel | |
| 87 // supports this feature. | |
| 88 // | |
| 89 // If self_address is non-null, it will be set to the address the peer sent | |
| 90 // packets to, assuming a packet was read. | |
| 91 // | |
| 92 // If timestamp is non-null, it will be filled with the timestamp of the | |
| 93 // received packet, assuming a packet was read and the platform supports | |
| 94 // packet receipt timestamping. If the platform does not support packet | |
| 95 // receipt timestamping, timestamp will not be changed. | |
| 96 static int ReadPacket(int fd, | |
| 97 char* buffer, | |
| 98 size_t buf_len, | |
| 99 QuicPacketCount* dropped_packets, | |
| 100 QuicIpAddress* self_address, | |
| 101 QuicWallTime* walltimestamp, | |
| 102 QuicSocketAddress* peer_address); | |
| 103 | |
| 104 // Writes buf_len to the socket. If writing is successful, sets the result's | |
| 105 // status to WRITE_STATUS_OK and sets bytes_written. Otherwise sets the | |
| 106 // result's status to WRITE_STATUS_BLOCKED or WRITE_STATUS_ERROR and sets | |
| 107 // error_code to errno. | |
| 108 static WriteResult WritePacket(int fd, | |
| 109 const char* buffer, | |
| 110 size_t buf_len, | |
| 111 const QuicIpAddress& self_address, | |
| 112 const QuicSocketAddress& peer_address); | |
| 113 | |
| 114 // A helper for WritePacket which fills in the cmsg with the supplied self | |
| 115 // address. | |
| 116 // Returns the length of the packet info structure used. | |
| 117 static size_t SetIpInfoInCmsg(const QuicIpAddress& self_address, | |
| 118 cmsghdr* cmsg); | |
| 119 | |
| 120 // Creates a UDP socket and sets appropriate socket options for QUIC. | |
| 121 // Returns the created FD if successful, -1 otherwise. | |
| 122 // |overflow_supported| is set to true if the socket supports it. | |
| 123 static int CreateUDPSocket(const QuicSocketAddress& address, | |
| 124 bool* overflow_supported); | |
| 125 | |
| 126 private: | |
| 127 DISALLOW_COPY_AND_ASSIGN(QuicSocketUtils); | |
| 128 }; | |
| 129 | |
| 130 } // namespace net | |
| 131 | |
| 132 #endif // NET_TOOLS_QUIC_QUIC_SOCKET_UTILS_H_ | |
| OLD | NEW |