Changes in uspace/app/nterm/conn.c [0d67e16:26de91a] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/nterm/conn.c
r0d67e16 r26de91a 33 33 */ 34 34 35 #include <bool.h> 35 #include <byteorder.h> 36 #include <stdbool.h> 36 37 #include <errno.h> 37 38 #include <fibril.h> 39 #include <inet/dnsr.h> 40 #include <net/inet.h> 38 41 #include <net/socket.h> 39 42 #include <stdio.h> 43 #include <stdlib.h> 40 44 #include <str_error.h> 41 45 #include <sys/types.h> … … 71 75 } 72 76 73 int conn_open(const char * addr_s, const char *port_s)77 int conn_open(const char *host, const char *port_s) 74 78 { 75 struct sockaddr_in addr; 76 int rc; 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 87 if (rc != EOK) { 88 /* Interpret as a host name */ 89 dnsr_hostinfo_t *hinfo = NULL; 90 rc = dnsr_name2host(host, &hinfo, ip_any); 91 92 if (rc != EOK) { 93 printf("Error resolving host '%s'.\n", host); 94 goto error; 95 } 96 97 iaddr = hinfo->addr; 98 } 99 77 100 char *endptr; 78 79 addr.sin_family = AF_INET; 80 81 rc = inet_pton(addr.sin_family, addr_s, (uint8_t *)&addr.sin_addr); 82 if (rc != EOK) { 83 printf("Invalid addres %s\n", addr_s); 84 return EINVAL; 85 } 86 87 addr.sin_port = htons(strtol(port_s, &endptr, 10)); 101 uint16_t port = strtol(port_s, &endptr, 10); 88 102 if (*endptr != '\0') { 89 103 printf("Invalid port number %s\n", port_s); 90 return EINVAL;104 goto error; 91 105 } 92 93 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); 94 117 if (conn_fd < 0) 95 118 goto error; 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)); 119 120 rc = connect(conn_fd, saddr, saddrlen); 100 121 if (rc != EOK) 101 122 goto error; 102 123 103 124 rcv_fid = fibril_create(rcv_fibril, NULL); 104 125 if (rcv_fid == 0) 105 126 goto error; 106 127 107 128 fibril_add_ready(rcv_fid); 108 129 130 free(saddr); 109 131 return EOK; 110 111 132 error: 112 133 if (conn_fd >= 0) { … … 114 135 conn_fd = -1; 115 136 } 116 137 free(saddr); 138 117 139 return EIO; 118 140 } … … 120 142 int conn_send(void *data, size_t size) 121 143 { 122 int rc; 123 124 rc = send(conn_fd, data, size, 0); 144 int rc = send(conn_fd, data, size, 0); 125 145 if (rc != EOK) 126 146 return EIO; 127 147 128 148 return EOK; 129 149 }
Note:
See TracChangeset
for help on using the changeset viewer.