Saturday, June 13, 2015


 #include <sys/types.h> /* See NOTES */   
 #include <sys/socket.h>  
   
 int socket(int domain, int type, int protocol);   

1. Domain (Address Families or Protocol Families)

#include <linux/socket.h>
 #define AF_UNSPEC     0  
 #define AF_UNIX          1     /* Unix domain sockets           */  
 #define AF_LOCAL     1     /* POSIX name for AF_UNIX     */  
 #define AF_INET          2     /* Internet IP Protocol      */  
 #define AF_AX25          3     /* Amateur Radio AX.25           */  
 #define AF_IPX          4     /* Novell IPX                */  
 #define AF_APPLETALK     5     /* AppleTalk DDP           */  
 #define AF_NETROM     6     /* Amateur Radio NET/ROM      */  
 #define AF_BRIDGE     7     /* Multiprotocol bridge      */  
 #define AF_ATMPVC     8     /* ATM PVCs               */  
 #define AF_X25          9     /* Reserved for X.25 project      */  
 #define AF_INET6     10     /* IP version 6               */  
 #define AF_ROSE          11     /* Amateur Radio X.25 PLP     */  
 #define AF_DECnet     12     /* Reserved for DECnet project     */  
 #define AF_NETBEUI     13     /* Reserved for 802.2LLC project*/  
 #define AF_SECURITY     14     /* Security callback pseudo AF */  
 #define AF_KEY          15   /* PF_KEY key management API */  
 #define AF_NETLINK     16  
 #define AF_ROUTE     AF_NETLINK /* Alias to emulate 4.4BSD */  
 #define AF_PACKET     17     /* Packet family          */  
 #define AF_ASH          18     /* Ash                    */  
 #define AF_ECONET     19     /* Acorn Econet               */  
 #define AF_ATMSVC     20     /* ATM SVCs               */  
 #define AF_RDS          21     /* RDS sockets                */  
 #define AF_SNA          22     /* Linux SNA Project (nutters!) */  
 #define AF_IRDA          23     /* IRDA sockets               */  
 #define AF_PPPOX     24     /* PPPoX sockets          */  
 #define AF_WANPIPE     25     /* Wanpipe API Sockets */  
 #define AF_LLC          26     /* Linux LLC               */  
 #define AF_CAN          29     /* Controller Area Network   */  
 #define AF_TIPC          30     /* TIPC sockets               */  
 #define AF_BLUETOOTH     31     /* Bluetooth sockets           */  
 #define AF_IUCV          32     /* IUCV sockets               */  
 #define AF_RXRPC     33     /* RxRPC sockets           */  
 #define AF_ISDN          34     /* mISDN sockets           */  
 #define AF_PHONET     35     /* Phonet sockets          */  
 #define AF_IEEE802154     36     /* IEEE802154 sockets          */  
 #define AF_MAX          37     /* For now.. */  
   
 /* Protocol families, same as address families. */  
 #define PF_UNSPEC     AF_UNSPEC  
 #define PF_UNIX          AF_UNIX  
 #define PF_LOCAL     AF_LOCAL  
 #define PF_INET          AF_INET  
 #define PF_AX25          AF_AX25  
 #define PF_IPX          AF_IPX  
 #define PF_APPLETALK     AF_APPLETALK  
 #define PF_NETROM     AF_NETROM  
 #define PF_BRIDGE     AF_BRIDGE  
 #define PF_ATMPVC     AF_ATMPVC  
 #define PF_X25          AF_X25  
 #define PF_INET6     AF_INET6  
 #define PF_ROSE          AF_ROSE  
 #define PF_DECnet     AF_DECnet  
 #define PF_NETBEUI     AF_NETBEUI  
 #define PF_SECURITY     AF_SECURITY  
 #define PF_KEY          AF_KEY  
 #define PF_NETLINK     AF_NETLINK  
 #define PF_ROUTE     AF_ROUTE  
 #define PF_PACKET     AF_PACKET  
 #define PF_ASH          AF_ASH  
 #define PF_ECONET     AF_ECONET  
 #define PF_ATMSVC     AF_ATMSVC  
 #define PF_RDS          AF_RDS  
 #define PF_SNA          AF_SNA  
 #define PF_IRDA          AF_IRDA  
 #define PF_PPPOX     AF_PPPOX  
 #define PF_WANPIPE     AF_WANPIPE  
 #define PF_LLC          AF_LLC  
 #define PF_CAN          AF_CAN  
 #define PF_TIPC          AF_TIPC  
 #define PF_BLUETOOTH     AF_BLUETOOTH  
 #define PF_IUCV          AF_IUCV  
 #define PF_RXRPC     AF_RXRPC  
 #define PF_ISDN          AF_ISDN  
 #define PF_PHONET     AF_PHONET  
 #define PF_IEEE802154     AF_IEEE802154  
 #define PF_MAX          AF_MAX  
   


Common:

Name Purpose
AF_UNIX, AF_LOCAL Local communication - IPC giữa hai process trên user space với nhau
AF_INET, AF_INET6 IPv4/6 Internet protocols - Giao tiếp giữa process trên user space với TCP/IP stack dưới kernel space
AF_NETLINK Kernel user interface device - Giao tiếp giữa process trên user space với process dưới kernel space
... ...

Ex:

AF_NETLINK




AF_INET6


2. Type

#include <bits/sock_type.h>
 SOCK_STREAM  
 Provides sequenced, reliable, two-way, connection-based byte streams. An out-of-band data transmission mechanism may be supported.  
 SOCK_DGRAM  
 Supports datagrams (connectionless, unreliable messages of a fixed maximum length).  
 SOCK_SEQPACKET  
 Provides a sequenced, reliable, two-way connection-based data transmission path for datagrams of fixed maximum length; a consumer is required to read an entire packet with each input system call.  
 SOCK_RAW  
 Provides raw network protocol access.  
 SOCK_RDM  
 Provides a reliable datagram layer that does not guarantee ordering.  
 SOCK_PACKET  
 Obsolete and should not be used in new programs; see packet(7).  
 Some socket types may not be implemented by all protocol families.  
   
 Since Linux 2.6.27, the type argument serves a second purpose: in addition to specifying a socket type, it may include the bitwise OR of any of the following values, to modify the behavior of socket():  
   
 SOCK_NONBLOCK  
 Set the O_NONBLOCK file status flag on the new open file description. Using this flag saves extra calls to fcntl(2) to achieve the same result.  
 SOCK_CLOEXEC  
 Set the close-on-exec (FD_CLOEXEC) flag on the new file descriptor. See the description of the O_CLOEXEC flag in open(2) for reasons why this may be useful.  
   


3. Protocol
Mỗi domain sẽ có những protocol tương ứng với domain đó, ví dụ:

Protocol for AF_INET domain 
#include <netinet/in.h>
 /* Standard well-defined IP protocols. */  
 enum  
  {  
   IPPROTO_IP = 0,       /* Dummy protocol for TCP. */  
 #define IPPROTO_IP          IPPROTO_IP  
   IPPROTO_HOPOPTS = 0,  /* IPv6 Hop-by-Hop options. */  
 #define IPPROTO_HOPOPTS          IPPROTO_HOPOPTS  
   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_ROUTING = 43, /* IPv6 routing header. */  
 #define IPPROTO_ROUTING          IPPROTO_ROUTING  
   IPPROTO_FRAGMENT = 44, /* IPv6 fragmentation header. */  
 #define IPPROTO_FRAGMENT     IPPROTO_FRAGMENT  
   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_ICMPV6 = 58,  /* ICMPv6. */  
 #define IPPROTO_ICMPV6          IPPROTO_ICMPV6  
   IPPROTO_NONE = 59,   /* IPv6 no next header. */  
 #define IPPROTO_NONE          IPPROTO_NONE  
   IPPROTO_DSTOPTS = 60, /* IPv6 destination options. */  
 #define IPPROTO_DSTOPTS          IPPROTO_DSTOPTS  
   IPPROTO_MTP = 92,       /* Multicast Transport Protocol. */  
 #define IPPROTO_MTP          IPPROTO_MTP  
   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_RAW = 255,       /* Raw IP packets. */  
 #define IPPROTO_RAW          IPPROTO_RAW  
   IPPROTO_MAX  
  };  

Protocol for AF_NETLINK domain
#include <linux/netlink.h>
   
 #define NETLINK_ROUTE          0     /* Routing/device hook                    */  
 #define NETLINK_UNUSED          1     /* Unused number                    */  
 #define NETLINK_USERSOCK     2     /* Reserved for user mode socket protocols      */  
 #define NETLINK_FIREWALL     3     /* Firewalling hook                    */  
 #define NETLINK_INET_DIAG     4     /* INET socket monitoring               */  
 #define NETLINK_NFLOG          5     /* netfilter/iptables ULOG */  
 #define NETLINK_XFRM          6     /* ipsec */  
 #define NETLINK_SELINUX          7     /* SELinux event notifications */  
 #define NETLINK_ISCSI          8     /* Open-iSCSI */  
 #define NETLINK_AUDIT          9     /* auditing */  
 #define NETLINK_FIB_LOOKUP     10       
 #define NETLINK_CONNECTOR     11  
 #define NETLINK_NETFILTER     12     /* netfilter subsystem */  
 #define NETLINK_IP6_FW          13  
 #define NETLINK_DNRTMSG          14     /* DECnet routing messages */  
 #define NETLINK_KOBJECT_UEVENT     15     /* Kernel messages to userspace */  
 #define NETLINK_GENERIC          16  
 /* leave room for NETLINK_DM (DM Events) */  
 #define NETLINK_SCSITRANSPORT     18     /* SCSI Transports */  
 #define NETLINK_ECRYPTFS     19  

...



Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © Lập trình hệ thống nhúng Linux . Powered by Luong Duy Ninh -