Changes in uspace/app/nterm/conn.c [fff7ef4:26de91a] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/nterm/conn.c
rfff7ef4 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 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);79 int conn_fd = -1; 80 struct sockaddr *saddr = NULL; 81 socklen_t saddrlen; 82 83 /* Interpret as address */ 84 inet_addr_t iaddr; 85 int rc = inet_addr_parse(host, &iaddr); 86 85 87 if (rc != EOK) { 86 /* Try interpreting as a host name */ 87 rc = dnsr_name2host(addr_s, &hinfo); 88 /* Interpret as a host name */ 89 dnsr_hostinfo_t *hinfo = NULL; 90 rc = dnsr_name2host(host, &hinfo, ip_any); 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 93 addr.sin_addr.s_addr = host2uint32_t_be(hinfo->addr.ipv4);96 97 iaddr = hinfo->addr; 94 98 } 95 96 addr.sin_port = htons(strtol(port_s, &endptr, 10)); 99 100 char *endptr; 101 uint16_t port = strtol(port_s, &endptr, 10); 97 102 if (*endptr != '\0') { 98 103 printf("Invalid port number %s\n", port_s); 99 104 goto error; 100 105 } 101 102 conn_fd = socket(PF_INET, SOCK_STREAM, 0); 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 } 113 114 printf("Connecting to host %s port %u\n", host, port); 115 116 conn_fd = socket(saddr->sa_family, SOCK_STREAM, 0); 103 117 if (conn_fd < 0) 104 118 goto error; 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)); 119 120 rc = connect(conn_fd, saddr, saddrlen); 109 121 if (rc != EOK) 110 122 goto error; 111 123 112 124 rcv_fid = fibril_create(rcv_fibril, NULL); 113 125 if (rcv_fid == 0) 114 126 goto error; 115 127 116 128 fibril_add_ready(rcv_fid); 117 129 130 free(saddr); 118 131 return EOK; 119 120 132 error: 121 133 if (conn_fd >= 0) { … … 123 135 conn_fd = -1; 124 136 } 125 137 free(saddr); 138 126 139 return EIO; 127 140 } … … 129 142 int conn_send(void *data, size_t size) 130 143 { 131 int rc; 132 133 rc = send(conn_fd, data, size, 0); 144 int rc = send(conn_fd, data, size, 0); 134 145 if (rc != EOK) 135 146 return EIO; 136 147 137 148 return EOK; 138 149 }
Note:
See TracChangeset
for help on using the changeset viewer.