Usermode/init - Fixed early free of command, fixed %s on array
[tpg/acess2.git] / Usermode / Applications / testserver_src / main.c
1 /*
2  * Acess2 Test Server
3  */
4 #include <acess/sys.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7
8 /**
9  * \fn int main(int argc, char *argv[])
10  * \brief Entrypoint
11  */
12 int main(int argc, char *argv[])
13 {
14          int    srv = -1;
15          int    con = -1;
16          int    len;
17         uint16_t        port;
18         char    buf[8+1];
19         uint8_t data[4096];     // Packet Data
20          
21         srv = open("/Devices/ip/1/tcps", OPENFLAG_READ|OPENFLAG_EXEC);
22         if(srv == -1) {
23                 fprintf(stderr, "Unable top open TCP server '/Devices/ip/1/tcps'\n");
24                 return -1;
25         }
26         port = 80;      ioctl(srv, 4, &port);   // Set Port
27         
28         for(;;)
29         {
30                 readdir( srv, buf );
31                 printf("Connection '/Devices/ip/1/tcps/%s'\n", buf);
32                 con = _SysOpenChild(srv, buf, OPENFLAG_READ|OPENFLAG_WRITE);
33                 if(con == -1) {
34                         fprintf(stderr, "Wtf! Why didn't this open?\n");
35                         return 1;
36                 }
37                 
38                 len = read(con, 4096, data);
39                 if( len == -1 ) {
40                         printf("Connection closed\n");
41                         close(con);
42                         continue;
43                 }
44                 if( len != 0 )
45                 {
46                         printf("Packet Data: (%i bytes)\n", len);
47                         printf("%s\n", data);
48                         printf("--- EOP ---\n");
49                 }
50                 
51                 #define RET_STR "HTTP/1.1 200 OK\r\n"\
52                         "Content-Type: text/plain\r\n"\
53                         "Content-Length: 92\r\n"\
54                         "\r\n"\
55                         "<html><head><title>Acess2 Web Server</title></head><body><h1>Hello World!</h1></body></html>\r\n"
56                 
57                 write(con, sizeof(RET_STR), RET_STR);
58                 
59                 close(con);
60         }
61         
62         return 0;
63 }

UCC git Repository :: git.ucc.asn.au