Changes for kernel and Busybox
[tomato.git] / release / src / router / bpalogin / bpalogin.c
blob5d1e245a250efa8f579a877a4030f271b6590a3f
1 /*
2 ** BPALogin - lightweight portable BIDS2 login client
3 ** Copyright (c) 2001-3 Shane Hyde, and others.
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
21 #include "bpalogin.h"
24 ** Main processing loop. Logs in, handles heartbeats, and logs out on request
26 ** Returns - 0 - We are no longer connected, and should not retry.
27 ** 1 - We are no longer connnected and should retry.
29 int mainloop(struct session * s)
31 int err;
32 struct sockaddr_in listenaddr;
33 struct hostent * he;
34 int addrsize;
35 FILE *fp;
37 s->lastheartbeat = 0;
38 s->sequence = 0;
39 s->listensock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
41 s->localaddr.sin_family = AF_INET;
42 s->localaddr.sin_port = htons(s->localport);
43 if(strcmp(s->localaddress,""))
45 s->debug(0,"Using local address %s\n",s->localaddress);
46 he = gethostbyname(s->localaddress);
48 if(he)
50 s->localaddr.sin_addr.s_addr = *((int*)(he->h_addr_list[0]));
52 else
54 s->localaddr.sin_addr.s_addr = inet_addr(s->localaddress);
57 else
59 s->localaddr.sin_addr.s_addr = INADDR_ANY;
62 addrsize = sizeof(struct sockaddr_in);
63 err = bind(s->listensock,(struct sockaddr *)&s->localaddr,sizeof(s->localaddr));
64 err = getsockname(s->listensock,(struct sockaddr *)&listenaddr,&addrsize);
66 //s->sessionid = time(NULL);
67 s->sessionid = get_time(NULL);
69 s->listenport = ntohs(listenaddr.sin_port);
70 strcpy(s->osname,"whocares");
71 strcpy(s->osrelease,"notme");
73 he = gethostbyname(s->authserver);
75 if(he)
77 s->authhost.sin_addr.s_addr = *((int*)(he->h_addr_list[0]));
79 else
81 s->authhost.sin_addr.s_addr = inet_addr(s->authserver);
84 s->authhost.sin_port = htons(s->authport);
85 s->authhost.sin_family = AF_INET;
87 s->debug(0,"Auth host = %s:%d\n",s->authserver,s->authport);
88 s->debug(0,"Auth domain = %s\n",s->authdomain);
89 s->debug(0,"Listening on port %d\n",s->listenport);
91 if(test_connect_success // by honor, for debug
92 || login(s))
94 s->onconnected(s->listenport);
95 if(!handle_heartbeats(s))
97 int i;
98 s->ondisconnected(0);
99 s->noncritical("Sleeping for 10 seconds (1)");
100 for(i=0;i<10;i++)
102 if(s->shutdown)
103 return 0;
104 else
105 sleep(1);
108 else
110 closesocket(s->listensock);
111 return 0;
113 closesocket(s->listensock);
115 else
117 int i;
118 s->noncritical("Sleeping for 10 seconds (2)");
119 for(i=0;i<10;i++)
121 if(s->shutdown)
122 return 0;
123 else
124 sleep(1);
126 closesocket(s->listensock);
128 return 1;
132 log_to_file(char *buf) // add by honor
134 FILE *fp;
136 if ((fp = fopen("/tmp/ppp/log", "w"))) {
137 fprintf(fp, "%s", buf);
138 fclose(fp);
139 return 1;
141 return 0;