]> git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Sockets/RawIp4Tx/RawIp4Tx.c
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / AppPkg / Applications / Sockets / RawIp4Tx / RawIp4Tx.c
1 /** @file
2 Raw IP4 transmit application
3
4 Copyright (c) 2011-2012, Intel Corporation. All rights reserved.
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "RawIp4Tx.h"
10
11 UINT8 mBuffer[1024];
12
13 /**
14 Transmit raw IP4 packets to the remote system.
15
16 @param [in] ArgC Argument count
17 @param [in] ArgV Argument value array
18
19 @retval 0 Successfully operation
20 **/
21
22 int
23 RawIp4Tx (
24 IN int ArgC,
25 IN char **ArgV
26 )
27 {
28 UINT32 BytesSent;
29 ssize_t BytesTransmitted;
30 struct sockaddr_in LocalPort;
31 UINT32 RemoteAddress[4];
32 struct sockaddr_in RemotePort;
33 int RetVal;
34 UINT32 TotalSent;
35 SOCKET s;
36
37 //
38 // Create the socket
39 //
40 s = socket ( AF_INET, SOCK_RAW, RAW_PROTOCOL );
41 if ( -1 == s ) {
42 RetVal = GET_ERRNO;
43 printf ( "ERROR - socket error, errno: %d\r\n", RetVal );
44 }
45 else {
46 //
47 // Use for/break; instead of goto
48 //
49 for ( ; ; ) {
50 //
51 // Validate the arguments
52 //
53 if (( 2 > ArgC )
54 || ( 4 != sscanf ( ArgV[1],
55 "%d.%d.%d.%d",
56 &RemoteAddress[0],
57 &RemoteAddress[1],
58 &RemoteAddress[2],
59 &RemoteAddress[3]))
60 || ( 224 < RemoteAddress[0])
61 || ( 255 < RemoteAddress[1])
62 || ( 255 < RemoteAddress[2])
63 || ( 255 < RemoteAddress[3])
64 || (( 0 == RemoteAddress[0])
65 && ( 0 == RemoteAddress[1])
66 && ( 0 == RemoteAddress[2])
67 && ( 0 == RemoteAddress[3]))) {
68 printf ( "%s <remote IP address>\r\n", ArgV[0]);
69 RetVal = EINVAL;
70 break;
71 }
72
73 //
74 // Bind the socket to a local port
75 //
76 memset ( &LocalPort, 0, sizeof ( LocalPort ));
77 SIN_LEN ( LocalPort ) = sizeof ( LocalPort );
78 SIN_FAMILY ( LocalPort ) = AF_INET;
79 SIN_ADDR ( LocalPort ) = 0;
80 SIN_PORT ( LocalPort ) = 0;
81 RetVal = bind ( s,
82 (struct sockaddr *)&LocalPort,
83 sizeof ( LocalPort ));
84 if ( -1 == RetVal ) {
85 RetVal = GET_ERRNO;
86 printf ( "ERROR - bind error, errno: %d\r\n", RetVal );
87 break;
88 }
89
90 //
91 // Specify the remote port
92 //
93 memset ( &RemotePort, 0, sizeof ( RemotePort ));
94 SIN_LEN ( RemotePort ) = sizeof ( RemotePort );
95 SIN_FAMILY ( RemotePort ) = AF_INET;
96 SIN_ADDR ( RemotePort ) = ( RemoteAddress[3] << 24 )
97 | ( RemoteAddress[2] << 16 )
98 | ( RemoteAddress[1] << 8 )
99 | RemoteAddress[0];
100 SIN_PORT ( RemotePort ) = 0;
101
102 //
103 // Initialize the messages
104 //
105 memset ( &mBuffer[0], 0, sizeof ( mBuffer ));
106
107 //
108 // Send the data before the out-of-band message
109 //
110 TotalSent = 0;
111 BytesSent = 0;
112 do {
113 BytesTransmitted = sendto ( s,
114 &mBuffer[BytesSent],
115 sizeof ( mBuffer ) - BytesSent,
116 0,
117 (struct sockaddr *)&RemotePort,
118 sizeof ( RemotePort ));
119 if ( -1 == BytesTransmitted ) {
120 RetVal = GET_ERRNO;
121 printf ( "ERROR - send before error, errno: %d\r\n", RetVal );
122 break;
123 }
124 BytesSent += (UINT32)BytesTransmitted;
125 RetVal = 0;
126 } while ( sizeof ( mBuffer ) > BytesSent );
127 if ( 0 != RetVal ) {
128 break;
129 }
130 TotalSent += BytesSent;
131
132 //
133 // Test completed successfully
134 //
135 if ( 0 == RetVal ) {
136 printf ( "Bytes sent: %8d\r\n", TotalSent );
137 }
138 break;
139 }
140
141 //
142 // Close the socket
143 //
144 CLOSE_SOCKET ( s );
145 }
146
147 //
148 // Return the operation status
149 //
150 return RetVal;
151 }