Debian Codegraph @

rplayd/server.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /* $Id: server.c,v 1.5 1999/03/10 07:58:04 boyns Exp $ */
2
3 /*
4 * Copyright (C) 1993-99 Mark R. Boyns <[email protected]>
5 *
6 * This file is part of rplay.
7 *
8 * rplay is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * rplay is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with rplay; see the file COPYING. If not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 #include "rplay.h"
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <netdb.h>
33 #include <stdlib.h>
34 #ifdef HAVE_STRING_H
35 #include <string.h>
36 #endif
37 #include "rplayd.h"
38 #include "server.h"
39 #include "connection.h"
40 #include "buffer.h"
41
42 BUFFER *server_list = NULL;
43 SERVER *servers = NULL;
44
45 static time_t server_read_time;
46
47 #ifdef __STDC__
48 void
49 server_read(char *filename)
50 #else
51 void
52 server_read(filename)
53 char *filename;
54 #endif
55 {
56 FILE *fp;
57 char buf[BUFSIZ], *p;
58 char line[RPTP_MAX_LINE];
59 struct hostent *hp, *local_hp;
60 unsigned long addr, local_addr;
61 struct sockaddr_in sin;
62 int port, n;
63 SERVER **s = &servers;
64 BUFFER *b;
65
66 server_read_time = time(0);
67
68 fp = fopen(filename, "r");
69 if (fp == NULL)
70 {
71 /*
72 * I guess it's ok to not have any servers.
73 */
74 report(REPORT_NOTICE, "warning: cannot open %s\n", filename);
75 return;
76 }
77
78 local_hp = gethostbyname(hostname);
79 if (local_hp == NULL)
80 {
81 report(REPORT_ERROR, "server_read: %s unknown host?!\n", hostname);
82 done(1);
83
84 }
85 memcpy((char *) &local_addr, (char *) local_hp->h_addr, local_hp->h_length);
86
87 b = buffer_create();
88 strcpy(b->buf, "+message=\"servers\"\r\n");
89 b->nbytes += strlen(b->buf);
90 b->status = BUFFER_KEEP;
91 server_list = b;
92
93 while (fgets(buf, sizeof(buf), fp))
94 {
95 switch (buf[0])
96 {
97 case '#':
98 case ' ':
99 case '\t':
100 case '\n':
101 continue;
102 }
103
104 p = strchr(buf, '\n');
105 if (p)
106 {
107 *p = '\0';
108 }
109
110 p = strchr(buf, ':');
111 if (p)
112 {
113 *p = '\0';
114 port = atoi(p + 1);
115 }
116 else
117 {
118 port = RPTP_PORT;
119 }
120 addr = inet_addr(buf);
121 memset((char *) &sin, 0, sizeof(sin));
122 if (addr == 0xffffffff)
123 {
124 hp = gethostbyname(buf);
125 if (hp == NULL)
126 {
127 report(REPORT_NOTICE, "warning: %s unknown host in %s\n", buf, filename);
128 continue;
129 }
130 memcpy((char *) &sin.sin_addr.s_addr, (char *) hp->h_addr, hp->h_length);
131 }
132 else
133 {
134 memcpy((char *) &sin.sin_addr.s_addr, (char *) &addr, sizeof(addr));
135 }
136
137 if (memcmp((char *) &sin.sin_addr.s_addr, (char *) &local_addr, sizeof(local_addr)) == 0)
138 {
139 /*
140 * ignore the server if it's the local host
141 */
142 continue;
143 }
144
145 *s = (SERVER *) malloc(sizeof(SERVER));
146 if (*s == NULL)
147 {
148 report(REPORT_ERROR, "server_read: out of memory\n");
149 done(1);
150 }
151 sin.sin_family = AF_INET;
152 sin.sin_port = htons(port);
153
154 SNPRINTF(SIZE(line, sizeof(line)), "host=%s port=%d\r\n", buf, port);
155 n = strlen(line);
156
157 if (b->nbytes + n > BUFFER_SIZE)
158 {
159 b->next = buffer_create();
160 b = b->next;
161 b->status = BUFFER_KEEP;
162 }
163 SNPRINTF(SIZE(b->buf + b->nbytes, BUFFER_SIZE - b->nbytes), line);
164 b->nbytes += n;
165
166 (*s)->sin = sin;
167 s = &(*s)->next;
168
169 report(REPORT_DEBUG, "server %s port %d\n", inet_ntoa(sin.sin_addr), port);
170 }
171
172 *s = NULL;
173 fclose(fp);
174
175 if (b->nbytes + 3 > BUFFER_SIZE)
176 {
177 b->next = buffer_create();
178 b = b->next;
179 b->status = BUFFER_KEEP;
180 }
181 SNPRINTF(SIZE(b->buf + b->nbytes, BUFFER_SIZE - b->nbytes), ".\r\n");
182 b->nbytes += 3;
183 }
184
185 #ifdef __STDC__
186 void
187 server_reread(char *filename)
188 #else
189 void
190 server_reread(filename)
191 char *filename;
192 #endif
193 {
194 BUFFER *b, *bb;
195 SERVER *s, *ss;
196
197 report(REPORT_DEBUG, "re-reading servers\n");
198
199 /*
200 * Free the server_list buffers.
201 */
202 for (b = server_list; b; bb = b, b = b->next, bb->status = BUFFER_FREE, buffer_destroy(bb)) ;
203
204 /*
205 * Free the servers list.
206 */
207 for (s = servers; s; ss = s, s = s->next, free((char *) ss)) ;
208
209 server_list = NULL;
210 servers = NULL;
211 server_read(filename);
212 }
213
214 #ifdef __STDC__
215 void
216 server_stat(char *filename)
217 #else
218 void
219 server_stat(filename)
220 char *filename;
221 #endif
222 {
223 if (modified(filename, server_read_time))
224 {
225 server_reread(filename);
226 }
227 }