debug in proxy and some net interface

来源:互联网 发布:上海好的java培训机构 编辑:程序博客网 时间:2024/05/01 09:59

gethostbyname和struct hostent详解

网络编程gethostbynamehostent 

struct hostent *gethostbyname(const char *name);

gethostbyname函数根据域名解析出服务器的ip地址,它返回一个结构体struct hostent:

#include <netdb.h>struct hostent {char  *h_name;            /* official name of host */char **h_aliases;         /* alias list */int    h_addrtype;        /* host address type */int    h_length;          /* length of address */char **h_addr_list;       /* list of addresses */}#define h_addr h_addr_list[0]  /* for backward compatibility */


包含头文件#include<netdb.h>函数原型int getaddrinfo( const char *hostname, const char *service, const struct addrinfo *hints, struct addrinfo **result );参数说明hostname:一个主机名或者地址串(IPv4的点分十进制串或者IPv6的16进制串)service:服务名可以是十进制的端口号,也可以是已定义的服务名称,如ftp、http等hints:可以是一个空指针,也可以是一个指向某个<a target=_blank href="http://www.cnblogs.com/cxz2009/archive/2010/11/19/1881661.html">addrinfo结构体</a>的指针,调用者在这个结构中填入关于期望返回的信息类型的暗示。举例来说:如果指定的服务既支持TCP也支持UDP,那么调用者可以把hints结构中的ai_socktype成员设置成SOCK_DGRAM使得返回的仅仅是适用于数据报套接口的信息。result:本函数通过result指针参数返回一个指向addrinfo结构体链表的指针。返回值:0——成功,非0——出错<a target=_blank href="http://blog.chinaunix.net/uid-23577393-id-2733234.html">C语言符号优先级</a>            <em>2011-09-06 15:34:25</em>THE FOLLOWING BOTH ARE RIGHTinet_ntoa (*(struct in_addr *)*localHost->h_addr_list);return *(struct in_addr *) ent->h_addr; 

socket编程之addrinfo结构体与getaddrinfo函数2012-04-09 19:39:36

分类: 嵌入式

addrinfo结构体的定义如下:

  1. struct addrinfo {
  2.      int ai_flags;/* customize behavior*/
  3.      int ai_family;/* address family*/
  4.      int ai_socktype;/* socket type*/
  5.      int ai_protocol;/* protocol*/
  6.      socklen_t ai_addrlen; /* length in bytes of address*/
  7.      struct sockaddr *ai_addr;/* address*/
  8.      char *ai_canonname;/* canonical name of host*/
  9.      struct addrinfo *ai_next;/*nextin list*/    .
  10.    };


google sockaddr_storage the following 3 are incorrect
http://msdn.microsoft.com/en-us/library/windows/desktop/ms740504%28v=vs.85%29.aspx
http://pubs.opengroup.org/onlinepubs/009695399/basedefs/sys/socket.h.html
http://www.ietf.org/mail-archive/web/ipv6/current/msg06896.html

below is correct
通用结构体2: struct sockaddr_storage,128个字节
http://www.tuicool.com/articles/jMfMRr is correct
1 /* Structure large enough to hold any socket address  2 (with the historical exception of AF_UNIX). 128 bytes reserved.  */ 3  4 #if ULONG_MAX > 0xffffffff 5 # define __ss_aligntype __uint64_t 6 #else 7 # define __ss_aligntype __uint32_t 8 #endif 9 #define _SS_SIZE        12810 #define _SS_PADSIZE     (_SS_SIZE - (2 * sizeof (__ss_aligntype)))11 12 struct sockaddr_storage13 {14     sa_family_t ss_family;      /* Address family */15     __ss_aligntype __ss_align;  /* Force desired alignment.  */16     char __ss_padding[_SS_PADSIZE];17 };



struct sockaddr_storage

  • To: libc-hacker@cygnus.com
  • Subject: struct sockaddr_storage
  • From: Philip Blundell <philb@gnu.org>
  • Date: Mon, 10 May 1999 22:47:19 +0100

Here's a patch to add a definition for `struct sockaddr_storage', which was introduced by the IPv6 API.  The intention is that it provides a block of memory with sufficient size and alignment that you can cast it to any `struct sockaddr_xxx *' and get sensible behaviour.Please check that it doesn't cause any breakage, especially on Hurd and MIPS.p.1999-05-10  Philip Blundell  <philb@gnu.org>* sysdeps/generic/bits/socket.h (struct sockaddr_storage): New structure; storage suitable for any socket address.* sysdeps/unix/sysv/linux/bits/socket.h: Likewise.* sysdeps/unix/sysv/linux/mips/bits/socket.h: Likewise.--- libc/sysdeps/generic/bits/socket.hFri Apr 24 21:52:30 1998+++ libc/sysdeps/generic/bits/socket.hMon May 10 20:03:52 1999@@ -127,6 +127,25 @@   };  +/* Structure large enough to hold any socket address (with the historical+   exception of AF_UNIX).  We reserve 128 bytes.  */+#include <sys/types.h>+#if (~0UL) > 0xffffffff+#define __ss_alignuint64_t+#else+#define __ss_alignuint32_t+#endif+#define _SS_SIZE128+#define _SS_PADSIZE(_SS_SIZE - (2 * sizeof(__ss_align)))++struct sockaddr_storage +  {+    __SOCKADDR_COMMON (__ss_);/* Address family, etc.  */+    __ss_align __ss_align;/* Force desired alignment.  */+    char __ss_padding[_SS_PADSIZE];+  };++ /* Bits in the FLAGS argument to `send', `recv', et al.  */ enum   {--- libc/sysdeps/unix/sysv/linux/bits/socket.hMon May 10 21:26:03 1999+++ libc/sysdeps/unix/sysv/linux/bits/socket.hMon May 10 20:31:54 1999@@ -139,6 +139,25 @@   };  +/* Structure large enough to hold any socket address (with the historical+   exception of AF_UNIX).  We reserve 128 bytes.  */+#include <sys/types.h>+#if (~0UL) > 0xffffffff+#define __ss_aligntype__uint64_t+#else+#define __ss_aligntype__uint32_t+#endif+#define _SS_SIZE128+#define _SS_PADSIZE(_SS_SIZE - (2 * sizeof(__ss_aligntype)))++struct sockaddr_storage +  {+    __SOCKADDR_COMMON (__ss_);/* Address family, etc.  */+    __ss_aligntype __ss_align;/* Force desired alignment.  */+    char __ss_padding[_SS_PADSIZE];+  };++ /* Bits in the FLAGS argument to `send', `recv', et al.  */ enum   {--- libc/sysdeps/unix/sysv/linux/mips/bits/socket.hThu Dec 31 12:44:46 1998+++ libc/sysdeps/unix/sysv/linux/mips/bits/socket.hMon May 10 20:04:06 1999@@ -133,6 +133,25 @@   };  +/* Structure large enough to hold any socket address (with the historical+   exception of AF_UNIX).  We reserve 128 bytes.  */+#include <sys/types.h>+#if (~0UL) > 0xffffffff+#define __ss_alignuint64_t+#else+#define __ss_alignuint32_t+#endif+#define _SS_SIZE128+#define _SS_PADSIZE(_SS_SIZE - (2 * sizeof(__ss_align)))++struct sockaddr_storage +  {+    __SOCKADDR_COMMON (__ss_);/* Address family, etc.  */+    __ss_align __ss_align;/* Force desired alignment.  */+    char __ss_padding[_SS_PADSIZE];+  };++ /* Bits in the FLAGS argument to `send', `recv', et al.  */ enum   {

in.s_addr = ((struct sockaddr_in *) storage[0])->sin_addr.s_addr;

                                             
0 0
原创粉丝点击