1 
2 /* utftpd_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 "utftpd.h"
39 
40 int
41 utftpd_nak(int peer, int ec, const char *et, struct utftpd_ctrl *flags)
42 {
43 	struct tftphdr *ehdr;
44 	int length;
45 
46 	ehdr = flags->sendbuf.hdr;
47 	ehdr->th_opcode = htons((u_short)ERROR);
48 	ehdr->th_code = htons((u_short)ec);
49 	length=TFTP_OFFSET;
50 	if (et) {
51 		size_t l=strlen(et);
52 		memcpy(flags->sendbuf.buf+length,et,l+1);
53 		length+=l+1;
54 	} else
55 		flags->sendbuf.buf[length++]=0;
56 	if (send(peer, flags->sendbuf.buf, length, 0) != length) {
57 		int e=errno;
58 		syslog(LOG_ERR, "send() for NAK: %s",strerror(errno));
59 		errno=e;
60 		return 1;
61 	}
62 	return 0;
63 }
64 
65