This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ircd.c
180 lines (163 loc) · 4.38 KB
/
ircd.c
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
/*************************************************************************
** ircd.c Beta v2.0 (22 Aug 1988)
**
** This file is part of Internet Relay Chat v2.0
**
** Author: Jarkko Oikarinen
** Internet: [email protected]
** UUCP: ...!mcvax!tut!oulu!jto
** BITNET: toljto at finou
**
** Copyright (c) 1988 University of Oulu, Computing Center
**
** All rights reserved.
**
** See file COPYRIGHT in this package for full copyright.
**
*************************************************************************/
char ircd_id[] = "ircd.c v2.0 (c) 1988 University of Oulu, Computing Center";
#include <sys/types.h>
#include <sys/file.h>
#include <stdio.h>
#include <signal.h>
#include "struct.h"
char *version = "2.1.0";
char *info1 = "Programmed by Jarkko Oikarinen";
char *info2 = "(c) 1988,1989 University of Oulu, Computing Center";
char *info3 = "INTERNET: [email protected] BITNET: toljto at finou";
char myhostname[HOSTLEN+1];
char *welcome1 = "Welcome to Internet Relay Server v";
#define TRUE 1
extern int dummy();
extern aClient *make_client();
aClient me; /* That's me */
aClient *client = &me; /* Pointer to beginning of Client list */
extern aConfItem *conf;
extern aConfItem *find_me();
char *uphost;
char **myargv;
int portnum;
int debuglevel;
int maxusersperchannel = MAXUSERSPERCHANNEL;
restart()
{
int i;
debug(DEBUG_NOTICE,"Restarting server...");
#ifdef NOTTY
for (i=0; i<MAXFD; i++)
#else
for (i=3; i<MAXFD; i++)
#endif
close(i);
execv(MYNAME, myargv);
debug(DEBUG_FATAL,"Couldn't restart server !!!!!!!!");
exit(-1);
}
main(argc, argv)
int argc;
char *argv[];
{
aClient *cptr;
aConfItem *aconf;
int length; /* Length of message received from client */
char buffer[BUFSIZE];
long lasttime, lastconnecttry;
myargv = argv;
signal(SIGPIPE, SIG_IGN);
signal(SIGHUP, restart);
signal(SIGALRM, dummy);
setuid(geteuid());
if (getuid() == 0) {
setgid(-2);
setuid(-2);
}
getmyname(myhostname,HOSTLEN-1);
portnum = PORTNUM;
debuglevel = -1;
while (argc > 1 && argv[1][0] == '-') {
switch (argv[1][1]) {
case 'h':
strncpy(myhostname, &argv[1][2], HOSTLEN-1);
myhostname[HOSTLEN] = '\0';
break;
case 'p':
if ((length = atoi(&argv[1][2])) > 0 )
portnum = length;
break;
case 'x':
debuglevel = atoi(&argv[1][2]);
break;
#ifdef never
case 'c':
maxusersperchannel = atoi(&argv[1][2]);
break;
#endif
default:
printf("Illegal option\n");
break;
}
argv++; argc--;
}
if (argc > 1 && argv[1][0] > '9')
uphost = argv[1];
else
uphost = NULL;
init_sys();
OpenLog();
initconf();
if (aconf = find_me())
strcpy(me.server, aconf->name);
else
me.server[0] = '\0';
me.next = NULL;
strcpy(me.host,myhostname);
me.fromhost[0] = me.nickname[0] = me.realname[0] = '\0';
me.fd = open_port(portnum);
me.status = STAT_ME;
lastconnecttry = getlongtime();
for (aconf = conf ; aconf; aconf=aconf->next )
if (aconf->status == CONF_CONNECT_SERVER && aconf->port > 0) {
if (!connect_server(aconf))
break;
}
debug(DEBUG_DEBUG,"Server ready...");
for (;;) {
if ((length = read_msg(buffer, BUFSIZE, &cptr)) > 0) {
cptr->lasttime = getlongtime();
CLRPINGSENT(cptr);
dopacket(cptr, buffer, length);
}
debug(DEBUG_DEBUG,"Got message");
lasttime = getlongtime();
if (lasttime - lastconnecttry > CONNECTFREQUENCY) {
lastconnecttry = lasttime;
for (aconf = conf; aconf; aconf=aconf->next) {
if (aconf->status != CONF_CONNECT_SERVER)
continue;
cptr = client;
for (cptr = client; cptr; cptr = cptr ->next)
if ((ISSERVER(cptr) || ISME(cptr)) &&
MyEqual(cptr->host, aconf->name, HOSTLEN))
break;
if (!cptr && aconf->port > 0)
if (!connect_server(aconf)) {
sendto_ops("Connection to %s established.", aconf->name);
break;
}
}
lasttime = getlongtime();
}
for (cptr = client; cptr; cptr = cptr->next)
if (!ISME(cptr) && cptr->fd >= 0 &&
(lasttime - cptr->lasttime) > PINGFREQUENCY) {
if ((lasttime - cptr->lasttime) > 2 * PINGFREQUENCY) {
m_bye(cptr, cptr, myhostname);
cptr = client;
}
else if (!ISPINGSENT(cptr)) {
SETPINGSENT(cptr);
sendto_one(cptr, "PING %s", myhostname);
}
}
}
}