Example #1
0
int main (int argc, char ** argv)
{
    struct addrinfo * res = get_address_info();
    int socket_fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    bind(sockfd, res->ai_addr, res->ai_addrlen);
    return EXIT_SUCCESS;
}
Example #2
0
static Bool 
get_displayname_auth(char *displayname, Xauth *auth)
{
    int family;
    char *host = NULL, *rest = NULL;
    int dpynum, scrnum;
    char *cp;
    int len;
    Xauth proto;
    int prelen = 0;

    /*
     * check to see if the display name is of the form "host/unix:"
     * which is how the list routine prints out local connections
     */
    cp = strchr(displayname, '/');
    if (cp && strncmp (cp, "/unix:", 6) == 0)
      prelen = (cp - displayname);

    #ifdef __APPLE__

    /*
     * FIXME: This is an attempt to get the right
     * cookie, because no one can grant that the
     * X server is running on the display number
     * reported in the launchd display name.
     */

    if (strncmp (displayname, "/tmp/launch", 11) == 0)
      displayname = strrchr(displayname, ':');

    #endif

    if (!parse_displayname (displayname + ((prelen > 0) ? prelen + 1 : 0),
			    &family, &host, &dpynum, &scrnum, &rest)) {
	return False;
    }

    proto.family = family;
    proto.address = get_address_info (family, displayname, prelen, host, &len);
    if (proto.address) {
	char buf[40];			/* want to hold largest display num */

	proto.address_length = len;
	buf[0] = '\0';
	sprintf (buf, "%d", dpynum);
	proto.number_length = strlen (buf);
	if (proto.number_length <= 0) {
	    free (proto.address);
	    proto.address = NULL;
	} else {
	    proto.number = copystring (buf, proto.number_length);
	}
    }

    if (host) free (host);
    if (rest) free (rest);

    if (proto.address) {
	auth->family = proto.family;
	auth->address = proto.address;
	auth->address_length = proto.address_length;
	auth->number = proto.number;
	auth->number_length = proto.number_length;
	auth->name = NULL;
	auth->name_length = 0;
	auth->data = NULL;
	auth->data_length = 0;
	return True;
    } else {
	return False;
    }
}
int main (int argc, const char* argv[]) {

    // Check if the user has not given enough parameters
    if (argc != 3) {
        printf("Invalid command. Usage: \"%s [address] [port]\"\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    // Init structs and other variables
    struct addrinfo* results;
    int sockfd, nbytes;
    char buf[MAXDATASIZE];

    // Try to get addrinfo, exiting on error
    printf("Getting address information...\n");
    results = get_address_info(argv[1], argv[2]);
    if (!results) {
        fprintf(stderr, "Implement assignment 1!\n");
	exit(EXIT_FAILURE);
    }

    // Get IP addresses
    printf("Printing IP addresses for %s...\n", argv[1]);
    char* addr = print_addresses(results);
    if (!addr) {
        fprintf(stderr, "Implement assignment 2!\n");
        exit(EXIT_FAILURE);
    }
    printf("%s", addr);
    free(addr);

    // Bind and connect socket
    printf("Connecting to server...\n");
    sockfd = create_and_connect(results);
    if (!sockfd) {
        fprintf(stderr, "Implement assignment 3!\n");
        close(sockfd);
        exit(EXIT_FAILURE);
    }

    // We don't need this struct anymore
    freeaddrinfo(results);

    // Receive data
    printf("Receiving data...\n");
    nbytes = receive_data(sockfd, buf);
    if (!nbytes) {
        fprintf(stderr, "Implement assignment 4!\n");
        exit(EXIT_FAILURE);
    }
    buf[nbytes] = '\0';
    printf("Received: %s\n", buf);

    // Send data
    printf("Sending data...\n");
    nbytes = send_data(sockfd, buf);
    if (!nbytes) {
        fprintf(stderr, "Implement assignment 5!\n");
        exit(EXIT_FAILURE);
    }
    buf[nbytes] = '\0';
    printf("Sent: %s\n", buf);

    // Receive confirmation
    if ((nbytes = recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) {
        perror("recv error");
        exit(EXIT_FAILURE);
    }
    buf[nbytes] = '\0';
    printf("Received: %s\n", buf);

    // Close the socket after the sending and receiving is done
    close(sockfd);

    return 0;
}
Example #4
0
static Bool
get_displayname_auth(const char *displayname, AuthList **authl)
{
    int family;
    char *host = NULL, *rest = NULL;
    int dpynum, scrnum;
    char *cp;
    int prelen = 0;
    struct addrlist *addrlist_head, *addrlist_cur;
    AuthList *authl_cur = NULL;

    *authl = NULL;
    /*
     * check to see if the display name is of the form "host/unix:"
     * which is how the list routine prints out local connections
     */
    cp = strchr(displayname, '/');
    if (cp && strncmp (cp, "/unix:", 6) == 0)
      prelen = (cp - displayname);

    if (!parse_displayname (displayname + ((prelen > 0) ? prelen + 1 : 0),
			    &family, &host, &dpynum, &scrnum, &rest)) {
	return False;
    }

    addrlist_head = get_address_info(family, displayname, prelen, host);
    if (addrlist_head) {
	char buf[40];			/* want to hold largest display num */
	unsigned short dpylen;

	buf[0] = '\0';
	sprintf (buf, "%d", dpynum);
	dpylen = strlen (buf);
	if (dpylen > 0) {
	    for (addrlist_cur = addrlist_head; addrlist_cur != NULL;
		 addrlist_cur = addrlist_cur->next) {
		AuthList *newal = malloc(sizeof(AuthList));
		Xauth *auth = malloc(sizeof(Xauth));

		if ((newal == NULL) || (auth == NULL)) {
		    if (newal != NULL) free(newal);
		    if (auth != NULL) free(auth);
		    break;
		}

		if (authl_cur == NULL) {
		    *authl = authl_cur = newal;
		} else {
		    authl_cur->next = newal;
		    authl_cur = newal;
		}

		newal->next = NULL;
		newal->auth = auth;

		auth->family = addrlist_cur->family;
		auth->address = addrlist_cur->address;
		auth->address_length = addrlist_cur->len;
		auth->number = copystring(buf, dpylen);
		auth->number_length = dpylen;
		auth->name = NULL;
		auth->name_length = 0;
		auth->data = NULL;
		auth->data_length = 0;
	    }
	}
    }

    if (host) free (host);
    if (rest) free (rest);

    if (*authl != NULL) {
	return True;
    } else {
	return False;
    }
}