Changes in uspace/app/nterm/conn.c [fff7ef4:02a09ed] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/nterm/conn.c
rfff7ef4 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); 90 90 goto error; 91 91 } 92 93 addr .sin_addr.s_addr = host2uint32_t_be(hinfo->addr.ipv4);92 93 addr_addr = hinfo->addr; 94 94 } 95 96 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); 97 102 if (*endptr != '\0') { 98 103 printf("Invalid port number %s\n", port_s); 99 104 goto error; 100 105 } 101 106 107 printf("Connecting to host %s port %u\n", addr_s, port); 108 102 109 conn_fd = socket(PF_INET, SOCK_STREAM, 0); 103 110 if (conn_fd < 0) 104 111 goto error; 105 106 printf("Connecting to host %s port %u\n", addr_s, ntohs(addr.sin_port)); 107 108 rc = connect(conn_fd, (struct sockaddr *)&addr, sizeof(addr)); 112 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 } 126 109 127 if (rc != EOK) 110 128 goto error; 111 129 112 130 rcv_fid = fibril_create(rcv_fibril, NULL); 113 131 if (rcv_fid == 0) 114 132 goto error; 115 133 116 134 fibril_add_ready(rcv_fid); 117 135 118 136 return EOK; 119 137 120 138 error: 121 139 if (conn_fd >= 0) { … … 123 141 conn_fd = -1; 124 142 } 125 143 126 144 return EIO; 127 145 } … … 129 147 int conn_send(void *data, size_t size) 130 148 { 131 int rc; 132 133 rc = send(conn_fd, data, size, 0); 149 int rc = send(conn_fd, data, size, 0); 134 150 if (rc != EOK) 135 151 return EIO; 136 152 137 153 return EOK; 138 154 }
Note:
See TracChangeset
for help on using the changeset viewer.