1 /*
2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2021, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
5 *
6 * Empire is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * ---
20 *
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
24 *
25 * ---
26 *
27 * mailbox.c: Mailbox file access
28 *
29 * Known contributors to this file:
30 * Markus Armbruster, 2009-2011
31 */
32
33 #include <config.h>
34
35 #include <stdio.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <sys/stat.h>
39 #include <unistd.h>
40 #include "optlist.h"
41 #include "tel.h"
42 #include "prototypes.h"
43
44 char *
mailbox(char * buf,natid cn)45 mailbox(char *buf, natid cn)
46 {
47 sprintf(buf, "%s/tel%d", teldir, cn);
48 return buf;
49 }
50
51 /*
52 * Create an empty telegram file named @mbox.
53 * Return 0 on success, -1 on failure.
54 */
55 int
mailbox_create(char * mbox)56 mailbox_create(char *mbox)
57 {
58 int fd;
59
60 fd = creat(mbox, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
61 if (fd < 0 || close(fd) < 0) {
62 logerror("Can't create mailbox %s (%s)", mbox, strerror(errno));
63 return -1;
64 }
65 return 0;
66 }
67
68 /*
69 * Read telegram header from @fp into @tel.
70 * @mbox is the file name, it is used for logging errors.
71 * Return 1 on success, 0 on EOF, -1 on error.
72 */
73 int
tel_read_header(FILE * fp,char * mbox,struct telstr * tel)74 tel_read_header(FILE *fp, char *mbox, struct telstr *tel)
75 {
76 size_t n;
77
78 n = fread(tel, 1, sizeof(*tel), fp);
79 if (n == 0 && feof(fp))
80 return 0;
81 if (n != sizeof(*tel)
82 || tel->tel_type > TEL_LAST || tel->tel_from > MAXNOC) {
83 logerror("Mailbox %s corrupt: bad header", mbox);
84 return -1;
85 }
86 return 1;
87 }
88
89 /*
90 * Read telegram body from @fp.
91 * @mbox is the file name, it is used for logging errors.
92 * @tel is the header.
93 * Unless @sink is null, it is called like @sink(CHUNK, SZ, @arg) to
94 * consume the body, chunk by chunk. The chunks are UTF-8, and
95 * CHUNK[SZ] is 0. Reading fails when @sink() returns a negative
96 * value.
97 * Return 0 on success, -1 on failure.
98 */
99 int
tel_read_body(FILE * fp,char * mbox,struct telstr * tel,int (* sink)(char *,size_t,void *),void * arg)100 tel_read_body(FILE *fp, char *mbox, struct telstr *tel,
101 int (*sink)(char *, size_t, void *),
102 void *arg)
103 {
104 char buf[4096];
105 size_t left, sz;
106
107 left = tel->tel_length;
108 while (left) {
109 sz = MIN(left, sizeof(buf) - 1);
110 if (fread(buf, 1, sz, fp) != sz) {
111 logerror("Mailbox %s corrupt: can't read body", mbox);
112 return -1;
113 }
114 buf[sz] = 0;
115 if (sink && sink(buf, sz, arg) < 0)
116 return -1;
117 left -= sz;
118 }
119 return 0;
120 }
121