File: udpmsg.c

package info (click to toggle)
xipmsg 0.8088-2.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 296 kB
  • ctags: 412
  • sloc: ansic: 3,313; makefile: 66; sh: 23
file content (317 lines) | stat: -rw-r--r-- 7,329 bytes parent folder | download | duplicates (3)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 * brocas.c - IP Messenger 1.20 protocol
 * Copyright (C) 1996 by candy
 */
char rcsid_brocas[] = "$Id: brocas.c,v 3.1 1996/11/21 13:28:23 candy Exp candy $";
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <unistd.h>
#include <sys/time.h> /* setitimer() */

/* includes below are order dependent */
#include <sys/param.h> /* htons() */
#include <sys/types.h> /* socket() */
#include <sys/socket.h> /* socket() */
#include <netinet/in.h> /* inet_addr() INADDR_ANY */
#include <arpa/inet.h> /* inet_addr() */
#include <netdb.h> /* gethostbyname() */

#ifdef SOCKS
#include <socks.h>
#endif

#include "xipmsg.h"
#include "kanji.h"
#include "brocas.h"
#include "dyna.h"

#if defined BSD4_4
#define SENDTO(s,msg,len,flags,to,tolen) sendto(s,msg,len,flags,to,tolen)
#define SETSOCKOPT(s,level,optname,optval,optlen) setsockopt(s,level,optname,optval,optlen)
#else
#define SENDTO(s,msg,len,flags,to,tolen) sendto(s,msg,len,flags,(struct sockaddr *)to,tolen)
#define SETSOCKOPT(s,level,optname,optval,optlen) setsockopt(s,level,optname,(char *)optval,optlen)
#endif

static int debug_flag;

static int bro_socket;
static int bro_port;
static char *bro_user;
static const char *bro_host;
static int verbose;

static char *myname;

#define lan_width 32
#define lan_height 32
static unsigned char lan_bits[] = {
 0xe0,0x00,0x00,0x1f,0x50,0x07,0xc0,0x35,0xb0,0xfa,0xbf,0x2a,0x58,0x55,0x55,
 0x35,0xa8,0xaa,0xaa,0x2a,0x58,0x55,0x55,0x55,0xa8,0xaa,0xaa,0x6a,0x58,0x55,
 0x55,0x55,0xa8,0xaa,0xaa,0x6a,0x58,0x01,0x55,0x50,0xa8,0x00,0x08,0x60,0x58,
 0x18,0x00,0x46,0x28,0x18,0x00,0x46,0x50,0x00,0x00,0x40,0x30,0x30,0x00,0x46,
 0x30,0x78,0x00,0x4f,0x30,0x78,0x00,0x4f,0x20,0x30,0x00,0x46,0x20,0x00,0x00,
 0x40,0x58,0x00,0x00,0x40,0x6c,0x00,0x00,0x40,0xd6,0x00,0x60,0x40,0xab,0x00,
 0xf0,0x40,0x55,0x01,0x60,0x20,0xaa,0x02,0x00,0x20,0x55,0x05,0x00,0x30,0xaa,
 0x1a,0x00,0x38,0x55,0x61,0xf8,0x6f,0xaa,0x80,0x27,0x55,0x55,0x00,0x04,0x6b,
 0xaa,0x00,0x88,0xd0,0x55,0x00,0x70,0xa8
};

static unsigned char *icon = lan_bits;

static unsigned long
atoaddr(const char *host)
{
	unsigned long ret = INADDR_NONE;
	struct hostent *he = gethostbyname(host);
	if (he == NULL) {
		fprintf(stderr, "%s: %s: unknown host.\n", myname, host);
	}
	if (he != NULL) {
		if (he->h_addrtype == AF_INET && he->h_length <= sizeof(ret)) {
			memcpy(&ret, he->h_addr_list[0], he->h_length);
		}
	}
	return ret;
}/* atoaddr */

static size_t
iov_total(const struct iov_t *v)
{
	size_t total = 0;
	while (v != NULL) {
		total += v->iov_len;
		v = v->iov_next;
	}/* while */
	return total;
}/* iov_total */

static void *
iov_gather(void *buf_, const struct iov_t *v)
{
	char *buf = buf_;
	while (v != NULL) {
		memcpy(buf, v->iov_base, v->iov_len);
		buf += v->iov_len;
		v = v->iov_next;
	}/* while */
	return buf_;
}/* iov_gather */

static ssize_t
sendtov(int so, int flags, const struct sockaddr *to, int tolen, const struct iov_t *iov)
{
	ssize_t err = -1;
	size_t total = iov_total(iov);
	char *buf = malloc(total);
	if (buf != NULL) {
		iov_gather(buf, iov);
		err = SENDTO(so, buf, total, flags, to, tolen);
		if (err < 0)
			perror("sendto");
		free(buf);
	}
	return err;
}/* sendtov */



static unsigned long msg_number;

static int
send_msgv(int so, const union saddr *to, unsigned long command, int retryflag, const struct iov_t *iov)
{
	char lbuf[2 + 12 + USERNAME_MAX + 1 + HOSTNAME_MAX + 1 + 12];
	int err = -1, flags = 0;
	struct iov_t v;
	sprintf(lbuf, "1:%ld:%.*s:%.*s:%lu:", msg_number++, USERNAME_MAX, bro_user, HOSTNAME_MAX, bro_host, command);
	SET_IOV(&v, (void *)iov, lbuf, strlen(lbuf));
	if (retryflag) {
		err = sendtov(so, flags, &to->sa, sizeof(to->sin), &v);
	}
	return err;
}/* send_msgv */

/*
 * ͥåȥϿ
 */

struct dynet_t STRUCT_DYNA(union saddr);
struct dynet_t dynet;

static int
network_add(const char *broad)
{
	unsigned long addr = atoaddr(broad);
	if (addr != INADDR_NONE) {
		union saddr *net;
		net = DYNA_NEXT(&dynet);
		if (net != NULL) {
			memset(net, '\0', sizeof(*net));
			net->sin.sin_family = AF_INET;
			net->sin.sin_addr.s_addr = atoaddr(broad);
			net->sin.sin_port = htons(bro_port);
		}
	}
	return 0;
}/* network_add */

static int
network_bro_send(int so, const char *msg)
{
	union saddr *sin = DYNA_BUF(&dynet);
	if (sin != NULL) {
		char *sjis = malloc(strlen(msg) + 1);
		if (sjis != NULL) {
			unsigned long cmd = IPMSG_SENDMSG | IPMSG_SENDCHECKOPT;
			int retry = 1;
			int i, err;
			struct iov_t v[2];
			stretos(sjis, msg);
			SET_IOV(&v[0], &v[1], sjis, strlen(sjis) + 1);
			SET_IOV(&v[1], NULL, (void *)icon, 128);
			for (i = 0; i < DYNA_USED(&dynet); i++) {
				err = send_msgv(so, sin, cmd, retry, v);
				sin++;
			}/* for */
			free(sjis);
		}
	}
	return 0;
}/* network_bro_send */

/*
 *
 */
int
bro_init(int port, const char *user, const char *host, const char * const *bros)
{
	int so = socket(PF_INET, SOCK_DGRAM, 0);
	DYNA_IZ(&dynet, 32);
	bro_port = port;
	bro_user = malloc(strlen(user) * 2 + 1);
	if (bro_user != NULL)
		stretos(bro_user, user);
	else
		bro_user = (char *)user;
	bro_host = host;
	msg_number = time(NULL);
	while (*bros != NULL)
		network_add(*bros++);
	if (so >= 0) {
		int optval = 1;
		int err = SETSOCKOPT(so, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval));
		if (err < 0) {
			close(so);
			so = -1;
		}
	}
	bro_socket = so;
	return so;
}/* bro_init */

static int
fnain(int so, FILE *fp)
{
	char buf[5120];
	size_t size = fread(buf, 1, sizeof(buf) - 1, fp);
	buf[size] = '\0';
	return network_bro_send(so, buf);
}/* fnain */

static int
nain(int so, const char *name)
{
	FILE *fp = fopen(name, "r");
	int err = -1;
	if (fp != NULL) {
		err = fnain(so, fp);
		fclose(fp);
	}
	return err;
}/* nain */

static char *
get_env(const char *e)
{
	char *ret = NULL, *p = getenv(e);
	if (p != NULL) {
		ret = malloc(strlen(p) + 1);
		if (ret != NULL)
			strcpy(ret, p);
	}
	return p;
}/* get_env */


static char usage_msg[] =
	"usage: %s [-v][-d n] {[-f file] | [-m message]} [-p port] [-u user] address address...\n"
	;

int
main(int argc, char *argv[])
{
	int ex, show_usage = 0, ch;
	int port = 2425;
	char *user = get_env("USER"), *filename = NULL, *message = NULL;
	myname = argv[0];
	while ((ch = getopt(argc, argv, "d:f:m:p:u:vV")) != EOF) {
		switch (ch) {
		default:
		case 'V':
			show_usage++;
			break;
		case 'd':
			debug_flag = strtol(optarg, NULL, 0);
			break;
		case 'f':
			filename = optarg;
			break;
		case 'm':
			message = optarg;
			break;
		case 'p':
			port = strtol(optarg, NULL, 0);
			break;
		case 'u':
			user = optarg;
			break;
		case 'v':
			verbose++;
			break;
		}/* switch */
	}/* while */
	if (argc - optind < 1)
		show_usage++;
	ex = 1;
	if (show_usage) {
		fprintf(stderr, usage_msg, myname);
	}
	else {
		const char * const *v = (const char * const *)&argv[optind];
		char hostname[64];
		int so;
		if (user == NULL)
			user = "anonymous";
		gethostname(hostname, sizeof(hostname));
		if (strchr(hostname, '.') != NULL)
			*strchr(hostname, '.') = '\0';
		so = bro_init(port, user, hostname, v);
		if (so >= 0) {
			int err = 0;
			if (message != NULL)
				err = network_bro_send(so, message);
			else if (filename != NULL)
				err = nain(so, filename);
			else
				err = fnain(so, stdin);
			ex = err != 0;
		}
	}
	return ex;
}/* main */