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