Skip to content
Permalink
758f42268a
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
266 lines (220 sloc) 6.88 KB
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
/***********************************************************/
// The original, uncompressed Raritan jar file as char Array
// unsigned char raritan_uncompressed_jar[]
#include "raritan_uncompressed.h"
/***********************************************************/
#define CR "\n"
#define IP_START 0xC7120
#define PORT_ID_START 0xC71A1
#define HEADLINE_START 0x10342E
#define NUMBER_OF_KVMS 19
#define IP_LENGTH 13
#define PORT_ID_LENGTH 16
#define HEADLINE_LENGTH 24
int verbose = 0;
void printhelp()
{
fprintf(stderr, "usage: ./Patcher -k kvmnumber [-v] [-h]" CR
" -h displays this help and exit" CR
" -k takes the kvm-number as argument" CR
" -v explain what is being done" CR
CR
"For more details visit: http://twiki.molgen.mpg.de/foswiki/Medv/RaritanDominion" CR
CR
"(C) Niclas Hofmann" CR
"Bugs to niclas@molgen.mpg.de" CR
);
}
// The spaces are REQUIRED!!!
const char newIP[NUMBER_OF_KVMS][IP_LENGTH+1] = {
"141.14.31.85 ", "141.14.31.86 ", "141.14.31.87 ", "141.14.31.88 ",
"141.14.31.89 ", "141.14.31.90 ", "141.14.18.128", "141.14.18.129",
"141.14.18.130", "141.14.17.155", "141.14.17.157", "141.14.17.158",
"141.14.17.159", "141.14.17.160", "141.14.17.162", "141.14.17.163",
"141.14.17.152", "141.14.17.153", "141.14.17.154"
};
const char newPortID[NUMBER_OF_KVMS][PORT_ID_LENGTH+1] = {
"", "P_000d5d068313_0", "P_000d5d069245_0", "P_000d5d06830f_0",
"P_000d5d06830e_0", "P_000d5d069247_0", "P_000d5d0693a6_0", "P_000d5d067089_0",
"P_000d5d0693aa_0", "P_000d5d0af2f1_0", "P_000d5d0af2ea_0", "P_000d5d0af2ed_0",
"P_000d5d0af2eb_0", "P_000d5d0af2ef_0", "P_000d5d0af2ee_0", "P_000d5d0af2e9_0",
"P_000d5d0af2ec_0", "P_000d5d0af2e7_0", "P_000d5d0af2f0_0"
};
void get_options(int* kvmnumber, int argc, char** argv)
{
int option = 0;
int kflag = 0; // To make sure just one kvm is chosen
char *kvmnumberAsString = NULL;
while((option = getopt(argc, argv, "vhk:")) != -1){
switch(option){
case 'h':
printhelp();
exit(EXIT_SUCCESS);
case 'k':
if (kflag == 1){
fprintf(stderr, "ERROR: -k is set multiple times\n");
printhelp();
exit(EXIT_FAILURE);
}
kflag = 1;
kvmnumberAsString = optarg;
*kvmnumber = atoi(kvmnumberAsString)-1;
if (*kvmnumber < 0 || *kvmnumber >= NUMBER_OF_KVMS){
fprintf(stderr, "ERROR: no possible kvm\n");
printhelp();
exit(EXIT_FAILURE);
}
break;
case 'v':
verbose = 1;
break;
default:
printf("ERROR: no possible option\n");
printhelp();
exit(EXIT_FAILURE);
}
}
// Checking if a KVM chosen?
if (kflag == 0){
fprintf(stderr, "ERROR: no kvm chosen\n");
printhelp();
exit(EXIT_FAILURE);
}
return;
}
void change_headline(int kvmnumber)
{
char *newheadline = NULL;
unsigned char *destinationInJar = NULL;
// Chance the Headline
if ((newheadline = calloc(HEADLINE_LENGTH+1, sizeof(char))) == NULL){
fprintf(stderr, "ERROR: Could not allocate enough memory for"
"the new headline\n");
exit(EXIT_FAILURE);
}
snprintf(newheadline, HEADLINE_LENGTH+1, "%s%2d%-19s", "KVM ", kvmnumber+1, " in use");
destinationInJar = &raritan_uncompressed_jar[HEADLINE_START];
memcpy(destinationInJar, newheadline, HEADLINE_LENGTH);
if (verbose){
printf("Headline changed.\n");
}
free(newheadline);
return;
}
void change_ip(int kvmnumber)
{
int i = 0;
// Print old IP
if (verbose){
for(i = 0; i < IP_LENGTH; i++)
printf("%c", raritan_uncompressed_jar[IP_START+i]);
printf(" --> ");
}
// Change IP
for(i = 0; i < IP_LENGTH; i++)
raritan_uncompressed_jar[IP_START + i] = newIP[kvmnumber][i];
// Print new IP
if (verbose){
for(i = 0; i < IP_LENGTH; i++)
printf("%c", raritan_uncompressed_jar[IP_START+i]);
printf(".\n");
}
return;
}
void change_portid(int kvmnumber)
{
int i = 0;
// Print old PORT_ID
if (verbose){
for(i = 0; i < PORT_ID_LENGTH; i++)
printf("%c", raritan_uncompressed_jar[PORT_ID_START+i]);
printf(" --> ");
}
// Change PORT_ID
for(i = 0; i < PORT_ID_LENGTH; i++)
raritan_uncompressed_jar[PORT_ID_START + i] = newPortID[kvmnumber][i];
// Print new PORT_ID
if (verbose){
for(i = 0; i < PORT_ID_LENGTH; i++)
printf("%c", raritan_uncompressed_jar[PORT_ID_START+i]);
printf(".\n");
}
return;
}
FILE* create_tmpfile(void)
{
FILE* temp = NULL;
if ((temp = tmpfile()) == NULL){
perror("tmpfile()");
exit(EXIT_FAILURE);
}
else{
if (verbose){
printf("Tempfile has been created.\n");
}
}
return temp;
}
void write_to_file(FILE* file)
{
int fd = fileno(file);
if (write(fd, raritan_uncompressed_jar, sizeof(raritan_uncompressed_jar)) == -1){
perror("write()");
exit(EXIT_FAILURE);
}
else{
if (verbose){
printf("raritan_uncompressed_jar is written to the tempfile.\n");
}
}
return;
}
void execute_raritan(FILE* file)
{
char *exec = NULL;
char *java_exe = "java -jar /proc/self/fd/";
int fd = fileno(file);
int fd_cp = fd;
int digits = 1;
int size = 0;
while(fd_cp >= 10){
digits++;
fd_cp /= 10;
}
size = strlen(java_exe)+digits+1;
if ((exec = calloc(size, sizeof(char))) == NULL){
fprintf(stderr, "ERROR: Could not allocate enough memory for"
"the java command\n");
exit(EXIT_FAILURE);
}
snprintf(exec, size, "%s%d", java_exe, fd);
// Execute the temporary file
if (system(exec) == -1){
fprintf(stderr, "ERROR: Could not creat child process to execute: %s\n", exec);
exit(EXIT_FAILURE);
}
free(exec);
return;
}
int main(int argc, char **argv)
{
int kvmnumber = 0;
FILE *temp_fp = NULL;
get_options(&kvmnumber, argc, argv);
change_headline(kvmnumber);
change_ip(kvmnumber);
change_portid(kvmnumber);
temp_fp = create_tmpfile();
write_to_file(temp_fp);
execute_raritan(temp_fp);
fclose(temp_fp);
return EXIT_SUCCESS;
}