Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IP address integer to string and string to integer functions #86

Closed
cirosantilli opened this issue Jun 21, 2023 · 2 comments
Closed

IP address integer to string and string to integer functions #86

cirosantilli opened this issue Jun 21, 2023 · 2 comments

Comments

@cirosantilli
Copy link

It would be good to add functions that convert IPv4 and IPv6 address to/from strings.

A use case I had is you get a .csv file with string IPs, but it is much more efficient to handle them as integer in DB.

E.g.:

sqlite3 :memory: '.load ./ip' "SELECT printf('%08x', str2ipv4('1.128.2.10'))"

would output:

0180020a

At: https://stackoverflow.com/questions/7638238/sqlite-ip-address-storage/76520885#76520885 I show an implementation of str -> int, but would need to add the inverse and IPv6 support too.

Lazy to make a clean pull request here, but it's an idea.

@terefang
Copy link

terefang commented Oct 25, 2023

let me also suggest

functions

ipmask(str)

sqlite> select ipmask('192.168.16.12/24');
255.255.255.0

ipmasktolen(str)

sqlite> select ipmasktolen('255.255.254.0');
23

ipmatch(pattern, needle)

sqlite> select ipmatch('192.168.*','192.168.16.12');
true
sqlite> select ipmatch('192.168.*.12','192.168.16.12');
true
sqlite> select ipmatch('192.*.*.12','192.168.16.12');
true
sqlite> select ipmatch('192.*','192.168.16.12');
true
sqlite> select ipmatch('*.12','192.168.16.12');
true
sqlite> select ipmatch('192.168.1-17.12','192.168.16.12');
true
sqlite> select ipmatch('192.168.1-14.12','192.168.16.12');
false
sqlite> select ipmatch('192.150-168.1-17.11-12','192.168.16.12');
true
sqlite> select ipmatch('192.150-168.*.10','192.168.16.12');
false

and the mysql functions

  • INET_ATON() | Return the numeric value of an IP address
  • INET_NTOA() | Return the IP address from a numeric value
  • INET6_ATON() | Return the numeric value of an IPv6 address
  • INET6_NTOA() | Return the IPv6 address from a numeric value
  • IS_IPV4() | Whether argument is an IPv4 address
  • IS_IPV4_COMPAT() | Whether argument is an IPv4-compatible address
  • IS_IPV4_MAPPED() | Whether argument is an IPv4-mapped address
  • IS_IPV6() | Whether argument is an IPv6 address

and the logical derivatives

  • INET_PTON() | Return the numeric value of an IP4/6 address
  • INET_NTOP() | Return the IP4/6 address from a numeric value

@nalgeon
Copy link
Owner

nalgeon commented Jan 10, 2024

Sorry, I have no plans to add new features at this time.

@nalgeon nalgeon closed this as not planned Won't fix, can't repro, duplicate, stale Jan 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants