NetUtils.h

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2004-2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
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  * Various overrides of {ntoh,hton}{l,s} that take a char buffer,
00072  * which can be used on systems that require alignment for integer
00073  * operations.
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 } // namespace oasys
00108 
00109 #endif /* _OASYS_NET_UTILS_H_ */

Generated on Thu Jun 7 16:56:51 2007 for DTN Reference Implementation by  doxygen 1.5.1