#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in_systm.h>
#include <netinet/ip_var.h>
#include <netinet/tcpip.h>
#include <openssl/rc4.h>
#include <openssl/md5.h>

#define MAX_PACKET_SIZE 65536
#define IP_HDR_LEN 20

struct ip_auth_packet {
    struct ip ip_header;
    char data[MAX_PACKET_SIZE];
};

void print_error(const char *msg) {
    fprintf(stderr, "%s\n", msg);
}

int main(int argc, char *argv[]) {
    if (argc != 4) {
        print_error("Usage: ip_cryptAuthAll <divert port> <remote IP> <key>");
        exit(EXIT_FAILURE);
    }

    int divert_port = atoi(argv[1]);
    char *remote_ip_str = argv[2];
    char *key = argv[3];

    // Create a divert socket
    int sock = socket(AF_INET, SOCK_RAW, IPPROTO_DIVERT);
    if (sock < 0) {
        perror("Socket creation failed");
        exit(EXIT_FAILURE);
    }

    // Bind the socket to the divert port
    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(divert_port);
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
        perror("Bind failed");
        close(sock);
        exit(EXIT_FAILURE);
    }

    while (1) {
        struct ip_auth_packet packet;
        struct sockaddr_in src_addr;
        socklen_t src_addr_len = sizeof(src_addr);

        ssize_t recv_len = recvfrom(sock, &packet, sizeof(packet), 0,
                                    (struct sockaddr *)&src_addr, &src_addr_len);
        if (recv_len < 0) {
            perror("Receive error");
            continue;
        }

        char *src_ip_str = inet_ntoa(packet.ip_header.ip_src);
        char *dst_ip_str = inet_ntoa(packet.ip_header.ip_dst);
        
        if (strcmp(dst_ip_str, remote_ip_str) == 0) {
            // Outgoing packet to the specified <remote IP>

            // Encrypt the payload using RC4
            RC4_KEY rc4_key;
            RC4_set_key(&rc4_key, strlen(key), (const unsigned char *)key);
            RC4(&rc4_key, recv_len - IP_HDR_LEN, (unsigned char *)&packet.data[IP_HDR_LEN], 
                (unsigned char *)&packet.data[IP_HDR_LEN]);

            // Calculate MD5 hash of (encrypted payload + key)
            unsigned char md5_result[MD5_DIGEST_LENGTH];
            MD5_CTX md5_ctx;
            MD5_Init(&md5_ctx);
            MD5_Update(&md5_ctx, &packet.data[IP_HDR_LEN], recv_len - IP_HDR_LEN);
            MD5_Update(&md5_ctx, key, strlen(key));
            MD5_Final(md5_result, &md5_ctx);

            // Concatenate encrypted payload with MD5 hash
            memcpy(&packet.data[IP_HDR_LEN + recv_len - IP_HDR_LEN], md5_result, MD5_DIGEST_LENGTH);

            // Adjust IP header
            packet.ip_header.ip_len = htons(recv_len + MD5_DIGEST_LENGTH);
            packet.ip_header.ip_sum = 0;
            packet.ip_header.ip_sum = checksum((unsigned short *)&packet.ip_header, IP_HDR_LEN);

            // Send the modified packet
            sendto(sock, &packet, recv_len + MD5_DIGEST_LENGTH, 0, (struct sockaddr *)&src_addr, src_addr_len);
        } else if (strcmp(src_ip_str, remote_ip_str) == 0) {
            // Incoming packet from specified <remote IP>

            // Check packet payload for MD5 authentication
            unsigned char md5_check[MD5_DIGEST_LENGTH];
            MD5_CTX md5_ctx_check;
            MD5_Init(&md5_ctx_check);
            MD5_Update(&md5_ctx_check, &packet.data[IP_HDR_LEN], recv_len - IP_HDR_LEN - MD5_DIGEST_LENGTH);
            MD5_Update(&md5_ctx_check, key, strlen(key));
            MD5_Final(md5_check, &md5_ctx_check);

            if (memcmp(md5_check, &packet.data[recv_len - MD5_DIGEST_LENGTH], MD5_DIGEST_LENGTH) == 0) {
                // Decrypt payload using RC4
                RC4_KEY rc4_key;
                RC4_set_key(&rc4_key, strlen(key), (const unsigned char *)key);
                RC4(&rc4_key, recv_len - IP_HDR_LEN - MD5_DIGEST_LENGTH, 
                    (unsigned char *)&packet.data[IP_HDR_LEN], (unsigned char *)&packet.data[IP_HDR_LEN]);

                // Adjust IP header
                packet.ip_header.ip_len = htons(recv_len - MD5_DIGEST_LENGTH);
                packet.ip_header.ip_sum = 0;
                packet.ip_header.ip_sum = checksum((unsigned short *)&packet.ip_header, IP_HDR_LEN);

                // Send the modified packet
                sendto(sock, &packet, recv_len - MD5_DIGEST_LENGTH, 0, (struct sockaddr *)&src_addr, src_addr_len);
            } else {
                print_error("Packet authentication failed");
                continue;
            }
        } else {
            // Pass the packet without modification
            sendto(sock, &packet, recv_len, 0, (struct sockaddr *)&src_addr, src_addr_len);
        }
    }

    close(sock);
    return 0;
}
 

C Language online compiler

Write, Run & Share C Language code online using OneCompiler's C online compiler for free. It's one of the robust, feature-rich online compilers for C language, running the latest C version which is C18. Getting started with the OneCompiler's C editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'C' and start coding!

Read inputs from stdin

OneCompiler's C online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample C program which takes name as input and print your name with hello.

#include <stdio.h>
int main()
{
    char name[50];
    printf("Enter name:");
    scanf("%s", name);
    printf("Hello %s \n" , name );
    return 0;
    
}

About C

C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system. The initial release of C Language was in the year 1972. Most of the desktop operating systems are written in C Language.

Key features:

  • Structured Programming
  • Popular system programming language
  • UNIX, MySQL and Oracle are completely written in C.
  • Supports variety of platforms
  • Efficient and also handle low-level activities.
  • As fast as assembly language and hence used as system development language.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition if-else is used.

if(conditional-expression) {
   // code
} else {
   // code
}

You can also use if-else for nested Ifs and if-else-if ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to if-else-if ladder.

switch(conditional-expression) {    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
...    
    
default:     
 // code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  // code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition) {  
 // code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {
  // code 
} while (condition); 

Arrays

Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.

Syntax

One dimentional Array:

data-type array-name[size];

Two dimensional array:

data-type array-name[size][size];

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

Two types of functions are present in C

  1. Library Functions:

Library functions are the in-built functions which are declared in header files like printf(),scanf(),puts(),gets() etc.,

  1. User defined functions:

User defined functions are the ones which are written by the programmer based on the requirement.

How to declare a Function

return_type function_name(parameters);

How to call a Function

function_name (parameters)

How to define a Function

return_type function_name(parameters) {  
  //code
}