SocketsOps.h revision 129fe122
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 Chen// the inline assembler code makes type blur,
232a18e699SShuo Chen// so we disable warnings for a while.
242a18e699SShuo Chen#pragma GCC diagnostic ignored "-Wconversion"
252a18e699SShuo Cheninline uint64_t hostToNetwork64(uint64_t host64)
262a18e699SShuo Chen{
272a18e699SShuo Chen  return htobe64(host64);
282a18e699SShuo Chen}
292a18e699SShuo Chen
302a18e699SShuo Cheninline uint32_t hostToNetwork32(uint32_t host32)
312a18e699SShuo Chen{
322a18e699SShuo Chen  return htonl(host32);
332a18e699SShuo Chen}
342a18e699SShuo Chen
352a18e699SShuo Cheninline uint16_t hostToNetwork16(uint16_t host16)
362a18e699SShuo Chen{
372a18e699SShuo Chen  return htons(host16);
382a18e699SShuo Chen}
392a18e699SShuo Chen
402a18e699SShuo Cheninline uint64_t networkToHost64(uint64_t net64)
412a18e699SShuo Chen{
422a18e699SShuo Chen  return be64toh(net64);
432a18e699SShuo Chen}
442a18e699SShuo Chen
452a18e699SShuo Cheninline uint32_t networkToHost32(uint32_t net32)
462a18e699SShuo Chen{
472a18e699SShuo Chen  return ntohl(net32);
482a18e699SShuo Chen}
492a18e699SShuo Chen
502a18e699SShuo Cheninline uint16_t networkToHost16(uint16_t net16)
512a18e699SShuo Chen{
522a18e699SShuo Chen  return ntohs(net16);
532a18e699SShuo Chen}
542a18e699SShuo Chen#pragma GCC diagnostic error "-Wconversion"
552a18e699SShuo Chen
562a18e699SShuo Chen///
572a18e699SShuo Chen/// Creates a non-blocking socket file descriptor,
582a18e699SShuo Chen/// abort if any error.
592a18e699SShuo Chenint createNonblockingOrDie();
602a18e699SShuo Chen
612a18e699SShuo Chenvoid bindOrDie(int sockfd, const struct sockaddr_in& addr);
622a18e699SShuo Chenvoid listenOrDie(int sockfd);
632a18e699SShuo Chenint  accept(int sockfd, struct sockaddr_in* addr);
642a18e699SShuo Chenvoid close(int sockfd);
65129fe122SShuo Chenvoid shutdownWrite(int sockfd);
662a18e699SShuo Chen
672a18e699SShuo Chenvoid toHostPort(char* buf, size_t size,
682a18e699SShuo Chen                const struct sockaddr_in& addr);
692a18e699SShuo Chenvoid fromHostPort(const char* ip, uint16_t port,
702a18e699SShuo Chen                  struct sockaddr_in* addr);
712a18e699SShuo Chen
722a18e699SShuo Chenstruct sockaddr_in getLocalAddr(int sockfd);
732a18e699SShuo Chen
742a18e699SShuo Chenint getSocketError(int sockfd);
752a18e699SShuo Chen
762a18e699SShuo Chen}
772a18e699SShuo Chen}
782a18e699SShuo Chen
792a18e699SShuo Chen#endif  // MUDUO_NET_SOCKETSOPS_H
80