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