Changes in uspace/app/nterm/conn.c [3e6a98c5:0aa70f4] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/nterm/conn.c
r3e6a98c5 r0aa70f4 33 33 */ 34 34 35 #include <byteorder.h> 35 36 #include <stdbool.h> 36 37 #include <errno.h> 37 38 #include <fibril.h> 39 #include <inet/dnsr.h> 38 40 #include <net/socket.h> 39 41 #include <stdio.h> … … 73 75 int conn_open(const char *addr_s, const char *port_s) 74 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 83 if (rc != EOK) { 84 /* Interpret as a host name */ 85 dnsr_hostinfo_t *hinfo = NULL; 86 rc = dnsr_name2host(addr_s, &hinfo, 0); 87 88 if (rc != EOK) { 89 printf("Error resolving host '%s'.\n", addr_s); 90 goto error; 91 } 92 93 addr_addr = hinfo->addr; 94 } 95 75 96 struct sockaddr_in addr; 76 int rc; 97 struct sockaddr_in6 addr6; 98 uint16_t af = inet_addr_sockaddr_in(&addr_addr, &addr, &addr6); 99 77 100 char *endptr; 78 79 addr.sin_family = AF_INET; 80 81 rc = inet_pton(addr.sin_family, addr_s, (uint8_t *)&addr.sin_addr); 82 if (rc != EOK) { 83 printf("Invalid addres %s\n", addr_s); 84 return EINVAL; 85 } 86 87 addr.sin_port = htons(strtol(port_s, &endptr, 10)); 101 uint16_t port = strtol(port_s, &endptr, 10); 88 102 if (*endptr != '\0') { 89 103 printf("Invalid port number %s\n", port_s); 90 return EINVAL;104 goto error; 91 105 } 92 106 107 printf("Connecting to host %s port %u\n", addr_s, port); 108 93 109 conn_fd = socket(PF_INET, SOCK_STREAM, 0); 94 110 if (conn_fd < 0) 95 111 goto error; 96 97 printf("Connecting to address %s port %u\n", addr_s, ntohs(addr.sin_port)); 98 99 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 100 127 if (rc != EOK) 101 128 goto error; 102 129 103 130 rcv_fid = fibril_create(rcv_fibril, NULL); 104 131 if (rcv_fid == 0) 105 132 goto error; 106 133 107 134 fibril_add_ready(rcv_fid); 108 135 109 136 return EOK; 110 137 111 138 error: 112 139 if (conn_fd >= 0) { … … 114 141 conn_fd = -1; 115 142 } 116 143 117 144 return EIO; 118 145 } … … 120 147 int conn_send(void *data, size_t size) 121 148 { 122 int rc; 123 124 rc = send(conn_fd, data, size, 0); 149 int rc = send(conn_fd, data, size, 0); 125 150 if (rc != EOK) 126 151 return EIO; 127 152 128 153 return EOK; 129 154 }
Note:
See TracChangeset
for help on using the changeset viewer.