Linux Socket Programming by Example PHẦN 2 ppt

51 475 1
Linux Socket Programming by Example PHẦN 2 ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Use of these functions is quite simple. For example, to convert a short integer to network order, the following code can be used: short host_short = 0x1234; short netw_short; netw_short = htons(host_short); The value netw_short will receive the appropriate value from the conversion to network order. To convert a value from network order back into host order is equally simple: host_short = ntohs(netw_short); Tip The h in the function name refers to "host," whereas n refers to "network." Similarly, s refers to "short" and l refers to "long." Using these conventions, it is a simple matter to pick the name of the conversion function you need. Caution The byteorder(3) functions may be implemented as macros on some systems. Linux systems that run on CPUs using the big- endian byte ordering might provide a simple macro instead, because no conversion of the value is required. Initializing a Wild Internet Address Now you are ready to create an Internet address. The example shown here will request that the address be wild. This is often done when you are connecting to a remote service. The reason for doing this is that your host might have two or more network interface cards, each with a different IP number. Furthermore, Linux also permits the assignment of more than one IP number to each interface. When you specify a wild IP number, you allow the system to pick the route to the remote service. The kernel will then determine what your final local socket address will be at the time the connection is established. Linux Socket Programming by Example - Warren W. Gay 53 There are also times when you want the kernel to assign a local port number for you. This is done by specifying sin_port as the value zero. The example code shown in Listing 2.8 demonstrates how to initialize an AF_INET address with both a wild port number and a wild IP number. Listing 2.8 Initializing an IN_ADDRANY AF_INET Address 1: struct sockaddr_in adr_inet; 2: int adr_len; 3: 4: memset(&adr_inet,0,sizeof adr_inet); 5: 6: adr_inet.sin_family = AF_INET; 7: adr_inet.sin_port = ntohs(0); 8: adr_inet.sin_addr.s_addr = ntohl(INADDR_ANY); 9: adr_len = sizeof adr_inet; The steps used in Listing 2.8 are as follows: 1. The value adr_inet is defined using the structure sockaddr_in (line 1). 2. The address adr_inet is zeroed by calling memset(3) in line 4. (This is optional.) 3. The address family is established by assigning the value AF_INET to adr_inet.sin_family (line 6). 4. A wild port number is specified in line 7. Notice the use of the function ntohs(3). The value zero indicates a wild port number. 5. A wild IP number is assigned in line 8. Again, note the use of the ntohl(3) function to perform the endian conversion. 6. The size of the address is simply computed as the size of the structure adr_inet (line 9). Another commonly used IP number is 127.0.0.1. This refers to the loopback device. The loopback device lets you communicate with another process on the same host as your process. You'll see more of this IP number later. For now, just note how the address can be assigned below. Line 8 of Listing 2.8 could be changed to the following statement: adr_inet.sin_addr.s_addr = ntohl(INADDR_LOOPBACK); This will address your current host through the loopback device. In the next section, you will learn how to set up any IP number and port number. Linux Socket Programming by Example - Warren W. Gay 54 Initializing a Specific Internet Address The previous section dealt with a simple case for AF_INET addresses. Things get more complicated when you want to establish a specific IP number in the address. Listing 2.9 shows a complete program listing that you can compile by simply performing the following command: $ make af_inet Then, just invoke the compiled program by the name af_inet. Listing 2.9 af_inet.c—Establishing a Specific AF_INET Address 1: /* af_inet.c: 2: * 3: * Establishing a Specific AF_INET 4: * Socket Address: 5: */ 6: #include <stdio.h> 7: #include <unistd.h> 8: #include <stdlib.h> 9: #include <errno.h> 10: #include <string.h> 11: #include <sys/types.h> 12: #include <sys/stat.h> 13: #include <sys/socket.h> 14: #include <netinet/in.h> 15: 16: /* 17: * This function reports the error and 18: * exits back to the shell: 19: */ 20: static void 21: bail(const char *on_what) { 22: perror(on_what); 23: exit(1); 24: } 25: 26: int 27: main(int argc,char **argv,char **envp) { 28: int z; /* Status return code */ 29: int sck_inet; /* Socket */ 30: struct sockaddr_in adr_inet;/* AF_INET */ 31: int len_inet; /* length */ 32: const unsigned char IPno[] = { 33: 127, 0, 0, 23 /* Local loopback */ 34: }; 35: 36: /* Create an IPv4 Internet Socket */ 37: sck_inet = socket(AF_INET,SOCK_STREAM,0); 38: 39: if ( sck_inet == -1 ) Linux Socket Programming by Example - Warren W. Gay 55 40: bail("socket()"); 41: 42: /* Create an AF_INET address */ 43: memset(&adr_inet,0,sizeof adr_inet); 44: 45: adr_inet.sin_family = AF_INET; 46: adr_inet.sin_port = htons(9000); 47: memcpy(&adr_inet.sin_addr.s_addr,IPno,4); 48: len_inet = sizeof adr_inet; 49: 50: /* Now bind the address to the socket */ 51: z = bind(sck_inet, 52: (struct sockaddr *)&adr_inet, 53: len_inet); 54: 55: if ( z == -1 ) 56: bail("bind()"); 57: 58: /* Display all of our bound sockets */ 59: system("netstat -pa tcp 2>/dev/null | " 60: "sed -n '1,/^Proto/p;/af_inet/p'"); 61: 62: close(sck_inet); 63: return 0; 64: } The steps used in this program are almost identical to the others shown in Listings 2.3 and 2.5. Lines 43 to 48, however, require some explanation: 1. Line 30 defines the sockaddr_in structure with the name adr_inet. Additionally, the socket address length is defined as an integer in line 31 as len_inet. 2. An unsigned character array is defined as IPno[4] in lines 32 and 33. Here the individual bytes spell out a specific IP address 127.0.0.23. 3. Line 43 zeros out adr_inet as usual. Note that, again, this is optional. 4. Line 45 establishes the address family as AF_INET. 5. This example chose to establish a TCP/IP port number 9000 in line 46. Note the use of the conversion function htons(3) in line 46. 6. The character array IPno[4] is copied to the location adr_inet.sin_addr.s_addr in line 47. Because the bytes are defined in network order back in step 2, there is no endian conversion required here. You will recall that network byte ordering has the most significant byte presented first. 7. The size of the address structure is computed as before (line 48). Linux Socket Programming by Example - Warren W. Gay 56 You might have noticed that Internet addresses have a fixed length. If you review Figure 2.3, this is readily apparent. However, you will remember that the AF_LOCAL address was variable in length (refer to Figure 2.2). For AF_INET addresses, you merely need to supply the size of the socket structure sockaddr_in. In C language terms, this is sizeof(struct sockaddr_in) You should be well equipped now for forming Internet IPv4 addresses. To broaden your knowledge on socket addressing, the next sections will show you how some other address families can be specified. Specifying an X.25 Address The socket interface allows the programmer to use other protocols that are available under Linux with very little effort. The only major part of the code that is different has to do with how the sockets are addressed. You have already seen the initialization required for AF_LOCAL and AF_INET addresses. The creation of an X.25 address is very similar. The structure used to define an X.25 protocol address is the sockaddr_x25 structure. The include statement that defines this structure is as follows: #include <linux/x25.h> Listing 2.10 shows the socket address structure for the AF_X25 address family. Listing 2.10 The X.25 Socket Address Structure struct sockaddr_x25 { sa_family_t sx25_family; /* Must be AF_X25 */ x25_address sx25_addr; /* X.121 Address */ }; typedef struct { char x25_addr[16]; } x25_address; You will notice that, again, a member sx25_family occupies the first two bytes of the generic socket structure. For this address, it must have the value AF_X25. Tip Linux Socket Programming by Example - Warren W. Gay 57 Information about X.25 socket addresses can be found in the x25(4) man page. An X.25 network address (the X.121 standard defines this address) consists of a series of decimal digits. The program af_x25.c has been provided to show you how you can establish an X.25 address and have netstat(1) display it. Listing 2.11 shows the program listing. Listing 2.11 af_x25.c—Establishing an X.25 Protocol Address 1: /* af_x25.c: 2: * 3: * X.25 Socket Address Example: 4: * 5: */ 6: #include <stdio.h> 7: #include <unistd.h> 8: #include <stdlib.h> 9: #include <errno.h> 10: #include <string.h> 11: #include <sys/types.h> 12: #include <sys/socket.h> 13: #include <linux/x25.h> 14: 15: /* 16: * This function reports the error and 17: * exits back to the shell: 18: */ 19: static void 20: bail(const char *on_what) { 21: perror(on_what); 22: exit(1); 23: } 24: 25: int 26: main(int argc,char **argv,char **envp) { 27: int z; /* Status return code */ 28: int sck_x25; /* Socket */ 29: struct sockaddr_x25 adr_x25;/* AF_X25 */ 30: int len_x25; /* length */ 31: const char x25_host[] /* X.121 addr */ 32: = "79400900"; 33: 34: /* Create an AF_X25 socket */ 35: sck_x25 = socket(AF_X25,SOCK_SEQPACKET,0); 36: 37: if ( sck_x25 == -1 ) 38: bail("socket()"); 39: 40: /* Form an AF_X25 Address */ Linux Socket Programming by Example - Warren W. Gay 58 41: adr_x25.sx25_family = AF_X25; 42: strcpy(adr_x25.sx25_addr.x25_addr,x25_host); 43: len_x25 = sizeof adr_x25; 44: 45: /* Bind the address to the socket */ 46: z = bind(sck_x25, 47: (struct sockaddr *)&adr_x25, 48: len_x25); 49: 50: if ( z == -1 ) 51: bail("bind()"); 52: 53: puts("X.25 SOCKETS :"); 54: system("cat /proc/net/x25"); 55: return 0; 56: } To compile the program in Listing 2.11, perform the following: $ make af_x25 Note You will not be successful running the program in Listing 2.11 if you do not have X.25 support compiled into your kernel. Be sure to enable X.25 support if you want to experiment with that protocol. To enable X.25 support you must configure and recompile your Linux kernel. The address establishing code consists of the following basic steps: 1. The sockaddr_x25 structure is used in line 29 to define adr_x25. The length variable len_x25 is defined as an int on line 30. 2. A character array constant x25_host[] is defined on lines 31 and 32 as the X.25 address is to be establish. 3. The address family is specified as AF_X25 in line 41. 4. The host address number is copied into the address structure with a terminating null byte in line 42. 5. The length of the sockaddr_x25 structure is the correct length to use with the current Linux implementation (line 43). Note that the program does not call upon netstat(1) this time. This is because netstat(1) does not report AF_X25 sockets at this Linux Socket Programming by Example - Warren W. Gay 59 time. Instead, the example program uses cat(1) to copy the contents of /proc/net/x25 to standard output. For this to be successful, however, you must have the proc file system support compiled into your kernel (this is now standard practice). Note Normally socket addresses with variable elements like the AF_UNIX address family require a computed length of the address. The Linux implementation of the AF_X25 socket address, however, simply requires the fixed length of sizeof(sockaddr_x25). The host number must be null terminated within the sockaddr_x25 structure. Running the program af_x25 provides the results shown in Listing 2.12. Listing 2.12 The Output of the af_x25 Program $ ./af_x25 X.25 SOCKETS : dest_addr src_addr dev lci st vs vr va t t2 t21 t22 t23 Snd-Q Rcv-Q inode * 79400900 ??? 000 0 0 0 0 0 3 200 180 180 0 0 104172 $ In the program output in Listing 2.12, you can see the host number listed under the src_addr column heading as 79400900. Specifying Other Address Families The scope of this book does not permit a full coverage of all address families supported by Linux. The list of supported protocols is growing longer with each new year. If you are looking for a fast track to TCP/IP programming, you can skip this section and advance to the next section. In this section, you will read briefly about a few other protocols that might be of interest to you. This section is intended as a roadmap to other places of interest, should you feel like some adventure. There are at least three more address families that Linux can support. They are • AF_INET6—IPv6, which is under development Linux Socket Programming by Example - Warren W. Gay 60 • AF_AX25—Amateur Radio X.25 protocol • AF_APPLETALK—Linux AppleTalk protocol implementation Each of these protocols requires that you have the corresponding support compiled into your kernel. Some of these protocols may not be complete implementations—programmer beware! Incomplete or experimental protocols will be buggy or sometimes even crash your system. Tip The AF_APPLETALK address family is documented in the ddp(4) man page. Listing 2.13 shows some of the C structures that are important to these other socket address families. These structures will help you visualize the address components that must be initialized when creating an address for the protocol chosen. Listing 2.13 Other Address Family Structures /* * IPv6 Address (AF_INET6): */ struct sockaddr_in6 { sa_family_t sin6_family; uint16_t sin6_port; /* port # */ uint32_t sin6_flowinfo; /* flow info */ struct in6_addr sin6_addr; /* IPv6 address */ }; struct in6_addr { union { uint8_t u6_addr8[16]; uint16_t u6_addr16[8]; uint32_t u6_addr32[4]; } in6_u; }; /* * Amateur Radio AX.25 Address (AF_AX25): */ struct full_sockaddr_ax25 { struct sockaddr_ax25 fsa_ax25; ax25_address fsa_digipeater[AX25_MAX_DIGIS]; }; struct sockaddr_ax25 { sa_family_t sax25_family; Linux Socket Programming by Example - Warren W. Gay 61 ax25_address sax25_call; int sax25_ndigis; }; typedef struct { /* 6 call + SSID (shifted ascii!) */ char ax25_call[7]; } ax25_address; #define sax25_uid sax25_ndigis /* * AppleTalk Address (AF_APPLETALK): */ struct sockaddr_atalk { sa_family_t sat_family; /* addr family */ u_char sat_port; /* port */ struct at_addr sat_addr; /* net/node */ }; struct at_addr { u_short s_net; u_char s_node; }; Fully addressing the steps to initialize each of these different protocols would require some knowledge about the underlying protocols themselves. This is outside of the scope of what you want to accomplish at this point in this book. However, if you would like to experiment further with the AF_AX25 family, there is one other program available for the purpose in source file af_ax25.c. Using the Makefile provided at the Web site associated with this book, you can compile it as follows: $ make af_ax25 gcc -c -D_GNU_SOURCE -Wall af_ax25.c gcc af_ax25.o -o af_ax25 $ Note All source code and make files for this book are provided at the following URL: http://www.quecorp.com/series/by_example Linux Socket Programming by Example - Warren W. Gay 62 [...]... 0.0.0.0 127 .25 5 .25 5 .25 5 7 24 B 128 .0.0.0 191 .25 5 .25 5 .25 5 14 16 C 1 92. 0.0.0 22 3 .25 5 .25 5 .25 5 21 8 D 22 4.0.0.0 23 9 .25 5 .25 5 .25 5 28 N/A E 24 0.0.0.0 24 7 .25 5 .25 5 .25 5 27 N/A Class A, B, and C define specific IP addresses of hosts For class D and E addresses, there are zero host bits available in the address Class D addresses are used for multicasting where the 28 bits are used to describe a multicast group The 27 ... notation Linux Socket Programming by Example - Warren W Gay 69 If you must set up your own IP network, then you will need to determine what the netmask values should be Table 3 .2 lists the netmask values for class A, B, and C addresses Table 3 .2 Netmask Values by IP Class Class Lowest Highest Netmask A 0.0.0.0 127 .25 5 .25 5 .25 5 25 5.0.0.0 B 128 .0.0.0 191 .25 5 .25 5 .25 5 25 5 .25 5.0.0 C 1 92. 0.0.0 22 3 .25 5 .25 5 .25 5 25 5 .25 5 .25 5.0... #include #include #include int main(int argc,char **argv) { int x; /* Index variable */ Linux Socket Programming by Example - Warren W Gay 70 15: 16: 17: 18: 19: 20 : 21 : 22 : 23 : 24 : 25 : 26 : 27 : 28 : 29 : 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: struct... bound to the socket Listing 3.3 inetaddr.c Example Program Using inet_addr(3) 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20 : 21 : 22 : 23 : 24 : 25 : 26 : 27 : 28 : 29 : 30: 31: 32: 33: 34: 35: /* inetaddr.c: * * Example using inet_addr(3): */ #include #include #include #include #include #include #include ... 3.3 provides a quick summary for you, complete with netmask values Table 3.3 Private IP Number Allocations Class Lowest Highest Netmask A 10.0.0.0 10 .25 5 .25 5 .25 5 25 5.0.0.0 B 1 72. 16.0.0 1 72. 31 .25 5 .25 5 25 5 .25 5.0.0 C 1 92. 168.0.0 1 92. 168 .25 5 .25 5 25 5 .25 5 .25 5.0 Your choice of a class A, B, or C IP number series will depend largely upon the number of separate networks and hosts that you plan to establish... inetaton.c—Using inet_aton(3) 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20 : 21 : 22 : 23 : 24 : 25 : 26 : 27 : 28 : 29 : 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: /* inetaton.c: * * Example using inet_aton(3) : */ #include #include #include #include #include #include #include #include ... the same steps to set up the address as did the previous example program The function inet_ntoa(3) is called upon to allow the IP number to be displayed Listing 3.5 inetntoa.c—Demonstration of inet_ntoa(3) Function 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20 : 21 : 22 : 23 : 24 : 25 : 26 : 27 : 28 : 29 : 30: 31: /* inetntoa.c: * * Example using inet_ntoa(3): */ #include #include... The example shown in Listing 3.6 illustrates how the inet_network(3) function can be used The program also calls upon Linux Socket Programming by Example - Warren W Gay 85 the htonl(3) function to display how the value looks in networkendian order Listing 3.6 network.c—Demonstration of the inet_network(3) Function 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20 : 21 : 22 : 23 : 24 : 25 :... also to 25 5 .25 5 .25 5 .25 5 (see step 9) 11.The results of the classification are reported in lines 67 to 74 Listing 3 .2 shows the output that results from running this demonstration program Listing 3 .2 The Output of the netmask.c Demonstration Program $ /netmask Address 44.135.86. 12 is class A netmask 25 5.0.0.0 Address 127 .0.0.1 is class A netmask 25 5.0.0.0 Address 1 72. 16 .23 .95 is class B netmask 25 5 .25 5.0.0... Significant Byte * 2 Classify by that byte */ msb = *(unsigned char *) &adr_inet.sin_addr.s_addr; if ( (msb & 0x80) == 0x00 ) { class = 'A'; netmask = "25 5.0.0.0"; } else if ( (msb & 0xC0) == 0x80 ) { class = 'B'; netmask = "25 5 .25 5.0.0"; } else if ( (msb & 0xE0) == 0xC0 ) { class = 'C'; netmask = "25 5 .25 5 .25 5.0"; } else if ( (msb & 0xF0) == 0xE0 ) { class = 'D'; netmask = "25 5 .25 5 .25 5 .25 5"; } else . Host Bits A 0.0.0.0 127 .25 5 .25 5 .25 5 7 24 B 128 .0.0.0 191 .25 5 .25 5 .25 5 14 16 C 1 92. 0.0.0 22 3 .25 5 .25 5 .25 5 21 8 D 22 4.0.0.0 23 9 .25 5 .25 5 .25 5 28 N/A E 24 0.0.0.0 24 7 .25 5 .25 5 .25 5 27 N/A Class A, B, and. addresses. Table 3 .2. Netmask Values by IP Class Class Lowest Highest Netmask A 0.0.0.0 127 .25 5 .25 5 .25 5 25 5.0.0.0 B 128 .0.0.0 191 .25 5 .25 5 .25 5 25 5 .25 5.0.0 C 1 92. 0.0.0 22 3 .25 5 .25 5 .25 5 25 5 .25 5 .25 5.0 Sometimes,. sck_x25 == -1 ) 38: bail(" ;socket( )"); 39: 40: /* Form an AF_X25 Address */ Linux Socket Programming by Example - Warren W. Gay 58 41: adr_x25.sx25_family = AF_X25; 42: strcpy(adr_x25.sx25_addr.x25_addr,x25_host); 43:

Ngày đăng: 12/08/2014, 21:20

Từ khóa liên quan

Mục lục

  • Part I: Basic Socket Concepts

    • Chapter 2. Domains and Address Families

      • Forming Internet (IPv4) Socket Addresses

        • Initializing a Wild Internet Address

          • Listing 2.8 Initializing an IN_ADDRANY AF_INET Address

          • Initializing a Specific Internet Address

            • Listing 2.9 af_inet.c—Establishing a Specific AF_INET Address

            • Specifying an X.25 Address

              • Listing 2.10 The X.25 Socket Address Structure

              • Listing 2.11 af_x25.c—Establishing an X.25 Protocol Address

              • Listing 2.12 The Output of the af_x25 Program

              • Specifying Other Address Families

                • Listing 2.13 Other Address Family Structures

                • Listing 2.14 Checking the Interface Status of bpq0

                • The AF_UNSPEC Address Family

                  • Listing 2.15 A Union of Address Families

                  • What's Next

                  • Chapter 3. Address Conversion Functions

                    • Internet IP Numbers

                      • Internet Address Classes

                        • Table 3.1. Internet Address Classes

                        • Figure 3.1. This figure illustrates the Internet address classes A, B, and C.

                        • Understanding Netmask Values

                          • Figure 3.2. Applying a netmask to 192.168.9.1 yields a network address.

                          • Figure 3.3. Here is the netmask expressed in dotted-quad notation.

                          • Table 3.2. Netmask Values by IP Class

                          • Listing 3.1 netmask.c—Classifying and Determining a Netmask

                          • Listing 3.2 The Output of the netmask.c Demonstration Program

                          • Allocating IP Addresses

                            • Private IP Numbers

                              • Table 3.3. Private IP Number Allocations

                              • Reserved IP Numbers

                                • Table 3.4. Amateur Radio Reserved IP Numbers

                                • Manipulating IP Numbers

                                  • Using the inet_addr(3) Function

                                    • Listing 3.3 inetaddr.c—Example Program Using inet_addr(3)

                                    • The inet_aton(3) Function

                                      • Listing 3.4 inetaton.c—Using inet_aton(3)

Tài liệu cùng người dùng

Tài liệu liên quan