00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _OASYS_NET_UTILS_H_
00019 #define _OASYS_NET_UTILS_H_
00020
00021 #include <string.h>
00022 #include <sys/types.h>
00023 #include <sys/socket.h>
00024 #include <netinet/in.h>
00025 #include <arpa/inet.h>
00026 #include "../compat/inttypes.h"
00027
00033 #define intoa(addr) oasys::Intoa(addr).buf()
00034
00035 namespace oasys {
00036
00040 extern const char* _intoa(u_int32_t addr, char* buf, size_t bufsize);
00041
00046 class Intoa {
00047 public:
00048 Intoa(in_addr_t addr) {
00049 str_ = _intoa(addr, buf_, bufsize_);
00050 }
00051
00052 ~Intoa() {
00053 buf_[0] = '\0';
00054 }
00055
00056 const char* buf() { return str_; }
00057
00058 static const int bufsize_ = sizeof(".xxx.xxx.xxx.xxx");
00059
00060 protected:
00061 char buf_[bufsize_];
00062 const char* str_;
00063 };
00064
00068 extern int gethostbyname(const char* name, in_addr_t* addrp);
00069
00070
00071
00072
00073
00074
00075
00076 inline u_int32_t
00077 safe_ntohl(const char* bp)
00078 {
00079 u_int32_t netval;
00080 memcpy(&netval, bp, sizeof(netval));
00081 return ntohl(netval);
00082 }
00083
00084 inline u_int16_t
00085 safe_ntohs(const char* bp)
00086 {
00087 u_int16_t netval;
00088 memcpy(&netval, bp, sizeof(netval));
00089 return ntohs(netval);
00090 }
00091
00092 inline void
00093 safe_htonl(u_int32_t val, char* bp)
00094 {
00095 val = htonl(val);
00096 memcpy(bp, &val, sizeof(val));
00097 }
00098
00099 inline void
00100 safe_htons(u_int16_t val, char* bp)
00101 {
00102 val = htons(val);
00103 memcpy(bp, &val, sizeof(val));
00104 }
00105
00106
00107 }
00108
00109 #endif