⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 netutil.c

📁 Plasma IP Core 你可以利用这个组件在FPGA中设计MIPS结构的CPU
💻 C
📖 第 1 页 / 共 2 页
字号:
static void TftpCallback(IPSocket *socket){   unsigned char buf[512+4];   int bytes, blockNum, length;   bytes = IPRead(socket, buf, sizeof(buf));   if(bytes < 4 || buf[0])      return;   blockNum = (buf[2] << 8) | buf[3];   length = blockNum * 512 - 512 + bytes - 4;   //printf("TftpCallback(%d,%d)\n", buf[1], blockNum);   if(length > (int)socket->userData)   {      bytes -= length - (int)socket->userData;      length = (int)socket->userData;   }   if(buf[1] == 3) //DATA   {      memcpy((uint8*)socket->userPtr + blockNum * 512 - 512, buf+4, bytes-4);      buf[1] = 4; //ACK      IPWrite(socket, buf, 4);      if(bytes-4 < 512)      {         printf("Done %d bytes\n", length);         socket->userFunc(socket->userPtr, length);         IPClose(socket);      }   }}IPSocket *TftpTransfer(uint32 ip, char *filename, uint8 *buffer, int size,                       void (*callback)(uint8 *data, int bytes)){   IPSocket *socket;   uint8 buf[512+4];   int bytes;   socket = IPOpen(IP_MODE_UDP, ip, 69, TftpCallback);   socket->userPtr = buffer;   socket->userData = size;   socket->userFunc = callback;   buf[0] = 0;   buf[1] = 1; //read   strcpy((char*)buf+2, filename);   bytes = strlen(filename);   strcpy((char*)buf+bytes+3, "octet");   IPWrite(socket, buf, bytes+9);   return socket;}//******************* Telnet Server ************************static TelnetFunc_t *TelnetFuncList;static void TelnetServer(IPSocket *socket){   uint8 buf[512+4];   int bytes, i;   char *ptr;   static char command[80];   char command2[80];   bytes = IPRead(socket, buf, sizeof(buf)-1);   if(bytes == 0)   {      if(socket->userData)         return;      socket->userData = 1;      socket->timeout = 0;      socket->headerSend[47] |= 0x08; //PSH      buf[0] = 255; //IAC      buf[1] = 251; //WILL      buf[2] = 3;   //suppress go ahead      buf[3] = 255; //IAC      buf[4] = 251; //WILL      buf[5] = 1;   //echo      strcpy((char*)buf+6, "Welcome to Plasma.\r\n-> ");      IPWrite(socket, buf, 6+23);      IPWriteFlush(socket);      command[0] = 0;      return;   }   buf[bytes] = 0;   if(buf[0] == 255 || buf[0] == 27)      return;   if(buf[0] != 8)   {      IPWrite(socket, buf, bytes);      IPWriteFlush(socket);      strncat(command, (char*)buf, sizeof(command));   }   else   {      //backspace      bytes = strlen(command);      if(bytes)      {         command[bytes-1] = 0;         buf[0] = 8;         buf[1] = ' ';         buf[2] = 8;         IPWrite(socket, buf, 3);         IPWriteFlush(socket);      }   }   ptr = strstr(command, "\r");   if(ptr)   {      ptr[0] = 0;      buf[0] = 0;      if(strncmp(command, "help", 4) == 0)      {         sprintf((char*)buf, "Commands: help, exit");         for(i = 0; TelnetFuncList[i].name; ++i)         {            strcat((char*)buf, ", ");            strcat((char*)buf, TelnetFuncList[i].name);         }         strcat((char*)buf, "\r\n");      }      else if(strncmp(command, "exit", 4) == 0)         IPClose(socket);      else if(command[0])      {         strcpy(command2, command);         ptr = strstr(command2, " ");         if(ptr)            ptr[0] = 0;         for(i = 0; TelnetFuncList[i].name; ++i)         {            if(strcmp(command2, TelnetFuncList[i].name) == 0)            {               TelnetFuncList[i].func(socket, command);               break;            }         }         if(TelnetFuncList[i].name == NULL)            sprintf((char*)buf, "Unknown command (%s)\r\n", command);      }      strcat((char*)buf, "-> ");      IPPrintf(socket, (char*)buf);      command[0] = 0;   }}void TelnetInit(TelnetFunc_t *funcList){   IPSocket *socket;   TelnetFuncList = funcList;   socket = IPOpen(IP_MODE_TCP, 0, 23, TelnetServer);}//******************* Console ************************static uint8 myBuffer[1024*3], myString[80];IPSocket *socketTelnet;void TransferDone(uint8 *data, int bytes){   printf("TransferDone(0x%x, %d)\n", data, bytes);   data[bytes] = 0;   if(bytes > 500)      data[500] = 0;   printf("%s\n", data);}static void TelnetInfo(IPSocket *socket, char *command){   (void)command;   IPPrintf(socket, "Steve was here!\r\n");}static void TelnetMath(IPSocket *socket, char *command){   int v1, v2;   char buf[20], buf2[20];   (void)socket;   if(strlen(command) < 6)   {      IPPrintf(socket, "Usage: math <number> <operator> <value>\r\n");      return;   }   sscanf(command, "%s%d%s%d", buf, &v1, buf2, &v2);   if(buf2[0] == '+')      v1 += v2;   else if(buf2[0] == '-')      v1 -= v2;   else if(buf2[0] == '*')      v1 *= v2;   else if(buf2[0] == '/')   {      if(v2 != 0)         v1 /= v2;   }   sprintf((char*)myBuffer, "%d\r\n", v1);   IPPrintf(socket, (char*)myBuffer);}void PingCallback(IPSocket *socket){   IPSocket *socket2 = socket->userPtr;   //printf("Ping Reply\n");   IPClose(socket);   if(socket2)      IPPrintf(socket2, "Ping Reply\r\n");   else      printf("Ping Reply\n");}static void DnsResultCallback(IPSocket *socket, uint32 ip, void *arg){   char buf[80];   IPSocket *socketTelnet = arg;   IPSocket *socketPing;   (void)socket;   sprintf(buf,  "ip=%d.%d.%d.%d\r\n",       (uint8)(ip >> 24), (uint8)(ip >> 16), (uint8)(ip >> 8), (uint8)ip);   IPPrintf(socketTelnet, buf);   socketPing = IPOpen(IP_MODE_PING, ip, 0, PingCallback);   socketPing->userPtr = socketTelnet;   myBuffer[0] = 'A';   IPWrite(socketPing, myBuffer, 1);}static void TelnetPing(IPSocket *socket, char *command){   int ip0, ip1, ip2, ip3;   char buf[20];   IPSocket *socketPing;   if('0' <= command[5] && command[5] <= '9')   {      sscanf(command, "%s %d.%d.%d.%d", buf, &ip0, &ip1, &ip2, &ip3);      ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;      socketPing = IPOpen(IP_MODE_PING, ip0, 0, PingCallback);      socketPing->userPtr = socket;      myBuffer[0] = 'A';      IPWrite(socketPing, myBuffer, 1);      IPPrintf(socket, "Sent ping\r\n");   }   else   {      IPResolve(command+5, DnsResultCallback, socket);      IPPrintf(socket, "Sent DNS request\r\n");   }}void TelnetTransferDone(uint8 *data, int length){   data[length] = 0;   IPPrintf(socketTelnet, "Transfer Done\r\n");}static void TelnetFtp(IPSocket *socket, char *command){   char buf[40], user[40], passwd[40], name[80];   int ip0, ip1, ip2, ip3;   if(strlen(command) < 10)   {      IPPrintf(socket, "ftp #.#.#.# User Password File\r\n");      return;   }   sscanf(command, "%s %d.%d.%d.%d %s %s %s",       buf, &ip0, &ip1, &ip2, &ip3, user, passwd, name);   ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;   socketTelnet = socket;   FtpTransfer(ip0, user, passwd, name, myBuffer, sizeof(myBuffer)-1,       0, TelnetTransferDone);}static void TelnetTftp(IPSocket *socket, char *command){   char buf[80], name[80];   int ip0, ip1, ip2, ip3;   if(strlen(command) < 10)   {      IPPrintf(socket, "tftp #.#.#.# File\r\n");      return;   }   sscanf(command, "%s %d.%d.%d.%d %s %s %s",       buf, &ip0, &ip1, &ip2, &ip3, name);   ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;   socketTelnet = socket;   TftpTransfer(ip0, name, myBuffer, sizeof(myBuffer)-1, TelnetTransferDone);}static void TelnetHttpCallback(IPSocket *socket){   int length;   if(myString[0])      IPPrintf(socket, myString);   myString[0] = 0;   length = strlen(myBuffer);   length += IPRead(socket, myBuffer+length, sizeof(myBuffer)-length-1);   myBuffer[length] = 0;   if(length >= sizeof(myBuffer)-1 || socket->state > IP_TCP)   {      IPClose(socket);      IPPrintf(socketTelnet, "Done\r\n");   }}static void TelnetHttp(IPSocket *socket, char *command){   char buf[80], name[80];   int ip0, ip1, ip2, ip3;   IPSocket *socketHttp;   if(strlen(command) < 5)   {      IPPrintf(socket, "http #.#.#.# File\r\n");      return;   }   sscanf(command, "%s %d.%d.%d.%d %s %s %s",       buf, &ip0, &ip1, &ip2, &ip3, name);   ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;   socketTelnet = socket;   sprintf(myString, "GET %s HTTP/1.0\r\n\r\n", name);   myBuffer[0] = 0;   socketHttp = IPOpen(IP_MODE_TCP, ip0, 80, TelnetHttpCallback);}static void TelnetShow(IPSocket *socket, char *command){   int i;   (void)command;   // Insert '\r' before '\n'   for(i = 0; myBuffer[i] && i < sizeof(myBuffer); ++i)   {      if(myBuffer[i] == '\n' && myBuffer[i-1] != '\r')      {         memcpy(myBuffer+i+1, myBuffer+i, sizeof(myBuffer)-2-i);         myBuffer[i] = '\r';      }   }   myBuffer[sizeof(myBuffer)-1] = 0;   IPPrintf(socket, (char*)myBuffer);   IPPrintf(socket, "\r\n");}static void TelnetClear(IPSocket *socket, char *command){   (void)socket;   (void)command;   memset(myBuffer, 0, sizeof(myBuffer));}static TelnetFunc_t MyFuncs[] = {    "info", 0, TelnetInfo,   "math", 0, TelnetMath,   "ping", 0, TelnetPing,   "ftp", 0, TelnetFtp,   "tftp", 0, TelnetTftp,   "http", 0, TelnetHttp,   "show", 0, TelnetShow,   "clear", 0, TelnetClear,   NULL, 0, NULL};void ConsoleInit(void){   FtpdInit(1);   TftpdInit();   TelnetInit(MyFuncs);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -