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