⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rt61apd.c

📁 Ralink RT61 SoftAP Driver source code. RT61:MiniPCI
💻 C
📖 第 1 页 / 共 2 页
字号:
	switch (msg->msgsubtype)
	{
		case OPEN_SERVICE_REQ:
			bMsgReady = TRUE;
			ApdSendMsg(rtapd, OPEN_SERVICE_RSP, NULL, 0);
			DBGPRINT(RT_DEBUG_TRACE,"ApdProcessIPCMsg (bMsgReady is TRUE)\n");
			break;

		case CLOSE_SERVICE_REQ:
			bMsgReady = FALSE;
			DBGPRINT(RT_DEBUG_TRACE,"ApdProcessIPCMsg (bMsgReady is FALSE)\n");
			break;

		case SET_OID_REQ:
			DBGPRINT(RT_DEBUG_TRACE,"ApdProcessIPCMsg (Receive SET_OID_REQ)\n");
			{
				POID_REQ		oidreq;

				oidreq = (POID_REQ)msg->buf;
				
				DBGPRINT(RT_DEBUG_TRACE,"OID=%x, LEN=%d\n",	oidreq->oid, oidreq->len);
				RT_ioctl(rtapd, RT_PRIV_IOCTL, oidreq->buf, oidreq->len, 0, oidreq->oid);
			}
			break;

		case QUERY_OID_REQ:
			DBGPRINT(RT_DEBUG_TRACE,"ApdProcessIPCMsg (Receive QUERY_OID_REQ)\n");
			break;
			
		default:
			DBGPRINT(RT_DEBUG_ERROR,"ApdProcessIPCMsg (unknown subtype)\n");
			break;
	}

	return 0;
}

static void usage(void)
{
#if MULTIPLE_RADIUS    
	DBGPRINT(RT_DEBUG_TRACE, "USAGE :  rt61apd <wireless_if_name> \n");
	exit(1);
#else	
    DBGPRINT(RT_DEBUG_TRACE, "USAGE :  rt61apd \n");
#endif	
}

static rtapd * Apd_init(const char *config_file, int pid)
{
	rtapd *rtapd;
	int i;

	rtapd = malloc(sizeof(*rtapd));
	if (rtapd == NULL)
    {
		DBGPRINT(RT_DEBUG_ERROR,"Could not allocate memory for hostapd data\n");
		exit(1);
	}
	memset(rtapd, 0, sizeof(*rtapd));

	rtapd->config_fname = strdup(config_file);
	if (rtapd->config_fname == NULL)
    {
		DBGPRINT(RT_DEBUG_ERROR,"Could not allocate memory for config_fname\n");
		exit(1);
	}

	rtapd->conf = Config_read(rtapd->config_fname, pid);
	if (rtapd->conf == NULL)
    {
		DBGPRINT(RT_DEBUG_ERROR,"Could not allocate memory for rtapd->conf \n");
		free(rtapd->config_fname);
		exit(1);
	}

	if (ApdSetupMsg(rtapd) < 0)
    {
		DBGPRINT(RT_DEBUG_ERROR,"Could not setup msg\n");
		free(rtapd->config_fname);
		exit(1);
	}

	for (i = 0; i < rtapd->conf->SsidNum; i++)
	    rtapd->sock[i] = -1;
	rtapd->ioctl_sock = -1;

	return rtapd;
}

static void Handle_usr1(int sig, void *eloop_ctx, void *signal_ctx)
{
	struct hapd_interfaces *rtapds = (struct hapd_interfaces *) eloop_ctx;
	struct rtapd_config *newconf;
	int i;

	DBGPRINT(RT_DEBUG_TRACE,"Reloading configuration\n");
	for (i = 0; i < rtapds->count; i++)
    {
		rtapd *rtapd = rtapds->rtapd[i];
		newconf = Config_read(rtapd->config_fname,0);
		if (newconf == NULL)
        {
			DBGPRINT(RT_DEBUG_ERROR,"Failed to read new configuration file - continuing with old.\n");
			continue;
		}

		/* TODO: update dynamic data based on changed configuration
		 * items (e.g., open/close sockets, remove stations added to
		 * deny list, etc.) */
		Radius_client_flush(rtapd);
		Config_free(rtapd->conf);
		rtapd->conf = newconf;
        Apd_free_stas(rtapd);

/* when reStartAP, no need to reallocate sock
        for (i = 0; i < rtapd->conf->SsidNum; i++)
        {
            if (rtapd->sock[i] >= 0)
                close(rtapd->sock[i]);
                
    	    rtapd->sock[i] = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    	    if (rtapd->sock[i] < 0)
            {
    		    perror("socket[PF_PACKET,SOCK_RAW]");
    		    return;
    	    }
        }*/

	    if (Radius_client_init(rtapd))
        {
		    DBGPRINT(RT_DEBUG_ERROR,"RADIUS client initialization failed.\n");
		    return;
	    }

        DBGPRINT(RT_DEBUG_TRACE,"rtapd->radius->auth_serv_sock = %d\n",rtapd->radius->auth_serv_sock);
	}
}

static void Handle_usr2(int sig, void *eloop_ctx, void *signal_ctx)
{
	struct hapd_interfaces	*rtapds = (struct hapd_interfaces *) eloop_ctx;
	RT_SIGNAL_STRUC			data;

	DBGPRINT(RT_DEBUG_TRACE,"Handle_usr2-->\n");
	
	RT_ioctl(rtapds->rtapd[0], RT_PRIV_IOCTL, (char*) &data, sizeof(RT_SIGNAL_STRUC), 0, RT_QUERY_SIGNAL_CONTEXT);
	ApdSendMsg(rtapds->rtapd[0], SIGNAL_REQ, (char*) &data, sizeof(RT_SIGNAL_STRUC));
}

void Handle_term(int sig, void *eloop_ctx, void *signal_ctx)
{
	struct hapd_interfaces	*rtapds = (struct hapd_interfaces *) eloop_ctx;
	FILE    *f;
	char    buf[256], *pos;
	int     line = 0, i;
    int     filesize,cur = 0;
    char    *ini_buffer;             /* storage area for .INI file */
#if MULTIPLE_RADIUS   
	FILE 	*pidfile; 
    char 	file_path[1024];
#endif

	// First, terminate IAPP daemon
	{
		RT_SIGNAL_STRUC	sig;

		sig.Sig = SIG_TERMINATE;

		ApdSendMsg(rtapds->rtapd[0], SIGNAL_REQ, (char*) &sig, sizeof(RT_SIGNAL_STRUC));
		sleep(1);
	}

	DBGPRINT(RT_DEBUG_ERROR,"Signal %d received - terminating\n", sig);
	f = fopen(RT61AP_SYSTEM_PATH, "r+");
	if (f == NULL)
    {
		DBGPRINT(RT_DEBUG_ERROR,"Could not open configuration file '%s' for reading.\n", RT61AP_SYSTEM_PATH);
		return;
	}

    if ((fseek(f, 0, SEEK_END))!=0)
        return;
    filesize=ftell(f);
	DBGPRINT(RT_DEBUG_ERROR,"filesize %d   - terminating\n", filesize);

    if ((ini_buffer=(char *)malloc(filesize + 1 ))==NULL)
        return;   //out of memory
    fseek(f,0,SEEK_SET);
    fread(ini_buffer, filesize, 1, f);
    fseek(f,0,SEEK_SET);
    ini_buffer[filesize]='\0';

	while ((fgets(buf, sizeof(buf), f)))
    {
		line++;
		if (buf[0] == '#')
			continue;
		pos = buf;
		while (*pos != '\0')
        {
			if (*pos == '\n')
            {
				*pos = '\0';
				break;
			}
			pos++;
		}
		if (buf[0] == '\0')
			continue;

		pos = strchr(buf, '=');
		if (pos == NULL)
        {
		    pos = strchr(buf, '[');                
			continue;
		}
		*pos = '\0';
		pos++;

        if ((strcmp(buf, "pid") == 0) )
        {
            cur = 0;
            while(cur < (int)filesize)
            {  
                if ((ini_buffer[cur]=='p') && (ini_buffer[cur+1]=='i') && (ini_buffer[cur+2]=='d'))
                {
                    cur += 4;
                    for( i=4; i>=0; i--)
                    {
                        if (ini_buffer[cur] !='\n' )
                        {
                            ini_buffer[cur] =0x30;
                        }
                        else
                        {
                            break;
                        }
                        cur++;
                    }   
                    break;
                }
                cur++;
            }
		} 
    }
    fseek(f,0,SEEK_SET);
    fprintf(f, "%s", ini_buffer);    
    fclose(f);

#if MULTIPLE_RADIUS 
	DBGPRINT(RT_DEBUG_TRACE,"terminating 802.1x for (%s)\n",wireless_ifname);
	sprintf(file_path,"/var/run/auth_%s.pid",wireless_ifname);

    if ((pidfile = fopen(file_path, "w")) != NULL) {
		fprintf(pidfile, "%d\n", 0);
		(void) fclose(pidfile);
    } else {
		DBGPRINT(RT_DEBUG_ERROR, "Failed to create pid file %s\n", file_path);
    }
#endif

	eloop_terminate();
}

void IappTimeoutFunc(void *eloop_ctx, void *timeout_ctx)
{
	rtapd			*rtapd = eloop_ctx;
	struct msgbuf	msg;
	int				flag = IPC_NOWAIT;
	int				msglen;

	DBGPRINT(RT_DEBUG_INFO, "IappTimeoutFunc --->\n");

	if ((msglen = msgrcv(msgid, &msg, sizeof(struct msgbuf), IAPPMSGQUEID, flag)) > 0)
	{
		ApdProcessIPCMsg(rtapd, &msg, msglen);
	}

	eloop_register_timeout(0, 1000, IappTimeoutFunc, rtapd, NULL);
}

int main(int argc, char *argv[])
{
	struct hapd_interfaces interfaces;
       pid_t child_pid;
	int ret = 1, i;
	int c;
       pid_t auth_pid;
    
#if MULTIPLE_RADIUS     
	if(argc != 2)
		usage();
	
	for (;;)
    {
		c = getopt(argc, argv, "h");
		if (c < 0)
			break;

		switch (c)
        {
		    case 'h':
		        usage();
			    break;

		    default:
		    	usage();
			    break;
		}
	} 

	strcpy(wireless_ifname,argv[1]);
	//if prefix is not "ra", please modify here.
	if(strncmp(wireless_ifname,"ra",2)!=0)
	{
		DBGPRINT(RT_DEBUG_ERROR, "Invalid Interface Name.\n");	
		usage();
	}
	DBGPRINT(RT_DEBUG_TRACE, "Wireless Ifname = %s\n",wireless_ifname);
#else
    
	for (;;)
    {
		c = getopt(argc, argv, "h");
		if (c < 0)
			break;

		switch (c)
        {
		    case 'h':
		        usage();
			    break;

		    default:
			    break;
		}
	} 

#endif	
	
    child_pid = fork();
    if (child_pid == 0)
    {           
		auth_pid = getpid();
		DBGPRINT(RT_DEBUG_TRACE, "Porcess ID = %d\n",auth_pid);
        
        openlog("rt61apd",0,LOG_DAEMON);
        // set number of configuration file 1
        interfaces.count = 1;
        interfaces.rtapd = malloc(sizeof(rtapd *));
        if (interfaces.rtapd == NULL)
        {
            DBGPRINT(RT_DEBUG_ERROR,"malloc failed\n");
            exit(1);    
        }

        eloop_init(&interfaces);
        eloop_register_signal(SIGINT, Handle_term, NULL);
        eloop_register_signal(SIGTERM, Handle_term, NULL);
        eloop_register_signal(SIGUSR1, Handle_usr1, NULL);
        eloop_register_signal(SIGUSR2, Handle_usr2, NULL);
        eloop_register_signal(SIGHUP, Handle_usr1, NULL);

        DBGPRINT(RT_DEBUG_ERROR,"\n Configuration file: %s\n", RT61AP_SYSTEM_PATH);
        interfaces.rtapd[0] = Apd_init(strdup(RT61AP_SYSTEM_PATH),(int)getpid());
        if (!interfaces.rtapd[0])
            goto out;
        if (Apd_setup_interface(interfaces.rtapd[0]))
            goto out;
        
        eloop_register_timeout(0, 1000, IappTimeoutFunc, interfaces.rtapd[0], NULL);
        RT_ioctl(interfaces.rtapd[0], RT_PRIV_IOCTL, (u8*)&auth_pid, sizeof(int), 0, RT_SET_APD_PID | OID_GET_SET_TOGGLE);
        
#if MULTIPLE_RADIUS        
        create_pidfile(wireless_ifname);
#endif        
        eloop_run();

        Apd_free_stas(interfaces.rtapd[0]);
	    ret = 0;

out:
	    for (i = 0; i < interfaces.count; i++)
        {
		    if (!interfaces.rtapd[i])
			    continue;

		    Apd_cleanup(interfaces.rtapd[i]);
		    free(interfaces.rtapd[i]);
	    }

	    free(interfaces.rtapd);
	    eloop_destroy();
        closelog();
	    return ret;
    }
    else        
        return 0;
}

#if MULTIPLE_RADIUS      
static void create_pidfile(const char* ifname)
{
    FILE *pidfile;
	char file_path[1024];

	sprintf(file_path,"/var/run/auth_%s.pid",ifname);

    if ((pidfile = fopen(file_path, "w")) != NULL) {
		fprintf(pidfile, "%d\n", getpid());
		(void) fclose(pidfile);
    } else {
		DBGPRINT(RT_DEBUG_ERROR, "Failed to create pid file %s\n", file_path);
    }
}
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -