exemplo_tftp
no way to compare when less than two revisions
Diferenças
Aqui você vê as diferenças entre duas revisões dessa página.
— | exemplo_tftp [2023/09/12 16:14] (atual) – criada - edição externa 127.0.0.1 | ||
---|---|---|---|
Linha 1: | Linha 1: | ||
+ | ====== Cliente e Servidor TFTP ====== | ||
+ | O //Trivial File Transfer Protocol// (ou apenas TFTP) é um protocolo de transferência de arquivos, muito simples, semelhante ao [[FTP]]. | ||
+ | |||
+ | O TFTP é usualmente utilizado para transferir pequenos arquivos entre //hosts// numa rede. O TFPT trabalha com o protocolo de transporte [[UDP]], o servidor TFTP recebe conexões na porta 69 e o tamanho de cada bloco é de 512 bytes. | ||
+ | |||
+ | Os exemplos abaixo trabalham na porta 2424 e o bloco tem tamanho de 1024 bytes. | ||
+ | |||
+ | ===== Servidor TFTP simples ===== | ||
+ | Recebe conexões (trata apenas 1 conexão por vez) e cria arquivo com permissão fixa. Neste Exemplo falta: | ||
+ | * Enviar mensagem de erro em caso de problemas | ||
+ | * Respeitar os atributos originais do arquivo | ||
+ | |||
+ | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define SIZE_BUFFER 1024 | ||
+ | int main(void) | ||
+ | { | ||
+ | int iSock, iFileDescriptor; | ||
+ | | ||
+ | | ||
+ | | ||
+ | int iNumBytes; | ||
+ | char sBuffer[SIZE_BUFFER]; | ||
+ | |||
+ | if ((iSock = socket(AF_INET, | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | if (bind(iSock, | ||
+ | { | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | /* loop infinito recebendo dados */ | ||
+ | | ||
+ | { | ||
+ | addr_len = sizeof(struct sockaddr); | ||
+ | if ((iNumBytes=recvfrom(iSock, | ||
+ | { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | fprintf(stderr, | ||
+ | fprintf(stderr, | ||
+ | sBuffer[iNumBytes] = ' | ||
+ | if( strcmp(sBuffer," | ||
+ | { | ||
+ | if ((iNumBytes=recvfrom(iSock, | ||
+ | { | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | if( iFileDescriptor < 0 ) | ||
+ | { | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | /* até todo o arquivo chegar, fico lendo */ | ||
+ | | ||
+ | | ||
+ | { | ||
+ | sBuffer[iNumBytes] = ' | ||
+ | if( strcmp(sBuffer, | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | fprintf(stderr, | ||
+ | write( iFileDescriptor, | ||
+ | iNumBytes=recvfrom(iSock, | ||
+ | } | ||
+ | if( iNumBytes < 0 ) | ||
+ | { | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | |||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Cliente TFTP Simples ===== | ||
+ | Envia um arquivo a partir da linha comando. Neste exemplo falta: | ||
+ | * Notificar servidor no caso de erro | ||
+ | * Fazer autenticação junto ao servidor | ||
+ | * Enviar múltiplos arquivos | ||
+ | |||
+ | |||
+ | <code c> | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define SIZE_BUFFER 1024 | ||
+ | |||
+ | int main(int argc, char *argv[]) | ||
+ | { | ||
+ | int iSock, iFileDescriptor, | ||
+ | | ||
+ | | ||
+ | |||
+ | char sBuffer[SIZE_BUFFER]; | ||
+ | |||
+ | if (argc != 3) | ||
+ | { | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | /* convertendo o endereco passado para uma estrutura que eu entendo */ | ||
+ | if ((he=gethostbyname(argv[1])) == NULL) | ||
+ | { | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | if ((iSock = socket(AF_INET, | ||
+ | { | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | /* abrir o arquivo passado */ | ||
+ | | ||
+ | if( iFileDescriptor < 0 ) | ||
+ | { | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | /* inicio do envio */ | ||
+ | | ||
+ | | ||
+ | { | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | /* primeiro é enviado o nome do arquivo */ | ||
+ | | ||
+ | | ||
+ | { | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | { | ||
+ | /* enviando o buffer */ | ||
+ | iNumBytes = sendto(iSock, | ||
+ | if(iNumBytes < 0 ) | ||
+ | { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | fprintf(stderr, | ||
+ | iNumBytes = read(iFileDescriptor, | ||
+ | } | ||
+ | |||
+ | if( iNumBytes < 0 ) | ||
+ | { | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | /* fim do envio */ | ||
+ | | ||
+ | | ||
+ | { | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | --- // |
exemplo_tftp.txt · Última modificação: 2023/09/12 16:14 por 127.0.0.1