运行效果:
代码:
/* GTK+ application: hostname and ip interconversion */ #include <gtk/gtk.h> #include <assert.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #define MAX_HOSTNAME_LEN 128 #define MAX_IP_LEN 128 struct HostnameIPEntryPack { GtkWidget *mHostname; GtkWidget *mIP; }; /** * func: hostname_to_ip * @return : 0 indicating success; -1 indicating failure; * @hostname: hostname of the target host, must not be NULL * @ip: parameter used to return an ip * **/ int hostname_to_ip(const char *hostname, char *ip) { assert(hostname != NULL); assert(ip != NULL); struct addrinfo hints; struct addrinfo *result, *rp; int ret; /* obtaining ip address matching hostname */ memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_CANONNAME; hints.ai_protocol = 0; /* any protocol */ ret = getaddrinfo(hostname, NULL, &hints, &result); if (ret != 0) { return -1; } for (rp = result; rp != NULL; rp = rp->ai_next) { ret = getnameinfo(rp->ai_addr, rp->ai_addrlen, ip, MAX_IP_LEN, NULL, 0, NI_NUMERICHOST); if (ret != 0) { continue; } else { break; /* get an ip, we break out, return immediately */ } } freeaddrinfo(result); if (rp == NULL) return -1; else return 0; } /** * func: ip_to_hostname * **/ int ip_to_hostname(const char *ip, char *hostname) { assert(hostname != NULL); assert(ip != NULL); struct addrinfo hints; struct addrinfo *result, *rp; int ret; /* obtaining ip address matching hostname */ memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_CANONNAME | AI_NUMERICHOST; hints.ai_protocol = 0; /* any protocol */ ret = getaddrinfo(ip, NULL, &hints, &result); if (ret != 0) { return -1; } for (rp = result; rp != NULL; rp = rp->ai_next) { ret = getnameinfo(rp->ai_addr, rp->ai_addrlen, hostname, MAX_HOSTNAME_LEN, NULL, 0, NI_NAMEREQD); if (ret != 0) { continue; } else { break; /* get an ip, we break out, return immediately */ } } freeaddrinfo(result); if (rp == NULL) return -1; else return 0; } void closeApp(GtkWidget *window, gpointer data) { gtk_main_quit(); } void button_host_to_ip_clicked(GtkWidget *button, gpointer data) { GtkWidget *hostnameEntry = ((struct HostnameIPEntryPack *)data)->mHostname; GtkWidget *IPEntry = ((struct HostnameIPEntryPack *)data)->mIP; const char *hostname = gtk_entry_get_text(GTK_ENTRY(hostnameEntry)); char ip[MAX_IP_LEN] = {0}; int ret = hostname_to_ip(hostname, ip); if (ret == 0) { gtk_entry_set_text(GTK_ENTRY(IPEntry), ip); } else { gtk_entry_set_text(GTK_ENTRY(IPEntry), "Not Found"); } } void button_ip_to_host_clicked(GtkWidget *button, gpointer data) { GtkWidget *hostnameEntry = ((struct HostnameIPEntryPack *)data)->mHostname; GtkWidget *IPEntry = ((struct HostnameIPEntryPack *)data)->mIP; const char *ip = gtk_entry_get_text(GTK_ENTRY(IPEntry)); char hostname[MAX_HOSTNAME_LEN] = {0}; int ret = ip_to_hostname(ip, hostname); if (ret == 0) { gtk_entry_set_text(GTK_ENTRY(hostnameEntry), hostname); } else { gtk_entry_set_text(GTK_ENTRY(hostnameEntry), "Not Found"); } } int main(int argc, char *argv[]) { GtkWidget *window; GtkWidget *hostname_label, *ip_label; GtkWidget *hostname_entry, *ip_entry; GtkWidget *hostname_to_ip_button, *ip_to_hostname_button; GtkWidget *hbox1, *hbox2, *hbox3; GtkWidget *vbox; struct HostnameIPEntryPack pack; gtk_init(&argc, &argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window), "IP<->Hostname"); gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); gtk_window_set_default_size(GTK_WINDOW(window), 200, 200); g_signal_connect(GTK_OBJECT(window), "destroy", GTK_SIGNAL_FUNC(closeApp), NULL); hostname_label = gtk_label_new("Hostname:"); ip_label = gtk_label_new("IP:"); hostname_entry = gtk_entry_new(); ip_entry = gtk_entry_new(); pack.mHostname = hostname_entry; pack.mIP = ip_entry; hostname_to_ip_button = gtk_button_new_with_label("Hostname -> IP"); ip_to_hostname_button = gtk_button_new_with_label("IP -> Hostname"); g_signal_connect(GTK_OBJECT(hostname_to_ip_button), "clicked", GTK_SIGNAL_FUNC(button_host_to_ip_clicked), &pack); g_signal_connect(GTK_OBJECT(ip_to_hostname_button), "clicked", GTK_SIGNAL_FUNC(button_ip_to_host_clicked), &pack); hbox1 = gtk_hbox_new(TRUE, 5); hbox2 = gtk_hbox_new(TRUE, 5); hbox3 = gtk_hbox_new(TRUE, 5); vbox = gtk_vbox_new(TRUE, 10); gtk_box_pack_start(GTK_BOX(hbox1), hostname_label, TRUE, FALSE, 5); gtk_box_pack_start(GTK_BOX(hbox1), hostname_entry, TRUE, FALSE, 5); gtk_box_pack_start(GTK_BOX(hbox2), ip_label, TRUE, FALSE, 5); gtk_box_pack_start(GTK_BOX(hbox2), ip_entry, TRUE, FALSE, 5); gtk_box_pack_start(GTK_BOX(hbox3), hostname_to_ip_button, TRUE, FALSE, 5); gtk_box_pack_start(GTK_BOX(hbox3), ip_to_hostname_button, TRUE, FALSE, 5); gtk_box_pack_start(GTK_BOX(vbox), hbox1, FALSE, FALSE, 5); gtk_box_pack_start(GTK_BOX(vbox), hbox2, FALSE, FALSE, 5); gtk_box_pack_start(GTK_BOX(vbox), hbox3, FALSE, FALSE, 5); gtk_container_add(GTK_CONTAINER(window), vbox); gtk_widget_show_all(window); gtk_main(); return 0; }
hostip: hostip.c gcc hostip.c -o hostip `pkg-config --cflags --libs gtk+-2.0`