Advertisement
captmicro

Untitled

May 16th, 2011
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 16.67 KB | None | 0 0
  1. #include "main.h"
  2.  
  3. USHORT B2LW(USHORT num)
  4. {
  5.     return ((num&0xFF)<<8) | ((num&0xFF00)>>8);
  6. }
  7.  
  8. UINT B2L(UINT num)
  9. {
  10.     return ((num>>24)&0xFF) | ((num<<8)&0xFF0000) | ((num>>8)&0xFF00) | ((num<<24)&0xFF000000);
  11. }
  12.  
  13. void rotateVec(double *x, double *y, double a)
  14. {
  15.     double oldX = *x;
  16.     *x = *x * cos(a) - *y * sin(a);
  17.     *y = oldX * sin(a) + *y * cos(a);
  18. }
  19.  
  20. double atof_s(char *str)
  21. {
  22.     UINT len = 0;
  23.     char *pstr = str;
  24.     for (; isdigit(*pstr) || *pstr=='.' || *pstr=='+'
  25.         || *pstr=='-'; *pstr++, len++);
  26.     if (len > 32) return 0.0f;
  27.     char *safe = (char*)malloc(len + 1);
  28.     memset(safe, 0, len + 1);
  29.     pstr = str; memcpy(safe, pstr, len);
  30.     double res = atof(safe);
  31.     free((void*)safe);
  32.     return res;
  33. }
  34.  
  35. DWORD prng_step()
  36. {
  37.     static DWORD prng_lfsr = 0x9D0A31FC;
  38.     prng_lfsr = (prng_lfsr >> 1) ^ (UINT)(0 - (prng_lfsr & 1) & 0xD0000001);
  39.     return prng_lfsr;
  40. }
  41.  
  42. UINT mapWidth = 0, mapHeight = 0; BYTE *world = 0;
  43. #define WORLD(x,y) (world[x+(y*mapWidth)])
  44. BYTE *texs[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  45.  
  46. BYTE *fb = 0, *pb = 0;
  47. UINT width = 640, height = 480, bpp = 32;
  48. UINT pmultx = 0, pmulty = 0;
  49.  
  50. BYTE NETWORK_IPADDR[4] = {10, 10, 0, 86};
  51. BYTE NETWORK_MACADDR[6] = {0x00, 0x50, 0xF2, 0xFB, 0x1F, 0xB1};
  52.  
  53. void PutPixel(UINT x, UINT y, DWORD color)
  54. {
  55.     pb[(y*pmulty)+(x*pmultx)] = GetB(color); //b
  56.     pb[(y*pmulty)+(x*pmultx)+1] = GetG(color); //g
  57.     pb[(y*pmulty)+(x*pmultx)+2] = GetR(color); //r
  58. }
  59.  
  60. void XBoxStartup()
  61. {
  62.     XVideoSetMode(width, height, bpp, 60);
  63.     XVideoSetSoftenFilter(0);
  64.     XVideoSetFlickerFilter(0);
  65.    
  66.     Pktdrv_Init();
  67.     Pktdrv_GetEthernetAddr(NETWORK_MACADDR);
  68.    
  69.     /*Open World*/
  70.     int worldfh = 0; UINT spawni = 0; WORLDNINFO worldinfo;
  71.     XCreateFile(&worldfh, "d:\\worlds\\test.bin", GENERIC_READ,
  72.         FILE_SHARE_READ, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
  73.     XReadFile(worldfh, &worldinfo, sizeof(WORLDNINFO), 0);
  74.    
  75.     worldinfo.magic = (DWORD)B2L((UINT)worldinfo.magic);
  76.     if (worldinfo.magic != WORLDINFO_MAGIC) {
  77.         XCloseHandle(worldfh);
  78.         HalReturnToFirmware(ReturnFirmwareQuickReboot);
  79.     }
  80.     debugPrint("    World Magic: %08X\n", worldinfo.magic);
  81.     debugPrint("    World Name: %s\n", worldinfo.name);
  82.     debugPrint("    PRNG value: %08X\n", prng_step());
  83.    
  84.     worldinfo.width = (UINT)B2L((UINT)worldinfo.width);
  85.     worldinfo.height = (UINT)B2L((UINT)worldinfo.height);
  86.     worldinfo.mapoff = (DWORD)B2L((UINT)worldinfo.mapoff);
  87.     mapWidth = worldinfo.width; mapHeight = worldinfo.height;
  88.     debugPrint("    World Size: %d, %d\n", worldinfo.width, worldinfo.height);
  89.    
  90.     worldinfo.t1_spawncnt = (UINT)B2L((UINT)worldinfo.t1_spawncnt);
  91.     worldinfo.t1_spawnoff = (DWORD)B2L((UINT)worldinfo.t1_spawnoff);
  92.     SPAWNSTR *t1_spawnstrs = (SPAWNSTR*)malloc(worldinfo.t1_spawncnt * sizeof(SPAWNSTR));
  93.     XSetFilePointer(worldfh, worldinfo.t1_spawnoff, 0, FILE_BEGIN);
  94.     XReadFile(worldfh, t1_spawnstrs, worldinfo.t1_spawncnt * sizeof(SPAWNSTR), 0);
  95.     SPAWN *t1_spawns = (SPAWN*)malloc(worldinfo.t1_spawncnt * sizeof(SPAWN));
  96.     for (spawni = 0; spawni < worldinfo.t1_spawncnt; spawni++)
  97.     {
  98.         t1_spawns[spawni].pX = atof_s(t1_spawnstrs[spawni].pX);
  99.         t1_spawns[spawni].pY = atof_s(t1_spawnstrs[spawni].pY);
  100.         t1_spawns[spawni].dX = atof_s(t1_spawnstrs[spawni].dX);
  101.         t1_spawns[spawni].dY = atof_s(t1_spawnstrs[spawni].dY);
  102.     }
  103.     free(t1_spawnstrs);
  104.    
  105.     worldinfo.t2_spawncnt = (UINT)B2L((UINT)worldinfo.t2_spawncnt);
  106.     worldinfo.t2_spawnoff = (DWORD)B2L((UINT)worldinfo.t2_spawnoff);
  107.     SPAWNSTR *t2_spawnstrs = (SPAWNSTR*)malloc(worldinfo.t2_spawncnt * sizeof(SPAWNSTR));
  108.     XSetFilePointer(worldfh, worldinfo.t2_spawnoff, 0, FILE_BEGIN);
  109.     XReadFile(worldfh, t2_spawnstrs, worldinfo.t2_spawncnt * sizeof(SPAWNSTR), 0);
  110.     SPAWN *t2_spawns = (SPAWN*)malloc(worldinfo.t2_spawncnt * sizeof(SPAWN));
  111.     for (spawni = 0; spawni < worldinfo.t2_spawncnt; spawni++)
  112.     {
  113.         t2_spawns[spawni].pX = atof_s(t2_spawnstrs[spawni].pX);
  114.         t2_spawns[spawni].pY = atof_s(t2_spawnstrs[spawni].pY);
  115.         t2_spawns[spawni].dX = atof_s(t2_spawnstrs[spawni].dX);
  116.         t2_spawns[spawni].dY = atof_s(t2_spawnstrs[spawni].dY);
  117.     }
  118.     free(t1_spawnstrs);
  119.    
  120.     world = (BYTE*)malloc(worldinfo.width * worldinfo.height);
  121.     memset((void*)world, 0, worldinfo.width * worldinfo.height);
  122.     XSetFilePointer(worldfh, worldinfo.mapoff, 0, FILE_BEGIN);
  123.     XReadFile(worldfh, world, worldinfo.width * worldinfo.height, 0);
  124.     UINT dbgX = 0, dbgY = 0;
  125.     for (dbgY = 0; dbgY < worldinfo.height; dbgY++) {
  126.         debugPrint("    ");
  127.         for (dbgX = 0; dbgX < worldinfo.width; dbgX++)
  128.             debugPrint("%01d", WORLD(dbgX, dbgY));
  129.         debugPrint("\n");
  130.     }
  131.     XCloseHandle(worldfh);
  132.     /*Open World*/
  133.    
  134.     XInput_Init();
  135.    
  136.     pmultx = bpp / 8;
  137.     pmulty = width * (bpp / 8);
  138.     fb = XVideoGetFB();
  139.     pb = (BYTE*)malloc(width * height * pmultx);
  140.    
  141.     int x, y; BYTE reverseRot = 0;
  142.     double posX = t1_spawns[0].pX, posY = t1_spawns[0].pY;  //x and y start position
  143.     double dirX = t1_spawns[0].dX, dirY = t1_spawns[0].dY; //initial direction vector
  144.     if (dirX < 0) reverseRot = 1; else reverseRot = 0;
  145.     double planeX = atof_s(worldinfo.planeX); //the 2d raycaster version of camera plane
  146.     double planeY = atof_s(worldinfo.planeY); //x = 0.00, y = 0.66 default
  147.     double frameTime = 0, lastTickCount = 0;
  148.     double rotSpeed = 0, moveSpeed = 0;
  149.    
  150.     GAME_SNAPSHOT _snapshot;
  151.     memset((void*)&_snapshot, 0, sizeof(GAME_SNAPSHOT));
  152.     GAME_SNAPSHOT *snapshot = (GAME_SNAPSHOT*)&_snapshot;
  153.     snapshot->PacketNum = 0;
  154.     snapshot->team = ((prng_step() >> 19) & 0x1);
  155.     debugPrint("    TEAM: [%d] %s\n",
  156.         snapshot->team, (snapshot->team)?"BLUE":"RED");
  157.     sprintf(snapshot->name, "%x", prng_step());
  158.     debugPrint("    NANE: %s\n", snapshot->name);
  159.     snapshot->pX = posX; snapshot->pY = posY;
  160.     snapshot->dX = dirX; snapshot->dY = dirY;
  161.     snapshot->planeX = planeX; snapshot->planeY = planeY;
  162.     switch (snapshot->team) {
  163.         case 0: snapshot->color = RGB(255,0,0); break;
  164.         case 1: snapshot->color = RGB(0,255,0); break;
  165.     }
  166.     XInput_GetEvents();
  167.     snapshot->btns = g_DefaultPad.CurrentButtons;
  168.     NetworkSendSnapshot(snapshot);
  169.    
  170.     debugPrint("    Press START to being.\n");
  171.     while ((g_DefaultPad.PressedButtons.usDigitalButtons & XPAD_START)==0) {
  172.         XInput_GetEvents(); prng_step();
  173.     }
  174.    
  175.     int yoffset = 0;
  176.     BYTE running = 1;
  177.     while (running == 1)
  178.     {
  179.         /*Handle Input*/
  180.         XInput_GetEvents();
  181.         while (g_DefaultPad.CurrentButtons.usDigitalButtons&XPAD_DPAD_DOWN)
  182.             XInput_GetEvents();
  183.         snapshot->btns = g_DefaultPad.CurrentButtons;
  184.         if (g_DefaultPad.PressedButtons.ucAnalogButtons[XPAD_WHITE]) {
  185.             running = 0;
  186.         }
  187.         if (g_DefaultPad.PressedButtons.ucAnalogButtons[XPAD_BLACK]) {
  188.             SPAWN *spawn = 0;
  189.             if (snapshot->team == 0) spawn = &t1_spawns[(prng_step() >> 4) & 0x1];
  190.             else if (snapshot->team == 1) spawn = &t2_spawns[(prng_step() >> 14) & 0x1];
  191.             posX = spawn->pX; posY = spawn->pY;
  192.             dirX = spawn->dX; dirY = spawn->dY;
  193.             planeX = atof_s(worldinfo.planeX);
  194.             planeY = atof_s(worldinfo.planeY);
  195.             if (dirX < 0) reverseRot = 1; else reverseRot = 0;
  196.             snapshot->pX = posX; snapshot->pY = posY;
  197.             snapshot->dX = dirX; snapshot->dY = dirY;
  198.             snapshot->planeX = planeX; snapshot->planeY = planeY;
  199.         }
  200.         if (g_DefaultPad.CurrentButtons.usDigitalButtons & XPAD_LEFT_THUMB) {
  201.             moveSpeed *= 1.5;
  202.         }
  203.         if (g_DefaultPad.CurrentButtons.usDigitalButtons & XPAD_RIGHT_THUMB) {
  204.             rotSpeed *= 1.25;
  205.         }
  206.         if (XPAD_AXISINUP(g_DefaultPad.sLThumbY)) { //MOVE FORWARD
  207.             if(WORLD((int)(posX + dirX * moveSpeed), (int)(posY)) == 0) posX += dirX * moveSpeed;
  208.             if(WORLD((int)(posX), (int)(posY + dirY * moveSpeed)) == 0) posY += dirY * moveSpeed;
  209.             snapshot->pX = posX; snapshot->pY = posY;
  210.         }
  211.         if (XPAD_AXISINDOWN(g_DefaultPad.sLThumbY)) { //MOVE BACKWARD
  212.             if(WORLD((int)(posX - dirX * moveSpeed), (int)(posY)) == 0) posX -= dirX * moveSpeed;
  213.             if(WORLD((int)(posX), (int)(posY - dirY * moveSpeed)) == 0) posY -= dirY * moveSpeed;
  214.             snapshot->pX = posX; snapshot->pY = posY;
  215.         }
  216.         if (XPAD_AXISINLEFT(g_DefaultPad.sLThumbX)) { //STRAFE LEFT
  217.             if(WORLD((int)(posX - planeX * moveSpeed), (int)(posY)) == 0) posX -= planeX  * moveSpeed;
  218.             if(WORLD((int)(posX), (int)(posY - planeY * moveSpeed)) == 0) posY -= planeY * moveSpeed;
  219.             snapshot->pX = posX; snapshot->pY = posY;
  220.         }
  221.         if (XPAD_AXISINRIGHT(g_DefaultPad.sLThumbX)) { //STRAFE RIGHT
  222.             if(WORLD((int)(posX + planeX * moveSpeed), (int)(posY)) == 0) posX += planeX * moveSpeed;
  223.             if(WORLD((int)(posX), (int)(posY + planeY* moveSpeed)) == 0) posY += planeY * moveSpeed;
  224.             snapshot->pX = posX; snapshot->pY = posY;
  225.         }
  226.         if (reverseRot?XPAD_AXISINRIGHT(g_DefaultPad.sRThumbX):XPAD_AXISINLEFT(g_DefaultPad.sRThumbX)) { //ROTATE LEFT
  227.             rotateVec(&dirX, &dirY, -rotSpeed);
  228.             rotateVec(&planeX, &planeY, -rotSpeed);
  229.             snapshot->dX = dirX; snapshot->dY = dirY;
  230.             snapshot->planeX = planeX; snapshot->planeY = planeY;
  231.         }
  232.         if (reverseRot?XPAD_AXISINLEFT(g_DefaultPad.sRThumbX):XPAD_AXISINRIGHT(g_DefaultPad.sRThumbX)) { //ROTATE RIGHT
  233.             rotateVec(&dirX, &dirY, rotSpeed);
  234.             rotateVec(&planeX, &planeY, rotSpeed);
  235.             snapshot->planeX = planeX; snapshot->planeY = planeY;
  236.         }
  237.        
  238.         /* Networking */
  239.         NetworkSendSnapshot(snapshot);
  240.         Pktdrv_ReceivePackets();
  241.        
  242.         /*Render/
  243.         for (x = 0; x < width; x++)
  244.         {
  245.             //calculate ray position and direction
  246.             double cameraX = 2 * x / (double)width - 1; //x-coordinate in camera space
  247.             double rayPosX = posX;
  248.             double rayPosY = posY;
  249.             double rayDirX = dirX + planeX * cameraX;
  250.             double rayDirY = dirY + planeY * cameraX;
  251.  
  252.             //which box of the map we're in  
  253.             int mapX = (int)rayPosX;
  254.             int mapY = (int)rayPosY;
  255.  
  256.             //length of ray from current position to next x or y-side
  257.             double sideDistX;
  258.             double sideDistY;
  259.  
  260.             //length of ray from one x or y-side to next x or y-side
  261.             double deltaDistX = sqrt(1 + (rayDirY * rayDirY) / (rayDirX * rayDirX));
  262.             double deltaDistY = sqrt(1 + (rayDirX * rayDirX) / (rayDirY * rayDirY));
  263.             double perpWallDist;
  264.  
  265.             //what direction to step in x or y-direction (either +1 or -1)
  266.             int stepX;
  267.             int stepY;
  268.  
  269.             int hit = 0; //was there a wall hit?
  270.             int side; //was a NS or a EW wall hit?
  271.  
  272.             //calculate step and initial sideDist
  273.             if (rayDirX < 0) {
  274.                 stepX = -1;
  275.                 sideDistX = (rayPosX - mapX) * deltaDistX;
  276.             } else {
  277.                 stepX = 1;
  278.                 sideDistX = (mapX + 1.0 - rayPosX) * deltaDistX;
  279.             }
  280.             if (rayDirY < 0) {
  281.                 stepY = -1;
  282.                 sideDistY = (rayPosY - mapY) * deltaDistY;
  283.             } else {
  284.                 stepY = 1;
  285.                 sideDistY = (mapY + 1.0 - rayPosY) * deltaDistY;
  286.             }
  287.  
  288.             //perform DDA
  289.             while (hit == 0)
  290.             {
  291.                 //jump to next map square, OR in x-direction, OR in y-direction
  292.                 if (sideDistX < sideDistY) {
  293.                     sideDistX += deltaDistX;
  294.                     mapX += stepX;
  295.                     side = 0;
  296.                 } else {
  297.                     sideDistY += deltaDistY;
  298.                     mapY += stepY;
  299.                     side = 1;
  300.                 }
  301.                 //Check if ray has hit a wall
  302.                 if (WORLD(mapX, mapY) > 0) hit = 1;
  303.             }
  304.  
  305.             //Calculate distance projected on camera direction (oblique distance will give fisheye effect!)
  306.             if (side == 0) perpWallDist = fabs((mapX - rayPosX + (1 - stepX) / 2) / rayDirX);
  307.             else perpWallDist = fabs((mapY - rayPosY + (1 - stepY) / 2) / rayDirY);
  308.  
  309.             //Calculate height of line to draw on screen
  310.             int lineHeight = abs((int)(height / perpWallDist));
  311.  
  312.             //calculate lowest and highest pixel to fill in current stripe
  313.             int drawStart = -lineHeight / 2 + height / 2;
  314.             if(drawStart < 0)drawStart = 0;
  315.             int drawEnd = lineHeight / 2 + height / 2;
  316.             if(drawEnd >= height)drawEnd = height - 1;
  317.             drawStart += yoffset; drawEnd += yoffset;
  318.  
  319.             //choose wall color
  320.             DWORD color = 0;
  321.             switch(WORLD(mapX, mapY))
  322.             {
  323.                 case 1: color = RGB(255, 0, 0); break; //RED
  324.                 case 2: color = RGB(0, 255, 0); break; //GREEN
  325.                 case 3: color = RGB(0, 0, 255); break; //BLUE
  326.                 case 4: color = RGB(255, 255, 255); break; //WHITE
  327.                 default: color = RGB(255, 255, 0); break; //YELLOW
  328.             }
  329.  
  330.             //give x and y sides different brightness
  331.             if (side == 1) {
  332.                 BYTE r = GetR(color), g = GetG(color), b = GetB(color);
  333.                 if (r >= 255) r -= 16; if (r >= 128) r -= 32; if (r >= 64) r -= 12;
  334.                 if (r >= 32) r -= 8; if (r >= 16) r -= 6; if (r >= 8) r -= 2;
  335.                 if (g >= 255) g -= 16; if (g >= 128) g -= 32; if (g >= 64) g -= 12;
  336.                 if (g >= 32) g -= 8; if (g >= 16) g -= 6; if (g >= 8) g -= 2;
  337.                 if (b >= 255) b -= 16; if (b >= 128) b -= 32; if (b >= 64) b -= 12;
  338.                 if (b >= 32) b -= 8; if (b >= 16) b -= 6; if (b >= 8) b -= 2;
  339.                 color = RGB(r,g,b);
  340.             }
  341.  
  342.             //draw the pixels of the stripe as a vertical line
  343.             int tmpy;
  344.             for (tmpy=0; tmpy<drawStart; tmpy++) PutPixel(x, tmpy, RGB(0, 191, 255));
  345.             for (tmpy=drawStart; tmpy<drawEnd; tmpy++) PutPixel(x, tmpy, color);
  346.             for (tmpy=drawEnd; tmpy<height; tmpy++) PutPixel(x, tmpy, RGB(100, 100, 100));
  347.         }
  348.         memcpy((void*)fb, (void*)pb, width*height*pmultx);
  349.        
  350.         /*FPS (skip time to benchmark)*/
  351.         frameTime = XGetTickCount() - lastTickCount;
  352.         lastTickCount = XGetTickCount();
  353.         if (frameTime < 10) frameTime = 10;
  354.         moveSpeed = frameTime * 0.005f;
  355.         rotSpeed = frameTime * 0.0030f;
  356.     }
  357.    
  358.     snapshot->team = TEAM_QUIT;
  359.     NetworkSendSnapshot(snapshot);
  360.    
  361.     free((void*)pb);
  362.     free((void*)world);
  363.     XInput_Quit();
  364.     Pktdrv_Quit();
  365.  
  366.     HalReturnToFirmware(ReturnFirmwareQuickReboot);
  367. }
  368.  
  369. void NetworkSendSnapshot(GAME_SNAPSHOT *ss)
  370. {
  371.     //debugPrint("    [SS] Send snapshot #%d\n", ss->PacketNum);
  372.     ss->PacketNum++;
  373. }
  374.  
  375. int Pktdrv_Callback(unsigned char *packet, unsigned int sz)
  376. {
  377.     char pktbuffer[4096];
  378.     memset(pktbuffer, 0, 4096);
  379.     memcpy(pktbuffer, packet, (sz<4096)?sz:4096);
  380.    
  381.     ETH *ethhdr = (ETH*)pktbuffer;
  382.     debugPrint("    [RECV PACKET]\n");
  383.     debugPrint("        Target MAC: %02X%02X%02X%02X%02X%02X\n",
  384.         ethhdr->EthDst[0], ethhdr->EthDst[1],
  385.         ethhdr->EthDst[2], ethhdr->EthDst[3],
  386.         ethhdr->EthDst[4], ethhdr->EthDst[5]);
  387.     debugPrint("        Sender MAC: %02X%02X%02X%02X%02X%02X\n",
  388.         ethhdr->EthSrc[0], ethhdr->EthSrc[1],
  389.         ethhdr->EthSrc[2], ethhdr->EthSrc[3],
  390.         ethhdr->EthSrc[4], ethhdr->EthSrc[5]);
  391.     debugPrint("        ProtocolType: 0x%04X\n", ethhdr->ProtocolType);
  392.    
  393.     ARP *arphdr = (ARP*)pktbuffer;
  394.     if (arphdr->ProtocolType==0x0608 && arphdr->Operation==0x0100)
  395.     {
  396.         DWORD dstip = *((DWORD*)arphdr->TargetProtocolAddr);
  397.         DWORD myip = *((DWORD*)&NETWORK_IPADDR[0]);
  398.         if (dstip == myip)
  399.         {
  400.             memcpy(arphdr->EthDst, arphdr->EthSrc, 6);
  401.             memcpy(arphdr->EthSrc, NETWORK_MACADDR, 6);
  402.             arphdr->Operation = 0x0200;
  403.             memcpy(arphdr->TargetHardwareAddr, arphdr->SenderHardwareAddr, 6);
  404.             memcpy(arphdr->SenderHardwareAddr, NETWORK_MACADDR, 6);
  405.             memcpy(arphdr->TargetProtocolAddr, arphdr->SenderProtocolAddr, 4);
  406.             memcpy(arphdr->SenderProtocolAddr, NETWORK_IPADDR, 4);
  407.             debugPrint("    [ARP] Answering with:\n");
  408.             debugPrint("        Target MAC: %02X%02X%02X%02X%02X%02X\n",
  409.                 arphdr->TargetHardwareAddr[0], arphdr->TargetHardwareAddr[1],
  410.                 arphdr->TargetHardwareAddr[2], arphdr->TargetHardwareAddr[3],
  411.                 arphdr->TargetHardwareAddr[4], arphdr->TargetHardwareAddr[5]);
  412.             debugPrint("        Sender MAC: %02X%02X%02X%02X%02X%02X\n",
  413.                 arphdr->SenderHardwareAddr[0], arphdr->SenderHardwareAddr[1],
  414.                 arphdr->SenderHardwareAddr[2], arphdr->SenderHardwareAddr[3],
  415.                 arphdr->SenderHardwareAddr[4], arphdr->SenderHardwareAddr[5]);
  416.             debugPrint("        Target IP: %d.%d.%d.%d\n",
  417.                 arphdr->TargetProtocolAddr[0], arphdr->TargetProtocolAddr[1],
  418.                 arphdr->TargetProtocolAddr[2], arphdr->TargetProtocolAddr[3]);
  419.             debugPrint("        Sender IP: %d.%d.%d.%d\n",
  420.                 arphdr->SenderProtocolAddr[0], arphdr->SenderProtocolAddr[1],
  421.                 arphdr->SenderProtocolAddr[2], arphdr->SenderProtocolAddr[3]);
  422.             while (Pktdrv_GetQueuedTxPkts() >= 1);
  423.             Pktdrv_SendPacket(pktbuffer, sizeof(ARP));
  424.         }
  425.     } else if (arphdr->ProtocolType==0x0608) {
  426.             debugPrint("    [ARP] Operation 0x%04X\n", arphdr->Operation);
  427.     }
  428.    
  429.     IP *iphdr = (IP*)pktbuffer;
  430.     if (iphdr->ProtocolType == 0x0008)
  431.     {
  432.         debugPrint("    [IPV4] Received\n");
  433.         debugPrint("        Protocol: %d\n", iphdr->Protocol);
  434.         debugPrint("        Scr IP: %d.%d.%d.%d\n",
  435.             iphdr->SrcIPAddr[0], iphdr->SrcIPAddr[1],
  436.             iphdr->SrcIPAddr[2], iphdr->SrcIPAddr[3]);
  437.         debugPrint("        Dst IP: %d.%d.%d.%d\n",
  438.             iphdr->DstIPAddr[0], iphdr->DstIPAddr[1],
  439.             iphdr->DstIPAddr[2], iphdr->DstIPAddr[3]);
  440.         if (iphdr->Protocol == 17)
  441.         {
  442.             DWORD dstip = *((DWORD*)iphdr->DstIPAddr);
  443.             DWORD myip = *((DWORD*)&NETWORK_IPADDR[0]);
  444.             if (dstip == myip)
  445.             {
  446.                 UDP *udp = (UDP*)pktbuffer;
  447.                 udp->SrcPort = B2LW(udp->SrcPort);
  448.                 udp->DstPort = B2LW(udp->DstPort);
  449.                 udp->Length = B2LW(udp->Length);
  450.                 debugPrint("    [UDP] Packed Received\n");
  451.                 debugPrint("        Src port: %d", udp->SrcPort);
  452.                 debugPrint("        Dst port: %d", udp->DstPort);
  453.                 debugPrint("        Length: %d", udp->Length);
  454.             }
  455.         }
  456.         else if (iphdr->Protocol == 1)
  457.         {
  458.             debugPrint("    [PING] Received\n");
  459.         }
  460.     }
  461.    
  462.     return 1; //0 to keep
  463. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement