Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.     umka_shell: User-Mode KolibriOS developer tools, the ping
  3.     Copyright (C) 2020  Ivan Baravy <dunkaist@gmail.com>
  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, see <https://www.gnu.org/licenses/>.
  17. */
  18.  
  19. #include <arpa/inet.h>
  20. #include <errno.h>
  21. #include <fcntl.h>
  22. #include <inttypes.h>
  23. #include <limits.h>
  24. #include <linux/if_tun.h>
  25. #include <net/if.h>
  26. #include <netinet/in.h>
  27. #include <stdbool.h>
  28. #include <stdint.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <sys/ioctl.h>
  33. #include <sys/select.h>
  34. #include <sys/socket.h>
  35. #include <sys/stat.h>
  36. #include <sys/time.h>
  37. #include <sys/types.h>
  38. #include <unistd.h>
  39. #include "shell.h"
  40. #include "umka.h"
  41. #include "vnet.h"
  42.  
  43. uint8_t packet[4096] = {0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 0x77, 'a','b',
  44.                         'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  45.                         'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
  46.                         'y', 'z', '0', '1', '2', '3', '4', '5'};
  47.  
  48. static int
  49. tap_alloc(char *dev) {
  50.     int flags = IFF_TAP | IFF_NO_PI;
  51.     struct ifreq ifr;
  52.     int fd, err;
  53.     char *clonedev = "/dev/net/tun";
  54.  
  55.     if( (fd = open(clonedev , O_RDWR | O_NONBLOCK)) < 0 )
  56.     {
  57.         perror("Opening /dev/net/tun");
  58.         return fd;
  59.     }
  60.  
  61.     memset(&ifr, 0, sizeof(ifr));
  62.  
  63.     ifr.ifr_flags = flags;
  64.  
  65.     if(*dev)
  66.     {
  67.         strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  68.     }
  69.  
  70.     if( (err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0 )
  71.     {
  72.         perror("ioctl(TUNSETIFF)");
  73.         close(fd);
  74.         return err;
  75.     }
  76.  
  77.     strcpy(dev, ifr.ifr_name);
  78.  
  79.     return fd;
  80. }
  81.  
  82. int go_ping = 0;
  83.  
  84. void
  85. umka_thread_ping(void) {
  86.     umka_sti();
  87.     while (!go_ping) { /* wait until initialized */ }
  88.  
  89.     f75ret_t r75;
  90.     r75 = umka_sys_net_open_socket(AF_INET4, SOCK_STREAM, IPPROTO_TCP);
  91.     if (r75.value == (uint32_t)-1) {
  92.         fprintf(stderr, "[ping] open error %u\n", r75.errorcode);
  93.         exit(1);
  94.     }
  95.     uint32_t sockfd = r75.value;
  96.  
  97. //    uint32_t addr = inet_addr("127.0.0.1");
  98. //    uint32_t addr = inet_addr("192.243.108.5");
  99.     uint32_t addr = inet_addr("10.50.0.1");
  100. //    uint32_t addr = inet_addr("192.168.1.30");
  101.     uint16_t port = 5000;
  102.  
  103.     struct sockaddr_in sa;
  104.     memset(&sa, 0, sizeof(sa));
  105.     sa.sin_family = AF_INET4;
  106.     sa.sin_port = htons(port);
  107.     sa.sin_addr.s_addr = addr;
  108.  
  109.     r75 = umka_sys_net_connect(sockfd, &sa, sizeof(struct sockaddr_in));
  110.     if (r75.value == (uint32_t)-1) {
  111.         fprintf(stderr, "[ping] connect error %u\n", r75.errorcode);
  112.         return;
  113.     }
  114.  
  115.     r75 = umka_sys_net_send(sockfd, packet, 128, 0);
  116.     if (r75.value == (uint32_t)-1) {
  117.         fprintf(stderr, "[ping] send error %u\n", r75.errorcode);
  118.         return;
  119.     }
  120.  
  121.     while (true) {}
  122.  
  123.     return;
  124. }
  125.  
  126. void
  127. umka_thread_net_drv(void) {
  128.     umka_sti();
  129.     fprintf(stderr, "[net_drv] starting\n");
  130.     int tapfd;
  131.     uint8_t buffer[2048];
  132.     int plen = 0;
  133.     char tapdev[IFNAMSIZ] = "tap0";
  134.     tapfd = tap_alloc(tapdev);
  135.     net_device_t *vnet = vnet_init(tapfd);
  136.     kos_net_add_device(vnet);
  137.  
  138.     char devname[64];
  139.     for (size_t i = 0; i < umka_sys_net_get_dev_count(); i++) {
  140.         umka_sys_net_dev_reset(i);
  141.         umka_sys_net_get_dev_name(i, devname);
  142.         uint32_t devtype = umka_sys_net_get_dev_type(i);
  143.         fprintf(stderr, "[net_drv] device %i: %s %u\n", i, devname, devtype);
  144.     }
  145.  
  146.     f76ret_t r76;
  147.     r76 = umka_sys_net_ipv4_set_subnet(1, inet_addr("255.255.255.0"));
  148.     if (r76.eax == (uint32_t)-1) {
  149.         fprintf(stderr, "[net_drv] set subnet error\n");
  150.         return;
  151.     }
  152.  
  153. //    r76 = umka_sys_net_ipv4_set_gw(1, inet_addr("192.168.1.1"));
  154.     r76 = umka_sys_net_ipv4_set_gw(1, inet_addr("10.50.0.1"));
  155.     if (r76.eax == (uint32_t)-1) {
  156.         fprintf(stderr, "set gw error\n");
  157.         return;
  158.     }
  159.  
  160.     r76 = umka_sys_net_ipv4_set_dns(1, inet_addr("217.10.36.5"));
  161.     if (r76.eax == (uint32_t)-1) {
  162.         fprintf(stderr, "[net_drv] set dns error\n");
  163.         return;
  164.     }
  165.  
  166.     r76 = umka_sys_net_ipv4_set_addr(1, inet_addr("10.50.0.2"));
  167.     if (r76.eax == (uint32_t)-1) {
  168.         fprintf(stderr, "[net_drv] set ip addr error\n");
  169.         return;
  170.     }
  171.  
  172.     go_ping = 1;
  173.  
  174.     while(true)
  175.     {
  176.         plen = read(tapfd, buffer, 2*1024);
  177.         if (plen > 0) {
  178.             fprintf(stderr, "[net_drv] read %i bytes\n", plen);
  179.             for (int i = 0; i < plen; i++) {
  180.                 fprintf(stderr, " %2.2x", buffer[i]);
  181.             }
  182.             fprintf(stderr, "\n");
  183.             vnet_receive_frame(vnet, buffer, plen);
  184.         } else if(plen == -1 && (errno == EAGAIN || errno == EINTR)) {
  185.             continue;
  186.         } else {
  187.             perror("[net_drv] reading data");
  188.             exit(1);
  189.         }
  190.     }
  191.  
  192. }
  193.