Changeset 02a09ed in mainline for uspace/app/nterm/conn.c
- Timestamp:
- 2013-06-28T20:20:03Z (11 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 1d24ad3
- Parents:
- edf0d27
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/nterm/conn.c
redf0d27 r02a09ed 75 75 int conn_open(const char *addr_s, const char *port_s) 76 76 { 77 struct sockaddr_in addr; 78 dnsr_hostinfo_t *hinfo = NULL; 79 int rc; 80 char *endptr; 81 82 addr.sin_family = AF_INET; 83 84 rc = inet_pton(addr.sin_family, addr_s, (uint8_t *)&addr.sin_addr); 77 int conn_fd = -1; 78 79 /* Interpret as address */ 80 inet_addr_t addr_addr; 81 int rc = inet_addr_parse(addr_s, &addr_addr); 82 85 83 if (rc != EOK) { 86 /* Try interpreting as a host name */ 84 /* Interpret as a host name */ 85 dnsr_hostinfo_t *hinfo = NULL; 87 86 rc = dnsr_name2host(addr_s, &hinfo); 87 88 88 if (rc != EOK) { 89 89 printf("Error resolving host '%s'.\n", addr_s); … … 91 91 } 92 92 93 rc = inet_addr_sockaddr_in(&hinfo->addr, &addr); 94 if (rc != EOK) { 95 printf("Host '%s' not resolved as IPv4 address.\n", addr_s); 96 return rc; 97 } 93 addr_addr = hinfo->addr; 98 94 } 99 100 addr.sin_port = htons(strtol(port_s, &endptr, 10)); 95 96 struct sockaddr_in addr; 97 struct sockaddr_in6 addr6; 98 uint16_t af = inet_addr_sockaddr_in(&addr_addr, &addr, &addr6); 99 100 char *endptr; 101 uint16_t port = strtol(port_s, &endptr, 10); 101 102 if (*endptr != '\0') { 102 103 printf("Invalid port number %s\n", port_s); … … 104 105 } 105 106 107 printf("Connecting to host %s port %u\n", addr_s, port); 108 106 109 conn_fd = socket(PF_INET, SOCK_STREAM, 0); 107 110 if (conn_fd < 0) 108 111 goto error; 109 112 110 printf("Connecting to host %s port %u\n", addr_s, ntohs(addr.sin_port)); 113 switch (af) { 114 case AF_INET: 115 addr.sin_port = htons(port); 116 rc = connect(conn_fd, (struct sockaddr *) &addr, sizeof(addr)); 117 break; 118 case AF_INET6: 119 addr6.sin6_port = htons(port); 120 rc = connect(conn_fd, (struct sockaddr *) &addr6, sizeof(addr6)); 121 break; 122 default: 123 printf("Unknown address family.\n"); 124 goto error; 125 } 111 126 112 rc = connect(conn_fd, (struct sockaddr *)&addr, sizeof(addr));113 127 if (rc != EOK) 114 128 goto error;
Note:
See TracChangeset
for help on using the changeset viewer.