-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend.c
More file actions
124 lines (101 loc) · 2.74 KB
/
send.c
File metadata and controls
124 lines (101 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*-
* "THE BEER-WARE LICENSE" (Revision 42):
* <tobias.rehbein@web.de> wrote this file. As long as you retain this notice
* you can do whatever you want with this stuff. If we meet some day, and you
* think this stuff is worth it, you can buy me a beer in return.
*/
#define _POSIX_C_SOURCE 200809
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include "itemtypes.h"
#include "send.h"
static void send_fake_item(FILE *out, char type, const char *info,
const char *detail);
void
send_item(FILE *out, struct item *it)
{
assert(out != NULL);
assert(it != NULL);
fprintf(out, "%c%s\t%s\t%s\t%s\r\n", it->type, it->display,
it->selector, it->host, it->port);
}
void
send_error(FILE *out, const char *error, const char *detail)
{
assert(out != NULL);
assert(error != NULL);
send_fake_item(out, IT_ERROR, error, detail);
}
void
send_info(FILE *out, const char *info, const char *detail)
{
assert(out != NULL);
assert(info != NULL);
send_fake_item(out, IT_INFO, info, detail);
}
void
send_line(FILE *out, const char *line)
{
assert(out != NULL);
assert(line != NULL);
/*
* RFC 1436 states the following note for Textfile Entities:
*
* Lines beginning with periods must be prepended with an extra
* period to ensure that the transmission is not terminated early.
* The client should strip extra periods at the beginning of the
* line.
*
* Nonetheless every tested client (gopher, lynx, OverbiteFF) is not
* stripping leading periods. That is the reason the following lines are
* commented out. If you want a strict RFC compliant server, feel free
* to uncomment these lines.
*/
// if (*line == '.')
// fputc('.', out);
fprintf(out, "%s\r\n", line);
}
void
send_eom(FILE *out)
{
assert(out != NULL);
fputs(".\r\n", out);
}
static void
send_fake_item(FILE *out, char type, const char *info, const char *detail)
{
assert(out != NULL);
assert(info != NULL);
char *display = malloc(LINE_MAX);
if (display == NULL) {
/* Explicitely do not use gopherized messages to avoid
* recursions. */
syslog(LOG_ERR, "malloc error: %m");
fprintf(stderr, "malloc: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
if (detail == NULL)
strncpy(display, info, LINE_MAX-1);
else
snprintf(display, LINE_MAX, "%s: %s", info, detail);
char selector[sizeof(FAKESELECTOR)];
strncpy(selector, FAKESELECTOR, sizeof(FAKESELECTOR));
char host[sizeof(FAKEHOST)];
strncpy(host, FAKEHOST, sizeof(FAKEHOST));
char port[sizeof(FAKEPORT)];
strncpy(port, FAKEPORT, sizeof(FAKEPORT));
struct item it = {
.type = type,
.display = display,
.selector = selector,
.host = host,
.port = port
};
send_item(out, &it);
free(display);
}