SocketsOps.h revision a06076b2
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 Chen// the inline assembler code makes type blur,
23a06076b2SShuo Chen// so we disable warnings for a while.
24a06076b2SShuo Chen#pragma GCC diagnostic ignored "-Wconversion"
25a06076b2SShuo Cheninline uint64_t hostToNetwork64(uint64_t host64)
26a06076b2SShuo Chen{
27a06076b2SShuo Chen  return htobe64(host64);
28a06076b2SShuo Chen}
29a06076b2SShuo Chen
30a06076b2SShuo Cheninline uint32_t hostToNetwork32(uint32_t host32)
31a06076b2SShuo Chen{
32a06076b2SShuo Chen  return htonl(host32);
33a06076b2SShuo Chen}
34a06076b2SShuo Chen
35a06076b2SShuo Cheninline uint16_t hostToNetwork16(uint16_t host16)
36a06076b2SShuo Chen{
37a06076b2SShuo Chen  return htons(host16);
38a06076b2SShuo Chen}
39a06076b2SShuo Chen
40a06076b2SShuo Cheninline uint64_t networkToHost64(uint64_t net64)
41a06076b2SShuo Chen{
42a06076b2SShuo Chen  return be64toh(net64);
43a06076b2SShuo Chen}
44a06076b2SShuo Chen
45a06076b2SShuo Cheninline uint32_t networkToHost32(uint32_t net32)
46a06076b2SShuo Chen{
47a06076b2SShuo Chen  return ntohl(net32);
48a06076b2SShuo Chen}
49a06076b2SShuo Chen
50a06076b2SShuo Cheninline uint16_t networkToHost16(uint16_t net16)
51a06076b2SShuo Chen{
52a06076b2SShuo Chen  return ntohs(net16);
53a06076b2SShuo Chen}
54a06076b2SShuo Chen#pragma GCC diagnostic error "-Wconversion"
55a06076b2SShuo Chen
56a06076b2SShuo Chen///
57a06076b2SShuo Chen/// Creates a non-blocking socket file descriptor,
58a06076b2SShuo Chen/// abort if any error.
59a06076b2SShuo Chenint createNonblockingOrDie();
60a06076b2SShuo Chen
61a06076b2SShuo Chenvoid bindOrDie(int sockfd, const struct sockaddr_in& addr);
62a06076b2SShuo Chenvoid listenOrDie(int sockfd);
63a06076b2SShuo Chenint  accept(int sockfd, struct sockaddr_in* addr);
64a06076b2SShuo Chenvoid close(int sockfd);
65a06076b2SShuo Chen
66a06076b2SShuo Chenvoid toHostPort(char* buf, size_t size,
67a06076b2SShuo Chen                const struct sockaddr_in& addr);
68a06076b2SShuo Chenvoid fromHostPort(const char* ip, uint16_t port,
69a06076b2SShuo Chen                  struct sockaddr_in* addr);
70a06076b2SShuo Chen}
71a06076b2SShuo Chen}
72a06076b2SShuo Chen
73a06076b2SShuo Chen#endif  // MUDUO_NET_SOCKETSOPS_H
74