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