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