1#!/usr/bin/env perl
2#***************************************************************************
3#                                  _   _ ____  _
4#  Project                     ___| | | |  _ \| |
5#                             / __| | | | |_) | |
6#                            | (__| |_| |  _ <| |___
7#                             \___|\___/|_| \_\_____|
8#
9# Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
10#
11# This software is licensed as described in the file COPYING, which
12# you should have received as part of this distribution. The terms
13# are also available at http://curl.haxx.se/docs/copyright.html.
14#
15# You may opt to use, copy, modify, merge, publish, distribute and/or sell
16# copies of the Software, and permit persons to whom the Software is
17# furnished to do so, under the terms of the COPYING file.
18#
19# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20# KIND, either express or implied.
21#
22#***************************************************************************
23
24BEGIN {
25    @INC=(@INC, $ENV{'srcdir'}, '.');
26}
27
28use strict;
29use warnings;
30
31use serverhelp qw(
32    server_pidfilename
33    server_logfilename
34    );
35
36my $verbose = 0;     # set to 1 for debugging
37my $port = 8997;     # just a default
38my $ipvnum = 4;      # default IP version of tftp server
39my $idnum = 1;       # dafault tftp server instance number
40my $proto = 'tftp';  # protocol the tftp server speaks
41my $pidfile;         # tftp server pid file
42my $logfile;         # tftp server log file
43my $srcdir;
44my $fork;
45
46my $flags  = "";
47my $path   = '.';
48my $logdir = $path .'/log';
49
50while(@ARGV) {
51    if($ARGV[0] eq '--pidfile') {
52        if($ARGV[1]) {
53            $pidfile = $ARGV[1];
54            shift @ARGV;
55        }
56    }
57    elsif($ARGV[0] eq '--logfile') {
58        if($ARGV[1]) {
59            $logfile = $ARGV[1];
60            shift @ARGV;
61        }
62    }
63    elsif($ARGV[0] eq '--srcdir') {
64        if($ARGV[1]) {
65            $srcdir = $ARGV[1];
66            shift @ARGV;
67        }
68    }
69    elsif($ARGV[0] eq '--ipv4') {
70        $ipvnum = 4;
71    }
72    elsif($ARGV[0] eq '--ipv6') {
73        $ipvnum = 6;
74    }
75    elsif($ARGV[0] eq '--port') {
76        if($ARGV[1] =~ /^(\d+)$/) {
77            $port = $1;
78            shift @ARGV;
79        }
80    }
81    elsif($ARGV[0] eq '--id') {
82        if($ARGV[1] =~ /^(\d+)$/) {
83            $idnum = $1 if($1 > 0);
84            shift @ARGV;
85        }
86    }
87    elsif($ARGV[0] eq '--verbose') {
88        $verbose = 1;
89    }
90    else {
91        print STDERR "\nWarning: tftpserver.pl unknown parameter: $ARGV[0]\n";
92    }
93    shift @ARGV;
94}
95
96if(!$srcdir) {
97    $srcdir = $ENV{'srcdir'} || '.';
98}
99if(!$pidfile) {
100    $pidfile = "$path/". server_pidfilename($proto, $ipvnum, $idnum);
101}
102if(!$logfile) {
103    $logfile = server_logfilename($logdir, $proto, $ipvnum, $idnum);
104}
105
106$flags .= "--pidfile \"$pidfile\" --logfile \"$logfile\" ";
107$flags .= "--ipv$ipvnum --port $port --srcdir \"$srcdir\"";
108
109exec("server/tftpd $flags");
110