Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @brief convert IP from network format to string, if
- * the only parameter starts with 0x, e.g.
- * ./printiphex 0x0100007F
- * => 127.0.0.1
- * or print the address on network notation otherwise e.g.
- * ./printiphex 127.0.0.1
- * => 0x0100007F
- *
- * @param 1th, The ip address to convert from network to
- * string or reverse.
- *
- */
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #define pexit(s) ({perror(s); exit(EXIT_FAILURE);})
- int main(int argc, char **argv)
- {
- if (argc < 2) {
- printf("Usage: %s IP\n", argv[0]);
- exit(EXIT_FAILURE);
- }
- if (!strncmp("0x", argv[1], 2)) {
- struct in_addr addr = {
- .s_addr = strtol(argv[1], NULL, 16),
- };
- printf("%s\n", inet_ntoa(addr));
- } else {
- printf("0x%08X\n", inet_addr(argv[1]));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement