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