runtime/lib_net.c - ktap

Global variables defined

Functions defined

Source code

  1. /*
  2. * lib_base.c - base library
  3. *
  4. * This file is part of ktap by Jovi Zhangwei.
  5. *
  6. * Copyright (C) 2012-2013 Jovi Zhangwei <jovi.zhangwei@gmail.com>.
  7. *
  8. * ktap is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * ktap is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  20. */

  21. #include <net/inet_sock.h>
  22. #include "../include/ktap_types.h"
  23. #include "ktap.h"
  24. #include "kp_obj.h"
  25. #include "kp_str.h"
  26. #include "kp_vm.h"

  27. /**
  28. * Return the source IP address for a given sock
  29. */
  30. static int kplib_net_ip_sock_saddr(ktap_state_t *ks)
  31. {
  32.     struct inet_sock *isk;
  33.     int family;

  34.     /* TODO: need to validate the address firstly */

  35.     isk = (struct inet_sock *)kp_arg_checknumber(ks, 1);
  36.     family = isk->sk.__sk_common.skc_family;

  37.     if (family == AF_INET) {
  38.         set_number(ks->top, isk->inet_rcv_saddr);
  39.     } else {
  40.         kp_error(ks, "ip_sock_saddr only support ipv4 now\n");
  41.         set_nil(ks->top);
  42.     }

  43.     incr_top(ks);
  44.     return 1;
  45. }

  46. /**
  47. * Return the destination IP address for a given sock
  48. */
  49. static int kplib_net_ip_sock_daddr(ktap_state_t *ks)
  50. {
  51.     struct inet_sock *isk;
  52.     int family;

  53.     /* TODO: need to validate the address firstly */

  54.     isk = (struct inet_sock *)kp_arg_checknumber(ks, 1);
  55.     family = isk->sk.__sk_common.skc_family;

  56.     if (family == AF_INET) {
  57.         set_number(ks->top, isk->inet_daddr);
  58.     } else {
  59.         kp_error(ks, "ip_sock_daddr only support ipv4 now\n");
  60.         set_nil(ks->top);
  61.     }

  62.     incr_top(ks);
  63.     return 1;

  64. }

  65. /**
  66. * Returns a string representation for an IP address
  67. */
  68. static int kplib_net_format_ip_addr(ktap_state_t *ks)
  69. {
  70.     __be32 ip = (__be32)kp_arg_checknumber(ks, 1);
  71.     ktap_str_t *ts;
  72.     char ipstr[32];

  73.     snprintf(ipstr, 32, "%pI4", &ip);
  74.     ts = kp_str_newz(ks, ipstr);
  75.     if (ts) {
  76.         set_string(ks->top, kp_str_newz(ks, ipstr));
  77.         incr_top(ks);
  78.         return 1;
  79.     } else
  80.         return -1;
  81. }

  82. static const ktap_libfunc_t net_lib_funcs[] = {
  83.     {"ip_sock_saddr", kplib_net_ip_sock_saddr},
  84.     {"ip_sock_daddr", kplib_net_ip_sock_daddr},
  85.     {"format_ip_addr", kplib_net_format_ip_addr},
  86.     {NULL}
  87. };

  88. int kp_lib_init_net(ktap_state_t *ks)
  89. {
  90.     return kp_vm_register_lib(ks, "net", net_lib_funcs);
  91. }