Skip to content

Instantly share code, notes, and snippets.

@fakessh
Created September 16, 2012 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fakessh/3730569 to your computer and use it in GitHub Desktop.
Save fakessh/3730569 to your computer and use it in GitHub Desktop.
tftpclient.c
/* gcc -Wall -Wextra -ggdb -pedantic -o tftpclient tftpclient.c */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#define MODE "netascii"
#define PORT 6969
int main(int c,char *v[])
{
char b[516];
int s,f,x;
unsigned int l;
struct sockaddr_in a,t;
if(c!=3)exit(1);
bzero(b,sizeof(b));
b[1]=1;
strcpy(b+2,v[2]);
strcpy(b+3+strlen(v[2]),MODE);
a.sin_family=AF_INET;
a.sin_port=htons(PORT);
a.sin_addr.s_addr=inet_addr(v[1]);
s=socket(PF_INET,SOCK_DGRAM,0);
sendto(s,b,4+strlen(MODE)+strlen(v[2]),0,(struct sockaddr*)&a,sizeof(struct sockaddr));
f=open(v[2],O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);
l=sizeof(struct sockaddr);
while((x=recvfrom(s,b,sizeof(b),0,(struct sockaddr*)&t,&l))==516)
{
if(b[1]!=3)exit(1);
if(b[3]==1){
close(s);
a.sin_port=t.sin_port;
s=socket(PF_INET,SOCK_DGRAM,0);
}
write(f,b+4,512);
b[1]=4;
sendto(s,b,4,0,(struct sockaddr*)&a,sizeof(struct sockaddr));
}
if(x>=4){
write(f,b+4,x-4);
b[1]=4;
sendto(s,b,4,0,(struct sockaddr*)&a,sizeof(struct sockaddr));
}
close(f);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment