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