quic_socket_utils.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
1c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 2c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// found in the LICENSE file. 4c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// 5c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Some socket related helper methods for quic. 6c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 7c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#ifndef NET_TOOLS_QUIC_QUIC_SOCKET_UTILS_H_ 8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#define NET_TOOLS_QUIC_QUIC_SOCKET_UTILS_H_ 9c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include <stddef.h> 11c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include <sys/socket.h> 12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include <string> 13c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "net/base/ip_endpoint.h" 154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "net/quic/quic_protocol.h" 16c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 17c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace net { 18c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace tools { 19c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class QuicSocketUtils { 21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) public: 22c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // If the msghdr contains IP_PKTINFO or IPV6_PKTINFO, this will return the 23c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // IPAddressNumber in that header. Returns an uninitialized IPAddress on 24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // failure. 25c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) static IPAddressNumber GetAddressFromMsghdr(struct msghdr *hdr); 26c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 27c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // If the msghdr contains an SO_RXQ_OVFL entry, this will set dropped_packets 28c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // to the correct value and return true. Otherwise it will return false. 29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) static bool GetOverflowFromMsghdr(struct msghdr *hdr, 30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) uint32 *dropped_packets); 31c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 32c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Sets either IP_PKTINFO or IPV6_PKTINFO on the socket, based on 33c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // address_family. Returns the return code from setsockopt. 34c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) static int SetGetAddressInfo(int fd, int address_family); 35c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 36c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Reads buf_len from the socket. If reading is successful, returns bytes 37c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // read and sets peer_address to the peer address. Otherwise returns -1. 38c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // 39c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // If dropped_packets is non-null, it will be set to the number of packets 40c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // dropped on the socket since the socket was created, assuming the kernel 41c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // supports this feature. 42c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // 43c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // If self_address is non-null, it will be set to the address the peer sent 44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // packets to, assuming a packet was read. 45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) static int ReadPacket(int fd, char* buffer, size_t buf_len, 46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) uint32* dropped_packets, 47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) IPAddressNumber* self_address, 48c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) IPEndPoint* peer_address); 49c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // Writes buf_len to the socket. If writing is successful, sets the result's 514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // status to WRITE_STATUS_OK and sets bytes_written. Otherwise sets the 524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // result's status to WRITE_STATUS_BLOCKED or WRITE_STATUS_ERROR and sets 534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // error_code to errno. 544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) static WriteResult WritePacket(int fd, const char* buffer, size_t buf_len, 554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) const IPAddressNumber& self_address, 564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) const IPEndPoint& peer_address); 57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}; 58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 59c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} // namespace tools 60c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} // namespace net 61c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 62c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#endif // NET_TOOLS_QUIC_QUIC_SOCKET_UTILS_H_ 63