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