Changes in uspace/app/nterm/conn.c [26de91a:0aa70f4] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/nterm/conn.c
r26de91a r0aa70f4 38 38 #include <fibril.h> 39 39 #include <inet/dnsr.h> 40 #include <net/inet.h>41 40 #include <net/socket.h> 42 41 #include <stdio.h> 43 #include <stdlib.h>44 42 #include <str_error.h> 45 43 #include <sys/types.h> … … 75 73 } 76 74 77 int conn_open(const char * host, const char *port_s)75 int conn_open(const char *addr_s, const char *port_s) 78 76 { 79 77 int conn_fd = -1; 80 struct sockaddr *saddr = NULL;81 socklen_t saddrlen;82 78 83 79 /* Interpret as address */ 84 inet_addr_t iaddr;85 int rc = inet_addr_parse( host, &iaddr);80 inet_addr_t addr_addr; 81 int rc = inet_addr_parse(addr_s, &addr_addr); 86 82 87 83 if (rc != EOK) { 88 84 /* Interpret as a host name */ 89 85 dnsr_hostinfo_t *hinfo = NULL; 90 rc = dnsr_name2host( host, &hinfo, ip_any);86 rc = dnsr_name2host(addr_s, &hinfo, 0); 91 87 92 88 if (rc != EOK) { 93 printf("Error resolving host '%s'.\n", host);89 printf("Error resolving host '%s'.\n", addr_s); 94 90 goto error; 95 91 } 96 92 97 iaddr = hinfo->addr;93 addr_addr = hinfo->addr; 98 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 99 100 100 char *endptr; … … 105 105 } 106 106 107 rc = inet_addr_sockaddr(&iaddr, port, &saddr, &saddrlen); 108 if (rc != EOK) { 109 assert(rc == ENOMEM); 110 printf("Out of memory.\n"); 111 return ENOMEM; 112 } 107 printf("Connecting to host %s port %u\n", addr_s, port); 113 108 114 printf("Connecting to host %s port %u\n", host, port); 115 116 conn_fd = socket(saddr->sa_family, SOCK_STREAM, 0); 109 conn_fd = socket(PF_INET, SOCK_STREAM, 0); 117 110 if (conn_fd < 0) 118 111 goto error; 119 112 120 rc = connect(conn_fd, saddr, saddrlen); 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 121 127 if (rc != EOK) 122 128 goto error; … … 128 134 fibril_add_ready(rcv_fid); 129 135 130 free(saddr);131 136 return EOK; 137 132 138 error: 133 139 if (conn_fd >= 0) { … … 135 141 conn_fd = -1; 136 142 } 137 free(saddr);138 143 139 144 return EIO;
Note:
See TracChangeset
for help on using the changeset viewer.