N-sim
Emulation and simulation of
Wireless Sensor Networks



   Home


   Project Page


   Download


   CVS



   Installation


   Configuration


   Plug-ins




 Hosted by
SourceForge.net Logo

/home/brennan/n-sim/OrbisQuartus/server/xen/tftp_client-xen.c

Go to the documentation of this file.
00001 
00014 /*
00015  * Copyright 2007. Los Alamos National Security, LLC. This material
00016  * was produced under U.S. Government contract DE-AC52-06NA25396 for
00017  * Los Alamos National Laboratory (LANL), which is operated by Los
00018  * Alamos National Security, LLC, for the Department of Energy. The
00019  * U.S. Government has rights to use, reproduce, and distribute this
00020  * software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY,
00021  * LLC, MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LEGAL
00022  * LIABILITY FOR THE USE OF THIS SOFTWARE. If software is modified to
00023  * produce derivative works, such modified software should be clearly
00024  * marked, so as not to confuse it with the version available from LANL.
00025  *
00026  * Additionally, this program is free software; you can redistribute
00027  * it and/or modify it under the terms of the GNU General Public
00028  * License as published by the Free Software Foundation; version 2 of
00029  * the License. Accordingly, this program is distributed in the hope
00030  * it will be useful, but WITHOUT ANY WARRANTY; without even the
00031  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00032  * PURPOSE. See the GNU General Public License for more details.
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 }


© 2007, Los Alamos National Security, LLC.