socket深入学习

socket(domain,type,protocol)

1、domain  PF协议族和AF地址族概念划分不同,底层调用为对应关系,只是逻辑区分(是否在不同的系统、网卡上面有不同的对应)

/* Protocol families.  */
#define PF_UNSPEC    0    /* Unspecified.  */
#define PF_LOCAL    1    /* Local to host (pipes and file-domain).  */
#define PF_UNIX        PF_LOCAL /* POSIX name for PF_LOCAL.  */
#define PF_FILE        PF_LOCAL /* Another non-standard name for PF_LOCAL.  */
#define PF_INET        2    /* IP protocol family.  */
#define PF_AX25        3    /* Amateur Radio AX.25.  */
#define PF_IPX        4    /* Novell Internet Protocol.  */
#define PF_APPLETALK    5    /* Appletalk DDP.  */
#define PF_NETROM    6    /* Amateur radio NetROM.  */
#define PF_BRIDGE    7    /* Multiprotocol bridge.  */
#define PF_ATMPVC    8    /* ATM PVCs.  */
#define PF_X25        9    /* Reserved for X.25 project.  */
#define PF_INET6    10    /* IP version 6.  */
#define PF_ROSE        11    /* Amateur Radio X.25 PLP.  */
#define PF_DECnet    12    /* Reserved for DECnet project.  */
#define PF_NETBEUI    13    /* Reserved for 802.2LLC project.  */
#define PF_SECURITY    14    /* Security callback pseudo AF.  */
#define PF_KEY        15    /* PF_KEY key management API.  */
#define PF_NETLINK    16
#define PF_ROUTE    PF_NETLINK /* Alias to emulate 4.4BSD.  */
#define PF_PACKET    17    /* Packet family.  */
#define PF_ASH        18    /* Ash.  */
#define PF_ECONET    19    /* Acorn Econet.  */
#define PF_ATMSVC    20    /* ATM SVCs.  */
#define PF_RDS        21    /* RDS sockets.  */
#define PF_SNA        22    /* Linux SNA Project */
#define PF_IRDA        23    /* IRDA sockets.  */
#define PF_PPPOX    24    /* PPPoX sockets.  */
#define PF_WANPIPE    25    /* Wanpipe API sockets.  */
#define PF_LLC        26    /* Linux LLC.  */
#define PF_IB        27    /* Native InfiniBand address.  */
#define PF_MPLS        28    /* MPLS.  */
#define PF_CAN        29    /* Controller Area Network.  */
#define PF_TIPC        30    /* TIPC sockets.  */
#define PF_BLUETOOTH    31    /* Bluetooth sockets.  */
#define PF_IUCV        32    /* IUCV sockets.  */
#define PF_RXRPC    33    /* RxRPC sockets.  */
#define PF_ISDN        34    /* mISDN sockets.  */
#define PF_PHONET    35    /* Phonet sockets.  */
#define PF_IEEE802154    36    /* IEEE 802.15.4 sockets.  */
#define PF_CAIF        37    /* CAIF sockets.  */
#define PF_ALG        38    /* Algorithm sockets.  */
#define PF_NFC        39    /* NFC sockets.  */
#define PF_VSOCK    40    /* vSockets.  */
#define PF_MAX        41    /* For now..  */

2、type

/* Types of sockets.  */
enum __socket_type
{
  SOCK_STREAM = 1,        /* Sequenced, reliable, connection-based
                   byte streams.  */
#define SOCK_STREAM SOCK_STREAM
  SOCK_DGRAM = 2,        /* Connectionless, unreliable datagrams
                   of fixed maximum length.  */
#define SOCK_DGRAM SOCK_DGRAM
  SOCK_RAW = 3,            /* Raw protocol interface.  */
#define SOCK_RAW SOCK_RAW
  SOCK_RDM = 4,            /* Reliably-delivered messages.  */
#define SOCK_RDM SOCK_RDM
  SOCK_SEQPACKET = 5,        /* Sequenced, reliable, connection-based,
                   datagrams of fixed maximum length.  */
#define SOCK_SEQPACKET SOCK_SEQPACKET
  SOCK_DCCP = 6,        /* Datagram Congestion Control Protocol.  */
#define SOCK_DCCP SOCK_DCCP
  SOCK_PACKET = 10,        /* Linux specific way of getting packets
                   at the dev level.  For writing rarp and
                   other similar things on the user level. */
#define SOCK_PACKET SOCK_PACKET

  /* Flags to be ORed into the type parameter of socket and socketpair and
     used for the flags parameter of paccept.  */

  SOCK_CLOEXEC = 02000000,    /* Atomically set close-on-exec flag for the
                   new descriptor(s).  */
#define SOCK_CLOEXEC SOCK_CLOEXEC
  SOCK_NONBLOCK = 00004000    /* Atomically mark descriptor(s) as
                   non-blocking.  */
#define SOCK_NONBLOCK SOCK_NONBLOCK
};

3、protocol

/* Standard well-defined IP protocols.  */
enum
  {
    IPPROTO_IP = 0,       /* Dummy protocol for TCP.  */
#define IPPROTO_IP        IPPROTO_IP
    IPPROTO_ICMP = 1,       /* Internet Control Message Protocol.  */
#define IPPROTO_ICMP        IPPROTO_ICMP
    IPPROTO_IGMP = 2,       /* Internet Group Management Protocol. */
#define IPPROTO_IGMP        IPPROTO_IGMP
    IPPROTO_IPIP = 4,       /* IPIP tunnels (older KA9Q tunnels use 94).  */
#define IPPROTO_IPIP        IPPROTO_IPIP
    IPPROTO_TCP = 6,       /* Transmission Control Protocol.  */
#define IPPROTO_TCP        IPPROTO_TCP
    IPPROTO_EGP = 8,       /* Exterior Gateway Protocol.  */
#define IPPROTO_EGP        IPPROTO_EGP
    IPPROTO_PUP = 12,       /* PUP protocol.  */
#define IPPROTO_PUP        IPPROTO_PUP
    IPPROTO_UDP = 17,       /* User Datagram Protocol.  */
#define IPPROTO_UDP        IPPROTO_UDP
    IPPROTO_IDP = 22,       /* XNS IDP protocol.  */
#define IPPROTO_IDP        IPPROTO_IDP
    IPPROTO_TP = 29,       /* SO Transport Protocol Class 4.  */
#define IPPROTO_TP        IPPROTO_TP
    IPPROTO_DCCP = 33,       /* Datagram Congestion Control Protocol.  */
#define IPPROTO_DCCP        IPPROTO_DCCP
    IPPROTO_IPV6 = 41,     /* IPv6 header.  */
#define IPPROTO_IPV6        IPPROTO_IPV6
    IPPROTO_RSVP = 46,       /* Reservation Protocol.  */
#define IPPROTO_RSVP        IPPROTO_RSVP
    IPPROTO_GRE = 47,       /* General Routing Encapsulation.  */
#define IPPROTO_GRE        IPPROTO_GRE
    IPPROTO_ESP = 50,      /* encapsulating security payload.  */
#define IPPROTO_ESP        IPPROTO_ESP
    IPPROTO_AH = 51,       /* authentication header.  */
#define IPPROTO_AH        IPPROTO_AH
    IPPROTO_MTP = 92,       /* Multicast Transport Protocol.  */
#define IPPROTO_MTP        IPPROTO_MTP
    IPPROTO_BEETPH = 94,   /* IP option pseudo header for BEET.  */
#define IPPROTO_BEETPH        IPPROTO_BEETPH
    IPPROTO_ENCAP = 98,       /* Encapsulation Header.  */
#define IPPROTO_ENCAP        IPPROTO_ENCAP
    IPPROTO_PIM = 103,       /* Protocol Independent Multicast.  */
#define IPPROTO_PIM        IPPROTO_PIM
    IPPROTO_COMP = 108,       /* Compression Header Protocol.  */
#define IPPROTO_COMP        IPPROTO_COMP
    IPPROTO_SCTP = 132,       /* Stream Control Transmission Protocol.  */
#define IPPROTO_SCTP        IPPROTO_SCTP
    IPPROTO_UDPLITE = 136, /* UDP-Lite protocol.  */
#define IPPROTO_UDPLITE        IPPROTO_UDPLITE
    IPPROTO_MPLS = 137,    /* MPLS in IP.  */
#define IPPROTO_MPLS        IPPROTO_MPLS
    IPPROTO_RAW = 255,       /* Raw IP packets.  */
#define IPPROTO_RAW        IPPROTO_RAW
    IPPROTO_MAX
  };

以太网卡for BSDsocket on linux Ethernet IEEE 802.3 interface,htons

/*
 *    IEEE 802.3 Ethernet magic constants.  The frame sizes omit the preamble
 *    and FCS/CRC (frame check sequence).
 */

#define ETH_ALEN    6        /* Octets in one ethernet addr     */
#define ETH_HLEN    14        /* Total octets in header.     */
#define ETH_ZLEN    60        /* Min. octets in frame sans FCS */
#define ETH_DATA_LEN    1500        /* Max. octets in payload     */
#define ETH_FRAME_LEN    1514        /* Max. octets in frame sans FCS */
#define ETH_FCS_LEN    4        /* Octets in the FCS         */

/*
 *    These are the defined Ethernet Protocol ID's.
 */

#define ETH_P_LOOP    0x0060        /* Ethernet Loopback packet    */
#define ETH_P_PUP    0x0200        /* Xerox PUP packet        */
#define ETH_P_PUPAT    0x0201        /* Xerox PUP Addr Trans packet    */
#define ETH_P_TSN    0x22F0        /* TSN (IEEE 1722) packet    */
#define ETH_P_IP    0x0800        /* Internet Protocol packet    */
#define ETH_P_X25    0x0805        /* CCITT X.25            */
#define ETH_P_ARP    0x0806        /* Address Resolution packet    */
#define    ETH_P_BPQ    0x08FF        /* G8BPQ AX.25 Ethernet Packet    [ NOT AN OFFICIALLY REGISTERED ID ] */
#define ETH_P_IEEEPUP    0x0a00        /* Xerox IEEE802.3 PUP packet */
#define ETH_P_IEEEPUPAT    0x0a01        /* Xerox IEEE802.3 PUP Addr Trans packet */
#define ETH_P_BATMAN    0x4305        /* B.A.T.M.A.N.-Advanced packet [ NOT AN OFFICIALLY REGISTERED ID ] */
#define ETH_P_DEC       0x6000          /* DEC Assigned proto           */
#define ETH_P_DNA_DL    0x6001          /* DEC DNA Dump/Load            */
#define ETH_P_DNA_RC    0x6002          /* DEC DNA Remote Console       */
#define ETH_P_DNA_RT    0x6003          /* DEC DNA Routing              */
#define ETH_P_LAT       0x6004          /* DEC LAT                      */
#define ETH_P_DIAG      0x6005          /* DEC Diagnostics              */
#define ETH_P_CUST      0x6006          /* DEC Customer use             */
#define ETH_P_SCA       0x6007          /* DEC Systems Comms Arch       */
#define ETH_P_TEB    0x6558        /* Trans Ether Bridging        */
#define ETH_P_RARP      0x8035        /* Reverse Addr Res packet    */
#define ETH_P_ATALK    0x809B        /* Appletalk DDP        */
#define ETH_P_AARP    0x80F3        /* Appletalk AARP        */
#define ETH_P_8021Q    0x8100          /* 802.1Q VLAN Extended Header  */
#define ETH_P_IPX    0x8137        /* IPX over DIX            */
#define ETH_P_IPV6    0x86DD        /* IPv6 over bluebook        */
#define ETH_P_PAUSE    0x8808        /* IEEE Pause frames. See 802.3 31B */
#define ETH_P_SLOW    0x8809        /* Slow Protocol. See 802.3ad 43B */
#define ETH_P_WCCP    0x883E        /* Web-cache coordination protocol
                     * defined in draft-wilson-wrec-wccp-v2-00.txt */
#define ETH_P_MPLS_UC    0x8847        /* MPLS Unicast traffic        */
#define ETH_P_MPLS_MC    0x8848        /* MPLS Multicast traffic    */
#define ETH_P_ATMMPOA    0x884c        /* MultiProtocol Over ATM    */
#define ETH_P_PPP_DISC    0x8863        /* PPPoE discovery messages     */
#define ETH_P_PPP_SES    0x8864        /* PPPoE session messages    */
#define ETH_P_LINK_CTL    0x886c        /* HPNA, wlan link local tunnel */
#define ETH_P_ATMFATE    0x8884        /* Frame-based ATM Transport
                     * over Ethernet
                     */
#define ETH_P_PAE    0x888E        /* Port Access Entity (IEEE 802.1X) */
#define ETH_P_AOE    0x88A2        /* ATA over Ethernet        */
#define ETH_P_8021AD    0x88A8          /* 802.1ad Service VLAN        */
#define ETH_P_802_EX1    0x88B5        /* 802.1 Local Experimental 1.  */
#define ETH_P_TIPC    0x88CA        /* TIPC             */
#define ETH_P_8021AH    0x88E7          /* 802.1ah Backbone Service Tag */
#define ETH_P_MVRP    0x88F5          /* 802.1Q MVRP                  */
#define ETH_P_1588    0x88F7        /* IEEE 1588 Timesync */
#define ETH_P_PRP    0x88FB        /* IEC 62439-3 PRP/HSRv0    */
#define ETH_P_FCOE    0x8906        /* Fibre Channel over Ethernet  */
#define ETH_P_TDLS    0x890D          /* TDLS */
#define ETH_P_FIP    0x8914        /* FCoE Initialization Protocol */
#define ETH_P_80221    0x8917        /* IEEE 802.21 Media Independent Handover Protocol */
#define ETH_P_LOOPBACK    0x9000        /* Ethernet loopback packet, per IEEE 802.3 */
#define ETH_P_QINQ1    0x9100        /* deprecated QinQ VLAN [ NOT AN OFFICIALLY REGISTERED ID ] */
#define ETH_P_QINQ2    0x9200        /* deprecated QinQ VLAN [ NOT AN OFFICIALLY REGISTERED ID ] */
#define ETH_P_QINQ3    0x9300        /* deprecated QinQ VLAN [ NOT AN OFFICIALLY REGISTERED ID ] */
#define ETH_P_EDSA    0xDADA        /* Ethertype DSA [ NOT AN OFFICIALLY REGISTERED ID ] */
#define ETH_P_AF_IUCV   0xFBFB        /* IBM af_iucv [ NOT AN OFFICIALLY REGISTERED ID ] */

#define ETH_P_802_3_MIN    0x0600        /* If the value in the ethernet type is less than this value
                     * then the frame is Ethernet II. Else it is 802.3 */

/*
 *    Non DIX types. Won't clash for 1500 types.
 */

#define ETH_P_802_3    0x0001        /* Dummy type for 802.3 frames  */
#define ETH_P_AX25    0x0002        /* Dummy protocol id for AX.25  */
#define ETH_P_ALL    0x0003        /* Every packet (be careful!!!) */
#define ETH_P_802_2    0x0004        /* 802.2 frames         */
#define ETH_P_SNAP    0x0005        /* Internal only        */
#define ETH_P_DDCMP     0x0006          /* DEC DDCMP: Internal only     */
#define ETH_P_WAN_PPP   0x0007          /* Dummy type for WAN PPP frames*/
#define ETH_P_PPP_MP    0x0008          /* Dummy type for PPP MP frames */
#define ETH_P_LOCALTALK 0x0009        /* Localtalk pseudo type     */
#define ETH_P_CAN    0x000C        /* CAN: Controller Area Network */
#define ETH_P_CANFD    0x000D        /* CANFD: CAN flexible data rate*/
#define ETH_P_PPPTALK    0x0010        /* Dummy type for Atalk over PPP*/
#define ETH_P_TR_802_2    0x0011        /* 802.2 frames         */
#define ETH_P_MOBITEX    0x0015        /* Mobitex (kaz@cafe.net)    */
#define ETH_P_CONTROL    0x0016        /* Card specific control frames */
#define ETH_P_IRDA    0x0017        /* Linux-IrDA            */
#define ETH_P_ECONET    0x0018        /* Acorn Econet            */
#define ETH_P_HDLC    0x0019        /* HDLC frames            */
#define ETH_P_ARCNET    0x001A        /* 1A for ArcNet :-)            */
#define ETH_P_DSA    0x001B        /* Distributed Switch Arch.    */
#define ETH_P_TRAILER    0x001C        /* Trailer switch tagging    */
#define ETH_P_PHONET    0x00F5        /* Nokia Phonet frames          */
#define ETH_P_IEEE802154 0x00F6        /* IEEE802.15.4 frame        */
#define ETH_P_CAIF    0x00F7        /* ST-Ericsson CAIF protocol    */
#define ETH_P_XDSA    0x00F8        /* Multiplexed DSA protocol    */
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值