Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
remote_command_exec_udp_server.c File Reference

Server-side implementation of [Remote Command Execution Using UDP](https://www.imperva.com/learn/ddos/udp-user-datagram-protocol/) More...

#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for remote_command_exec_udp_server.c:

Macros

#define PORT   10000
 For the type in_addr_t and in_port_t For structures returned by the network database library - formatted internet addresses and port numbers For in_addr and sockaddr_in structures For macro definitions related to the creation of sockets For definitions to allow for the porting of BSD programs.
 

Functions

void error ()
 Utility function used to print an error message to stderr.
 
int main ()
 Main function.
 

Detailed Description

Server-side implementation of [Remote Command Execution Using UDP](https://www.imperva.com/learn/ddos/udp-user-datagram-protocol/)

Author
NVombat
See also
remote_command_exec_udp_server.c

The algorithm is based on the simple UDP client and server model. It runs an infinite loop which takes user input and sends it to the server for execution. The server receives the commands and executes them until the user exits the loop. In this way, Remote Command Execution using UDP is shown using the server-client model & socket programming

Macro Definition Documentation

◆ PORT

#define PORT   10000

For the type in_addr_t and in_port_t For structures returned by the network database library - formatted internet addresses and port numbers For in_addr and sockaddr_in structures For macro definitions related to the creation of sockets For definitions to allow for the porting of BSD programs.

To indicate what went wrong if an error occurs For specific bit size values of variables Variable types, several macros, and various functions for performing input and output Variable types, several macros, and various functions for performing general functions Various functions for manipulating arrays of characters

Function Documentation

◆ error()

void error ( )

Utility function used to print an error message to stderr.

It prints str and an implementation-defined error message corresponding to the global variable errno.

Returns
void
47{
48 perror("Socket Creation Failed");
49 exit(EXIT_FAILURE);
50}

◆ main()

int main ( void  )

Main function.

Returns
0 on exit

Variable Declarations

< socket descriptors - Like file handles but for sockets

< character arrays to read and store string data for communication & Success message

< basic structures for all syscalls and functions that deal with internet addresses. Structures for handling internet addresses

size of address

The UDP socket is created using the socket function.

AF_INET (Family) - it is an address family that is used to designate the type of addresses that your socket can communicate with

SOCK_DGRAM (Type) - Indicates UDP Connection - UDP does not require the source and destination to establish a three-way handshake before transmission takes place. Additionally, there is no need for an end-to-end connection

0 (Protocol) - Specifies a particular protocol to be used with the socket. Specifying a protocol of 0 causes socket() to use an unspecified default protocol appropriate for the requested socket type.

Server Address Information

The bzero() function erases the data in the n bytes of the memory starting at the location pointed to, by writing zeros (bytes containing '\0') to that area.

We bind the server_addr to the internet address and port number thus giving our socket an identity with an address and port where it can listen for connections

htons - The htons() function translates a short integer from host byte order to network byte order

htonl - The htonl() function translates a long integer from host byte order to network byte order

These functions are necessary so that the binding of address and port takes place with data in the correct format

This binds the socket descriptor to the server thus enabling the server to listen for connections and communicate with other clients

If binding is unsuccessful

Communication between client and server

The bzero() function erases the data in the n bytes of the memory starting at the location pointed to, by writing zeros (bytes containing '\0') to that area. The variables are emptied and then ready for use

The server receives data from the client which is a command. It then executes the command.

The client then receives a response from the server when the command has been executed

The server and client can communicate indefinitely till one of them exits the connection

The client sends the server a command which it executes thus showing remote command execution using UDP

Close socket

57{
58 /** Variable Declarations */
59 uint32_t
60 sockfd; ///< socket descriptors - Like file handles but for sockets
61 char recv_msg[1024],
62 success_message[] =
63 "Command Executed Successfully!\n"; ///< character arrays to read
64 /// and store string data
65 /// for communication & Success
66 /// message
67
68 struct sockaddr_in server_addr,
69 client_addr; ///< basic structures for all syscalls and functions that
70 /// deal with internet addresses. Structures for handling
71 /// internet addresses
72 socklen_t clientLength = sizeof(client_addr); /// size of address
73
74 /**
75 * The UDP socket is created using the socket function.
76 *
77 * AF_INET (Family) - it is an address family that is used to designate the
78 * type of addresses that your socket can communicate with
79 *
80 * SOCK_DGRAM (Type) - Indicates UDP Connection - UDP does not require the
81 * source and destination to establish a three-way handshake before
82 * transmission takes place. Additionally, there is no need for an
83 * end-to-end connection
84 *
85 * 0 (Protocol) - Specifies a particular protocol to be used with the
86 * socket. Specifying a protocol of 0 causes socket() to use an unspecified
87 * default protocol appropriate for the requested socket type.
88 */
89 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
90 {
91 error();
92 }
93
94 /**
95 * Server Address Information
96 *
97 * The bzero() function erases the data in the n bytes of the memory
98 * starting at the location pointed to, by writing zeros (bytes
99 * containing '\0') to that area.
100 *
101 * We bind the server_addr to the internet address and port number thus
102 * giving our socket an identity with an address and port where it can
103 * listen for connections
104 *
105 * htons - The htons() function translates a short integer from host byte
106 * order to network byte order
107 *
108 * htonl - The htonl() function translates a long integer from host byte
109 * order to network byte order
110 *
111 * These functions are necessary so that the binding of address and port
112 * takes place with data in the correct format
113 */
114 bzero(&server_addr, sizeof(server_addr));
115 server_addr.sin_family = AF_INET;
116 server_addr.sin_port = htons(PORT);
117 server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
118
119 /**
120 * This binds the socket descriptor to the server thus enabling the server
121 * to listen for connections and communicate with other clients
122 */
123 if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
124 {
125 error(); /// If binding is unsuccessful
126 }
127
128 printf("Server is Connected Successfully...\n");
129
130 /**
131 * Communication between client and server
132 *
133 * The bzero() function erases the data in the n bytes of the memory
134 * starting at the location pointed to, by writing zeros (bytes
135 * containing '\0') to that area. The variables are emptied and then
136 * ready for use
137 *
138 * The server receives data from the client which is a command. It then
139 * executes the command.
140 *
141 * The client then receives a response from the server when the
142 * command has been executed
143 *
144 * The server and client can communicate indefinitely till one of them
145 * exits the connection
146 *
147 * The client sends the server a command which it executes thus showing
148 * remote command execution using UDP
149 */
150 while (1)
151 {
152 bzero(recv_msg, sizeof(recv_msg));
153 recvfrom(sockfd, recv_msg, sizeof(recv_msg), 0,
154 (struct sockaddr *)&client_addr, &clientLength);
155 printf("Command Output: \n");
156 system(recv_msg);
157 printf("Command Executed\n");
158 sendto(sockfd, success_message, sizeof(success_message), 0,
159 (struct sockaddr *)&client_addr, clientLength);
160 }
161
162 /// Close socket
163 close(sockfd);
164 printf("Server is offline...\n");
165 return 0;
166}
#define PORT
For the type in_addr_t and in_port_t For structures returned by the network database library - format...
Definition remote_command_exec_udp_server.c:38
void error()
Utility function used to print an error message to stderr.
Definition remote_command_exec_udp_server.c:46
Here is the call graph for this function: