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

📄 tftpd.c

📁 tftp-1.0源码
💻 C
📖 第 1 页 / 共 3 页
字号:
    {        if (!strncasecmp (mode, "mail", 4))            len = sprintf ((char *) packetbuf,                    "%c%c%c%cThis tftp server will not operate as a mail relay%c",                    0x00, 0x05, 0x00, 0x04, 0x00);        else            len = sprintf ((char *) packetbuf,                    "%c%c%c%cUnrecognized mode (%s)%c",                    0x00, 0x05, 0x00, 0x04, mode, 0x00);        if (sendto (sock, packetbuf, len, 0, (struct sockaddr *) &client, sizeof (client)) != len)	/* send the data packet */        {            printf                ("Mismatch in number of sent bytes while trying to send mode error packet\n");        }        return;    }    if (strchr (filename, 0x5C) || strchr (filename, 0x2F))	//look for illegal characters in the filename string these are \ and /    {        if (debug)            printf ("Client requested to upload bad file: forbidden name\n");        len =            sprintf ((char *) packetbuf,                    "%c%c%c%cIllegal filename.(%s) You may not attempt to descend or ascend directories.%c",                    0x00, 0x05, 0x00, 0x00, filename, 0x00);        if (sendto (sock, packetbuf, len, 0, (struct sockaddr *) &client, sizeof (client)) != len)	/* send the data packet */        {            printf                ("Mismatch in number of sent bytes while trying to send error packet\n");        }        return;    }    strcpy (fullpath, path);    strncat (fullpath, filename, sizeof (fullpath) - 1);	//build the full file path by appending filename to path    fp = fopen (fullpath, "w");	/* open the file for writing */    if (fp == NULL)    {				//if the pointer is null then the file can't be opened - Bad perms         if (debug)            printf ("Server requested bad file: cannot open for writing (%s)\n",                    fullpath);        len =            sprintf ((char *) packetbuf,                    "%c%c%c%cFile cannot be opened for writing (%s)%c", 0x00,                    0x05, 0x00, 0x02, fullpath, 0x00);        if (sendto (sock, packetbuf, len, 0, (struct sockaddr *) &client, sizeof (client)) != len)	/* send the data packet */        {            printf                ("Mismatch in number of sent bytes while trying to send error packet\n");        }        return;    }    else				/* everything worked fine */    {        if (debug)            printf ("Getting file... (destination: %s) \n", fullpath);    }    /* zero the buffer before we begin */    memset (filebuf, 0, sizeof (filebuf));    n = datasize + 4;    do    {        /* zero buffers so if there are any errors only NULLs will be exposed */        memset (packetbuf, 0, sizeof (packetbuf));        memset (ackbuf, 0, sizeof (ackbuf));        if (debug)            printf ("== just entered do-while count: %d  n: %d\n", count, n);        if (count == 0 || (count % ackfreq) == 0 || n != (datasize + 4))	/* ack the first packet, count % ackfreq will make it so we only ACK everyone ackfreq ACKs, ack the last packet */        {            len = sprintf (ackbuf, "%c%c%c%c", 0x00, 0x04, 0x00, 0x00);            ackbuf[2] = (count & 0xFF00) >> 8;	//fill in the count (top number first)            ackbuf[3] = (count & 0x00FF);	//fill in the lower part of the count            if (debug)                printf ("Sending ack # %04d (length: %d)\n", count, len);            if (sendto                    (sock, ackbuf, len, 0, (struct sockaddr *) &client,                     sizeof (client)) != len)            {                if (debug)                    printf ("Mismatch in number of sent bytes\n");                return;            }        }        else if (debug)        {            printf ("No ack required on packet count %d\n", count);        }        if (n != (datasize + 4))	/* remember if our datasize is less than a full packet this was the last packet to be received */        {            if (debug)                printf                    ("Last chunk detected (file chunk size: %d). exiting while loop\n",                     n - 4);            goto done;		/* gotos are not optimal, but a good solution when exiting a multi-layer loop */        }        memset (filebuf, 0, sizeof (filebuf));        count++;        for (j = 0; j < RETRIES; j++)	/* this allows us to loop until we either break out by getting the correct ack OR time out because we've looped more than RETRIES times */        {            client_len = sizeof (data);            errno = EAGAIN;	/* this allows us to enter the loop */            n = -1;            for (i = 0; errno == EAGAIN && i <= TIMEOUT && n < 0; i++)	/* this for loop will just keep checking the non-blocking socket until timeout */            {                n =                    recvfrom (sock, packetbuf, sizeof (packetbuf) - 1,                            MSG_DONTWAIT, (struct sockaddr *) &data,                            (socklen_t *) & client_len);                /*if (debug)                  printf ("The value recieved is n: %d\n",n); */                usleep (1000);            }            if (n < 0 && errno != EAGAIN)	/* this will be true when there is an error that isn't the WOULD BLOCK error */            {                if (debug)                    printf                        ("The server could not receive from the client (errno: %d n: %d)\n",                         errno, n);                //resend packet            }            else if (n < 0 && errno == EAGAIN)	/* this is true when the error IS would block. This means we timed out */            {                if (debug)                    printf ("Timeout waiting for data (errno: %d == %d n: %d)\n",                            errno, EAGAIN, n);                //resend packet            }            else            {                if (client.sin_addr.s_addr != data.sin_addr.s_addr)	/* checks to ensure get from ip is same from ACK IP */                {                    if (debug)                        printf                            ("Error recieving file (data from invalid address)\n");                    j--;                    continue;	/* we aren't going to let another connection spoil our first connection */                }                if (tid != ntohs (client.sin_port))	/* checks to ensure get from the correct TID */                {                    if (debug)                        printf ("Error recieving file (data from invalid tid)\n");                    len = sprintf ((char *) packetbuf,                            "%c%c%c%cBad/Unknown TID%c",                            0x00, 0x05, 0x00, 0x05, 0x00);                    if (sendto (sock, packetbuf, len, 0, (struct sockaddr *) &client, sizeof (client)) != len)	/* send the data packet */                    {                        printf                            ("Mismatch in number of sent bytes while trying to send mode error packet\n");                    }                    j--;                    continue;	/* we aren't going to let another connection spoil our first connection */                }                /* this formatting code is just like the code in the main function */                bufindex = (char *) packetbuf;	//start our pointer going                if (bufindex++[0] != 0x00)                    printf ("bad first nullbyte!\n");                opcode = *bufindex++;                rcount = *bufindex++ << 8;                rcount &= 0xff00;                rcount += (*bufindex++ & 0x00ff);                memcpy ((char *) filebuf, bufindex, n - 4);	/* copy the rest of the packet (data portion) into our data array */                if (debug)                    printf                        ("Remote host sent data packet #%d (Opcode: %d packetsize: %d filechunksize: %d)\n",                         rcount, opcode, n, n - 4);                if (flag)                {                    if (n > 516)                        datasize = n - 4;                    flag = 0;                }                if (opcode != 3 || rcount != count)	/* ack packet should have code 3 (data) and should be ack+1 the packet we just sent */                {                    if (debug)                        printf                            ("Badly ordered/invalid data packet (Got OP: %d Block: %d) (Wanted Op: 3 Block: %d)\n",                             opcode, rcount, count);                    /* sending error message */                    if (opcode > 5)                    {                        len = sprintf ((char *) packetbuf,                                "%c%c%c%cIllegal operation%c",                                0x00, 0x05, 0x00, 0x04, 0x00);                        if (sendto (sock, packetbuf, len, 0, (struct sockaddr *) &client, sizeof (client)) != len)	/* send the data packet */                        {                            printf                                ("Mismatch in number of sent bytes while trying to send mode error packet\n");                        }                    }                }                else                {                    break;                }            }            if (sendto                    (sock, ackbuf, len, 0, (struct sockaddr *) &client,                     sizeof (client)) != len)            {                if (debug)                    printf ("Mismatch in number of sent bytes\n");                return;            }        }        if (j == RETRIES)        {            if (debug)                printf ("Data recieve Timeout. Aborting transfer\n");            fclose (fp);            return;        }    }    while (fwrite (filebuf, 1, n - 4, fp) == n - 4);	/* if it doesn't write the file the length of the packet received less 4 then it didn't work */    fclose (fp);    sync ();    if (debug)        printf ("fclose and sync successful. File failed to recieve properly\n");    return;done:    fclose (fp);    sync ();    if (debug)        printf ("fclose and sync successful. File received successfully\n");    return;}voidtsend (char *pFilename, struct sockaddr_in client, char *pMode, int tid){    int sock, len, client_len, opcode, ssize = 0, n, i, j, bcount = 0;    unsigned short int count = 0, rcount = 0, acked = 0;    unsigned char filebuf[MAXDATASIZE + 1];    unsigned char packetbuf[MAXACKFREQ][MAXDATASIZE + 12],                  recvbuf[MAXDATASIZE + 12];    char filename[128], mode[12], fullpath[196], *bufindex;    struct sockaddr_in ack;    FILE *fp;			/* pointer to the file we will be sending */    strcpy (filename, pFilename);	//copy the pointer to the filename into a real array    strcpy (mode, pMode);		//same as above    if (debug)        printf ("branched to file send function\n");    if ((sock = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)	//startup a socket    {        printf ("Server reconnect for sending did not work correctly\n");

⌨️ 快捷键说明

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