/* * getnet.c * * get network info (ip,mask,gw,dns) from user * Author: Jimmy Devenport (jimmyd@lanl.gov) * Last modified: Thu Feb 21 2008 14:28 * */ #include #include #include #include #include "version.h" #include /* Provided by libutil */ #include #define HNLEN 58 #define INVALID 0 #define VALID 1 #define IPADDRWIDTH 16 /* Global vars */ char hostName[HNLEN]; char ip[IPADDRWIDTH]="128.165."; char mask[IPADDRWIDTH]="255.255.255.0"; char gw[IPADDRWIDTH]=""; char dns1[IPADDRWIDTH]="128.165.4.4"; char dns2[IPADDRWIDTH]="128.165.11.88"; char advancedOptions[HNLEN]=""; // ie nicdelay=0 ksdevice=link + passThru stuff int inStep=1; int dhcp=0; // no dhcp (static) = 0, yes (dhcp) = 1 int numSteps; char *kernel_name=""; char cmdline[1024]=""; char *initrd=""; char passThru[1024]=""; char *dtext="Static "; char *netColor="Yellow"; char isolinuxver[512]=""; static void doStep(); static void doDone(); static void doAdv(); static void doDisplay(); static void doErrorCheck(); static void doGetHN(); static void doGetNetType(); static void doGet(char *); static void wait_key(void); static void clear_screen(void) { fputs("e%@)0(B\1#0[?25l", stdout); } static void doDisplay() { int cursorY=3,cursorX=2; if (inStep != 2) { printf("[%d;%dH", cursorY, cursorX); printf("[S]tatic or [D]ynamic IP: %s",dtext); } if (dhcp) { cursorY=3; int j; int k; for (j=cursorY+1;j<9;j++) { for (k=2;k numSteps) inStep=1; switch (inStep) { case 1: doGetHN(); break; case 2: doGetNetType(); break; case 3: doGet("IP Address"); break; case 4: doGet("Netmask"); break; case 5: doGet("Gateway"); break; case 6: doGet("DNS1"); break; case 7: doGet("DNS2"); break; case 8: doAdv(); break; case 9: doDone(); break; } } static void doAdv() { int cursorX=2,cursorY=9; int done=0; int key; char *p; int sp; printf("[%d;%dHExamples: nicdelay=50 ksdevice=eth0",20,cursorX); printf("[%d;%dH", cursorY, cursorX); printf("Advanced Options: "); printf("[%d;%dH",cursorY,20); // see doGet for explanation of "20" p=advancedOptions; // fill in field to show it for (sp=0; sp advancedOptions ) { printf("\b \b"); p--; } break; case KEY_CTRL('U'): while ( p > advancedOptions ) { printf("\b \b"); p--; } break; case KEY_ESC: syslinux_run_command("isolinux.bin"); break; default: if ( ( (p - advancedOptions) < HNLEN-1 ) && ( ( key >= 0x30 && key <= 0x39 ) || // 0 .. 9 ( key >= 0x61 && key <= 0x7a ) || // a .. z ( key >= 0x41 && key <= 0x5a ) || // A .. Z key == 0x2e || // . key == 0x20 || // space key == 0x3d || // = key == 0x2d // - ) ) { // 0..9(0x30-39), a..z(0x61-7a), '-'(0x2d) and '.'(0x2e) *p++ = key; putchar(key); } break; } } *p = '\0'; p=advancedOptions; printf("[%d;%dH",cursorY,20); int x; for (x=0;x 255) return INVALID; if (strspn(ipadd, "0123456789.") < strlen(ipadd)) return INVALID; return VALID; } void printErrorMessage(char *msg,int step) { printf("[%d;%dHERROR: %s!\n\n Press any key to continue...", 20, 2,msg); wait_key(); int z; printf("[%d;%dH", 20, 2); for (z=0;z<79;z++) printf(" "); printf("[%d;%dH", 22, 2); for (z=0;z<79;z++) printf(" "); inStep=step; doStep(); } static void checkIP(char *ip2check, char *atype, int step) { int isvalid=0; char my_msg[64]=""; isvalid=validateIP(ip2check); if (!isvalid) { sprintf(my_msg,"%s is NOT a valid %s",ip2check,atype); printErrorMessage(my_msg,step); } } static void doErrorCheck() { if (strcmp(hostName,"") == 0) { printErrorMessage("Invalid Hostname",1); } if(!dhcp) { checkIP(ip,"IP Address",3); checkIP(mask,"Netmask",4); checkIP(gw,"Gateway",5); checkIP(dns1,"DNS1",6); checkIP(dns2,"DNS2",7); } } static void doDone() { int key; int done=0; int cursorX=2,cursorY=12; int nextStep=inStep+1, prevStep=inStep-1; if (dhcp) { nextStep=inStep+1; prevStep=8; } printf("\n"); printf(""); printf("[%d;%dH", cursorY, cursorX); printf(" NEXT "); printf(""); while (!done) { key = get_key(stdin,0); switch(key) { case KEY_CTRL('X'): printf("\n"); exit(0); case KEY_UP: case (0x00): // SHIFT_TAB inStep=prevStep; done=1; break; case KEY_DOWN: case KEY_TAB: inStep=nextStep; done=1; break; case KEY_ENTER: case KEY_CTRL('J'): inStep=-99; done=1; break; case KEY_ESC: syslinux_run_command("isolinux.bin"); break; } } if (inStep == -99) { // create cmdline doErrorCheck(); clear_screen(); printf("%s\n",passThru); printf("%s",isolinuxver); printf(" \n\n\n\n\n\n"); char ksURL[128]="ks=http://"; char ksServer[32]="rhus.lanl.gov"; if (strcmp(netColor,"Green ")==0) strcpy(ksServer,"rhusproxy.lanl.gov"); strcat(ksURL,ksServer); strcat(ksURL,"/cgi-bin/ks?hn="); strcat(ksURL,hostName); if (strstr(passThru,"rescue ")!=0) ksURL[0]='\0'; if (!dhcp) { sprintf(cmdline,"%s %s %s %s ip=%s netmask=%s gateway=%s dns=%s,%s %s", kernel_name,initrd,passThru,ksURL,ip,mask,gw,dns1,dns2,advancedOptions); } else { sprintf(cmdline,"%s %s %s %s %s", kernel_name,initrd,passThru,ksURL,advancedOptions); } /*printf(" \n%s\n",cmdline);*/ printf(" %s\n",cmdline); if (strstr(advancedOptions,"debug=1")!=0) { printf("\nLength of cmdline: %d",strlen(cmdline)); printf("\nPress any key to continue..."); wait_key(); } printf("\n"); // to load kernel - see ../modules/linux.c, ./localboot.c syslinux_run_command(cmdline); exit(0); } else { printf("[%d;%dH", cursorY, cursorX); printf(" NEXT "); printf(""); doStep(); } } /* Wait for a keypress */ static void wait_key(void) { char ch; while (fread(&ch, 1, 1, stdin) == 0) ; } static int get_ip_addr(char *addrtype, int thisY, int thisX) { int key; int done; char ip_addr[IPADDRWIDTH]=""; char *p; int sp; p=0; // strcpy(s1,s2) copies s2 into s1 if (addrtype == "IP Address") strcpy(ip_addr,ip); if (addrtype == "Netmask") strcpy(ip_addr,mask); if (addrtype == "Gateway") { if (strlen(gw) == 0) strcpy(ip_addr,ip); else strcpy(ip_addr,gw); } if (addrtype == "DNS1") strcpy(ip_addr,dns1); if (addrtype == "DNS2") strcpy(ip_addr,dns2); done = 0; if (addrtype == "Gateway" && (strcmp(ip_addr,ip) == 0)) { char newgw[IPADDRWIDTH]=""; char temp[IPADDRWIDTH]; strcpy(temp,ip_addr); char *sub_str; sub_str = strtok(temp,"."); strcat(newgw,sub_str); strcat(newgw,"."); for (sp=0; sp < 2; sp++) { sub_str=strtok(NULL,"."); strcat(newgw,sub_str); strcat(newgw,"."); } strcat(newgw,"241"); strcpy(ip_addr,newgw); } p=ip_addr; printf("[%d;%dH", thisY, thisX); // fill in field to show it for (sp=0; sp ip_addr ) { printf("\b \b"); p--; } break; case KEY_CTRL('U'): while ( p > ip_addr ) { printf("\b \b"); p--; } break; case KEY_ESC: syslinux_run_command("isolinux.bin"); break; default: if ( ( (p - ip_addr) < IPADDRWIDTH-1 ) && ( ( key >= 0x30 && key <= 0x39 ) || key == 0x2e ) ) { // 0..9 and '.' *p++ = key; putchar(key); } break; } } *p='\0'; // strcpy(s1,s2) copies s2 into s1 if (addrtype == "IP Address") { strcpy(ip,ip_addr); } if (addrtype == "Netmask") { strcpy(mask,ip_addr); } if (addrtype == "Gateway") { strcpy(gw,ip_addr); } if (addrtype == "DNS1") { strcpy(dns1,ip_addr); } if (addrtype == "DNS2") { strcpy(dns2,ip_addr); } return key; } static void doGet(char *addrtype) { int isValidIP=0,thisY=3,thisX=20; //2 (margin) + 8 (Enter : )+10(IP Address) char *unip; int key; unip=0; while (!isValidIP) { unip=0; thisX=20; if (addrtype == "IP Address") { thisY=4; } if (addrtype == "Netmask") { thisY=5; } if (addrtype == "Gateway") { thisY=6; } if (addrtype == "DNS1") { thisY=7; } if (addrtype == "DNS2") { thisY=8; } printf("[%d;%dH", thisY, 2); printf("Enter %s: ",addrtype); fflush(stdout); key=get_ip_addr(addrtype,thisY,thisX); if (addrtype == "IP Address") { unip=ip; } if (addrtype == "Netmask") { unip=mask; } if (addrtype == "Gateway") { unip=gw; } if (addrtype == "DNS1") { unip=dns1; } if (addrtype == "DNS2") { unip=dns2; } isValidIP=validateIP(unip); if (!isValidIP) { printf(""); printf("[%d;%dH", 20, 2); printf("ERROR: %s is NOT a valid %s!\n\n Press any key to continue...", unip, addrtype); wait_key(); int z; printf("[%d;%dH", 20, 2); for (z=0;z<79;z++) printf(" "); printf("[%d;%dH", 22, 2); for (z=0;z<79;z++) printf(" "); } else { int x; printf("[%d;%dH",thisY,20); for (x=0;x hostName ) { printf("\b \b"); p--; } break; case KEY_CTRL('U'): while ( p > hostName ) { printf("\b \b"); p--; } break; case KEY_ESC: syslinux_run_command("isolinux.bin"); break; default: if ( ( (p - hostName) < HNLEN-1 ) && ( ( key >= 0x30 && key <= 0x39 ) || ( key >= 0x61 && key <= 0x7a ) || key == 0x2e || key == 0x2d ) ) { // 0..9(0x30-39), a..z(0x61-7a), '-'(0x2d) and '.'(0x2e) *p++ = key; putchar(key); } break; } } *p = '\0'; p=hostName; printf("[%d;%dH",cursorY,20); int x; for (x=0;x 2) { for (i=3;i