1 
2 /* tftp_nak.c: send a NAK */
3 
4 /*
5  * Copyright (C) 1999 Uwe Ohse
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 #include "config.h"
23 #include <sys/types.h>
24 #include <sys/ioctl.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include "no_tftp.h"
31 /* #include <arpa/inet.h> */
32 
33 #include <syslog.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include "timselsysdep.h"
38 #include "str2num.h"
39 #include "uostr.h"
40 #include "uoio.h"
41 #include "tftplib.h"
42 
43 int
tftp_nak(int peer,int ec,const char * et,struct tftplib_ctrl * flags)44 tftp_nak(int peer, int ec, const char *et, struct tftplib_ctrl *flags)
45 {
46 	struct tftphdr *ehdr;
47 	int length;
48 
49 	ehdr = flags->sendbuf.hdr;
50 	ehdr->th_opcode = htons((u_short)ERROR);
51 	ehdr->th_code = htons((u_short)ec);
52 	length=TFTP_OFFSET;
53 	if (et) {
54 		size_t l=strlen(et);
55 		memcpy(flags->sendbuf.buf+length,et,l+1);
56 		length+=l+1;
57 	} else
58 		flags->sendbuf.buf[length++]=0;
59 	if (send(peer, flags->sendbuf.buf, length, 0) != length) {
60 		int e=errno;
61 		syslog(LOG_ERR, "send() for NAK: %s",strerror(errno));
62 		errno=e;
63 		return 1;
64 	}
65 	return 0;
66 }
67 
68