12a18e699SShuo Chen// Copyright 2010, Shuo Chen. All rights reserved. 22a18e699SShuo Chen// http://code.google.com/p/muduo/ 32a18e699SShuo Chen// 42a18e699SShuo Chen// Use of this source code is governed by a BSD-style license 52a18e699SShuo Chen// that can be found in the License file. 62a18e699SShuo Chen 72a18e699SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 82a18e699SShuo Chen// 92a18e699SShuo Chen// This is an internal header file, you should not include this. 102a18e699SShuo Chen 112a18e699SShuo Chen#ifndef MUDUO_NET_SOCKETSOPS_H 122a18e699SShuo Chen#define MUDUO_NET_SOCKETSOPS_H 132a18e699SShuo Chen 142a18e699SShuo Chen#include <arpa/inet.h> 152a18e699SShuo Chen#include <endian.h> 162a18e699SShuo Chen 172a18e699SShuo Chennamespace muduo 182a18e699SShuo Chen{ 192a18e699SShuo Chennamespace sockets 202a18e699SShuo Chen{ 212a18e699SShuo Chen 222a18e699SShuo Cheninline uint64_t hostToNetwork64(uint64_t host64) 232a18e699SShuo Chen{ 242a18e699SShuo Chen return htobe64(host64); 252a18e699SShuo Chen} 262a18e699SShuo Chen 272a18e699SShuo Cheninline uint32_t hostToNetwork32(uint32_t host32) 282a18e699SShuo Chen{ 292a18e699SShuo Chen return htonl(host32); 302a18e699SShuo Chen} 312a18e699SShuo Chen 322a18e699SShuo Cheninline uint16_t hostToNetwork16(uint16_t host16) 332a18e699SShuo Chen{ 342a18e699SShuo Chen return htons(host16); 352a18e699SShuo Chen} 362a18e699SShuo Chen 372a18e699SShuo Cheninline uint64_t networkToHost64(uint64_t net64) 382a18e699SShuo Chen{ 392a18e699SShuo Chen return be64toh(net64); 402a18e699SShuo Chen} 412a18e699SShuo Chen 422a18e699SShuo Cheninline uint32_t networkToHost32(uint32_t net32) 432a18e699SShuo Chen{ 442a18e699SShuo Chen return ntohl(net32); 452a18e699SShuo Chen} 462a18e699SShuo Chen 472a18e699SShuo Cheninline uint16_t networkToHost16(uint16_t net16) 482a18e699SShuo Chen{ 492a18e699SShuo Chen return ntohs(net16); 502a18e699SShuo Chen} 512a18e699SShuo Chen 522a18e699SShuo Chen/// 532a18e699SShuo Chen/// Creates a non-blocking socket file descriptor, 542a18e699SShuo Chen/// abort if any error. 552a18e699SShuo Chenint createNonblockingOrDie(); 562a18e699SShuo Chen 572a18e699SShuo Chenvoid bindOrDie(int sockfd, const struct sockaddr_in& addr); 582a18e699SShuo Chenvoid listenOrDie(int sockfd); 592a18e699SShuo Chenint accept(int sockfd, struct sockaddr_in* addr); 602a18e699SShuo Chenvoid close(int sockfd); 61129fe122SShuo Chenvoid shutdownWrite(int sockfd); 622a18e699SShuo Chen 632a18e699SShuo Chenvoid toHostPort(char* buf, size_t size, 642a18e699SShuo Chen const struct sockaddr_in& addr); 652a18e699SShuo Chenvoid fromHostPort(const char* ip, uint16_t port, 662a18e699SShuo Chen struct sockaddr_in* addr); 672a18e699SShuo Chen 682a18e699SShuo Chenstruct sockaddr_in getLocalAddr(int sockfd); 692a18e699SShuo Chen 702a18e699SShuo Chenint getSocketError(int sockfd); 712a18e699SShuo Chen 722a18e699SShuo Chen} 732a18e699SShuo Chen} 742a18e699SShuo Chen 752a18e699SShuo Chen#endif // MUDUO_NET_SOCKETSOPS_H 76