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