]> code.citadel.org Git - citadel.git/blob - citadel/netpoll.c
irix fixen
[citadel.git] / citadel / netpoll.c
1 /* $Id$ */
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <stdio.h>
5 #include <fcntl.h>
6 #include <string.h>
7 #include <errno.h>
8 #include "citadel.h"
9 #include "tools.h"
10
11 /*
12  * This variable defines the amount of network spool data that may be carried
13  * in one server transfer command.  For some reason, some networks get hung
14  * up on larger packet sizes.  We don't know why.  In any case, never set the
15  * packet size higher than 4096 or your server sessions will crash.
16  */
17 #define IGNET_PACKET_SIZE 64
18
19 long atol(const char *);
20
21 void attach_to_server(int argc, char **argv);
22 void serv_read(char *buf, int bytes);
23 void serv_write(char *buf, int nbytes);
24 void get_config(void);
25 struct config config;
26
27
28 void logoff(int code)
29 {
30         exit(code);
31         }
32
33
34 /*
35  * receive network spool from the remote system
36  */
37 void receive_spool(void) {
38         long download_len;
39         long bytes_received;
40         char buf[256];
41         static char pbuf[IGNET_PACKET_SIZE];
42         char tempfilename[64];
43         long plen;
44         FILE *fp;
45
46         sprintf(tempfilename,"/tmp/netpoll.%ld",(long)getpid());
47         serv_puts("NDOP");
48         serv_gets(buf);
49         printf("%s\n",buf);
50         if (buf[0]!='2') return;
51         download_len = extract_long(&buf[4],0);
52
53         bytes_received = 0L;
54         fp = fopen(tempfilename,"w");
55         if (fp==NULL) {
56                 perror("cannot open download file locally");
57                 return;
58                 }
59
60         while (bytes_received < download_len) {
61                 sprintf(buf,"READ %ld|%ld",
62                         bytes_received,
63                         ( (download_len - bytes_received > IGNET_PACKET_SIZE)
64                           ? IGNET_PACKET_SIZE : (download_len - bytes_received) ) );
65                 serv_puts(buf);
66                 serv_gets(buf);
67                 if (buf[0]=='6') {
68                         plen = extract_long(&buf[4],0);
69                         serv_read(pbuf,plen);
70                         fwrite((char *)pbuf,plen,1,fp);
71                         bytes_received = bytes_received + plen;
72                         }
73                 }
74
75         fclose(fp);
76         serv_puts("CLOS");
77         serv_gets(buf);
78         printf("%s\n",buf);
79         sprintf(buf,"mv %s %s/network/spoolin/netpoll.%ld",
80                 tempfilename,BBSDIR,(long)getpid());
81         system(buf);
82         system("exec nohup ./netproc >/dev/null 2>&1 &");
83         }
84
85 /*
86  * transmit network spool to the remote system
87  */
88 void transmit_spool(char *remote_nodename)
89 {
90         char buf[256];
91         char pbuf[4096];
92         long plen;
93         long bytes_to_write, thisblock;
94         int fd;
95         char sfname[128];
96
97         serv_puts("NUOP");
98         serv_gets(buf);
99         printf("%s\n",buf);
100         if (buf[0]!='2') return;
101
102         sprintf(sfname,"%s/network/spoolout/%s",BBSDIR,remote_nodename);
103         fd = open(sfname,O_RDONLY);
104         if (fd<0) {
105                 if (errno == ENOENT) {
106                         printf("Nothing to send.\n");
107                         }
108                 else {
109                         perror("cannot open upload file locally");
110                         }
111                 return;
112                 }
113
114         while ( plen=(long)read(fd,pbuf,IGNET_PACKET_SIZE), plen>0L) {
115                 bytes_to_write = plen; 
116                 while (bytes_to_write > 0L) {
117                         sprintf(buf,"WRIT %ld", bytes_to_write);
118                         serv_puts(buf);
119                         serv_gets(buf);
120                         thisblock = atol(&buf[4]);
121                         if (buf[0]=='7') {
122                                 serv_write(pbuf, (int)thisblock);
123                                 bytes_to_write = bytes_to_write - thisblock;
124                                 }
125                         else {
126                                 goto ABORTUPL;
127                                 }
128                         }
129                 }
130
131 ABORTUPL:
132         close(fd);
133         serv_puts("UCLS 1");
134         serv_gets(buf);
135         printf("%s\n",buf);
136         if (buf[0]=='2') unlink(sfname);
137         }
138
139
140
141
142 int main(int argc, char **argv)
143 {
144         char buf[256];
145         char remote_nodename[32];
146         int a;
147
148         if (argc != 4) {
149                 fprintf(stderr,
150                 "%s: usage: %s <address> <port number> <remote netpassword>\n",
151                         argv[0],argv[0]);
152                 exit(1);
153                 }
154
155         get_config();
156
157         attach_to_server(argc,argv);
158         serv_gets(buf);
159         printf("%s\n",buf);
160         if ((buf[0]!='2')&&(strncmp(buf,"551",3))) {
161                 fprintf(stderr,"%s: %s\n",argv[0],&buf[4]);
162                 logoff(atoi(buf));
163                 }
164
165         serv_puts("INFO");
166         serv_gets(buf);
167         if (buf[0]=='1') {
168                 a = 0;
169                 while (serv_gets(buf), strcmp(buf,"000")) {
170                         if (a==1) strcpy(remote_nodename,buf);
171                         if (a==1) printf("Connected to: %s ",buf);
172                         if (a==2) printf("(%s) ",buf);
173                         if (a==6) printf("%s\n",buf);
174                         ++a;
175                         }
176                 }
177
178         if (!strcmp(remote_nodename,config.c_nodename)) {
179                 fprintf(stderr,"Connected to local system\n");
180                 }
181         else {
182                 sprintf(buf,"NETP %s|%s",config.c_nodename,argv[3]);
183                 serv_puts(buf);
184                 serv_gets(buf);
185                 printf("%s\n",buf);
186         
187                 /* only do the transfers if we authenticated correctly! */
188                 if (buf[0]=='2') {
189                         receive_spool();
190                         transmit_spool(remote_nodename);
191                         }
192                 }
193
194         serv_puts("QUIT");
195         serv_gets(buf);
196         exit(0);
197         }