Changeset 26de91a in mainline
- Timestamp:
- 2013-10-04T17:19:20Z (11 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 9749e47
- Parents:
- e2839d7
- Location:
- uspace
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/netspeed/netspeed.c
re2839d7 r26de91a 36 36 */ 37 37 38 #include <assert.h> 39 #include <inet/dnsr.h> 40 #include <net/in.h> 41 #include <net/inet.h> 42 #include <net/socket.h> 38 43 #include <stdio.h> 39 44 #include <stdlib.h> … … 41 46 #include <str_error.h> 42 47 #include <task.h> 43 44 #include <net/in.h>45 #include <net/inet.h>46 #include <net/socket.h>47 48 48 49 #define NAME "netspeed" … … 114 115 } 115 116 116 static int client(sock_type_t sock_type, const char * address, unsigned port,117 static int client(sock_type_t sock_type, const char *host, unsigned port, 117 118 unsigned long count, char *buf, size_t bufsize) 118 119 { 119 struct sockaddr_in addr; 120 121 addr.sin_family = AF_INET; 122 addr.sin_port = htons(port); 123 124 int rc = inet_pton(AF_INET, address, (void *) &addr.sin_addr.s_addr); 125 if (rc != EOK) { 126 fprintf(stderr, "inet_pton failed: %s\n", str_error(rc)); 127 return rc; 128 } 129 130 int conn_sd = socket(PF_INET, sock_type, 0); 120 inet_addr_t iaddr; 121 struct sockaddr *saddr; 122 socklen_t saddrlen; 123 124 int rc = inet_addr_parse(host, &iaddr); 125 if (rc != EOK) { 126 dnsr_hostinfo_t *hinfo = NULL; 127 rc = dnsr_name2host(host, &hinfo, ip_any); 128 if (rc != EOK) { 129 fprintf(stderr, "Error resolving host '%s'.\n", host); 130 return ENOENT; 131 } 132 133 iaddr = hinfo->addr; 134 } 135 136 rc = inet_addr_sockaddr(&iaddr, port, &saddr, &saddrlen); 137 if (rc != EOK) { 138 assert(rc == ENOMEM); 139 fprintf(stderr, "Out of memory.\n"); 140 return ENOMEM; 141 } 142 143 int conn_sd = socket(saddr->sa_family, sock_type, 0); 131 144 if (conn_sd < 0) { 132 145 fprintf(stderr, "socket failed: %s\n", str_error(rc)); … … 135 148 136 149 if (sock_type == SOCK_STREAM) { 137 rc = connect(conn_sd, (struct sockaddr *) &addr, sizeof(addr));150 rc = connect(conn_sd, saddr, saddrlen); 138 151 if (rc != EOK) { 139 152 fprintf(stderr, "connect failed: %s\n", str_error(rc)); … … 151 164 rc = send(conn_sd, buf, bufsize, 0); 152 165 } else { 153 rc = sendto(conn_sd, buf, bufsize, 0, 154 (struct sockaddr *) &addr, sizeof(addr)); 166 rc = sendto(conn_sd, buf, bufsize, 0, saddr, saddrlen); 155 167 } 156 168 if (rc != EOK) { … … 161 173 162 174 closesocket(conn_sd); 175 free(saddr); 163 176 return rc; 164 177 } … … 167 180 { 168 181 fprintf(stderr, "Usage: netspeed <tcp|udp> server [port] <buffer size>\n"); 169 fprintf(stderr, " netspeed <tcp|udp> client < ip> <port> <count> <buffer size>\n");182 fprintf(stderr, " netspeed <tcp|udp> client <host> <port> <count> <buffer size>\n"); 170 183 } 171 184 -
uspace/app/nettest1/nettest1.c
re2839d7 r26de91a 38 38 #include "print_error.h" 39 39 40 #include <assert.h> 40 41 #include <malloc.h> 41 42 #include <stdio.h> … … 326 327 } 327 328 328 char * addr_s= argv[argc - 1];329 char *host = argv[argc - 1]; 329 330 330 331 /* Interpret as address */ 331 inet_addr_t addr_addr;332 rc = inet_addr_parse( addr_s, &addr_addr);332 inet_addr_t iaddr; 333 rc = inet_addr_parse(host, &iaddr); 333 334 334 335 if (rc != EOK) { 335 336 /* Interpret as a host name */ 336 337 dnsr_hostinfo_t *hinfo = NULL; 337 rc = dnsr_name2host( addr_s, &hinfo, ipver_from_af(family));338 rc = dnsr_name2host(host, &hinfo, ipver_from_af(family)); 338 339 339 340 if (rc != EOK) { 340 printf("Error resolving host '%s'.\n", addr_s);341 printf("Error resolving host '%s'.\n", host); 341 342 return EINVAL; 342 343 } 343 344 344 addr_addr = hinfo->addr; 345 } 346 347 struct sockaddr_in addr; 348 struct sockaddr_in6 addr6; 349 uint16_t af = inet_addr_sockaddr_in(&addr_addr, &addr, &addr6); 345 iaddr = hinfo->addr; 346 } 347 348 rc = inet_addr_sockaddr(&iaddr, port, &address, &addrlen); 349 if (rc != EOK) { 350 assert(rc == ENOMEM); 351 printf("Out of memory.\n"); 352 return ENOMEM; 353 } 350 354 351 355 if (family == AF_NONE) 352 family = a f;353 354 if (a f!= family) {356 family = address->sa_family; 357 358 if (address->sa_family != family) { 355 359 printf("Address family does not match explicitly set family.\n"); 356 360 return EINVAL; 357 }358 359 /* Prepare the address buffer */360 361 switch (af) {362 case AF_INET:363 addr.sin_port = htons(port);364 address = (struct sockaddr *) &addr;365 addrlen = sizeof(addr);366 break;367 case AF_INET6:368 addr6.sin6_port = htons(port);369 address = (struct sockaddr *) &addr6;370 addrlen = sizeof(addr6);371 break;372 default:373 fprintf(stderr, "Address family is not supported\n");374 return EAFNOSUPPORT;375 361 } 376 362 … … 437 423 &time_before)); 438 424 425 free(address); 426 439 427 if (verbose) 440 428 printf("Exiting\n"); -
uspace/app/nettest2/nettest2.c
re2839d7 r26de91a 38 38 #include "print_error.h" 39 39 40 #include <assert.h> 40 41 #include <malloc.h> 41 42 #include <stdio.h> … … 281 282 } 282 283 283 struct sockaddr_in addr; 284 struct sockaddr_in6 addr6; 285 uint16_t af = inet_addr_sockaddr_in(&addr_addr, &addr, &addr6); 284 struct sockaddr *address; 285 socklen_t addrlen; 286 rc = inet_addr_sockaddr(&addr_addr, port, &address, &addrlen); 287 if (rc != EOK) { 288 assert(rc == ENOMEM); 289 printf("Out of memory.\n"); 290 return ENOMEM; 291 } 286 292 287 293 if (family == AF_NONE) 288 family = a f;289 290 if (a f!= family) {294 family = address->sa_family; 295 296 if (address->sa_family != family) { 291 297 printf("Address family does not match explicitly set family.\n"); 292 298 return EINVAL; 293 }294 295 /* Prepare the address buffer */296 297 struct sockaddr *address;298 socklen_t addrlen;299 300 switch (af) {301 case AF_INET:302 addr.sin_port = htons(port);303 address = (struct sockaddr *) &addr;304 addrlen = sizeof(addr);305 break;306 case AF_INET6:307 addr6.sin6_port = htons(port);308 address = (struct sockaddr *) &addr6;309 addrlen = sizeof(addr6);310 break;311 default:312 fprintf(stderr, "Address family is not supported\n");313 return EAFNOSUPPORT;314 299 } 315 300 … … 427 412 return rc; 428 413 414 free(address); 415 429 416 if (verbose) 430 417 printf("\nExiting\n"); -
uspace/app/nettest3/nettest3.c
re2839d7 r26de91a 37 37 #include <async.h> 38 38 #include <stdio.h> 39 #include <stdlib.h> 39 40 #include <str.h> 40 41 … … 52 53 static char buf[BUF_SIZE]; 53 54 54 static struct sockaddr_in addr; 55 static struct sockaddr *address; 56 static socklen_t addrlen; 55 57 56 58 static uint16_t port; … … 62 64 char *endptr; 63 65 dnsr_hostinfo_t *hinfo; 66 inet_addr_t addr; 67 char *addr_s; 64 68 65 69 port = 7; … … 69 73 70 74 /* Connect to local IP address by default */ 71 addr.sin_family = AF_INET; 72 addr.sin_port = htons(port); 73 addr.sin_addr.s_addr = htonl(0x7f000001); 75 inet_addr(&addr, 127, 0, 0, 1); 74 76 75 77 if (argc >= 2) { 76 78 printf("parsing address '%s'\n", argv[1]); 77 rc = inet_ pton(AF_INET, argv[1], (uint8_t *)&addr.sin_addr.s_addr);79 rc = inet_addr_parse(argv[1], &addr); 78 80 if (rc != EOK) { 79 81 /* Try interpreting as a host name */ … … 84 86 } 85 87 86 uint16_t af = inet_addr_sockaddr_in(&hinfo->addr, &addr, NULL); 87 if (af != AF_INET) { 88 printf("Host '%s' not resolved as IPv4 address.\n", argv[1]); 89 return rc; 90 } 88 addr = hinfo->addr; 91 89 } 92 printf("result: rc=%d, family=%d, addr=%x\n", rc, 93 addr.sin_family, addr.sin_addr.s_addr); 90 rc = inet_addr_format(&addr, &addr_s); 91 if (rc != EOK) { 92 assert(rc == ENOMEM); 93 printf("Out of memory.\n"); 94 return rc; 95 } 96 printf("result: rc=%d, ver=%d, addr=%s\n", rc, 97 addr.version, addr_s); 98 free(addr_s); 94 99 } 95 100 96 101 if (argc >= 3) { 97 102 printf("parsing port '%s'\n", argv[2]); 98 addr.sin_port = htons(strtoul(argv[2], &endptr, 10));103 port = htons(strtoul(argv[2], &endptr, 10)); 99 104 if (*endptr != '\0') { 100 105 fprintf(stderr, "Error parsing port\n"); … … 103 108 } 104 109 110 rc = inet_addr_sockaddr(&hinfo->addr, port, &address, &addrlen); 111 if (rc != EOK) { 112 printf("Out of memory.\n"); 113 return rc; 114 } 115 105 116 printf("socket()\n"); 106 fd = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP);117 fd = socket(address->sa_family, SOCK_STREAM, IPPROTO_TCP); 107 118 printf(" -> %d\n", fd); 108 119 if (fd < 0) … … 110 121 111 122 printf("connect()\n"); 112 rc = connect(fd, (struct sockaddr *) &addr, sizeof(addr));123 rc = connect(fd, address, addrlen); 113 124 printf(" -> %d\n", rc); 114 125 if (rc != 0) … … 133 144 printf(" -> %d\n", rc); 134 145 146 free(address); 147 135 148 return 0; 136 149 } -
uspace/app/nterm/conn.c
re2839d7 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, ip_any);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; -
uspace/lib/c/generic/inet/addr.c
re2839d7 r26de91a 636 636 } 637 637 638 int inet_addr_sockaddr(const inet_addr_t *addr, uint16_t port, 639 sockaddr_t **nsockaddr, socklen_t *naddrlen) 640 { 641 sockaddr_in_t *sa4; 642 sockaddr_in6_t *sa6; 643 644 switch (addr->version) { 645 case ip_v4: 646 sa4 = calloc(1, sizeof(sockaddr_in_t)); 647 if (sa4 == NULL) 648 return ENOMEM; 649 650 sa4->sin_family = AF_INET; 651 sa4->sin_port = host2uint16_t_be(port); 652 sa4->sin_addr.s_addr = host2uint32_t_be(addr->addr); 653 if (nsockaddr != NULL) 654 *nsockaddr = (sockaddr_t *)sa4; 655 if (naddrlen != NULL) 656 *naddrlen = sizeof(*sa4); 657 break; 658 case ip_v6: 659 sa6 = calloc(1, sizeof(sockaddr_in6_t)); 660 if (sa6 == NULL) 661 return ENOMEM; 662 663 sa6->sin6_family = AF_INET6; 664 sa6->sin6_port = host2uint16_t_be(port); 665 host2addr128_t_be(addr->addr6, sa6->sin6_addr.s6_addr); 666 if (nsockaddr != NULL) 667 *nsockaddr = (sockaddr_t *)sa6; 668 if (naddrlen != NULL) 669 *naddrlen = sizeof(*sa6); 670 break; 671 default: 672 assert(false); 673 break; 674 } 675 676 return EOK; 677 } 678 638 679 /** @} 639 680 */ -
uspace/lib/c/include/inet/addr.h
re2839d7 r26de91a 39 39 #include <net/in.h> 40 40 #include <net/in6.h> 41 #include <net/socket.h> 41 42 42 43 typedef uint32_t addr32_t; … … 135 136 136 137 extern ip_ver_t ipver_from_af(int af); 138 extern int inet_addr_sockaddr(const inet_addr_t *, uint16_t, sockaddr_t **, 139 socklen_t *); 137 140 138 141 #endif -
uspace/lib/http/http.c
re2839d7 r26de91a 174 174 } 175 175 176 struct sockaddr_in addr; 177 struct sockaddr_in6 addr6; 178 uint16_t af = inet_addr_sockaddr_in(&http->addr, &addr, &addr6); 179 180 http->conn_sd = socket(PF_INET, SOCK_STREAM, 0); 176 struct sockaddr *saddr; 177 socklen_t saddrlen; 178 179 rc = inet_addr_sockaddr(&http->addr, http->port, &saddr, &saddrlen); 180 if (rc != EOK) { 181 assert(rc == ENOMEM); 182 return ENOMEM; 183 } 184 185 http->conn_sd = socket(saddr->sa_family, SOCK_STREAM, 0); 181 186 if (http->conn_sd < 0) 182 187 return http->conn_sd; 183 188 184 switch (af) { 185 case AF_INET: 186 addr.sin_port = htons(http->port); 187 rc = connect(http->conn_sd, (struct sockaddr *) &addr, sizeof(addr)); 188 break; 189 case AF_INET6: 190 addr6.sin6_port = htons(http->port); 191 rc = connect(http->conn_sd, (struct sockaddr *) &addr6, sizeof(addr6)); 192 break; 193 default: 194 return ENOTSUP; 195 } 189 rc = connect(http->conn_sd, saddr, saddrlen); 190 free(saddr); 196 191 197 192 return rc; -
uspace/srv/net/dnsrsrv/transport.c
re2839d7 r26de91a 182 182 { 183 183 trans_req_t *treq = NULL; 184 struct sockaddr *saddr = NULL; 185 socklen_t saddrlen; 184 186 185 187 void *req_data; … … 189 191 goto error; 190 192 191 struct sockaddr_in addr; 192 struct sockaddr_in6 addr6; 193 uint16_t af = 194 inet_addr_sockaddr_in(&dns_server_addr, &addr, &addr6); 195 196 struct sockaddr *address; 197 socklen_t addrlen; 198 199 switch (af) { 200 case AF_INET: 201 addr.sin_port = htons(DNS_SERVER_PORT); 202 address = (struct sockaddr *) &addr; 203 addrlen = sizeof(addr); 204 break; 205 case AF_INET6: 206 addr6.sin6_port = htons(DNS_SERVER_PORT); 207 address = (struct sockaddr *) &addr6; 208 addrlen = sizeof(addr6); 209 break; 210 default: 211 rc = EAFNOSUPPORT; 193 rc = inet_addr_sockaddr(&dns_server_addr, DNS_SERVER_PORT, 194 &saddr, &saddrlen); 195 if (rc != EOK) { 196 assert(rc == ENOMEM); 212 197 goto error; 213 198 } … … 217 202 while (ntry < REQ_RETRY_MAX) { 218 203 rc = sendto(transport_fd, req_data, req_size, 0, 219 (struct sockaddr *) address,addrlen);204 saddr, saddrlen); 220 205 if (rc != EOK) 221 206 goto error; … … 256 241 treq_destroy(treq); 257 242 free(req_data); 243 free(saddr); 258 244 return EOK; 259 245 … … 263 249 264 250 free(req_data); 251 free(saddr); 265 252 return rc; 266 253 }
Note:
See TracChangeset
for help on using the changeset viewer.