?? tftpdlib.c
字號:
; strncpy(mode, ++strIndex, 32); mode [31] = EOS; } return (OK); }/******************************************************************************** tftpdFileRead - handle a read request** This routine constructs and executes the tftpPut() command to put the file* to the remote system. Normally this routine is the entry point for a task* created by tftpdTask() in response to a read request.** RETURNS: OK, or ERROR if the file requested could not be opened.*/LOCAL STATUS tftpdFileRead ( char *fileName, /* file to be sent */ TFTP_DESC *pReplyDesc /* where to send the file */ ) { int requestFd; int returnValue = OK; /* * XXX - X windows needs files that don't work with DOS - they have * more than three chars after a dot (as in fonts.alias). If the * funcpointer is non-null, call the routine with a pointer to * filename, and the called routine should change the name to something * DOS understands. */ if (tftpdNameMunge != NULL) (*tftpdNameMunge) (fileName); requestFd = open (fileName, O_RDONLY, 0); if (requestFd == ERROR) { if (tftpdDebug) { printErr ("%s: Could not open file %s\n", tftpdErrStr, fileName); } tftpdErrorSend (pReplyDesc, errnoGet()); } else { /* * We call tftpPut from the server on a read request because the * server is putting the file to the client */ returnValue = tftpPut (pReplyDesc, fileName, requestFd, TFTP_SERVER); close (requestFd); } /* * Close the socket, and delete the * tftpd descriptor. */ if (returnValue == ERROR) { printErr ("%s: could not send client file \"%s\"\n", tftpdErrStr, fileName); } tftpdDescriptorDelete (pReplyDesc); return (returnValue); }/******************************************************************************** tftpdFileWrite - handle a write request** This routine constructs and executes the tftpGet() command to get the file* from the remote system. Normally this routine is the entry point for a* task created by tftpdTask() in response to a write request.** RETURNS: OK, or ERROR if the file requested could not be opened.*/LOCAL STATUS tftpdFileWrite ( char *fileName, /* file to be sent */ TFTP_DESC *pReplyDesc /* where to send the file */ ) { int requestFd; int returnValue = OK; requestFd = open (fileName, O_WRONLY | O_CREAT | O_TRUNC, 0); if (requestFd == ERROR) { if (tftpdDebug) { printErr ("%s: Could not open file %s\n", tftpdErrStr, fileName); } tftpdErrorSend (pReplyDesc, errnoGet()); } else { /* * We call tftpGet from the server on a read request because the * server is putting the file to the client */ returnValue = tftpGet (pReplyDesc, fileName, requestFd, TFTP_SERVER); close (requestFd); } /* * Close the socket, and delete the * tftpd descriptor. */ if (returnValue == ERROR) { printErr ("%s: could not send \"%s\" to client\n", tftpdErrStr); } tftpdDescriptorDelete (pReplyDesc); return (returnValue); }/******************************************************************************** tftpdDescriptorQueueInit - set up pool of available descriptors** This routine sets up the system for managing a pool of available reply* descriptors. A piece of contiguous memory, large enough to hold an* array of <nEntries> descriptors, is allocated (with malloc()).* Pointers to each element in the array are written into a message* queue. The routine tftpdDescriptorCreate() reads pointers out of this* queue, and tftpdDescriptorDelete() writes them back in when the* descriptor space is no longer needed.** RETURNS: OK, or ERROR if either out of memory or the queue could not* be allocated.*/LOCAL STATUS tftpdDescriptorQueueInit ( int nEntries /* maximum number of descriptors in * use at any time */ ) { TFTP_DESC *theAddress; /* * If nEntries is 0, set it to the default number of connections */ if (nEntries == 0) nEntries = tftpdMaxConnections; /* * Create the message queue */ if ((tftpdDescriptorQueue = (MSG_Q_ID) msgQCreate (nEntries, sizeof (TFTP_DESC *), MSG_Q_FIFO)) == NULL) { return (ERROR); } /* * Allocate space for the descriptors. */ tftpdDescriptorPool = (TFTP_DESC *) KHEAP_ALLOC(sizeof (TFTP_DESC) * nEntries); if (tftpdDescriptorPool == NULL) { msgQDelete (tftpdDescriptorQueue); return (ERROR); } while (nEntries--) { theAddress = &(tftpdDescriptorPool [nEntries]); msgQSend (tftpdDescriptorQueue, (char *) &theAddress, sizeof (TFTP_DESC *), WAIT_FOREVER, MSG_PRI_NORMAL); } return (OK); }/******************************************************************************** tftpdDescriptorQueueDelete - delete pool of available descriptors** This routine deletes the memory and message queues allocated by* tftpdDescriptorQueueInit.** RETURNS: OK, or ERROR if either memory could not be freed or if the* message queue could not be deleted.*/LOCAL STATUS tftpdDescriptorQueueDelete ( void ) { STATUS returnValue = OK; if (msgQDelete (tftpdDescriptorQueue) == ERROR) { printErr ("%s: could not delete message queue\n", tftpdErrStr); returnValue = ERROR; } /* * free() is now void according to ANSI, so can't check the return value */ KHEAP_FREE((char *)tftpdDescriptorPool); return (returnValue); }/******************************************************************************** tftpdDescriptorCreate - build a tftp descriptor to use with tftpLib** The routines in tftpLib, tftpPut() and tftpGet() in particular, expect to* be passed a pointer to a struct of type TFTP_DESC that contains the* information about the connection to the host. This is a convenience* routine to allocate space for a TFTP_DESC struct, fill in the elements,* and return a pointer to it.** This routine pends until a descriptor is available.** RETURNS:* A pointer to a newly allocated TFTP_DESC struct, or NULL on failure.*/LOCAL TFTP_DESC *tftpdDescriptorCreate ( char *mode, /* mode */ BOOL connected, /* state */ int sock, /* socket number */ u_short clientPort, /* client port number */ struct sockaddr_in *pClientAddr /* client address */ ) { TFTP_DESC *pTftpDesc; /* pointer to the struct to return */ char clientName [INET_ADDR_LEN]; if (msgQReceive (tftpdDescriptorQueue, (char *) &pTftpDesc, sizeof (pTftpDesc), WAIT_FOREVER) == ERROR) { /* * Couldn't get a descriptor from the queue, return an error */ return (NULL); } /* * Copy the arguments into the appropriate elements of the struct */ strncpy (pTftpDesc->mode, mode, 31); pTftpDesc->mode [31] = EOS; pTftpDesc->connected = connected; /* * clientName and serverName are reversed, because the routines * that use this descriptor are expecting to look at the universe * from the client side. */ inet_ntoa_b (pClientAddr->sin_addr, clientName); strncpy (pTftpDesc->serverName, clientName, 128); pTftpDesc->serverName [127] = EOS; bcopy ((char *) pClientAddr, (char *) &pTftpDesc->serverAddr, sizeof (struct sockaddr_in)); pTftpDesc->sock = sock; pTftpDesc->serverPort = clientPort; /* * We've filled the struct, now return a pointer to it */ return (pTftpDesc); }/******************************************************************************** tftpdDescriptorDelete - delete a reply descriptor** This routine returns the space for a reply descriptor back to the* descriptor pool.** RETURNS: OK or ERROR.*/LOCAL STATUS tftpdDescriptorDelete ( TFTP_DESC *descriptor ) { close (descriptor->sock); return (msgQSend (tftpdDescriptorQueue, (char *) &descriptor, sizeof (TFTP_DESC *), WAIT_FOREVER, MSG_PRI_NORMAL)); }/******************************************************************************** tftpdDirectoryAdd - add a directory to the access list** This routine adds the specified directory name to the access list * for the TFTP server.** RETURNS: N/A*/STATUS tftpdDirectoryAdd ( char *fileName /* name of directory to add to access list */ ) { TFTPD_DIR *newNode; /* * Allocate memory for the node */ newNode = (TFTPD_DIR *) KHEAP_ALLOC(sizeof (TFTPD_DIR)); if (newNode == NULL) { return (ERROR); } /* * Copy the filename into the node */ strncpy (newNode->dirName, fileName, MAX_FILENAME_LENGTH); newNode->dirName [MAX_FILENAME_LENGTH - 1] = EOS; /* * Add the node to the list */ semTake (tftpdDirectorySem, WAIT_FOREVER); lstAdd (&tftpdDirectoryList, (NODE *) newNode); semGive (tftpdDirectorySem); return (OK); }/******************************************************************************** tftpdDirectoryRemove - delete a directory from the access list** This routine deletes the specified directory name from the access list * for the TFTP server.** RETURNS: N/A*/STATUS tftpdDirectoryRemove ( char *fileName /* name of directory to add to access list */ ) { TFTPD_DIR *dirNode; for (dirNode = (TFTPD_DIR *) lstFirst (&tftpdDirectoryList); dirNode != NULL; dirNode = (TFTPD_DIR *) lstNext ((NODE *) dirNode)) { /* * if the name of the file matches the name in the current * element, delete the element */ if (strcmp (dirNode->dirName, fileName) == 0) { semTake (tftpdDirectorySem, WAIT_FOREVER); lstDelete (&tftpdDirectoryList, (NODE *) dirNode); semGive (tftpdDirectorySem); /* * Also need to free the memory */ KHEAP_FREE((char *)dirNode); return (OK); } } /* * If we got to this point, then there was no match. Return error. */ return (ERROR); }/******************************************************************************** tftpdDirectoryValidate - confirm that file requested is in valid directory** This routine checks that the file requested is in a directory that* matches an entry in the directory access list. The validation* procedure is:** 1. If the requested file matches the directory exactly,* deny access. This prevents opening devices rather* than files.** 2. If the directory and the first part of the file* are equal, permission is granted.** Examples:** File Directory Result* /mem /mem rejected by #1* /mem/foo /mem granted* /stuff/foo /mem rejected by #2* (first four chars of file* don't match "/mem")** RETURNS: OK, or ERROR if access is not allowed.*/LOCAL STATUS tftpdDirectoryValidate ( char *fileName ) { DEV_HDR *deviceHdr; char fullPathName [MAX_FILENAME_LENGTH]; char tmpString [MAX_FILENAME_LENGTH]; TFTPD_DIR *dirNode; STATUS returnValue = ERROR; /* * Use ioFullFileNameGet to get the complete name, including * the device, of the requested file. */ ioFullFileNameGet (fileName, &deviceHdr, fullPathName); strcpy (tmpString, deviceHdr->name); strcat (tmpString, fullPathName); strcpy (fullPathName, tmpString); /* * If the access list is empty, there are no restrictions. Return OK. */ if (lstCount (&tftpdDirectoryList) == 0) return (OK); semTake (tftpdDirectorySem, WAIT_FOREVER); for (dirNode = (TFTPD_DIR *) lstFirst (&tftpdDirectoryList); dirNode != NULL; dirNode = (TFTPD_DIR *) lstNext ((NODE *) dirNode)) { /* * If the filename is exactly the same as the directory, break the loop * and return an error (rejected by #1) */ if (strcmp (fullPathName, dirNode->dirName) == 0) { returnValue = ERROR; break; } /* * If the first part of the filename is exactly equal * to the directory name, return OK (#2). */ if (strncmp (dirNode->dirName, fullPathName, strlen (dirNode->dirName)) == 0) { returnValue = OK; break; } } semGive (tftpdDirectorySem); if (returnValue == ERROR) errnoSet (EACCESS); return (returnValue); }/******************************************************************************** tftpdErrorSend - send an error to a client** Given a client connection and an error number, this routine builds an error* packet and sends it to the client.** RETURNS:* OK, or ERROR if tftpSend() fails. Note that no further action is required if* tftpSend() fails.*/LOCAL STATUS tftpdErrorSend ( TFTP_DESC *pReplyDesc, /* client to send to */ int errorNum /* error to send */ ) { TFTP_MSG errMsg; int errSize; int repeatCount = tftpdErrorSendTries; STATUS returnValue = OK; errSize = tftpErrorCreate (&errMsg, errorNum); /* * Try to send the error message a few times. */ while (repeatCount--) { returnValue = tftpSend (pReplyDesc, &errMsg, errSize, (TFTP_MSG *) 0, 0, 0, (int *) 0); /* * If we didn't get an error, break out of the loop. Otherwise, * wait one second and retry */ if (returnValue != ERROR) { break; } else { taskDelay (sysClkRateGet ()); } } return (returnValue); } /* XXX * everything below here doesn't go into final product */char *dirs[] = { "/mem", "/tftpboot", "/folk/james/zap", "/xt" };void tftpTst (void) { tftpdTask (sizeof (dirs) / sizeof (char *), dirs, 3); }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -