?login_element?

Subversion Repositories NedoOS

Rev

Rev 401 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <string.h>
  5. #include <signal.h>
  6. #include <alloca.h>
  7. #include <unistd.h>
  8.  
  9. #include "../net.h"
  10.  
  11.  
  12.  
  13. int sock_listen;
  14.  
  15. int sock;
  16. int sock_valid;
  17.  
  18. unsigned int tick=123456;
  19.  
  20.  
  21. //void signal_handler(int);
  22.  
  23.  
  24. int send_bytes(char * ptr, int len);
  25. int send_hello(void);
  26. int send_tick(int was_syncreq, uint32_t sync_value);
  27.  
  28. int recv_bytes(char * ptr, int len);
  29.  
  30.  
  31. int main(int argc, char ** argv)
  32. {
  33.         sock_valid = 0;
  34.  
  35.         struct sockaddr_in addr;
  36.  
  37.         int was_syncreq = 0;
  38.         uint32_t sync_value;
  39.  
  40.  
  41.         // init network (required for nedoVindOvS)
  42.         net_init();
  43.  
  44.  
  45.  
  46.  
  47.         // listening socket create
  48.         sock_listen = socket(AF_INET,SOCK_STREAM,0);
  49.         if( sock_listen==(-1) )
  50.         {
  51.                 fprintf(stderr,"%s: socket() failed! strerror() says: %s\n",__PRETTY_FUNCTION__,strerror(errno));
  52.                 exit(1);
  53.         }
  54.  
  55.         // bind it
  56.         memset(&addr,0,sizeof(addr));
  57.         addr.sin_family      = AF_INET;
  58.         addr.sin_port        = htons(AY_PORT);
  59.         addr.sin_addr.s_addr = INADDR_ANY;
  60.  
  61.         if( bind(sock_listen, (const struct sockaddr *)&addr, sizeof(addr))==(-1) )
  62.         {
  63.                 fprintf(stderr,"%s: bind() failed! strerror() says: %s\n",__PRETTY_FUNCTION__,strerror(errno));
  64.                 exit(1);
  65.         }
  66.  
  67.         // listen on it
  68.         if( listen(sock_listen,1)==(-1) )
  69.         {
  70.                 fprintf(stderr,"%s: listen() failed! strerror() says: %s\n",__PRETTY_FUNCTION__,strerror(errno));
  71.                 exit(1);
  72.         }
  73.  
  74.  
  75.         // accept/work loop
  76.         for(;;)
  77.         {
  78.                 sock = accept(sock_listen, NULL, NULL);
  79.                 if( sock==(-1) )
  80.                 {
  81.                         if( errno==EAGAIN       ||
  82.                             errno==EWOULDBLOCK  ||
  83.                             errno==ECONNABORTED )
  84.                         {
  85.                                 continue; // try again
  86.                         }
  87.                         else
  88.                         {
  89.                                 fprintf(stderr,"%s: accept() failed! strerror() says: %s\n",__PRETTY_FUNCTION__,strerror(errno));
  90.                                 exit(1);
  91.                         }
  92.                 }
  93.  
  94.                 sock_valid = 1;
  95.                
  96.                 // set TCP_NODELAY to the socket
  97.                 int dummy = 1;
  98.                 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &dummy, sizeof(dummy));
  99.  
  100.  
  101.                 if( !send_hello() ) continue;
  102. printf("Hello sent!\n");
  103.  
  104.                 was_syncreq = 0;
  105.  
  106.                 // send ticks, accept dumps
  107.                 for(;;)
  108.                 {
  109.                         usleep(20000); // 20ms
  110.                         tick++;
  111.                         if( !send_tick(was_syncreq,sync_value) ) goto STOPTICKS;
  112. printf("Tick=%08x sent!\n",tick);
  113. if( was_syncreq ) printf("Syncrply=%08x sent!\n",sync_value);
  114.                         was_syncreq = 0;
  115.  
  116.                         uint8_t buf[15];
  117.  
  118.                         while(1)
  119.                         {
  120.                                 if( !recv_bytes(&buf[0],1) ) goto STOPTICKS;
  121.  
  122.                                 if( buf[0]==0x00 )
  123.                                 {
  124. printf("SHUTUP received!\n");
  125.                                         break;
  126.                                 }
  127.                                 else if( buf[0]==0x01 )
  128.                                 {
  129.                                         if( !recv_bytes(&buf[1],14) ) goto STOPTICKS;
  130. printf("DUMP received!\n");
  131. for(int i=0;i<14;i++)
  132. printf("DUMP: [reg %02x] = %02x\n",i,buf[i+1]);
  133.                                         break;
  134.                                 }
  135.                                 else if( buf[0]==0x02 )
  136.                                 {
  137.                                         if( !recv_bytes(&buf[1],4) ) goto STOPTICKS;
  138.  
  139.                                         was_syncreq = 1;
  140.                                         sync_value = *((uint32_t *) &buf[1]);
  141. printf("SYNCREQ received!\n");
  142. printf("SYNCREQ: value = %08x\n",sync_value);
  143.                                 }
  144.                                 else
  145.                                 {
  146.                                         fprintf(stderr,"%s: unknown incoming pkt type = %02x\n",__PRETTY_FUNCTION__,buf[0]);
  147.                                         exit(1);
  148.                                 }
  149.                         }
  150.                 }
  151. STOPTICKS:
  152. printf("Connection closed, waiting for another...\n");
  153.         }
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.         net_dispose();
  165.  
  166.         return 0;
  167. }
  168.  
  169.  
  170. /*
  171. void signal_handler(int num)
  172. {
  173.         if( sock_set )
  174.         { // try shut up remote AY by sending lots of ZX<< SHUTUP
  175.                 uint8_t a[100]; // must be greater than any other packet size
  176.                 memset(a,0,sizeof(a));
  177.                 send(sock, (void*)&a, sizeof(a), 0);
  178.  
  179.                 net_disconnect(sock);
  180.         }
  181.  
  182.         if( frames ) free_psg_frames(frames);
  183.  
  184.         if( psg ) free_psg_file(psg);
  185.  
  186.         net_dispose();
  187.         exit(1);
  188. }
  189. */
  190.  
  191.  
  192. int send_bytes(char * ptr, int len)
  193. {
  194.         while(len>0)
  195.         {
  196.                 int curr_len = send(sock,ptr,len,MSG_NOSIGNAL);
  197.                 if( curr_len==(-1) )
  198.                 {
  199.                         if( errno==EAGAIN || errno==EWOULDBLOCK )
  200.                                 continue;
  201.                         else if( errno==ECONNRESET || errno==EPIPE )
  202.                                 return 0; // connection lost
  203.                         else
  204.                         {
  205.                                 fprintf(stderr,"%s: send() failed! strerror() says: %s\n",__PRETTY_FUNCTION__,strerror(errno));
  206.                                 exit(1);
  207.                         }
  208.                 }
  209.  
  210.                 len -= curr_len;
  211.                 ptr += curr_len;
  212.         }
  213.  
  214.         return 1;
  215. }
  216.  
  217. int recv_bytes(char * ptr, int len)
  218. {
  219.         char bigbuf[1024];
  220.  
  221.         if( len>sizeof(bigbuf) )
  222.         {
  223.                 fprintf(stderr,"%s: len=%d is greater than buffer size %ld\n",__PRETTY_FUNCTION__,len,sizeof(bigbuf));
  224.                 exit(1);
  225.         }
  226.  
  227.        
  228.         while(len>0)
  229.         {
  230.                 int curr_len = recv(sock,ptr,len,0);
  231.                 if( curr_len==(-1) )
  232.                 {
  233.                         if( errno==EAGAIN || errno==EWOULDBLOCK )
  234.                                 continue;
  235.  
  236.                         else if( errno==ECONNREFUSED )
  237.                                 return 0;
  238.                         else
  239.                         {
  240.                                 fprintf(stderr,"%s: recv() failed! strerror() says: %s\n",__PRETTY_FUNCTION__,strerror(errno));
  241.                                 exit(1);
  242.                         }
  243.                 }
  244.  
  245.                 len -= curr_len;
  246.                 ptr += curr_len;
  247.         }
  248. }
  249.  
  250.  
  251.  
  252. int send_hello(void)
  253. {
  254.         char hello_str[] = "Hello Shitty";
  255.         char * hello_pkt = alloca(2+sizeof(hello_str));
  256.  
  257.         hello_pkt[0] = 0;
  258.         hello_pkt[1] = strlen(hello_str);
  259.         strcpy(&hello_pkt[2], hello_str);
  260.  
  261.         return send_bytes(hello_pkt,2+hello_pkt[1]);
  262. }
  263.  
  264. int send_tick(int was_syncreq, uint32_t sync_value)
  265. {
  266.         char buf[10];
  267.  
  268.         buf[0] = 0x01;
  269.         memcpy(&buf[1],&tick,4);
  270.  
  271.         if( was_syncreq )
  272.         {
  273.                 buf[5] = 0x02;
  274.                 memcpy(&buf[6],&sync_value,4);
  275.  
  276.                 return send_bytes(buf,10);
  277.         }
  278.         else
  279.         {
  280.                 return send_bytes(buf,5);
  281.         }
  282. }
  283.  
  284.  
  285.