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