/* TCP/IP UNAPI implementations control program By Konamiman 5/2010 Compilation command line: sdcc --code-loc 0x170 --data-loc 0 -mz80 --disable-warning 196 --no-std-crt0 crt0_msxdos_advanced.rel msxchar.rel asm.lib tcpip.c hex2bin -e com tcpip.ihx ASM.LIB, MSXCHAR.REL and crt0msx_msxdos_advanced.rel are available at www.konamiman.com (You don't need MSXCHAR.LIB if you manage to put proper PUTCHAR.REL, GETCHAR.REL and PRINTF.REL in the standard Z80.LIB... I couldn't manage to do it, I get a "Library not created with SDCCLIB" error) Comments are welcome: konamiman@konamiman.com */ #include #include #include #include #include "asm.h" #define _TERM0 0 enum TcpipUnapiFunctions { UNAPI_GET_INFO = 0, TCPIP_GET_CAPAB = 1, TCPIP_GET_IPINFO = 2, TCPIP_NET_STATE = 3, TCPIP_DNS_Q = 6, TCPIP_CONFIG_AUTOIP = 25, TCPIP_CONFIG_IP = 26, TCPIP_CONFIG_TTL = 27, TCPIP_CONFIG_PING = 28, }; enum TcpipErrorCodes { ERR_OK, ERR_NOT_IMP, ERR_NO_NETWORK, ERR_NO_DATA, ERR_INV_PARAM, ERR_QUERY_EXISTS, ERR_INV_IP, ERR_NO_DNS, ERR_DNS, ERR_NO_FREE_CONN, ERR_CONN_EXISTS, ERR_NO_CONN, ERR_CONN_STATE, ERR_BUFFER, ERR_LARGE_DGRAM, ERR_INV_OPER }; enum IpAddresses { IP_LOCAL = 1, IP_REMOTE = 2, IP_MASK = 3, IP_GATEWAY = 4, IP_DNS1 = 5, IP_DNS2 = 6 }; const char* strPresentation= "TCP/IP UNAPI control program 1.0\r\n" "By Konamiman, 5/2010\r\n" "\r\n"; const char* strUsage= "Usage: tcpip f | s\r\n" " tcpip ip [l
] [r
] [m
] [g
]\r\n" " [p
] [s
] [a(0|1|2|3)]]\r\n" " tcpip set [p(0|1)] [ttl] [tos]\r\n" "\r\n" "f: Show implementation capabilities and features\r\n" "s: Show current configuration and status information\r\n" "ip l/r/m/g/p/s: Change the IP address (local, remote, subnet mask,\r\n" " primary DNS server, secondary DNS server)\r\n" "ip a: Configure IP addresses to retrieve automatically\r\n" " (0=none, 1=local+subnet+gateway, 2=DNS servers, 3=all)\r\n" "set p: Enable (1) or disable (0) reply to incoming PINGs\r\n" "set ttl: Set TTL for outgoing deatagrams (0-255)\r\n" "set tos: Set ToS for outgoing deatagrams (0-255)\r\n"; const char* strMissingParams = "Missing parameters"; const char* strInvParam = "Invalid parameter"; const char* strYES="YES"; const char* strNO="NO"; const char* strON="ON"; const char* strOFF="OFF"; Z80_registers regs; int i; int param; unapi_code_block codeBlock; byte mustChangeIP[6]; byte newIP[6*4]; byte mustChangeAutoIP = 0; byte newAutoIP; byte mustChangeTTL = 0; byte newTTL; byte mustChangeTOS = 0; byte newTOS; byte mustChangePingReply = 0; byte newPingReply; uint specVersion; // I know, these should be on a tcpip.h void PrintUsageAndEnd(); void PrintImplementationName(); void DoShowInfo(); void Terminate(char* errorMessage); int strcmpi(const char *a1, const char *a2); int strncmpi(const char *a1, const char *a2, unsigned size); void DoShowFeatures(); void DoShowStatus(); void ParseFlagParam(char* param, byte* flagAddress, byte* flagValue); void ParseIP(char* IP, byte* flagAddress, byte* ipAddress); void ParseAutoIP(char* param, byte* flagAddress, byte* flagValue); void ParseByteParam(char* param, byte* flagAddress, byte* valueAddress); void DoChangeIP(byte IPindex, byte* IPvalue); void DoChangeAutoIP(byte autoIP); void DoChangePingReply(byte newPingReply); void DoChangeTtlTos(byte mustChangeTTL, byte newTTL, byte mustChangeTOS, byte newTOS); void print(char* str); #define print(x) printf(x) /********************** *** MAIN is here *** **********************/ int main(char** argv, int argc) { memset(mustChangeIP, 0, 6); print(strPresentation); if(argc==0) { PrintUsageAndEnd(); } i = UnapiGetCount("TCP/IP"); if(i==0) { print("*** No TCP/IP UNAPI implementations found"); return 0; } UnapiBuildCodeBlock(NULL, 1, &codeBlock); if(strcmpi(argv[0], "f")==0) { PrintImplementationName(); DoShowFeatures(); Terminate(NULL); } else if(strcmpi(argv[0], "s")==0) { PrintImplementationName(); DoShowStatus(); Terminate(NULL); } else if(strcmpi(argv[0], "ip")==0) { if(argc == 1) { Terminate(strMissingParams); } for(param=1; param 0) && (c1=*a1) | (c2=*a2)) { if (!c1 || !c2 || /* Unneccesary? */ (islower(c1) ? toupper(c1) : c1) != (islower(c2) ? toupper(c2) : c2)) return (c1 - c2); a1++; a2++; size--; } return 0; } void PrintYesNoBit(char* message, uint flags, int bitIndex) { printf("%s: %s\r\n", message, (flags & (1<= '0' && param[0] <= '3') { *flagValue = (byte)param[0] - (byte)'0'; } else { Terminate(strInvParam); } } void DoChangeIP(byte IPindex, byte* IPvalue) { regs.Bytes.B = IPindex; regs.Words.HL = *((int*)IPvalue); regs.Words.DE = *(((int*)IPvalue)+1); UnapiCall(&codeBlock, TCPIP_CONFIG_IP, ®s, REGS_MAIN, REGS_MAIN); } void DoChangeAutoIP(byte autoIP) { regs.Bytes.B = 1; regs.Bytes.C = autoIP; UnapiCall(&codeBlock, TCPIP_CONFIG_AUTOIP, ®s, REGS_MAIN, REGS_MAIN); } void ParseByteParam(char* param, byte* flagAddress, byte* valueAddress) { int value; *flagAddress = 1; value = atoi(param); if(value>255 || (*valueAddress == 0 && (param[0]!='0' || param[1]!='\0'))) { Terminate(strInvParam); } *valueAddress = (byte)value; } void DoChangePingReply(byte newPingReply) { regs.Bytes.B = 1; regs.Bytes.C = newPingReply; UnapiCall(&codeBlock, TCPIP_CONFIG_PING, ®s, REGS_MAIN, REGS_MAIN); } void DoChangeTtlTos(byte mustChangeTTL, byte newTTL, byte mustChangeTOS, byte newTOS) { regs.Bytes.B = 0; UnapiCall(&codeBlock, TCPIP_CONFIG_TTL, ®s, REGS_MAIN, REGS_MAIN); if(mustChangeTTL) { regs.Bytes.D = newTTL; } if(mustChangeTOS) { regs.Bytes.E = newTOS; } regs.Bytes.B = 1; UnapiCall(&codeBlock, TCPIP_CONFIG_TTL, ®s, REGS_MAIN, REGS_MAIN); }