00001 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 #include <sys/file.h>
00037 #include <ctype.h>
00038 #include <netdb.h>
00039 #include "extern.h"
00040 
00041 #define TIMEOUT    5
00042 
00043 
00044 struct sockaddr_in peeraddr;
00045 int f;
00046 int trace = 0;
00047 int verbose = 0;
00048 int rexmtval = TIMEOUT;
00049 int maxtimeout = 5 * TIMEOUT;
00050 
00051 char *xstrdup(const char*);
00052 
00053 
00054 struct mode {
00055         const char *name;
00056         int flag;
00057 };
00058 
00059 char *hostname;
00060 struct mode *mode;
00061 static const struct mode modes[] =
00062 {
00063         { "netascii", O_TEXT },
00064         { "binary", O_BINARY },
00065         { 0, 0 }
00066 };
00067 
00068 
00069 
00070 void tftp_init(char *host) {
00071         struct servent *sp = getservbyname("tftp", "udp");
00072         struct hostent *hp = gethostbyname(host);
00073         struct sockaddr_in s_in;
00074         int port;
00075 
00076         if (hp == NULL)
00077                 return;
00078 
00079         if (sp)
00080                 port = sp->s_port;
00081         else
00082                 port = htons(IPPORT_TFTP);
00083         f = socket(AF_INET, SOCK_DGRAM, 0);
00084         if (f < 0)
00085                 return;
00086 
00087         memset((char*)&s_in, '\0', sizeof(s_in));
00088         s_in.sin_family = AF_INET;
00089 
00090         if (pick_port_bind(f, &s_in, portrange_from, portrange_to)) 
00091                 return;
00092 
00093         peeraddr.sin_family = AF_INET;
00094         peeraddr.sin_family = hp->h_addrtype;
00095         memcpy(hp->h_addr, &peeraddr.sin_addr, hp->h_length);
00096         hostname = xstrdup(hp->h_name);
00097         peeraddr.sin_port = port;
00098 }
00099 
00100 
00101 void tftp_get(char *file) {
00102         int fd = open(file, O_WRONLY | O_CREAT | O_TRUNC | mode->flag, 0666);
00103         if (fd < 0)
00104                 return;
00105         tftp_recvfile(fd, file, mode->name);
00106 }
00107 
00108 
00109 void tftp_put(char *file) {
00110         int fd = open(file, O_RDONLY | mode->flag);
00111         if (fd < 0)
00112                 return;
00113         tftp_sendfile(fd, file, mode->name);
00114 }
00115 
00116 
00117 void tftp_mode(enum TFTP_mode mode) {
00118         switch (mode) {
00119         case ASCII_MODE:
00120                 mode = &modes[0];
00121                 break;
00122         case BINARY_MODE:
00123                 mode = &modes[1];
00124                 break;
00125         }
00126 }