Debian Codegraph @

rplayd/server.c

raw symbols ◀ ▶ SCIP
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 <time.h>
38 #include "rplayd.h"
39 #include "server.h"
40 #include "connection.h"
41 #include "buffer.h"
42
43 BUFFER *server_list = NULL;
44 SERVER *servers = NULL;
45
46 static time_t server_read_time;
47
48 #ifdef __STDC__
49 void
50 server_read(char *filename)
51 #else
52 void
53 server_read(filename)
54 char *filename;
55 #endif
56 {
57 FILE *fp;
58 char buf[BUFSIZ], *p;
59 char line[RPTP_MAX_LINE];
60 struct hostent *hp, *local_hp;
61 unsigned long addr, local_addr;
62 struct sockaddr_in sin;
63 int port, n;
64 SERVER **s = &servers;
65 BUFFER *b;
66
67 server_read_time = time(0);
68
69 fp = fopen(filename, "r");
70 if (fp == NULL)
71 {
72 /*
73 * I guess it's ok to not have any servers.
74 */
75 report(REPORT_NOTICE, "warning: cannot open %s\n", filename);
76 return;
77 }
78
79 local_hp = gethostbyname(hostname);
80 if (local_hp == NULL)
81 {
82 report(REPORT_ERROR, "server_read: %s unknown host?!\n", hostname);
83 done(1);
84
85 }
86 memcpy((char *) &local_addr, (char *) local_hp->h_addr, local_hp->h_length);
87
88 b = buffer_create();
89 strcpy(b->buf, "+message=\"servers\"\r\n");
90 b->nbytes += strlen(b->buf);
91 b->status = BUFFER_KEEP;
92 server_list = b;
93
94 while (fgets(buf, sizeof(buf), fp))
95 {
96 switch (buf[0])
97 {
98 case '#':
99 case ' ':
100 case '\t':
101 case '\n':
102 continue;
103 }
104
105 p = strchr(buf, '\n');
106 if (p)
107 {
108 *p = '\0';
109 }
110
111 p = strchr(buf, ':');
112 if (p)
113 {
114 *p = '\0';
115 port = atoi(p + 1);
116 }
117 else
118 {
119 port = RPTP_PORT;
120 }
121 addr = inet_addr(buf);
122 memset((char *) &sin, 0, sizeof(sin));
123 if (addr == 0xffffffff)
124 {
125 hp = gethostbyname(buf);
126 if (hp == NULL)
127 {
128 report(REPORT_NOTICE, "warning: %s unknown host in %s\n", buf, filename);
129 continue;
130 }
131 memcpy((char *) &sin.sin_addr.s_addr, (char *) hp->h_addr, hp->h_length);
132 }
133 else
134 {
135 memcpy((char *) &sin.sin_addr.s_addr, (char *) &addr, sizeof(addr));
136 }
137
138 if (memcmp((char *) &sin.sin_addr.s_addr, (char *) &local_addr, sizeof(local_addr)) == 0)
139 {
140 /*
141 * ignore the server if it's the local host
142 */
143 continue;
144 }
145
146 *s = (SERVER *) malloc(sizeof(SERVER));
147 if (*s == NULL)
148 {
149 report(REPORT_ERROR, "server_read: out of memory\n");
150 done(1);
151 }
152 sin.sin_family = AF_INET;
153 sin.sin_port = htons(port);
154
155 SNPRINTF(SIZE(line, sizeof(line)), "host=%s port=%d\r\n", buf, port);
156 n = strlen(line);
157
158 if (b->nbytes + n > BUFFER_SIZE)
159 {
160 b->next = buffer_create();
161 b = b->next;
162 b->status = BUFFER_KEEP;
163 }
164 SNPRINTF(SIZE(b->buf + b->nbytes, BUFFER_SIZE - b->nbytes), line);
165 b->nbytes += n;
166
167 (*s)->sin = sin;
168 s = &(*s)->next;
169
170 report(REPORT_DEBUG, "server %s port %d\n", inet_ntoa(sin.sin_addr), port);
171 }
172
173 *s = NULL;
174 fclose(fp);
175
176 if (b->nbytes + 3 > BUFFER_SIZE)
177 {
178 b->next = buffer_create();
179 b = b->next;
180 b->status = BUFFER_KEEP;
181 }
182 SNPRINTF(SIZE(b->buf + b->nbytes, BUFFER_SIZE - b->nbytes), ".\r\n");
183 b->nbytes += 3;
184 }
185
186 #ifdef __STDC__
187 void
188 server_reread(char *filename)
189 #else
190 void
191 server_reread(filename)
192 char *filename;
193 #endif
194 {
195 BUFFER *b, *bb;
196 SERVER *s, *ss;
197
198 report(REPORT_DEBUG, "re-reading servers\n");
199
200 /*
201 * Free the server_list buffers.
202 */
203 for (b = server_list; b; bb = b, b = b->next, bb->status = BUFFER_FREE, buffer_destroy(bb)) ;
204
205 /*
206 * Free the servers list.
207 */
208 for (s = servers; s; ss = s, s = s->next, free((char *) ss)) ;
209
210 server_list = NULL;
211 servers = NULL;
212 server_read(filename);
213 }
214
215 #ifdef __STDC__
216 void
217 server_stat(char *filename)
218 #else
219 void
220 server_stat(filename)
221 char *filename;
222 #endif
223 {
224 if (modified(filename, server_read_time))
225 {
226 server_reread(filename);
227 }
228 }