Exemplo n.º 1
0
/* POSIX.1 defines several new functions to allow an application to map
 * from a host name and a service name to an address and vice versa.
 * These functions replace the older gethostbyname() and gethostbyaddr()
 * functions. The getaddrinfo() function allows us to map a host name and
 * a service name to an address.
 * #include <sys/socket.h>
 * #include <netdb.h>
 * int getaddrinfo(const char *host, const char *service,
 *      const struct *hint, struct addrinfo **res);
 *      Returns: 0 if OK, nonzero error code on error
 * void freeaddrinfo(struct addrinfo *ai);
 *
 * We need to provide the host name, the service name, or both. If we
 * provide only one name, the other should be a null pointer. The host
 * name can be either a node name or the host address in dotted-decimal
 * notation. 如 www.baidu.com, www.google.com 等
 * The getaddrinfo() function returns a linked list of addrinfo structures.
 * We can use freeaddrinfo() to free one or more of these structures,
 * depending on how many structures are linked together using the ai_next
 * field. The addrinfo structure is defined to include at least the
 * following members:
 * struct addrinfo {
 *      int     ai_flags;       // customize behavior
 *      int     ai_family;      // address family
 *      int     ai_socktype;    // socket type
 *      int     ai_protocol;    // protocol
 *      socklen_t ai_addrlen;   // length in bytes of address
 *      struct sockaddr *ai_addr;   // address
 *      char   *ai_canonname;   // canonical name of host
 *      struct addrinfo *ai_next;   // next in list
 * };
 *
 * We can supply an optional hint to select addresses that meet certain
 * criteria. The hint is a template used for filtering addresses and uses
 * only the ai_family, ai_flags, ai_protocol, and ai_socktype fields. The
 * remainning integer fields must be set to 0, and the pointer fields must
 * be null.
 *
 * If getaddrinfo() fails, we can't use perror() or strerror() to generate
 * an error message. Instead, we need to call gai_strerror() to convert
 * the error code returned into an error message.
 * #include <netdb.h>
 * const char *gai_strerror(int error);
 *  Returns: a pointer to a string describing the error
 *
 * getaddrinfo() 函数的host参数可以是本地的主机名(通过hostname命令来获取),
 * 也可以是网络上的域名,如www.baidu.com, www.google.com 等,此时就能获取
 * 到www.baidu.com对应的地址信息,如ip地址,服务的端口地址,协议类型等.
 *
 * The getnameinfo() function converts an address into a host name and
 * a service name.
 * #include <sys/socket.h>
 * #include <netdb.h>
 * int getnameinfo(const struct sockaddr *addr, socklen_t alen, char *host,
 *      socklen_t hostlen, char *service, socklen_t servlen,
 *      unsigned int flags);
 *          Returns: 0 if OK, nonzero on error
 * The socket address (addr) is translated into a host name and a service
 * name. If host is non-null, it points to a buffer hostlen bytes long
 * that will be used to return the host name. Similarly, if service is
 * non-null, it points to a buffer servlen bytes long that will be used to
 * return the service name.
 *
 * The flags argument gives us some control over how the translation is done
 */
int main(int argc, char *argv[])
{
    struct addrinfo *aliases, *aip;
    struct addrinfo hint;
    struct sockaddr_in *sinp;
    const char *addr;
    int err;
    char abuf[INET_ADDRSTRLEN];

    if (argc != 3) {
        printf("usage: %s  nodename servicename\n", argv[0]);
        return 1;
    }

    hint.ai_flags = AI_CANONNAME;
    hint.ai_family = 0;
    hint.ai_socktype = 0;
    hint.ai_protocol = 0;
    hint.ai_addrlen = 0;
    hint.ai_addr = NULL;
    hint.ai_canonname = NULL;
    hint.ai_next = NULL;
    if ((err = getaddrinfo(argv[1], argv[2], &hint, &aliases)) != 0) {
        printf("getaddrinfo error: %s\n", gai_strerror(err));
        return 1;
    }

    /* If multiple protocols provide the given service for the given host,
     * the program will print more than one entry.
     */
    for (aip = aliases; aip != NULL; aip = aip->ai_next) {
        print_flags(aip);
        print_family(aip);
        print_type(aip);
        print_protocol(aip);
        printf("\n\thost: %s", aip->ai_canonname?aip->ai_canonname:"-");
        if (aip->ai_family == AF_INET) {
            sinp = (struct sockaddr_in *)aip->ai_addr;
            /* inet_ntop() 函数返回 const char * 类型指针,所以 addr 也要
             * 定义为 const char * 类型,否则编译报警:
             * assignment discards qualifiers from pointer target type
             */
            addr = inet_ntop(AF_INET, &sinp->sin_addr, abuf,
                    INET_ADDRSTRLEN);
            printf(", address: %s", addr ? addr : "unknown");
            printf(", port: %d", ntohs(sinp->sin_port));
        }
        printf("\n");
    }

    freeaddrinfo(aliases);
    return 0;
}
Exemplo n.º 2
0
int main(int argc, char *argv[])
{
	struct addrinfo *ailist, *aip;
	struct addrinfo hint;
	struct sockaddr_in *sinp;
	const char *addr;
	int err;
	char abuf[INET_ADDRSTRLEN];
	if (argc != 3)
	{
		printf("usage: %s nodename service\n", argv[0]);
		exit(0);
	}
	hint.ai_flags = AI_CANONNAME;
	hint.ai_family = 0;
	hint.ai_socktype = 0;
	hint.ai_protocol = 0;
	hint.ai_addrlen = 0;
	hint.ai_canonname = NULL;
	hint.ai_addr = NULL;
	hint.ai_next = NULL;
	if ((err = getaddrinfo(argv[1], argv[2], &hint, &ailist)) != 0)
	{
		printf("getaddrinfo error: %s", gai_strerror(err));
		exit(0);
	}
	for (aip = ailist; aip != NULL; aip = aip->ai_next) 
	{
		printf("~~~~~~~~~~~~~~~~~~~~~~~~\n");
		print_flags(aip);
		print_family(aip);
		print_type(aip);
		print_protocol(aip);
		printf("\n\thost %s", aip->ai_canonname?aip->ai_canonname:"-");
		if (aip->ai_family == AF_INET)
		{
			sinp = (struct sockaddr_in *)aip->ai_addr;
			addr = inet_ntop(AF_INET, &sinp->sin_addr, abuf, INET_ADDRSTRLEN);
			printf(" address %s", addr?addr:"unknown");
			printf(" port %d", ntohs(sinp->sin_port));
		}
		printf("\n");
	}
	exit(0);
}
Exemplo n.º 3
0
void print_addrinfo(struct addrinfo *aip) {
	print_flags(aip);
	putchar('\n');
	print_family(aip);
	putchar('\n');
	print_type(aip);
	putchar('\n');
	print_proto(aip);
	putchar('\n');
	printf("host: %s", aip->ai_canonname ? aip->ai_canonname : "-");
	putchar('\n');
	if (aip->ai_family == AF_INET) {
		struct sockaddr_in *sinp = (struct sockaddr_in *)aip->ai_addr;
		char abuf[INET_ADDRSTRLEN];
		const char *addr = inet_ntop(AF_INET, &sinp->sin_addr, abuf,
				INET_ADDRSTRLEN);
		printf("address: %s\n", addr ? addr : "unknown");
		printf("port: %d\n", ntohs(sinp->sin_port));
	}
	putchar('\n');
}
Exemplo n.º 4
0
void
ofm_read_simple(void)
{

    if (ofm_on==TRUE) {
        print_ofm_level(ofm_level-1);
        print_font_dir(font_dir);
    }
    header = (char *) ofm+check_sum_pos;
    retrieve_header();
    print_family();
    print_face();
    print_coding_scheme();
    print_design_size();
    out("(COMMENT DESIGNSIZE IS IN POINTS)"); out_ln();
    out("(COMMENT OTHER SIZES ARE MULTIPLES OF DESIGNSIZE)"); out_ln();
    print_check_sum();
    if ((seven_bit == TRUE) || (ofm_level != OFM_TFM))
      print_seven_bit_safe_flag();
    retrieve_parameters(ofm+(4*param_base));
    print_parameters();
}
Exemplo n.º 5
0
Arquivo: pk2dft.c Projeto: GBert/misc
int dump_device_file(char *dumpdir) {
    char fname[255], fbase[255], *p;
    FILE *f, *fcfg;
    int i;

    /* create diretory */
    strcpy(fname, "mkdir ");
    strcat(fname, dumpdir);
    system(fname);

    /* write the config file */
    strcpy(fname, dumpdir);
    strcat(fname, "/config.cfg");
    if ((fcfg = fopen(fname, "w+")) == NULL)
	return -1;

    fprintf(fcfg, "# this configiration file was generated automatically\n");
    fprintf(fcfg, "# please modify it according to the rules :-)\n#\n");
    fprintf(fcfg, "# the source file contained:\n");
    fprintf(fcfg, "#   %d families,\n", hdr.num_families);
    fprintf(fcfg, "#   %d devices and\n", hdr.num_devices);
    fprintf(fcfg, "#   %d scripts.\n\n\n", hdr.num_scripts);
    fprintf(fcfg, "# for the device file header\n");
    fprintf(fcfg, "ver_major = %d\n", hdr.version[0]);
    fprintf(fcfg, "ver_minor = %d\n", hdr.version[1]);
    fprintf(fcfg, "ver_dot   = %d\n", hdr.version[2]);
    fprintf(fcfg, "compat    = %d\n", hdr.compat);
    fprintf(fcfg, "unused1a  = %d\n", hdr.unused1a);
    fprintf(fcfg, "unused1b  = %d\n", hdr.unused1b);
    fprintf(fcfg, "unused2   = %d\n", hdr.unused2);
    fprintf(fcfg, "notes     = \n\"%s\"\n\n\n", hdr.notes);

    /* write families */
    strcpy(fname, "mkdir ");
    strcat(fname, dumpdir);
    strcat(fname, "/families");
    system(fname);

    strcpy(fbase, dumpdir);
    strcat(fbase, "/families/");
    p = fbase + strlen(fbase);
    fprintf(fcfg, "# list of device family files\n");
    fprintf(fcfg, "families  = {\n");
    for (i = 0; i < hdr.num_families; i++) {
	special_cat(p, families[i].name);
	strcat(fbase, ".fam");
	printf("filename : \"%s\"\n", fbase);
	fprintf(fcfg, "\t\"%s\",\n", valid_string(fbase, g_tmp));
	if ((f = fopen(fbase, "w+")) == NULL) {
	    printf("error: cannot create file\n");
	    return -1;
	}
	print_family(f, &families[i]);
	fclose(f);
    }
    fprintf(fcfg, "}\n\n");

    /* write devices */
    strcpy(fname, "mkdir ");
    strcat(fname, dumpdir);
    strcat(fname, "/devices");
    system(fname);

    strcpy(fbase, dumpdir);
    strcat(fbase, "/devices/");
    p = fbase + strlen(fbase);
    fprintf(fcfg, "# list of device files\n");
    fprintf(fcfg, "devices   = {\n");
    for (i = 0; i < hdr.num_devices; i++) {
	special_cat(p, devices[i].part_name);
	strcat(fbase, ".dev");
	printf("filename : \"%s\"\n", fbase);
	fprintf(fcfg, "\t\"%s\",\n", valid_string(fbase, g_tmp));
	if ((f = fopen(fbase, "w+")) == NULL) {
	    printf("error: cannot create file\n");
	    return -1;
	}
	print_device(f, &devices[i]);
	fclose(f);
    }
    fprintf(fcfg, "}\n\n");

    /* write scripts */
    strcpy(fname, "mkdir ");
    strcat(fname, dumpdir);
    strcat(fname, "/scripts");
    system(fname);

    strcpy(fbase, dumpdir);
    strcat(fbase, "/scripts/");
    p = fbase + strlen(fbase);
    fprintf(fcfg, "# list of script files\n");
    fprintf(fcfg, "scripts   = {\n");
    for (i = 0; i < hdr.num_scripts; i++) {
	special_cat(p, scripts[i].name);
	strcat(fbase, ".scr");
	printf("filename : \"%s\"\n", fbase);
	fprintf(fcfg, "\t\"%s\",\n", valid_string(fbase, g_tmp));
	if ((f = fopen(fbase, "w+")) == NULL) {
	    printf("error: cannot create file\n");
	    return -1;
	}
	print_script(f, &scripts[i]);
	fclose(f);
    }
    fprintf(fcfg, "}");

    fclose(fcfg);

    return 0;
}