Source code Analysis of ROUTER/AP based on rtl819x implementation [i]

Source: Internet
Author: User
Tags small web server



*************************************************************************************************************** ************
Easywave Time: 2015.01.11



Category: Router class-source code analysis of ROUTER/AP based on rtl819x implementation [a] Disclaimer: reproduced, please keep the link



Note: If there is an error, please correct me. These are the journal articles I studied ...



*************************************************************************************************************** ************


A: A brief introduction to the deep customization of BOA based on rt819x platform


Detailed introduction to Boa, please refer to my blog: Embedded Linux under the BOA Web server porting details, Boa webserver is a single-task HTTP server, unlike other Web servers, is when there is a connection request to come, It does not create a process for each connection individually, nor does it use replication itself to process multiple links, but rather by establishing an HTTP request list to handle multiple HTTP connection requests, and it creates a new abbreviation for CGI programs, saving system resources to the greatest extent possible. This is very important for resource-constrained embedded systems, and it also automatically generates directories, auto-unzip files and so on, so Boa has high HTTP request processing speed and efficiency, which is of great value in embedded systems.


Second: Based on the rt819x platform of deep customization of the BOA source code Analysis


Based on the rt819x platform Router/ap is built on the open-source Boa Web server, by entering the BOA directory, you can find that the rt819x platform Router/ap is in the BOA Open source code on the depth of customization, the specific situation is as follows:







There are many more folders with the Open source Boa Web server, because the ROUTER/AP based on the rt819x implementation adds an ASP server to the open source Boa, and also implements a miniature small Web server, and the code for the microsite is as follows:







This is the entire ROUTER/AP front desk Settings page configuration microsite, this with our own home use of router, such as the need to enter the http://192.168.1.1 so that the role of entering the page setup is the same, all the settings are implemented in the HTML folder, Since the open source Boa does not support ASP servers, we need to make a deep customization of the BOA code, so let's look at how rt819x is implemented. Let's start by analyzing the entrance to the BOA main function, in the USERS/BOA/SRC/BOA.C file, as follows:



int main(int argc, char *argv[])
{
    int server_s;               /* boa socket */
    pid_t pid;
 
    /* set umask to u+rw, u-x, go-rwx */
    /* according to the man page, umask always succeeds */
    umask(077);
 
    /* but first, update timestamp, because log_error_time uses it */
    (void) time(¤t_time);
 
    /* set timezone right away */
    tzset();
 
    {
        int devnullfd = -1;
        devnullfd = open("/dev/null", 0);
 
        /* make STDIN point to /dev/null */
        if (devnullfd == -1) {
            DIE("can't open /dev/null");
        }
 
        if (dup2(devnullfd, STDIN_FILENO) == -1) {
            DIE("can't dup2 /dev/null to STDIN_FILENO");
        }
 
        (void) close(devnullfd);
    }
 
    parse_commandline(argc, argv);
    fixup_server_root();
 
#ifdef SUPPORT_ASP  extern void asp_init(int argc,char **argv); // davidhsu  asp_init(argc,argv);
#ifdef VOIP_SUPPORT  web_voip_init();
#endif 
#endif
 
    read_config_files();
    create_common_env();
    open_logs();
    server_s = create_server_socket();
    //init_signals(); //Brad comment out, move to later 
    build_needs_escape();
 
    /* background ourself */
    if (do_fork) {
        pid = fork();
    } else {
        pid = getpid();
    }
 
    switch (pid) {
    case -1:
        /* error */
        perror("fork/getpid");
        exit(EXIT_FAILURE);
    case 0:
        /* child, success */
        break;
    default:
        /* parent, success */
        if (pid_file != NULL) {
            FILE *PID_FILE = fopen(pid_file, "w");
            if (PID_FILE != NULL) {
                fprintf(PID_FILE, "%d\n", pid);
                fclose(PID_FILE);
            } else {
                perror("fopen pid file");
            }
        }
 
        if (do_fork)
            exit(EXIT_SUCCESS);
        break;
    }
 
    boa_start = 1;
    init_signals(); //Brad move here
    drop_privs();
    /* main loop */
    timestamp();
 
    status.requests = 0;
    status.errors = 0;
 
    start_time = current_time;
    alarm(1);
    loop(server_s);
    return 0;
}

Let's take a look at the macro definition above in support_asp alone, as shown below:


#ifdef support_asp

extern void Asp_init (int argc,char **argv); Davidhsu

Asp_init (ARGC,ARGV);

#ifdef Voip_support

Web_voip_init ();

#endif

#endif


So we see that the rt819x implementation of the ROUTER/AP is based on the open source Boa customized an ASP server, into the Asp_init (ARGC,ARGV) function to see, in Users/boa/src/asp_page.c, as follows:



void asp_init(int argc,char **argv)
{  int i, num;  char interface[10];  extern int getWlStaNum(char *interface, int *num);
   root_temp.next=NULL;  root_temp.str=NULL;     // david ---- queury number of wlan interface ----------------  wlan_num = 0;  for (i=0; i<NUM_WLAN_INTERFACE; i++) {  sprintf(interface, "wlan%d", i);  if (getWlStaNum(interface, &num) < 0)  break;  wlan_num++;  }   #if defined(VOIP_SUPPORT) && defined(ATA867x)  // no wlan interface in ATA867x
#else  if (wlan_num==0)  wlan_num = 1; // set 1 as default
#endif
 
#ifdef MBSSID  vwlan_num = NUM_VWLAN_INTERFACE; 
#endif  //---------------------------------------------------------
 
//conti:  root_temp.next = NULL;  root_temp.str = NULL;     if (apmib_init() == 0) {  printf("Initialize AP MIB failed!\n");  return;  }
   save_cs_to_file();  apmib_get(MIB_WAN_DHCP, (void *)&last_wantype);
   /* determine interface name by mib value */  WAN_IF = "eth1";  BRIDGE_IF = "br0";  ELAN_IF = "eth0";  ELAN2_IF = "eth2";  ELAN3_IF = "eth3";  ELAN4_IF = "eth4";
 
#ifdef HOME_GATEWAY  PPPOE_IF = "ppp0";
#elif defined(VOIP_SUPPORT) && defined(ATA867x)  BRIDGE_IF = "eth0";  ELAN_IF = "eth0";
#else  BRIDGE_IF = "br0";  ELAN_IF = "eth0";
#endif  strcpy(WLAN_IF,"wlan0");  //---------------------------
   query_temp_var = (char *)malloc(MAX_QUERY_TEMP_VAL_SIZE);  if (query_temp_var==NULL)  exit(0);  return;   //main_end:  //shmdt(pRomeCfgParam);  exit(0);
}  
This function is the core code of the ASP server, note that this is a deeply customized ASP server, the distribution and processing of the entire ASP server is processed in void Handleform (Request *req), this void handleform (Request * REQ) function is called by the int init_form (Request * req) function in get.c, and the int init_form (Request * req) function is an int read.c in write_body (Request * req) function is called, and the int write_body (Request * req) function is called by the void process_requests (int server_sock) function in request.c, and void Process_ The requests (int server_sock) function is called by the Void Loop (int server_s) function in POLL.C or select.c, and the void loop (int server_s) function is called by BOA.C. As shown below:







Figure one: void Handleform (Request *req) function in asp_page.c







Figure two: void Handleform (Request *req) function is called by the int init_form (Request * req) function in get.c





Figure three: INT Init_form (Request * req) function is an int write_body in READ.C (Request * req)







Figure four: The INT write_body (Request * req) function is called by the void process_requests (int server_sock) function in the request.c







Figure five: void process_requests (int server_sock) function is called by the Void Loop (int server_s) function in POLL.C or select.c




The void loop (int server_s) function is called in the main function in BOA.C, as follows:







Analysis here probably know boa for ASP command parsing basic flow, then we use an instance command to analyze how void Handleform (Request *req) is handling the command in the HTML page, in boa/html/ Open the Syscmd.htm code in Syscmd.htm, as follows:







In the code above you can see <form action=/boafrm/formsyscmd method=post name= "Formsyscmd" > This line of code, we are from this line of code to analyze it, we first look at void The Handleform (Request *req) function is as follows:







From which we can see the table Root_form, and the # define Script_alias "/boafrm/" is defined as boafrm, then we can see from the line of code that we are doing in/boafrm/formsyscmd formsyscmd function, and this function is in the table Root_form, as follows:






Three: Based on the rt819x platform of deep customization boa Source Summary



This blog post simple analysis of the deep customization of the ASP Server command processing process, detailed introduction will continue to update the blog, this is my study of some summary, in the course of learning will certainly have such a mistake, but this is a learning process.



Source code Analysis of ROUTER/AP based on rtl819x implementation [i]

original link:https://blog.csdn.net/wavemcu/article/details/42611109


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.