Last active
September 10, 2019 13:22
-
-
Save anthager/f12c98437680fd69caf26bfa89a3fb6d to your computer and use it in GitHub Desktop.
failed socket api for debug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /********************************************************* -- SOURCE -{{{1- */ | |
| /** Translate host name into IPv4 | |
| * | |
| * Resolve IPv4 address for a given host name. The host name is specified as | |
| * the first command line argument to the program. | |
| * | |
| * Build program: | |
| * $ gcc -Wall -g -o resolve <file>.cpp | |
| */ | |
| #include <stdio.h> | |
| #include <stddef.h> | |
| #include <assert.h> | |
| #include <limits.h> | |
| #include <unistd.h> | |
| #include <sys/socket.h> | |
| #include <netinet/ip.h> | |
| #include <sys/types.h> | |
| #include <netdb.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <arpa/inet.h> | |
| void print_usage(const char *aProgramName); | |
| /* HOST_NAME_MAX may be missing, e.g. if you're running this on an MacOS X | |
| * machine. In that case, use MAXHOSTNAMELEN from <sys/param.h>. Otherwise | |
| * generate an compiler error. | |
| */ | |
| #if !defined(HOST_NAME_MAX) | |
| #if defined(__APPLE__) | |
| #include <sys/param.h> | |
| #define HOST_NAME_MAX MAXHOSTNAMELEN | |
| #else // !__APPLE__ | |
| #error "HOST_NAME_MAX undefined!" | |
| #endif // ~ __APPLE__ | |
| #endif // ~ HOST_NAME_MAX | |
| int main(int aArgc, char *aArgv[]) | |
| { | |
| // Check if the user supplied a command line argument. | |
| if (aArgc != 2) | |
| { | |
| print_usage(aArgv[0]); | |
| return 1; | |
| } | |
| // The (only) argument is the remote host that we should resolve. | |
| const char *remoteHostName = aArgv[1]; | |
| // Get the local host's name (i.e. the machine that the program is | |
| // currently running on). | |
| const size_t kHostNameMaxLength = HOST_NAME_MAX + 1; | |
| char localHostName[kHostNameMaxLength]; | |
| if (-1 == gethostname(localHostName, kHostNameMaxLength)) | |
| { | |
| perror("gethostname(): "); | |
| return 1; | |
| } | |
| // Print the initial message | |
| printf("Resolving `%s' from `%s':\n", remoteHostName, localHostName); | |
| // printf("nice"); | |
| struct addrinfo *hints = malloc(sizeof(struct addrinfo)); | |
| struct addrinfo *iter; | |
| // memset(hints, 0, sizeof(*hints)); | |
| struct sockaddr *sockAddr; | |
| struct sockaddr_in *inAddr; | |
| struct addrinfo **res; | |
| char buff[INET6_ADDRSTRLEN]; | |
| int port; | |
| int ipNumber; | |
| hints->ai_addr = 0; | |
| hints->ai_addrlen = 0; | |
| hints->ai_canonname = 0; | |
| hints->ai_family = AF_INET; | |
| hints->ai_flags = AI_PASSIVE; | |
| hints->ai_next = 0; | |
| hints->ai_protocol = IPPROTO_TCP; | |
| hints->ai_socktype = SOCK_STREAM; | |
| int resCode = getaddrinfo(remoteHostName, NULL, hints, res); | |
| if (0 != resCode) | |
| { | |
| fprintf(stderr, "%s", gai_strerror(resCode)); | |
| exit(EXIT_FAILURE); | |
| } | |
| printf("%i", res); | |
| for (iter = *res; iter != NULL; iter = iter->ai_next) | |
| { | |
| sockAddr = iter->ai_addr; | |
| inAddr = (struct sockaddr_in *)sockAddr; | |
| port = inAddr->sin_port; | |
| ipNumber = inAddr->sin_addr.s_addr; | |
| inet_ntop(AF_INET, &ipNumber, buff, sizeof(buff)); | |
| printf("your address is %s", buff); | |
| } | |
| // Ok, we're done. Return success. | |
| return 0; | |
| } | |
| void print_usage(const char *aProgramName) | |
| { | |
| fprintf(stderr, "Usage: %s <hostname>\n", aProgramName); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment