1// Copyright 2010, Shuo Chen. All rights reserved. 2// http://code.google.com/p/muduo/ 3// 4// Use of this source code is governed by a BSD-style license 5// that can be found in the License file. 6 7// Author: Shuo Chen (chenshuo at chenshuo dot com) 8// 9// This is an internal header file, you should not include this. 10 11#ifndef MUDUO_NET_SOCKETSOPS_H 12#define MUDUO_NET_SOCKETSOPS_H 13 14#include <arpa/inet.h> 15#include <endian.h> 16 17namespace muduo 18{ 19namespace sockets 20{ 21 22inline uint64_t hostToNetwork64(uint64_t host64) 23{ 24 return htobe64(host64); 25} 26 27inline uint32_t hostToNetwork32(uint32_t host32) 28{ 29 return htonl(host32); 30} 31 32inline uint16_t hostToNetwork16(uint16_t host16) 33{ 34 return htons(host16); 35} 36 37inline uint64_t networkToHost64(uint64_t net64) 38{ 39 return be64toh(net64); 40} 41 42inline uint32_t networkToHost32(uint32_t net32) 43{ 44 return ntohl(net32); 45} 46 47inline uint16_t networkToHost16(uint16_t net16) 48{ 49 return ntohs(net16); 50} 51 52/// 53/// Creates a non-blocking socket file descriptor, 54/// abort if any error. 55int createNonblockingOrDie(); 56 57void bindOrDie(int sockfd, const struct sockaddr_in& addr); 58void listenOrDie(int sockfd); 59int accept(int sockfd, struct sockaddr_in* addr); 60void close(int sockfd); 61 62void toHostPort(char* buf, size_t size, 63 const struct sockaddr_in& addr); 64void fromHostPort(const char* ip, uint16_t port, 65 struct sockaddr_in* addr); 66 67struct sockaddr_in getLocalAddr(int sockfd); 68 69} 70} 71 72#endif // MUDUO_NET_SOCKETSOPS_H 73