Changeset 02a09ed in mainline
- Timestamp:
- 2013-06-28T20:20:03Z (11 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 1d24ad3
- Parents:
- edf0d27
- Location:
- uspace
- Files:
-
- 2 deleted
- 46 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/nettest1/nettest1.c
redf0d27 r02a09ed 59 59 #define NETTEST1_TEXT "Networking test 1 - sockets" 60 60 61 static int family = PF_INET;61 static uint16_t family = AF_INET; 62 62 static sock_type_t type = SOCK_DGRAM; 63 static char *data;64 63 static size_t size = 27; 65 static int verbose = 0; 64 static bool verbose = false; 65 static int sockets = 10; 66 static int messages = 10; 67 static uint16_t port = 7; 66 68 67 69 static struct sockaddr *address; 68 70 static socklen_t addrlen; 69 71 70 static int sockets; 71 static int messages; 72 static uint16_t port; 72 static char *data; 73 73 74 74 static void nettest1_print_help(void) … … 299 299 int main(int argc, char *argv[]) 300 300 { 301 struct sockaddr_in address_in;302 struct sockaddr_in6 address_in6;303 dnsr_hostinfo_t *hinfo;304 uint8_t *address_start;305 306 int *socket_ids;307 int index;308 struct timeval time_before;309 struct timeval time_after;310 311 int rc;312 313 sockets = 10;314 messages = 10;315 port = 7;316 317 301 /* 318 302 * Parse the command line arguments. Stop before the last argument 319 303 * if it does not start with dash ('-') 320 304 */ 321 for (index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); index++) { 305 int index; 306 int rc; 307 308 for (index = 1; (index < argc - 1) || ((index == argc - 1) && 309 (argv[index][0] == '-')); index++) { 322 310 /* Options should start with dash ('-') */ 323 311 if (argv[index][0] == '-') { … … 331 319 } 332 320 333 /* If not before the last argument containing the host */321 /* The last argument containing the host */ 334 322 if (index >= argc) { 335 printf(" Command line error: missing host name\n");323 printf("Host name missing.\n"); 336 324 nettest1_print_help(); 337 325 return EINVAL; 338 326 } 339 327 328 char *addr_s = argv[argc - 1]; 329 330 /* Interpret as address */ 331 inet_addr_t addr_addr; 332 rc = inet_addr_parse(addr_s, &addr_addr); 333 334 if (rc != EOK) { 335 /* Interpret as a host name */ 336 dnsr_hostinfo_t *hinfo = NULL; 337 rc = dnsr_name2host(addr_s, &hinfo); 338 339 if (rc != EOK) { 340 printf("Error resolving host '%s'.\n", addr_s); 341 return EINVAL; 342 } 343 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); 350 351 if (af != family) { 352 printf("Address family does not match explicitly set family.\n"); 353 return EINVAL; 354 } 355 340 356 /* Prepare the address buffer */ 341 342 switch (family) { 343 case PF_INET: 344 address_in.sin_family = AF_INET; 345 address_in.sin_port = htons(port); 346 address = (struct sockaddr *) &address_in; 347 addrlen = sizeof(address_in); 348 address_start = (uint8_t *) &address_in.sin_addr.s_addr; 349 break; 350 case PF_INET6: 351 address_in6.sin6_family = AF_INET6; 352 address_in6.sin6_port = htons(port); 353 address = (struct sockaddr *) &address_in6; 354 addrlen = sizeof(address_in6); 355 address_start = (uint8_t *) &address_in6.sin6_addr.s6_addr; 357 358 switch (af) { 359 case AF_INET: 360 addr.sin_port = htons(port); 361 address = (struct sockaddr *) &addr; 362 addrlen = sizeof(addr); 363 break; 364 case AF_INET6: 365 addr6.sin6_port = htons(port); 366 address = (struct sockaddr *) &addr6; 367 addrlen = sizeof(addr6); 356 368 break; 357 369 default: 358 370 fprintf(stderr, "Address family is not supported\n"); 359 371 return EAFNOSUPPORT; 360 }361 362 /* Parse the last argument which should contain the host/address */363 rc = inet_pton(family, argv[argc - 1], address_start);364 if (rc != EOK) {365 /* Try interpreting as a host name */366 rc = dnsr_name2host(argv[argc - 1], &hinfo);367 if (rc != EOK) {368 printf("Error resolving host '%s'.\n", argv[argc - 1]);369 return rc;370 }371 372 rc = inet_addr_sockaddr_in(&hinfo->addr, &address_in);373 if (rc != EOK) {374 printf("Host '%s' not resolved as IPv4 address.\n", argv[argc - 1]);375 return rc;376 }377 372 } 378 373 … … 406 401 * null (\0). 407 402 */ 408 socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));403 int *socket_ids = (int *) malloc(sizeof(int) * (sockets + 1)); 409 404 if (!socket_ids) { 410 405 fprintf(stderr, "Failed to allocate receive buffer.\n"); … … 417 412 printf("Starting tests\n"); 418 413 414 struct timeval time_before; 419 415 rc = gettimeofday(&time_before, NULL); 420 416 if (rc != EOK) { … … 428 424 nettest1_test(socket_ids, sockets, messages); 429 425 426 struct timeval time_after; 430 427 rc = gettimeofday(&time_after, NULL); 431 428 if (rc != EOK) { -
uspace/app/nettest2/nettest2.c
redf0d27 r02a09ed 60 60 #define NETTEST2_TEXT "Networking test 2 - transfer" 61 61 62 static size_t size;63 static bool verbose;64 static sock_type_t type;65 static int sockets;66 static int messages;67 static int family;68 static uint16_t port ;62 static uint16_t family = PF_INET; 63 static size_t size = 28; 64 static bool verbose = false; 65 static sock_type_t type = SOCK_DGRAM; 66 static int sockets = 10; 67 static int messages = 10; 68 static uint16_t port = 7; 69 69 70 70 static void nettest2_print_help(void) … … 234 234 int main(int argc, char *argv[]) 235 235 { 236 struct sockaddr *address;237 struct sockaddr_in address_in;238 struct sockaddr_in6 address_in6;239 dnsr_hostinfo_t *hinfo;240 socklen_t addrlen;241 uint8_t *address_start;242 243 int *socket_ids;244 char *data;245 236 int index; 246 struct timeval time_before;247 struct timeval time_after;248 249 237 int rc; 250 251 size = 28; 252 verbose = false; 253 type = SOCK_DGRAM; 254 sockets = 10; 255 messages = 10; 256 family = PF_INET; 257 port = 7; 258 238 259 239 /* 260 240 * Parse the command line arguments. … … 264 244 for (index = 1; (index < argc - 1) || ((index == argc - 1) && 265 245 (argv[index][0] == '-')); ++index) { 266 267 246 /* Options should start with dash ('-') */ 268 247 if (argv[index][0] == '-') { … … 276 255 } 277 256 278 /* If not before the last argument containing the host */257 /* The last argument containing the host */ 279 258 if (index >= argc) { 280 printf(" Command line error: missing host name\n");259 printf("Host name missing.\n"); 281 260 nettest2_print_help(); 282 261 return EINVAL; 283 262 } 284 263 264 char *addr_s = argv[argc - 1]; 265 266 /* Interpret as address */ 267 inet_addr_t addr_addr; 268 rc = inet_addr_parse(addr_s, &addr_addr); 269 270 if (rc != EOK) { 271 /* Interpret as a host name */ 272 dnsr_hostinfo_t *hinfo = NULL; 273 rc = dnsr_name2host(addr_s, &hinfo); 274 275 if (rc != EOK) { 276 printf("Error resolving host '%s'.\n", addr_s); 277 return EINVAL; 278 } 279 280 addr_addr = hinfo->addr; 281 } 282 283 struct sockaddr_in addr; 284 struct sockaddr_in6 addr6; 285 uint16_t af = inet_addr_sockaddr_in(&addr_addr, &addr, &addr6); 286 287 if (af != family) { 288 printf("Address family does not match explicitly set family.\n"); 289 return EINVAL; 290 } 291 285 292 /* Prepare the address buffer */ 286 287 switch (family) { 288 case PF_INET: 289 address_in.sin_family = AF_INET; 290 address_in.sin_port = htons(port); 291 address = (struct sockaddr *) &address_in; 292 addrlen = sizeof(address_in); 293 address_start = (uint8_t *) &address_in.sin_addr.s_addr; 294 break; 295 case PF_INET6: 296 address_in6.sin6_family = AF_INET6; 297 address_in6.sin6_port = htons(port); 298 address = (struct sockaddr *) &address_in6; 299 addrlen = sizeof(address_in6); 300 address_start = (uint8_t *) &address_in6.sin6_addr.s6_addr; 293 294 struct sockaddr *address; 295 socklen_t addrlen; 296 297 switch (af) { 298 case AF_INET: 299 addr.sin_port = htons(port); 300 address = (struct sockaddr *) &addr; 301 addrlen = sizeof(addr); 302 break; 303 case AF_INET6: 304 addr6.sin6_port = htons(port); 305 address = (struct sockaddr *) &addr6; 306 addrlen = sizeof(addr6); 301 307 break; 302 308 default: 303 309 fprintf(stderr, "Address family is not supported\n"); 304 310 return EAFNOSUPPORT; 305 }306 307 /* Parse the last argument which should contain the host/address */308 rc = inet_pton(family, argv[argc - 1], address_start);309 if (rc != EOK) {310 /* Try interpreting as a host name */311 rc = dnsr_name2host(argv[argc - 1], &hinfo);312 if (rc != EOK) {313 printf("Error resolving host '%s'.\n", argv[argc - 1]);314 return rc;315 }316 317 rc = inet_addr_sockaddr_in(&hinfo->addr, &address_in);318 if (rc != EOK) {319 printf("Host '%s' not resolved as IPv4 address.\n", argv[argc - 1]);320 return rc;321 }322 311 } 323 312 … … 333 322 * null character. 334 323 */ 335 data = (char *) malloc(size + 1);324 char *data = (char *) malloc(size + 1); 336 325 if (!data) { 337 326 fprintf(stderr, "Failed to allocate data buffer.\n"); … … 353 342 * Allocate count entries plus the terminating null (\0) 354 343 */ 355 socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));344 int *socket_ids = (int *) malloc(sizeof(int) * (sockets + 1)); 356 345 if (!socket_ids) { 357 346 fprintf(stderr, "Failed to allocate receive buffer.\n"); 358 347 return ENOMEM; 359 348 } 349 360 350 socket_ids[sockets] = 0; 361 351 … … 377 367 printf("\n"); 378 368 369 struct timeval time_before; 379 370 rc = gettimeofday(&time_before, NULL); 380 371 if (rc != EOK) { … … 388 379 return rc; 389 380 381 struct timeval time_after; 390 382 rc = gettimeofday(&time_after, NULL); 391 383 if (rc != EOK) { -
uspace/app/nettest3/nettest3.c
redf0d27 r02a09ed 84 84 } 85 85 86 rc = inet_addr_sockaddr_in(&hinfo->addr, &addr);87 if ( rc != EOK) {86 uint16_t af = inet_addr_sockaddr_in(&hinfo->addr, &addr, NULL); 87 if (af != AF_INET) { 88 88 printf("Host '%s' not resolved as IPv4 address.\n", argv[1]); 89 89 return rc; -
uspace/app/nterm/conn.c
redf0d27 r02a09ed 75 75 int conn_open(const char *addr_s, const char *port_s) 76 76 { 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); 77 int conn_fd = -1; 78 79 /* Interpret as address */ 80 inet_addr_t addr_addr; 81 int rc = inet_addr_parse(addr_s, &addr_addr); 82 85 83 if (rc != EOK) { 86 /* Try interpreting as a host name */ 84 /* Interpret as a host name */ 85 dnsr_hostinfo_t *hinfo = NULL; 87 86 rc = dnsr_name2host(addr_s, &hinfo); 87 88 88 if (rc != EOK) { 89 89 printf("Error resolving host '%s'.\n", addr_s); … … 91 91 } 92 92 93 rc = inet_addr_sockaddr_in(&hinfo->addr, &addr); 94 if (rc != EOK) { 95 printf("Host '%s' not resolved as IPv4 address.\n", addr_s); 96 return rc; 97 } 93 addr_addr = hinfo->addr; 98 94 } 99 100 addr.sin_port = htons(strtol(port_s, &endptr, 10)); 95 96 struct sockaddr_in addr; 97 struct sockaddr_in6 addr6; 98 uint16_t af = inet_addr_sockaddr_in(&addr_addr, &addr, &addr6); 99 100 char *endptr; 101 uint16_t port = strtol(port_s, &endptr, 10); 101 102 if (*endptr != '\0') { 102 103 printf("Invalid port number %s\n", port_s); … … 104 105 } 105 106 107 printf("Connecting to host %s port %u\n", addr_s, port); 108 106 109 conn_fd = socket(PF_INET, SOCK_STREAM, 0); 107 110 if (conn_fd < 0) 108 111 goto error; 109 112 110 printf("Connecting to host %s port %u\n", addr_s, ntohs(addr.sin_port)); 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 } 111 126 112 rc = connect(conn_fd, (struct sockaddr *)&addr, sizeof(addr));113 127 if (rc != EOK) 114 128 goto error; -
uspace/app/ping/ping.c
redf0d27 r02a09ed 37 37 #include <errno.h> 38 38 #include <fibril_synch.h> 39 #include <net/socket_codes.h> 39 40 #include <inet/dnsr.h> 40 41 #include <inet/addr.h> … … 63 64 }; 64 65 65 static uint32_t src;66 static uint32_t dest;66 static addr32_t src; 67 static addr32_t dest; 67 68 68 69 static bool ping_repeat = false; … … 84 85 { 85 86 inet_addr_t src_addr; 86 inet_addr_ unpack(sdu->src, &src_addr);87 inet_addr_set(sdu->src, &src_addr); 87 88 88 89 inet_addr_t dest_addr; 89 inet_addr_ unpack(sdu->dest, &dest_addr);90 inet_addr_set(sdu->dest, &dest_addr); 90 91 91 92 char *asrc; … … 220 221 } 221 222 222 rc = inet_addr_pack(&dest_addr, &dest);223 if ( rc != EOK) {223 uint16_t af = inet_addr_get(&dest_addr, &dest, NULL); 224 if (af != AF_INET) { 224 225 printf(NAME ": Destination '%s' is not an IPv4 address.\n", 225 226 argv[argi]); … … 235 236 236 237 inet_addr_t src_addr; 237 inet_addr_ unpack(src, &src_addr);238 inet_addr_set(src, &src_addr); 238 239 239 240 rc = inet_addr_format(&src_addr, &asrc); -
uspace/lib/c/generic/dnsr.c
redf0d27 r02a09ed 69 69 int dnsr_name2host(const char *name, dnsr_hostinfo_t **rinfo) 70 70 { 71 dnsr_hostinfo_t *info = calloc(1, sizeof(dnsr_hostinfo_t)); 72 if (info == NULL) 73 return ENOMEM; 74 71 75 async_exch_t *exch = dnsr_exchange_begin(); 72 char cname_buf[DNSR_NAME_MAX_SIZE + 1]; 73 ipc_call_t cnreply; 74 size_t act_size; 75 dnsr_hostinfo_t *info; 76 76 77 77 ipc_call_t answer; 78 78 aid_t req = async_send_0(exch, DNSR_NAME2HOST, &answer); 79 sysarg_t retval = async_data_write_start(exch, name, str_size(name)); 80 aid_t cnreq = async_data_read(exch, cname_buf, DNSR_NAME_MAX_SIZE, 81 &cnreply); 82 79 80 int rc = async_data_write_start(exch, name, str_size(name)); 81 if (rc != EOK) { 82 async_exchange_end(exch); 83 async_forget(req); 84 return rc; 85 } 86 87 ipc_call_t answer_addr; 88 aid_t req_addr = async_data_read(exch, &info->addr, 89 sizeof(inet_addr_t), &answer_addr); 90 91 sysarg_t retval_addr; 92 async_wait_for(req_addr, &retval_addr); 93 94 if (retval_addr != EOK) { 95 async_exchange_end(exch); 96 async_forget(req); 97 return (int) retval_addr; 98 } 99 100 ipc_call_t answer_cname; 101 char cname_buf[DNSR_NAME_MAX_SIZE + 1]; 102 aid_t req_cname = async_data_read(exch, cname_buf, DNSR_NAME_MAX_SIZE, 103 &answer_cname); 104 83 105 dnsr_exchange_end(exch); 84 85 if (retval != EOK) { 106 107 sysarg_t retval_cname; 108 async_wait_for(req_cname, &retval_cname); 109 110 if (retval_cname != EOK) { 86 111 async_forget(req); 87 async_forget(cnreq); 88 return retval; 112 return (int) retval_cname; 89 113 } 90 114 115 sysarg_t retval; 91 116 async_wait_for(req, &retval); 92 if (retval != EOK) { 93 async_forget(cnreq); 94 return EIO; 95 } 96 97 async_wait_for(cnreq, &retval); 117 98 118 if (retval != EOK) 99 return EIO; 100 101 info = calloc(1, sizeof(dnsr_hostinfo_t)); 102 if (info == NULL) 103 return ENOMEM; 104 105 act_size = IPC_GET_ARG2(cnreply); 119 return (int) retval; 120 121 size_t act_size = IPC_GET_ARG2(answer_cname); 106 122 assert(act_size <= DNSR_NAME_MAX_SIZE); 123 107 124 cname_buf[act_size] = '\0'; 108 125 109 126 info->cname = str_dup(cname_buf); 110 inet_addr_unpack(IPC_GET_ARG1(answer), &info->addr); 111 127 112 128 *rinfo = info; 113 129 return EOK; -
uspace/lib/c/generic/inet.c
redf0d27 r02a09ed 30 30 #include <assert.h> 31 31 #include <errno.h> 32 #include <net/socket_codes.h> 32 33 #include <inet/inet.h> 33 34 #include <ipc/inet.h> … … 107 108 int inet_send(inet_dgram_t *dgram, uint8_t ttl, inet_df_t df) 108 109 { 109 uint32_t src;110 int rc = inet_addr_pack(&dgram->src, &src);111 if (rc != EOK)112 return rc;113 114 uint32_t dest;115 rc = inet_addr_pack(&dgram->dest, &dest);116 if (rc != EOK)117 return EOK;118 119 async_exch_t *exch = async_exchange_begin(inet_sess);120 110 addr32_t src_v4; 111 addr128_t src_v6; 112 uint16_t src_af = inet_addr_get(&dgram->src, &src_v4, &src_v6); 113 114 addr32_t dest_v4; 115 addr128_t dest_v6; 116 uint16_t dest_af = inet_addr_get(&dgram->dest, &dest_v4, &dest_v6); 117 118 if (src_af != dest_af) 119 return EINVAL; 120 121 async_exch_t *exch; 121 122 ipc_call_t answer; 122 aid_t req = async_send_5(exch, INET_SEND, (sysarg_t) src, 123 (sysarg_t) dest, dgram->tos, ttl, df, &answer); 124 rc = async_data_write_start(exch, dgram->data, dgram->size); 125 126 async_exchange_end(exch); 123 aid_t req; 124 int rc; 125 126 switch (src_af) { 127 case AF_INET: 128 exch = async_exchange_begin(inet_sess); 129 130 req = async_send_5(exch, INET_SEND, (sysarg_t) src_v4, 131 (sysarg_t) dest_v4, dgram->tos, ttl, df, &answer); 132 rc = async_data_write_start(exch, dgram->data, dgram->size); 133 134 async_exchange_end(exch); 135 break; 136 case AF_INET6: 137 // FIXME TODO 138 return ENOTSUP; 139 default: 140 return EINVAL; 141 } 127 142 128 143 if (rc != EOK) { … … 133 148 sysarg_t retval; 134 149 async_wait_for(req, &retval); 135 if (retval != EOK) 136 return retval; 137 138 return EOK; 150 151 return (int) retval; 139 152 } 140 153 141 154 int inet_get_srcaddr(inet_addr_t *remote, uint8_t tos, inet_addr_t *local) 142 155 { 143 uint32_t remote_addr; 144 int rc = inet_addr_pack(remote, &remote_addr); 145 if (rc != EOK) 146 return rc; 147 148 async_exch_t *exch = async_exchange_begin(inet_sess); 149 150 sysarg_t local_addr; 151 rc = async_req_2_1(exch, INET_GET_SRCADDR, (sysarg_t) remote_addr, 152 tos, &local_addr); 153 154 async_exchange_end(exch); 155 156 if (rc != EOK) 157 return rc; 158 159 inet_addr_unpack(local_addr, local); 160 return EOK; 161 } 162 163 static void inet_ev_recv(ipc_callid_t callid, ipc_call_t *call) 164 { 156 addr32_t remote_v4; 157 addr128_t remote_v6; 158 uint16_t remote_af = inet_addr_get(remote, &remote_v4, &remote_v6); 159 160 async_exch_t *exch; 165 161 int rc; 162 163 switch (remote_af) { 164 case AF_INET: 165 exch = async_exchange_begin(inet_sess); 166 167 sysarg_t local_v4; 168 rc = async_req_2_1(exch, INET_GET_SRCADDR, (sysarg_t) remote_v4, 169 tos, &local_v4); 170 171 async_exchange_end(exch); 172 173 if (rc != EOK) 174 return rc; 175 176 inet_addr_set(local_v4, local); 177 return EOK; 178 case AF_INET6: 179 // FIXME TODO 180 return ENOTSUP; 181 default: 182 return EINVAL; 183 } 184 } 185 186 static void inet_ev_recv(ipc_callid_t iid, ipc_call_t *icall) 187 { 166 188 inet_dgram_t dgram; 167 189 168 inet_addr_unpack(IPC_GET_ARG1(*call), &dgram.src); 169 inet_addr_unpack(IPC_GET_ARG2(*call), &dgram.dest); 170 dgram.tos = IPC_GET_ARG3(*call); 190 dgram.tos = IPC_GET_ARG1(*icall); 191 192 ipc_callid_t callid; 193 size_t size; 194 if (!async_data_write_receive(&callid, &size)) { 195 async_answer_0(callid, EINVAL); 196 async_answer_0(iid, EINVAL); 197 return; 198 } 199 200 if (size != sizeof(inet_addr_t)) { 201 async_answer_0(callid, EINVAL); 202 async_answer_0(iid, EINVAL); 203 return; 204 } 205 206 int rc = async_data_write_finalize(callid, &dgram.src, size); 207 if (rc != EOK) { 208 async_answer_0(callid, rc); 209 async_answer_0(iid, rc); 210 return; 211 } 212 213 if (!async_data_write_receive(&callid, &size)) { 214 async_answer_0(callid, EINVAL); 215 async_answer_0(iid, EINVAL); 216 return; 217 } 218 219 if (size != sizeof(inet_addr_t)) { 220 async_answer_0(callid, EINVAL); 221 async_answer_0(iid, EINVAL); 222 return; 223 } 224 225 rc = async_data_write_finalize(callid, &dgram.dest, size); 226 if (rc != EOK) { 227 async_answer_0(callid, rc); 228 async_answer_0(iid, rc); 229 return; 230 } 171 231 172 232 rc = async_data_write_accept(&dgram.data, false, 0, 0, 0, &dgram.size); 173 233 if (rc != EOK) { 174 async_answer_0( callid, rc);234 async_answer_0(iid, rc); 175 235 return; 176 236 } 177 237 178 238 rc = inet_ev_ops->recv(&dgram); 179 async_answer_0( callid, rc);239 async_answer_0(iid, rc); 180 240 } 181 241 -
uspace/lib/c/generic/inet/addr.c
redf0d27 r02a09ed 39 39 #include <net/inet.h> 40 40 #include <stdio.h> 41 42 static inet_addr_t inet_addr_any_addr = { 41 #include <malloc.h> 42 #include <bitops.h> 43 44 #define INET_PREFIXSTRSIZE 5 45 46 #if !(defined(__BE__) ^ defined(__LE__)) 47 #error The architecture must be either big-endian or little-endian. 48 #endif 49 50 const addr48_t addr48_broadcast = { 51 0xff, 0xff, 0xff, 0xff, 0xff, 0xff 52 }; 53 54 static const inet_addr_t inet_addr_any_addr = { 43 55 .family = AF_INET, 44 .addr = {0, 0, 0, 0}56 .addr = 0 45 57 }; 46 58 47 static inet_addr_t inet_addr_any_addr6 = {59 static const inet_addr_t inet_addr_any_addr6 = { 48 60 .family = AF_INET6, 49 .addr = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}61 .addr6 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 50 62 }; 63 64 void addr48(const addr48_t src, addr48_t dst) 65 { 66 memcpy(dst, src, 6); 67 } 68 69 void addr128(const addr128_t src, addr128_t dst) 70 { 71 memcpy(dst, src, 16); 72 } 73 74 void host2addr128_t_be(const addr128_t host, addr128_t be) 75 { 76 #ifdef __BE__ 77 memcpy(be, host, 16); 78 #else 79 be[0] = host[15]; 80 be[1] = host[14]; 81 be[2] = host[13]; 82 be[3] = host[12]; 83 be[4] = host[11]; 84 be[5] = host[10]; 85 be[6] = host[9]; 86 be[7] = host[8]; 87 be[8] = host[7]; 88 be[9] = host[6]; 89 be[10] = host[5]; 90 be[11] = host[4]; 91 be[12] = host[3]; 92 be[13] = host[2]; 93 be[14] = host[1]; 94 be[15] = host[0]; 95 #endif 96 } 97 98 void addr128_t_be2host(const addr128_t be, addr128_t host) 99 { 100 #ifdef __BE__ 101 memcpy(host, be, 16); 102 #else 103 host[0] = be[15]; 104 host[1] = be[14]; 105 host[2] = be[13]; 106 host[3] = be[12]; 107 host[4] = be[11]; 108 host[5] = be[10]; 109 host[6] = be[9]; 110 host[7] = be[8]; 111 host[8] = be[7]; 112 host[9] = be[6]; 113 host[10] = be[5]; 114 host[11] = be[4]; 115 host[12] = be[3]; 116 host[13] = be[2]; 117 host[14] = be[1]; 118 host[15] = be[0]; 119 #endif 120 } 121 122 void inet_addr(inet_addr_t *addr, uint8_t a, uint8_t b, uint8_t c, uint8_t d) 123 { 124 addr->family = AF_INET; 125 addr->addr = ((addr32_t) a << 24) | ((addr32_t) b << 16) | 126 ((addr32_t) c << 8) | ((addr32_t) d); 127 } 128 129 void inet_naddr(inet_naddr_t *naddr, uint8_t a, uint8_t b, uint8_t c, uint8_t d, 130 uint8_t prefix) 131 { 132 naddr->family = AF_INET; 133 naddr->addr = ((addr32_t) a << 24) | ((addr32_t) b << 16) | 134 ((addr32_t) c << 8) | ((addr32_t) d); 135 naddr->prefix = prefix; 136 } 137 138 void inet_addr6(inet_addr_t *addr, uint16_t a, uint16_t b, uint16_t c, 139 uint16_t d, uint16_t e, uint16_t f, uint16_t g, uint16_t h) 140 { 141 addr->family = AF_INET6; 142 addr->addr6[0] = (a >> 8) & 0xff; 143 addr->addr6[1] = a & 0xff; 144 addr->addr6[2] = (b >> 8) & 0xff; 145 addr->addr6[3] = b & 0xff; 146 addr->addr6[4] = (c >> 8) & 0xff; 147 addr->addr6[5] = c & 0xff; 148 addr->addr6[6] = (d >> 8) & 0xff; 149 addr->addr6[7] = d & 0xff; 150 addr->addr6[8] = (e >> 8) & 0xff; 151 addr->addr6[9] = e & 0xff; 152 addr->addr6[10] = (f >> 8) & 0xff; 153 addr->addr6[11] = f & 0xff; 154 addr->addr6[12] = (g >> 8) & 0xff; 155 addr->addr6[13] = g & 0xff; 156 addr->addr6[14] = (h >> 8) & 0xff; 157 addr->addr6[15] = h & 0xff; 158 } 159 160 void inet_naddr6(inet_naddr_t *naddr, uint16_t a, uint16_t b, uint16_t c, 161 uint16_t d, uint16_t e, uint16_t f, uint16_t g, uint16_t h, uint8_t prefix) 162 { 163 naddr->family = AF_INET6; 164 naddr->addr6[0] = (a >> 8) & 0xff; 165 naddr->addr6[1] = a & 0xff; 166 naddr->addr6[2] = (b >> 8) & 0xff; 167 naddr->addr6[3] = b & 0xff; 168 naddr->addr6[4] = (c >> 8) & 0xff; 169 naddr->addr6[5] = c & 0xff; 170 naddr->addr6[6] = (d >> 8) & 0xff; 171 naddr->addr6[7] = d & 0xff; 172 naddr->addr6[8] = (e >> 8) & 0xff; 173 naddr->addr6[9] = e & 0xff; 174 naddr->addr6[10] = (f >> 8) & 0xff; 175 naddr->addr6[11] = f & 0xff; 176 naddr->addr6[12] = (g >> 8) & 0xff; 177 naddr->addr6[13] = g & 0xff; 178 naddr->addr6[14] = (h >> 8) & 0xff; 179 naddr->addr6[15] = h & 0xff; 180 naddr->prefix = prefix; 181 } 51 182 52 183 /** Parse network address family. … … 72 203 } 73 204 74 *af = AF_NONE;75 205 return EINVAL; 206 } 207 208 void inet_naddr_addr(const inet_naddr_t *naddr, inet_addr_t *addr) 209 { 210 addr->family = naddr->family; 211 memcpy(addr->addr6, naddr->addr6, 16); 212 } 213 214 void inet_addr_any(inet_addr_t *addr) 215 { 216 addr->family = AF_NONE; 217 memset(addr->addr6, 0, 16); 218 } 219 220 void inet_naddr_any(inet_naddr_t *naddr) 221 { 222 naddr->family = AF_NONE; 223 memset(naddr->addr6, 0, 16); 224 naddr->prefix = 0; 225 } 226 227 int inet_addr_compare(const inet_addr_t *a, const inet_addr_t *b) 228 { 229 if (a->family != b->family) 230 return 0; 231 232 switch (a->family) { 233 case AF_INET: 234 return (a->addr == b->addr); 235 case AF_INET6: 236 return memcmp(&a->addr6, &b->addr6, 16); 237 default: 238 return 0; 239 } 240 } 241 242 int inet_addr_is_any(const inet_addr_t *addr) 243 { 244 return ((addr->family == 0) || 245 (inet_addr_compare(addr, &inet_addr_any_addr)) || 246 (inet_addr_compare(addr, &inet_addr_any_addr6))); 247 } 248 249 int inet_naddr_compare_mask(const inet_naddr_t *naddr, const inet_addr_t *addr) 250 { 251 if (naddr->family != addr->family) 252 return 0; 253 254 switch (naddr->family) { 255 case AF_INET: 256 if (naddr->prefix > 32) 257 return 0; 258 259 addr32_t mask = 260 BIT_RANGE(addr32_t, 31, 31 - (naddr->prefix - 1)); 261 return ((naddr->addr & mask) == (addr->addr & mask)); 262 case AF_INET6: 263 if (naddr->prefix > 128) 264 return 0; 265 266 size_t pos = 0; 267 for (size_t i = 0; i < 16; i++) { 268 /* Further bits do not matter */ 269 if (naddr->prefix < pos) 270 break; 271 272 if (naddr->prefix - pos > 8) { 273 /* Comparison without masking */ 274 if (naddr->addr6[i] != addr->addr6[i]) 275 return 0; 276 } else { 277 /* Comparison with masking */ 278 uint8_t mask = 279 BIT_RANGE(uint8_t, 8, 8 - (naddr->prefix - pos - 1)); 280 if ((naddr->addr6[i] & mask) != (addr->addr6[i] & mask)) 281 return 0; 282 } 283 284 pos += 8; 285 } 286 287 return 1; 288 default: 289 return 0; 290 } 76 291 } 77 292 … … 90 305 return rc; 91 306 92 rc = inet_pton(addr->family, text, addr->addr); 307 uint8_t buf[16]; 308 rc = inet_pton(addr->family, text, buf); 93 309 if (rc != EOK) 94 310 return rc; 311 312 switch (addr->family) { 313 case AF_INET: 314 addr->addr = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | 315 buf[3]; 316 break; 317 case AF_INET6: 318 memcpy(addr->addr6, buf, 16); 319 break; 320 default: 321 return EINVAL; 322 } 95 323 96 324 return EOK; … … 117 345 return rc; 118 346 119 rc = inet_pton(naddr->family, text, naddr->addr); 347 uint8_t buf[16]; 348 rc = inet_pton(naddr->family, text, buf); 120 349 *slash = '/'; 121 350 … … 124 353 125 354 slash++; 355 uint8_t prefix; 126 356 127 357 switch (naddr->family) { 128 358 case AF_INET: 129 naddr->prefix = strtoul(slash, &slash, 10);130 if ( naddr->prefix > 32)359 prefix = strtoul(slash, &slash, 10); 360 if (prefix > 32) 131 361 return EINVAL; 132 break; 133 case AF_INET6: 134 naddr->prefix = strtoul(slash, &slash, 10); 135 if (naddr->prefix > 128) 362 363 naddr->addr = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | 364 buf[3]; 365 naddr->prefix = prefix; 366 367 break; 368 case AF_INET6: 369 prefix = strtoul(slash, &slash, 10); 370 if (prefix > 128) 136 371 return EINVAL; 372 373 memcpy(naddr->addr6, buf, 16); 374 naddr->prefix = prefix; 375 137 376 break; 138 377 default: … … 153 392 * 154 393 */ 155 int inet_addr_format( inet_addr_t *addr, char **bufp)156 { 157 int rc ;394 int inet_addr_format(const inet_addr_t *addr, char **bufp) 395 { 396 int rc = 0; 158 397 159 398 switch (addr->family) { … … 162 401 break; 163 402 case AF_INET: 164 rc = asprintf(bufp, "%u.%u.%u.%u", addr->addr[0], 165 addr->addr[1], addr->addr[2], addr->addr[3]); 166 break; 167 case AF_INET6: 168 // FIXME TODO 169 return ENOTSUP; 403 rc = asprintf(bufp, "%u.%u.%u.%u", (addr->addr >> 24) & 0xff, 404 (addr->addr >> 16) & 0xff, (addr->addr >> 8) & 0xff, 405 addr->addr & 0xff); 406 break; 407 case AF_INET6: 408 *bufp = (char *) malloc(INET6_ADDRSTRLEN); 409 if (*bufp == NULL) 410 return ENOMEM; 411 412 return inet_ntop(AF_INET6, addr->addr6, *bufp, INET6_ADDRSTRLEN); 170 413 default: 171 414 return ENOTSUP; … … 188 431 * 189 432 */ 190 int inet_naddr_format(inet_naddr_t *naddr, char **bufp) 191 { 192 int rc; 433 int inet_naddr_format(const inet_naddr_t *naddr, char **bufp) 434 { 435 int rc = 0; 436 char prefix[INET_PREFIXSTRSIZE]; 193 437 194 438 switch (naddr->family) { … … 197 441 break; 198 442 case AF_INET: 199 rc = asprintf(bufp, "%u.%u.%u.%u/%u", naddr->addr[0], 200 naddr->addr[1], naddr->addr[2], naddr->addr[3], 443 rc = asprintf(bufp, "%" PRIu8 ".%" PRIu8 ".%" PRIu8 ".%" PRIu8 444 "/%" PRIu8, (naddr->addr >> 24) & 0xff, 445 (naddr->addr >> 16) & 0xff, (naddr->addr >> 8) & 0xff, 446 naddr->addr & 0xff, naddr->prefix); 447 break; 448 case AF_INET6: 449 *bufp = (char *) malloc(INET6_ADDRSTRLEN + INET_PREFIXSTRSIZE); 450 if (*bufp == NULL) 451 return ENOMEM; 452 453 rc = inet_ntop(AF_INET6, naddr->addr6, *bufp, 454 INET6_ADDRSTRLEN + INET_PREFIXSTRSIZE); 455 if (rc != EOK) { 456 free(*bufp); 457 return rc; 458 } 459 460 rc = snprintf(prefix, INET_PREFIXSTRSIZE, "/%" PRIu8, 201 461 naddr->prefix); 202 break; 203 case AF_INET6: 204 // FIXME TODO 205 return ENOTSUP; 462 if (rc < 0) { 463 free(*bufp); 464 return ENOMEM; 465 } 466 467 str_append(*bufp, INET6_ADDRSTRLEN + INET_PREFIXSTRSIZE, prefix); 468 469 break; 206 470 default: 207 471 return ENOTSUP; … … 214 478 } 215 479 216 /** Create packed IPv4 address 217 * 218 * Convert an IPv4 address to a packed 32 bit representation. 219 * 220 * @param addr Source address. 221 * @param packed Place to store the packed 32 bit representation. 222 * 223 * @return EOK on success. 224 * @return EINVAL if addr is not an IPv4 address. 225 * 226 */ 227 int inet_addr_pack(inet_addr_t *addr, uint32_t *packed) 228 { 229 if (addr->family != AF_INET) 230 return EINVAL; 231 232 *packed = (addr->addr[0] << 24) | (addr->addr[1] << 16) | 233 (addr->addr[2] << 8) | addr->addr[3]; 234 return EOK; 235 } 236 237 /** Create packed IPv4 address 238 * 239 * Convert an IPv4 address to a packed 32 bit representation. 240 * 241 * @param naddr Source address. 242 * @param packed Place to store the packed 32 bit representation. 243 * @param prefix Place to store the number of valid bits. 244 * 245 * @return EOK on success. 246 * @return EINVAL if addr is not an IPv4 address. 247 * 248 */ 249 int inet_naddr_pack(inet_naddr_t *naddr, uint32_t *packed, uint8_t *prefix) 250 { 251 if (naddr->family != AF_INET) 252 return EINVAL; 253 254 *packed = (naddr->addr[0] << 24) | (naddr->addr[1] << 16) | 255 (naddr->addr[2] << 8) | naddr->addr[3]; 256 *prefix = naddr->prefix; 257 258 return EOK; 259 } 260 261 void inet_addr_unpack(uint32_t packed, inet_addr_t *addr) 480 uint16_t inet_addr_get(const inet_addr_t *addr, addr32_t *v4, addr128_t *v6) 481 { 482 switch (addr->family) { 483 case AF_INET: 484 if (v4 != NULL) 485 *v4 = addr->addr; 486 487 break; 488 case AF_INET6: 489 if (v6 != NULL) 490 memcpy(*v6, addr->addr6, 16); 491 492 break; 493 } 494 495 return addr->family; 496 } 497 498 uint16_t inet_naddr_get(const inet_naddr_t *naddr, addr32_t *v4, addr128_t *v6, 499 uint8_t *prefix) 500 { 501 switch (naddr->family) { 502 case AF_INET: 503 if (v4 != NULL) 504 *v4 = naddr->addr; 505 506 if (prefix != NULL) 507 *prefix = naddr->prefix; 508 509 break; 510 case AF_INET6: 511 if (v6 != NULL) 512 memcpy(*v6, naddr->addr6, 16); 513 514 if (prefix != NULL) 515 *prefix = naddr->prefix; 516 517 break; 518 } 519 520 return naddr->family; 521 } 522 523 void inet_addr_set(addr32_t v4, inet_addr_t *addr) 262 524 { 263 525 addr->family = AF_INET; 264 addr->addr[0] = (packed >> 24) & 0xff; 265 addr->addr[1] = (packed >> 16) & 0xff; 266 addr->addr[2] = (packed >> 8) & 0xff; 267 addr->addr[3] = packed & 0xff; 268 } 269 270 void inet_naddr_unpack(uint32_t packed, uint8_t prefix, inet_naddr_t *naddr) 526 addr->addr = v4; 527 } 528 529 void inet_naddr_set(addr32_t v4, uint8_t prefix, inet_naddr_t *naddr) 271 530 { 272 531 naddr->family = AF_INET; 273 naddr->addr[0] = (packed >> 24) & 0xff; 274 naddr->addr[1] = (packed >> 16) & 0xff; 275 naddr->addr[2] = (packed >> 8) & 0xff; 276 naddr->addr[3] = packed & 0xff; 532 naddr->addr = v4; 277 533 naddr->prefix = prefix; 278 534 } 279 535 280 int inet_addr_sockaddr_in(inet_addr_t *addr, sockaddr_in_t *sockaddr_in) 281 { 282 uint32_t packed; 283 int rc = inet_addr_pack(addr, &packed); 284 if (rc != EOK) 285 return rc; 286 287 sockaddr_in->sin_family = AF_INET; 288 sockaddr_in->sin_addr.s_addr = host2uint32_t_be(packed); 289 return EOK; 290 } 291 292 void inet_naddr_addr(inet_naddr_t *naddr, inet_addr_t *addr) 293 { 294 addr->family = naddr->family; 295 memcpy(addr->addr, naddr->addr, INET_ADDR_SIZE); 296 } 297 298 void inet_addr(inet_addr_t *addr, uint8_t a, uint8_t b, uint8_t c, uint8_t d) 536 void inet_sockaddr_in_addr(const sockaddr_in_t *sockaddr_in, inet_addr_t *addr) 299 537 { 300 538 addr->family = AF_INET; 301 addr->addr[0] = a; 302 addr->addr[1] = b; 303 addr->addr[2] = c; 304 addr->addr[3] = d; 305 } 306 307 void inet_naddr(inet_naddr_t *naddr, uint8_t a, uint8_t b, uint8_t c, uint8_t d, 308 uint8_t prefix) 309 { 310 naddr->family = AF_INET; 311 naddr->addr[0] = a; 312 naddr->addr[1] = b; 313 naddr->addr[2] = c; 314 naddr->addr[3] = d; 539 addr->addr = uint32_t_be2host(sockaddr_in->sin_addr.s_addr); 540 } 541 542 void inet_addr_set6(addr128_t v6, inet_addr_t *addr) 543 { 544 addr->family = AF_INET6; 545 memcpy(addr->addr6, v6, 16); 546 } 547 548 void inet_naddr_set6(addr128_t v6, uint8_t prefix, inet_naddr_t *naddr) 549 { 550 naddr->family = AF_INET6; 551 memcpy(naddr->addr6, v6, 16); 315 552 naddr->prefix = prefix; 316 553 } 317 554 318 void inet_addr_any(inet_addr_t *addr) 319 { 320 addr->family = 0; 321 memset(addr->addr, 0, INET_ADDR_SIZE); 322 } 323 324 void inet_naddr_any(inet_naddr_t *naddr) 325 { 326 naddr->family = 0; 327 memset(naddr->addr, 0, INET_ADDR_SIZE); 328 naddr->prefix = 0; 329 } 330 331 int inet_addr_compare(inet_addr_t *a, inet_addr_t *b) 332 { 333 if (a->family != b->family) 334 return 0; 335 336 switch (a->family) { 337 case AF_INET: 338 return ((a->addr[0] == b->addr[0]) && (a->addr[1] == b->addr[1]) && 339 (a->addr[2] == b->addr[2]) && (a->addr[3] == b->addr[3])); 340 case AF_INET6: 341 // FIXME TODO 342 return 0; 343 default: 344 return 0; 345 } 346 } 347 348 int inet_addr_is_any(inet_addr_t *addr) 349 { 350 return ((addr->family == 0) || 351 (inet_addr_compare(addr, &inet_addr_any_addr)) || 352 (inet_addr_compare(addr, &inet_addr_any_addr6))); 555 void inet_sockaddr_in6_addr(const sockaddr_in6_t *sockaddr_in6, 556 inet_addr_t *addr) 557 { 558 addr->family = AF_INET6; 559 addr128_t_be2host(sockaddr_in6->sin6_addr.s6_addr, addr->addr6); 560 } 561 562 uint16_t inet_addr_sockaddr_in(const inet_addr_t *addr, 563 sockaddr_in_t *sockaddr_in, sockaddr_in6_t *sockaddr_in6) 564 { 565 switch (addr->family) { 566 case AF_INET: 567 if (sockaddr_in != NULL) { 568 sockaddr_in->sin_family = AF_INET; 569 sockaddr_in->sin_addr.s_addr = host2uint32_t_be(addr->addr); 570 } 571 572 break; 573 case AF_INET6: 574 if (sockaddr_in6 != NULL) { 575 sockaddr_in6->sin6_family = AF_INET6; 576 host2addr128_t_be(addr->addr6, sockaddr_in6->sin6_addr.s6_addr); 577 } 578 579 break; 580 } 581 582 return addr->family; 353 583 } 354 584 -
uspace/lib/c/generic/inetcfg.c
redf0d27 r02a09ed 136 136 sysarg_t link_id, sysarg_t *addr_id) 137 137 { 138 uint32_t naddr_addr; 139 uint8_t naddr_bits; 140 int rc = inet_naddr_pack(naddr, &naddr_addr, &naddr_bits); 141 if (rc != EOK) 142 return rc; 143 144 async_exch_t *exch = async_exchange_begin(inetcfg_sess); 145 146 ipc_call_t answer; 147 aid_t req = async_send_3(exch, INETCFG_ADDR_CREATE_STATIC, 148 (sysarg_t) naddr_addr, (sysarg_t) naddr_bits, link_id, &answer); 138 async_exch_t *exch = async_exchange_begin(inetcfg_sess); 139 140 ipc_call_t answer; 141 aid_t req = async_send_1(exch, INETCFG_ADDR_CREATE_STATIC, link_id, 142 &answer); 143 144 int rc = async_data_write_start(exch, naddr, sizeof(inet_naddr_t)); 145 if (rc != EOK) { 146 async_exchange_end(exch); 147 async_forget(req); 148 return rc; 149 } 150 151 rc = async_data_write_start(exch, name, str_size(name)); 152 153 async_exchange_end(exch); 154 155 if (rc != EOK) { 156 async_forget(req); 157 return rc; 158 } 159 160 sysarg_t retval; 161 async_wait_for(req, &retval); 162 163 *addr_id = IPC_GET_ARG1(answer); 164 165 return (int) retval; 166 } 167 168 int inetcfg_addr_delete(sysarg_t addr_id) 169 { 170 async_exch_t *exch = async_exchange_begin(inetcfg_sess); 171 172 int rc = async_req_1_0(exch, INETCFG_ADDR_DELETE, addr_id); 173 async_exchange_end(exch); 174 175 return rc; 176 } 177 178 int inetcfg_addr_get(sysarg_t addr_id, inet_addr_info_t *ainfo) 179 { 180 async_exch_t *exch = async_exchange_begin(inetcfg_sess); 181 182 ipc_call_t answer; 183 aid_t req = async_send_1(exch, INETCFG_ADDR_GET, addr_id, &answer); 184 185 ipc_call_t answer_naddr; 186 aid_t req_naddr = async_data_read(exch, &ainfo->naddr, 187 sizeof(inet_naddr_t), &answer_naddr); 188 189 sysarg_t retval_naddr; 190 async_wait_for(req_naddr, &retval_naddr); 191 192 if (retval_naddr != EOK) { 193 async_exchange_end(exch); 194 async_forget(req); 195 return (int) retval_naddr; 196 } 197 198 ipc_call_t answer_name; 199 char name_buf[LOC_NAME_MAXLEN + 1]; 200 aid_t req_name = async_data_read(exch, name_buf, LOC_NAME_MAXLEN, 201 &answer_name); 202 203 async_exchange_end(exch); 204 205 sysarg_t retval_name; 206 async_wait_for(req_name, &retval_name); 207 208 if (retval_name != EOK) { 209 async_forget(req); 210 return (int) retval_name; 211 } 212 213 sysarg_t retval; 214 async_wait_for(req, &retval); 215 216 if (retval != EOK) 217 return (int) retval; 218 219 size_t act_size = IPC_GET_ARG2(answer_name); 220 assert(act_size <= LOC_NAME_MAXLEN); 221 222 name_buf[act_size] = '\0'; 223 224 ainfo->ilink = IPC_GET_ARG1(answer); 225 ainfo->name = str_dup(name_buf); 226 227 return EOK; 228 } 229 230 int inetcfg_addr_get_id(const char *name, sysarg_t link_id, sysarg_t *addr_id) 231 { 232 async_exch_t *exch = async_exchange_begin(inetcfg_sess); 233 234 ipc_call_t answer; 235 aid_t req = async_send_1(exch, INETCFG_ADDR_GET_ID, link_id, &answer); 149 236 sysarg_t retval = async_data_write_start(exch, name, str_size(name)); 150 151 async_exchange_end(exch); 152 237 238 async_exchange_end(exch); 239 153 240 if (retval != EOK) { 154 241 async_forget(req); 155 242 return retval; 156 243 } 157 244 158 245 async_wait_for(req, &retval); 159 246 *addr_id = IPC_GET_ARG1(answer); 160 161 return (int) retval; 162 } 163 164 int inetcfg_addr_delete(sysarg_t addr_id) 165 { 166 async_exch_t *exch = async_exchange_begin(inetcfg_sess); 167 168 int rc = async_req_1_0(exch, INETCFG_ADDR_DELETE, addr_id); 169 async_exchange_end(exch); 170 171 return rc; 172 } 173 174 int inetcfg_addr_get(sysarg_t addr_id, inet_addr_info_t *ainfo) 247 248 return retval; 249 } 250 251 int inetcfg_get_addr_list(sysarg_t **addrs, size_t *count) 252 { 253 return inetcfg_get_ids_internal(INETCFG_GET_ADDR_LIST, 254 0, addrs, count); 255 } 256 257 int inetcfg_get_link_list(sysarg_t **links, size_t *count) 258 { 259 return inetcfg_get_ids_internal(INETCFG_GET_LINK_LIST, 260 0, links, count); 261 } 262 263 int inetcfg_get_sroute_list(sysarg_t **sroutes, size_t *count) 264 { 265 return inetcfg_get_ids_internal(INETCFG_GET_SROUTE_LIST, 266 0, sroutes, count); 267 } 268 269 int inetcfg_link_get(sysarg_t link_id, inet_link_info_t *linfo) 175 270 { 176 271 ipc_call_t dreply; … … 178 273 size_t act_size; 179 274 char name_buf[LOC_NAME_MAXLEN + 1]; 180 181 async_exch_t *exch = async_exchange_begin(inetcfg_sess);182 183 ipc_call_t answer;184 aid_t req = async_send_1(exch, INETCFG_ADDR_GET, addr_id, &answer);185 aid_t dreq = async_data_read(exch, name_buf, LOC_NAME_MAXLEN, &dreply);186 async_wait_for(dreq, &dretval);187 188 async_exchange_end(exch);189 190 if (dretval != EOK) {191 async_forget(req);192 return dretval;193 }194 195 sysarg_t retval;196 async_wait_for(req, &retval);197 198 if (retval != EOK)199 return (int) retval;200 201 act_size = IPC_GET_ARG2(dreply);202 assert(act_size <= LOC_NAME_MAXLEN);203 204 name_buf[act_size] = '\0';205 206 inet_naddr_unpack(IPC_GET_ARG1(answer), IPC_GET_ARG2(answer),207 &ainfo->naddr);208 ainfo->ilink = IPC_GET_ARG3(answer);209 ainfo->name = str_dup(name_buf);210 211 return EOK;212 }213 214 int inetcfg_addr_get_id(const char *name, sysarg_t link_id, sysarg_t *addr_id)215 {216 async_exch_t *exch = async_exchange_begin(inetcfg_sess);217 218 ipc_call_t answer;219 aid_t req = async_send_1(exch, INETCFG_ADDR_GET_ID, link_id, &answer);220 sysarg_t retval = async_data_write_start(exch, name, str_size(name));221 222 async_exchange_end(exch);223 224 if (retval != EOK) {225 async_forget(req);226 return retval;227 }228 229 async_wait_for(req, &retval);230 *addr_id = IPC_GET_ARG1(answer);231 232 return retval;233 }234 235 int inetcfg_get_addr_list(sysarg_t **addrs, size_t *count)236 {237 return inetcfg_get_ids_internal(INETCFG_GET_ADDR_LIST,238 0, addrs, count);239 }240 241 int inetcfg_get_link_list(sysarg_t **links, size_t *count)242 {243 return inetcfg_get_ids_internal(INETCFG_GET_LINK_LIST,244 0, links, count);245 }246 247 int inetcfg_get_sroute_list(sysarg_t **sroutes, size_t *count)248 {249 return inetcfg_get_ids_internal(INETCFG_GET_SROUTE_LIST,250 0, sroutes, count);251 }252 253 int inetcfg_link_get(sysarg_t link_id, inet_link_info_t *linfo)254 {255 ipc_call_t dreply;256 sysarg_t dretval;257 size_t act_size;258 char name_buf[LOC_NAME_MAXLEN + 1];259 275 260 276 async_exch_t *exch = async_exchange_begin(inetcfg_sess); … … 291 307 inet_addr_t *router, sysarg_t *sroute_id) 292 308 { 293 uint32_t dest_addr; 294 uint8_t dest_bits; 295 int rc = inet_naddr_pack(dest, &dest_addr, &dest_bits); 296 if (rc != EOK) 297 return rc; 298 299 uint32_t router_addr; 300 rc = inet_addr_pack(router, &router_addr); 301 if (rc != EOK) 302 return rc; 303 304 async_exch_t *exch = async_exchange_begin(inetcfg_sess); 305 306 ipc_call_t answer; 307 aid_t req = async_send_3(exch, INETCFG_SROUTE_CREATE, 308 (sysarg_t) dest_addr, (sysarg_t) dest_bits, (sysarg_t) router_addr, 309 &answer); 310 sysarg_t retval = async_data_write_start(exch, name, str_size(name)); 311 312 async_exchange_end(exch); 313 314 if (retval != EOK) { 315 async_forget(req); 316 return retval; 317 } 318 319 async_wait_for(req, &retval); 309 async_exch_t *exch = async_exchange_begin(inetcfg_sess); 310 311 ipc_call_t answer; 312 aid_t req = async_send_0(exch, INETCFG_SROUTE_CREATE, &answer); 313 314 int rc = async_data_write_start(exch, dest, sizeof(inet_naddr_t)); 315 if (rc != EOK) { 316 async_exchange_end(exch); 317 async_forget(req); 318 return rc; 319 } 320 321 rc = async_data_write_start(exch, router, sizeof(inet_addr_t)); 322 if (rc != EOK) { 323 async_exchange_end(exch); 324 async_forget(req); 325 return rc; 326 } 327 328 rc = async_data_write_start(exch, name, str_size(name)); 329 330 async_exchange_end(exch); 331 332 if (rc != EOK) { 333 async_forget(req); 334 return rc; 335 } 336 337 sysarg_t retval; 338 async_wait_for(req, &retval); 339 320 340 *sroute_id = IPC_GET_ARG1(answer); 321 341 322 return retval;342 return (int) retval; 323 343 } 324 344 … … 335 355 int inetcfg_sroute_get(sysarg_t sroute_id, inet_sroute_info_t *srinfo) 336 356 { 337 ipc_call_t dreply; 338 sysarg_t dretval; 339 size_t act_size; 357 async_exch_t *exch = async_exchange_begin(inetcfg_sess); 358 359 ipc_call_t answer; 360 aid_t req = async_send_1(exch, INETCFG_SROUTE_GET, sroute_id, &answer); 361 362 ipc_call_t answer_dest; 363 aid_t req_dest = async_data_read(exch, &srinfo->dest, 364 sizeof(inet_naddr_t), &answer_dest); 365 366 sysarg_t retval_dest; 367 async_wait_for(req_dest, &retval_dest); 368 369 if (retval_dest != EOK) { 370 async_exchange_end(exch); 371 async_forget(req); 372 return (int) retval_dest; 373 } 374 375 ipc_call_t answer_router; 376 aid_t req_router = async_data_read(exch, &srinfo->router, 377 sizeof(inet_addr_t), &answer_router); 378 379 sysarg_t retval_router; 380 async_wait_for(req_router, &retval_router); 381 382 if (retval_router != EOK) { 383 async_exchange_end(exch); 384 async_forget(req); 385 return (int) retval_router; 386 } 387 388 ipc_call_t answer_name; 340 389 char name_buf[LOC_NAME_MAXLEN + 1]; 341 342 async_exch_t *exch = async_exchange_begin(inetcfg_sess); 343 344 ipc_call_t answer; 345 aid_t req = async_send_1(exch, INETCFG_SROUTE_GET, sroute_id, &answer); 346 aid_t dreq = async_data_read(exch, name_buf, LOC_NAME_MAXLEN, &dreply); 347 async_wait_for(dreq, &dretval); 348 349 async_exchange_end(exch); 350 351 if (dretval != EOK) { 352 async_forget(req); 353 return dretval; 390 aid_t req_name = async_data_read(exch, name_buf, LOC_NAME_MAXLEN, 391 &answer_name); 392 393 async_exchange_end(exch); 394 395 sysarg_t retval_name; 396 async_wait_for(req_name, &retval_name); 397 398 if (retval_name != EOK) { 399 async_forget(req); 400 return (int) retval_name; 354 401 } 355 402 … … 360 407 return (int) retval; 361 408 362 act_size = IPC_GET_ARG2(dreply);409 size_t act_size = IPC_GET_ARG2(answer_name); 363 410 assert(act_size <= LOC_NAME_MAXLEN); 411 364 412 name_buf[act_size] = '\0'; 365 366 inet_naddr_unpack(IPC_GET_ARG1(answer), IPC_GET_ARG2(answer), 367 &srinfo->dest); 368 inet_addr_unpack(IPC_GET_ARG3(answer), &srinfo->router); 413 369 414 srinfo->name = str_dup(name_buf); 370 415 -
uspace/lib/c/generic/iplink.c
redf0d27 r02a09ed 86 86 87 87 ipc_call_t answer; 88 aid_t req = async_send_2(exch, IPLINK_SEND, (sysarg_t) sdu->lsrc, 89 (sysarg_t) sdu->ldest, &answer); 90 int rc = async_data_write_start(exch, sdu->data, sdu->size); 88 aid_t req = async_send_0(exch, IPLINK_SEND, &answer); 89 90 int rc = async_data_write_start(exch, &sdu->src, sizeof(inet_addr_t)); 91 if (rc != EOK) { 92 async_exchange_end(exch); 93 async_forget(req); 94 return rc; 95 } 96 97 rc = async_data_write_start(exch, &sdu->dest, sizeof(inet_addr_t)); 98 if (rc != EOK) { 99 async_exchange_end(exch); 100 async_forget(req); 101 return rc; 102 } 103 104 rc = async_data_write_start(exch, sdu->data, sdu->size); 91 105 92 106 async_exchange_end(exch); … … 120 134 int iplink_addr_add(iplink_t *iplink, inet_addr_t *addr) 121 135 { 122 uint32_t addr_addr; 123 int rc = inet_addr_pack(addr, &addr_addr); 124 if (rc != EOK) 125 return rc; 126 127 async_exch_t *exch = async_exchange_begin(iplink->sess); 128 rc = async_req_1_0(exch, IPLINK_ADDR_ADD, (sysarg_t) addr_addr); 129 async_exchange_end(exch); 130 131 return rc; 136 async_exch_t *exch = async_exchange_begin(iplink->sess); 137 138 ipc_call_t answer; 139 aid_t req = async_send_0(exch, IPLINK_ADDR_ADD, &answer); 140 141 int rc = async_data_write_start(exch, addr, sizeof(inet_addr_t)); 142 async_exchange_end(exch); 143 144 if (rc != EOK) { 145 async_forget(req); 146 return rc; 147 } 148 149 sysarg_t retval; 150 async_wait_for(req, &retval); 151 152 return (int) retval; 132 153 } 133 154 134 155 int iplink_addr_remove(iplink_t *iplink, inet_addr_t *addr) 135 156 { 136 uint32_t addr_addr; 137 int rc = inet_addr_pack(addr, &addr_addr); 138 if (rc != EOK) 139 return rc; 140 141 async_exch_t *exch = async_exchange_begin(iplink->sess); 142 rc = async_req_1_0(exch, IPLINK_ADDR_REMOVE, (sysarg_t) addr_addr); 143 async_exchange_end(exch); 144 145 return rc; 146 } 147 148 static void iplink_ev_recv(iplink_t *iplink, ipc_callid_t callid, 149 ipc_call_t *call) 150 { 151 iplink_sdu_t sdu; 152 153 sdu.lsrc = IPC_GET_ARG1(*call); 154 sdu.ldest = IPC_GET_ARG2(*call); 155 156 int rc = async_data_write_accept(&sdu.data, false, 0, 0, 0, &sdu.size); 157 if (rc != EOK) { 158 async_answer_0(callid, rc); 157 async_exch_t *exch = async_exchange_begin(iplink->sess); 158 159 ipc_call_t answer; 160 aid_t req = async_send_0(exch, IPLINK_ADDR_REMOVE, &answer); 161 162 int rc = async_data_write_start(exch, addr, sizeof(inet_addr_t)); 163 async_exchange_end(exch); 164 165 if (rc != EOK) { 166 async_forget(req); 167 return rc; 168 } 169 170 sysarg_t retval; 171 async_wait_for(req, &retval); 172 173 return (int) retval; 174 } 175 176 static void iplink_ev_recv(iplink_t *iplink, ipc_callid_t iid, 177 ipc_call_t *icall) 178 { 179 iplink_recv_sdu_t sdu; 180 181 uint16_t af = IPC_GET_ARG1(*icall); 182 183 int rc = async_data_write_accept(&sdu.data, false, 0, 0, 0, 184 &sdu.size); 185 if (rc != EOK) { 186 async_answer_0(iid, rc); 159 187 return; 160 188 } 161 189 162 rc = iplink->ev_ops->recv(iplink, &sdu );190 rc = iplink->ev_ops->recv(iplink, &sdu, af); 163 191 free(sdu.data); 164 async_answer_0( callid, rc);192 async_answer_0(iid, rc); 165 193 } 166 194 -
uspace/lib/c/generic/iplink_srv.c
redf0d27 r02a09ed 34 34 * @brief IP link server stub 35 35 */ 36 36 37 #include <errno.h> 37 38 #include <ipc/iplink.h> 38 39 #include <stdlib.h> 39 40 #include <sys/types.h> 40 41 #include <inet/addr.h> 41 42 #include <inet/iplink_srv.h> 42 43 … … 49 50 } 50 51 51 static void iplink_addr_add_srv(iplink_srv_t *srv, ipc_callid_t callid, 52 ipc_call_t *call) 53 { 54 int rc = srv->ops->addr_add(srv, IPC_GET_ARG1(*call)); 55 async_answer_0(callid, rc); 56 } 57 58 static void iplink_addr_remove_srv(iplink_srv_t *srv, ipc_callid_t callid, 59 ipc_call_t *call) 60 { 61 int rc = srv->ops->addr_remove(srv, IPC_GET_ARG1(*call)); 62 async_answer_0(callid, rc); 63 } 64 65 static void iplink_send_srv(iplink_srv_t *srv, ipc_callid_t callid, 66 ipc_call_t *call) 67 { 68 iplink_srv_sdu_t sdu; 69 int rc; 70 71 sdu.lsrc = IPC_GET_ARG1(*call); 72 sdu.ldest = IPC_GET_ARG2(*call); 73 74 rc = async_data_write_accept(&sdu.data, false, 0, 0, 0, &sdu.size); 75 if (rc != EOK) { 76 async_answer_0(callid, rc); 77 return; 78 } 52 static void iplink_addr_add_srv(iplink_srv_t *srv, ipc_callid_t iid, 53 ipc_call_t *icall) 54 { 55 ipc_callid_t callid; 56 size_t size; 57 if (!async_data_write_receive(&callid, &size)) { 58 async_answer_0(callid, EREFUSED); 59 async_answer_0(iid, EREFUSED); 60 return; 61 } 62 63 if (size != sizeof(inet_addr_t)) { 64 async_answer_0(callid, EINVAL); 65 async_answer_0(iid, EINVAL); 66 return; 67 } 68 69 inet_addr_t addr; 70 int rc = async_data_write_finalize(callid, &addr, size); 71 if (rc != EOK) { 72 async_answer_0(callid, (sysarg_t) rc); 73 async_answer_0(iid, (sysarg_t) rc); 74 } 75 76 rc = srv->ops->addr_add(srv, &addr); 77 async_answer_0(iid, (sysarg_t) rc); 78 } 79 80 static void iplink_addr_remove_srv(iplink_srv_t *srv, ipc_callid_t iid, 81 ipc_call_t *icall) 82 { 83 ipc_callid_t callid; 84 size_t size; 85 if (!async_data_write_receive(&callid, &size)) { 86 async_answer_0(callid, EREFUSED); 87 async_answer_0(iid, EREFUSED); 88 return; 89 } 90 91 if (size != sizeof(inet_addr_t)) { 92 async_answer_0(callid, EINVAL); 93 async_answer_0(iid, EINVAL); 94 return; 95 } 96 97 inet_addr_t addr; 98 int rc = async_data_write_finalize(callid, &addr, size); 99 if (rc != EOK) { 100 async_answer_0(callid, (sysarg_t) rc); 101 async_answer_0(iid, (sysarg_t) rc); 102 } 103 104 rc = srv->ops->addr_remove(srv, &addr); 105 async_answer_0(iid, (sysarg_t) rc); 106 } 107 108 static void iplink_send_srv(iplink_srv_t *srv, ipc_callid_t iid, 109 ipc_call_t *icall) 110 { 111 iplink_sdu_t sdu; 112 113 ipc_callid_t callid; 114 size_t size; 115 if (!async_data_write_receive(&callid, &size)) { 116 async_answer_0(callid, EREFUSED); 117 async_answer_0(iid, EREFUSED); 118 return; 119 } 120 121 if (size != sizeof(inet_addr_t)) { 122 async_answer_0(callid, EINVAL); 123 async_answer_0(iid, EINVAL); 124 return; 125 } 126 127 int rc = async_data_write_finalize(callid, &sdu.src, size); 128 if (rc != EOK) { 129 async_answer_0(callid, (sysarg_t) rc); 130 async_answer_0(iid, (sysarg_t) rc); 131 } 132 133 if (!async_data_write_receive(&callid, &size)) { 134 async_answer_0(callid, EREFUSED); 135 async_answer_0(iid, EREFUSED); 136 return; 137 } 138 139 if (size != sizeof(inet_addr_t)) { 140 async_answer_0(callid, EINVAL); 141 async_answer_0(iid, EINVAL); 142 return; 143 } 144 145 rc = async_data_write_finalize(callid, &sdu.dest, size); 146 if (rc != EOK) { 147 async_answer_0(callid, (sysarg_t) rc); 148 async_answer_0(iid, (sysarg_t) rc); 149 } 150 151 rc = async_data_write_accept(&sdu.data, false, 0, 0, 0, 152 &sdu.size); 153 if (rc != EOK) 154 return; 79 155 80 156 rc = srv->ops->send(srv, &sdu); 81 157 free(sdu.data); 82 async_answer_0( callid, rc);158 async_answer_0(iid, rc); 83 159 } 84 160 … … 155 231 } 156 232 157 int iplink_ev_recv(iplink_srv_t *srv, iplink_ srv_sdu_t *sdu)233 int iplink_ev_recv(iplink_srv_t *srv, iplink_recv_sdu_t *sdu, uint16_t af) 158 234 { 159 235 if (srv->client_sess == NULL) … … 163 239 164 240 ipc_call_t answer; 165 aid_t req = async_send_2(exch, IPLINK_EV_RECV, (sysarg_t) sdu->lsrc, 166 (sysarg_t) sdu->ldest, &answer); 241 aid_t req = async_send_1(exch, IPLINK_EV_RECV, (sysarg_t) af, 242 &answer); 243 167 244 int rc = async_data_write_start(exch, sdu->data, sdu->size); 168 245 async_exchange_end(exch); -
uspace/lib/c/include/inet/addr.h
redf0d27 r02a09ed 38 38 #include <stdint.h> 39 39 #include <net/in.h> 40 #include <net/in6.h> 40 41 41 #define INET_ADDR_SIZE 16 42 typedef uint32_t addr32_t; 43 typedef uint8_t addr48_t[6]; 44 typedef uint8_t addr128_t[16]; 42 45 43 46 /** Node address */ 44 47 typedef struct { 45 48 uint16_t family; 46 uint8_t addr[INET_ADDR_SIZE]; 49 union { 50 addr32_t addr; 51 addr128_t addr6; 52 }; 47 53 } inet_addr_t; 48 54 … … 53 59 54 60 /** Address */ 55 uint8_t addr[INET_ADDR_SIZE]; 61 union { 62 addr32_t addr; 63 addr128_t addr6; 64 }; 56 65 57 66 /** Number of valid bits */ … … 59 68 } inet_naddr_t; 60 69 61 extern int inet_addr_family(const char *, uint16_t *);70 extern const addr48_t addr48_broadcast; 62 71 63 extern int inet_addr_parse(const char *, inet_addr_t *);64 extern int inet_naddr_parse(const char *, inet_naddr_t *);72 extern void addr48(const addr48_t, addr48_t); 73 extern void addr128(const addr128_t, addr128_t); 65 74 66 extern int inet_addr_format(inet_addr_t *, char **); 67 extern int inet_naddr_format(inet_naddr_t *, char **); 68 69 extern int inet_addr_pack(inet_addr_t *, uint32_t *); 70 extern int inet_naddr_pack(inet_naddr_t *, uint32_t *, uint8_t *); 71 72 extern void inet_addr_unpack(uint32_t, inet_addr_t *); 73 extern void inet_naddr_unpack(uint32_t, uint8_t, inet_naddr_t *); 74 75 extern int inet_addr_sockaddr_in(inet_addr_t *, sockaddr_in_t *); 76 extern void inet_naddr_addr(inet_naddr_t *, inet_addr_t *); 75 extern void host2addr128_t_be(const addr128_t, addr128_t); 76 extern void addr128_t_be2host(const addr128_t, addr128_t); 77 77 78 78 extern void inet_addr(inet_addr_t *, uint8_t, uint8_t, uint8_t, uint8_t); … … 80 80 uint8_t); 81 81 82 extern void inet_addr6(inet_addr_t *, uint16_t, uint16_t, uint16_t, uint16_t, 83 uint16_t, uint16_t, uint16_t, uint16_t); 84 extern void inet_naddr6(inet_naddr_t *, uint16_t, uint16_t, uint16_t, uint16_t, 85 uint16_t, uint16_t, uint16_t, uint16_t, uint8_t); 86 87 extern int inet_addr_family(const char *, uint16_t *); 88 extern void inet_naddr_addr(const inet_naddr_t *, inet_addr_t *); 89 82 90 extern void inet_addr_any(inet_addr_t *); 83 91 extern void inet_naddr_any(inet_naddr_t *); 84 92 85 extern int inet_addr_compare(inet_addr_t *, inet_addr_t *); 86 extern int inet_addr_is_any(inet_addr_t *); 93 extern int inet_addr_compare(const inet_addr_t *, const inet_addr_t *); 94 extern int inet_addr_is_any(const inet_addr_t *); 95 96 extern int inet_naddr_compare_mask(const inet_naddr_t *, const inet_addr_t *); 97 98 extern int inet_addr_parse(const char *, inet_addr_t *); 99 extern int inet_naddr_parse(const char *, inet_naddr_t *); 100 101 extern int inet_addr_format(const inet_addr_t *, char **); 102 extern int inet_naddr_format(const inet_naddr_t *, char **); 103 104 extern uint16_t inet_addr_get(const inet_addr_t *, addr32_t *, addr128_t *); 105 extern uint16_t inet_naddr_get(const inet_naddr_t *, addr32_t *, addr128_t *, 106 uint8_t *); 107 108 extern void inet_addr_set(addr32_t, inet_addr_t *); 109 extern void inet_naddr_set(addr32_t, uint8_t, inet_naddr_t *); 110 extern void inet_sockaddr_in_addr(const sockaddr_in_t *, inet_addr_t *); 111 112 extern void inet_addr_set6(addr128_t, inet_addr_t *); 113 extern void inet_naddr_set6(addr128_t, uint8_t, inet_naddr_t *); 114 extern void inet_sockaddr_in6_addr(const sockaddr_in6_t *, inet_addr_t *); 115 116 extern uint16_t inet_addr_sockaddr_in(const inet_addr_t *, sockaddr_in_t *, 117 sockaddr_in6_t *); 87 118 88 119 #endif -
uspace/lib/c/include/inet/iplink.h
redf0d27 r02a09ed 47 47 } iplink_t; 48 48 49 /** I Pv4link Service Data Unit */49 /** Internet link Service Data Unit */ 50 50 typedef struct { 51 51 /** Local source address */ 52 uint32_t lsrc;52 inet_addr_t src; 53 53 /** Local destination address */ 54 uint32_t ldest;54 inet_addr_t dest; 55 55 /** Serialized IP packet */ 56 56 void *data; … … 59 59 } iplink_sdu_t; 60 60 61 /** I Pv6 linkService Data Unit */61 /** Internet link receive Service Data Unit */ 62 62 typedef struct { 63 /** Target MAC address */ 64 uint64_t hwaddr; 65 /** Serialized IP packet */ 63 /** Serialized datagram */ 66 64 void *data; 67 65 /** Size of @c data in bytes */ 68 66 size_t size; 69 } iplink_ sdu6_t;67 } iplink_recv_sdu_t; 70 68 71 69 typedef struct iplink_ev_ops { 72 int (*recv)(iplink_t *, iplink_ sdu_t *);70 int (*recv)(iplink_t *, iplink_recv_sdu_t *, uint16_t); 73 71 } iplink_ev_ops_t; 74 72 -
uspace/lib/c/include/inet/iplink_srv.h
redf0d27 r02a09ed 40 40 #include <stdbool.h> 41 41 #include <sys/types.h> 42 #include <inet/addr.h> 43 #include <inet/iplink.h> 42 44 43 45 struct iplink_ops; … … 51 53 } iplink_srv_t; 52 54 53 /** IP link Service Data Unit */54 typedef struct {55 /** Local source address */56 uint32_t lsrc;57 /** Local destination address */58 uint32_t ldest;59 /** Serialized IP packet */60 void *data;61 /** Size of @c data in bytes */62 size_t size;63 } iplink_srv_sdu_t;64 65 55 typedef struct iplink_ops { 66 56 int (*open)(iplink_srv_t *); 67 57 int (*close)(iplink_srv_t *); 68 int (*send)(iplink_srv_t *, iplink_s rv_sdu_t *);58 int (*send)(iplink_srv_t *, iplink_sdu_t *); 69 59 int (*get_mtu)(iplink_srv_t *, size_t *); 70 int (*addr_add)(iplink_srv_t *, uint32_t);71 int (*addr_remove)(iplink_srv_t *, uint32_t);60 int (*addr_add)(iplink_srv_t *, inet_addr_t *); 61 int (*addr_remove)(iplink_srv_t *, inet_addr_t *); 72 62 } iplink_ops_t; 73 63 … … 75 65 76 66 extern int iplink_conn(ipc_callid_t, ipc_call_t *, void *); 77 extern int iplink_ev_recv(iplink_srv_t *, iplink_ srv_sdu_t *);67 extern int iplink_ev_recv(iplink_srv_t *, iplink_recv_sdu_t *, uint16_t); 78 68 79 69 #endif -
uspace/srv/net/dnsrsrv/dnsrsrv.c
redf0d27 r02a09ed 84 84 } 85 85 86 static void dnsr_name2host_srv(dnsr_client_t *client, ipc_callid_t callid, 87 ipc_call_t *call) 88 { 86 static void dnsr_name2host_srv(dnsr_client_t *client, ipc_callid_t iid, 87 ipc_call_t *icall) 88 { 89 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_get_srvaddr_srv()"); 90 89 91 char *name; 92 int rc = async_data_write_accept((void **) &name, true, 0, 93 DNS_NAME_MAX_SIZE, 0, NULL); 94 if (rc != EOK) { 95 async_answer_0(iid, rc); 96 return; 97 } 98 90 99 dns_host_info_t *hinfo; 91 ipc_callid_t rcallid;92 size_t size;93 sysarg_t retval;94 size_t act_size;95 int rc;96 97 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_get_srvaddr_srv()");98 99 rc = async_data_write_accept((void **) &name, true, 0,100 DNS_NAME_MAX_SIZE, 0, NULL);101 if (rc != EOK) {102 async_answer_0(callid, rc);103 return;104 }105 106 if (!async_data_read_receive(&rcallid, &size)) {107 async_answer_0(rcallid, EREFUSED);108 async_answer_0(callid, EREFUSED);109 return;110 }111 112 100 rc = dns_name2host(name, &hinfo); 113 101 if (rc != EOK) { 114 async_answer_0(rcallid, rc); 115 async_answer_0(callid, rc); 116 return; 117 } 118 119 uint32_t addr; 120 rc = inet_addr_pack(&hinfo->addr, &addr); 121 if (rc != EOK) { 122 async_answer_0(rcallid, rc); 123 async_answer_0(callid, rc); 124 return; 125 } 126 127 act_size = str_size(hinfo->cname); 128 if (act_size > size) { 129 async_answer_0(rcallid, EOVERFLOW); 130 async_answer_0(callid, EOVERFLOW); 131 return; 132 } 133 134 retval = async_data_read_finalize(rcallid, hinfo->cname, act_size); 135 async_answer_1(callid, retval, (sysarg_t) addr); 136 137 dns_hostinfo_destroy(hinfo); 138 } 139 140 static void dnsr_get_srvaddr_srv(dnsr_client_t *client, ipc_callid_t iid, 141 ipc_call_t *icall) 142 { 143 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_get_srvaddr_srv()"); 102 async_answer_0(iid, rc); 103 return; 104 } 144 105 145 106 ipc_callid_t callid; … … 157 118 } 158 119 120 rc = async_data_read_finalize(callid, &hinfo->addr, size); 121 if (rc != EOK) { 122 async_answer_0(callid, rc); 123 async_answer_0(iid, rc); 124 return; 125 } 126 127 if (!async_data_read_receive(&callid, &size)) { 128 async_answer_0(callid, EREFUSED); 129 async_answer_0(iid, EREFUSED); 130 return; 131 } 132 133 size_t act_size = str_size(hinfo->cname); 134 if (act_size > size) { 135 async_answer_0(callid, EINVAL); 136 async_answer_0(iid, EINVAL); 137 return; 138 } 139 140 rc = async_data_read_finalize(callid, hinfo->cname, act_size); 141 if (rc != EOK) 142 async_answer_0(callid, rc); 143 144 async_answer_0(iid, (sysarg_t) rc); 145 146 dns_hostinfo_destroy(hinfo); 147 } 148 149 static void dnsr_get_srvaddr_srv(dnsr_client_t *client, ipc_callid_t iid, 150 ipc_call_t *icall) 151 { 152 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_get_srvaddr_srv()"); 153 154 ipc_callid_t callid; 155 size_t size; 156 if (!async_data_read_receive(&callid, &size)) { 157 async_answer_0(callid, EREFUSED); 158 async_answer_0(iid, EREFUSED); 159 return; 160 } 161 162 if (size != sizeof(inet_addr_t)) { 163 async_answer_0(callid, EINVAL); 164 async_answer_0(iid, EINVAL); 165 return; 166 } 167 159 168 // FIXME locking 160 169 161 sysarg_t retval = 162 async_data_read_finalize(callid, &dns_server_addr, size); 163 async_answer_0(iid, retval); 170 int rc = async_data_read_finalize(callid, &dns_server_addr, size); 171 if (rc != EOK) 172 async_answer_0(callid, rc); 173 174 async_answer_0(iid, rc); 164 175 } 165 176 … … 185 196 // FIXME locking 186 197 187 sysarg_t retval = 188 async_data_write_finalize(callid, &dns_server_addr, size); 189 async_answer_0(iid, retval); 198 int rc = async_data_write_finalize(callid, &dns_server_addr, size); 199 if (rc != EOK) 200 async_answer_0(callid, rc); 201 202 async_answer_0(iid, (sysarg_t) rc); 190 203 } 191 204 -
uspace/srv/net/dnsrsrv/query.c
redf0d27 r02a09ed 128 128 129 129 info->cname = str_dup(rr->name); 130 inet_addr_ unpack(dns_uint32_t_decode(rr->rdata, rr->rdata_size),130 inet_addr_set(dns_uint32_t_decode(rr->rdata, rr->rdata_size), 131 131 &info->addr); 132 132 -
uspace/srv/net/dnsrsrv/transport.c
redf0d27 r02a09ed 183 183 int dns_request(dns_message_t *req, dns_message_t **rresp) 184 184 { 185 int rc; 185 trans_req_t *treq = NULL; 186 186 187 void *req_data; 187 188 size_t req_size; 189 int rc = dns_message_encode(req, &req_data, &req_size); 190 if (rc != EOK) 191 goto error; 192 188 193 struct sockaddr_in addr; 189 trans_req_t *treq; 190 int ntry; 191 192 req_data = NULL; 193 treq = NULL; 194 195 addr.sin_family = AF_INET; 196 addr.sin_port = htons(DNS_SERVER_PORT); 197 inet_addr_sockaddr_in(&dns_server_addr, &addr); 198 199 rc = dns_message_encode(req, &req_data, &req_size); 200 if (rc != EOK) 201 goto error; 202 203 ntry = 0; 204 194 struct sockaddr_in6 addr6; 195 uint16_t af = 196 inet_addr_sockaddr_in(&dns_server_addr, &addr, &addr6); 197 198 struct sockaddr *address; 199 socklen_t addrlen; 200 201 switch (af) { 202 case AF_INET: 203 addr.sin_port = htons(DNS_SERVER_PORT); 204 address = (struct sockaddr *) &addr; 205 addrlen = sizeof(addr); 206 break; 207 case AF_INET6: 208 addr6.sin6_port = htons(DNS_SERVER_PORT); 209 address = (struct sockaddr *) &addr6; 210 addrlen = sizeof(addr6); 211 break; 212 default: 213 rc = EAFNOSUPPORT; 214 goto error; 215 } 216 217 size_t ntry = 0; 218 205 219 while (ntry < REQ_RETRY_MAX) { 206 220 rc = sendto(transport_fd, req_data, req_size, 0, 207 (struct sockaddr *) &addr, sizeof(addr));221 (struct sockaddr *) address, addrlen); 208 222 if (rc != EOK) 209 223 goto error; -
uspace/srv/net/ethip/arp.c
redf0d27 r02a09ed 38 38 #include <io/log.h> 39 39 #include <inet/iplink_srv.h> 40 #include <inet/addr.h> 40 41 #include <stdlib.h> 41 42 #include <net/socket_codes.h> 42 43 #include "arp.h" 43 44 #include "atrans.h" … … 54 55 void arp_received(ethip_nic_t *nic, eth_frame_t *frame) 55 56 { 56 int rc; 57 log_msg(LOG_DEFAULT, LVL_DEBUG, "arp_received()"); 58 57 59 arp_eth_packet_t packet; 58 arp_eth_packet_t reply; 59 ethip_link_addr_t *laddr; 60 61 log_msg(LOG_DEFAULT, LVL_DEBUG, "arp_received()"); 62 63 rc = arp_pdu_decode(frame->data, frame->size, &packet); 60 int rc = arp_pdu_decode(frame->data, frame->size, &packet); 64 61 if (rc != EOK) 65 62 return; 66 63 67 64 log_msg(LOG_DEFAULT, LVL_DEBUG, "ARP PDU decoded, opcode=%d, tpa=%x", 68 65 packet.opcode, packet.target_proto_addr); 69 70 laddr = ethip_nic_addr_find(nic, packet.target_proto_addr); 71 if (laddr != NULL) { 72 log_msg(LOG_DEFAULT, LVL_DEBUG, "Request/reply to my address"); 73 74 (void) atrans_add(packet.sender_proto_addr, 75 &packet.sender_hw_addr); 76 77 if (packet.opcode == aop_request) { 78 reply.opcode = aop_reply; 79 reply.sender_hw_addr = nic->mac_addr; 80 reply.sender_proto_addr = laddr->addr; 81 reply.target_hw_addr = packet.sender_hw_addr; 82 reply.target_proto_addr = packet.sender_proto_addr; 83 84 arp_send_packet(nic, &reply); 85 } 66 67 inet_addr_t addr; 68 inet_addr_set(packet.target_proto_addr, &addr); 69 70 ethip_link_addr_t *laddr = ethip_nic_addr_find(nic, &addr); 71 if (laddr == NULL) 72 return; 73 74 addr32_t laddr_v4; 75 uint16_t laddr_af = inet_addr_get(&laddr->addr, &laddr_v4, NULL); 76 if (laddr_af != AF_INET) 77 return; 78 79 log_msg(LOG_DEFAULT, LVL_DEBUG, "Request/reply to my address"); 80 81 (void) atrans_add(packet.sender_proto_addr, 82 packet.sender_hw_addr); 83 84 if (packet.opcode == aop_request) { 85 arp_eth_packet_t reply; 86 87 reply.opcode = aop_reply; 88 addr48(nic->mac_addr, reply.sender_hw_addr); 89 reply.sender_proto_addr = laddr_v4; 90 addr48(packet.sender_hw_addr, reply.target_hw_addr); 91 reply.target_proto_addr = packet.sender_proto_addr; 92 93 arp_send_packet(nic, &reply); 86 94 } 87 95 } 88 96 89 int arp_translate(ethip_nic_t *nic, uint32_t src_addr, uint32_t ip_addr,90 mac48_addr_t *mac_addr)97 int arp_translate(ethip_nic_t *nic, addr32_t src_addr, addr32_t ip_addr, 98 addr48_t mac_addr) 91 99 { 92 int rc; 93 arp_eth_packet_t packet; 94 95 rc = atrans_lookup(ip_addr, mac_addr); 100 int rc = atrans_lookup(ip_addr, mac_addr); 96 101 if (rc == EOK) 97 102 return EOK; 98 103 104 arp_eth_packet_t packet; 105 99 106 packet.opcode = aop_request; 100 packet.sender_hw_addr = nic->mac_addr;107 addr48(nic->mac_addr, packet.sender_hw_addr); 101 108 packet.sender_proto_addr = src_addr; 102 packet.target_hw_addr.addr = MAC48_BROADCAST;109 addr48(addr48_broadcast, packet.target_hw_addr); 103 110 packet.target_proto_addr = ip_addr; 104 111 105 112 rc = arp_send_packet(nic, &packet); 106 113 if (rc != EOK) … … 128 135 return rc; 129 136 130 frame.dest.addr = packet->target_hw_addr.addr;131 frame.src.addr = packet->sender_hw_addr.addr;137 addr48(packet->target_hw_addr, frame.dest); 138 addr48(packet->sender_hw_addr, frame.src); 132 139 frame.etype_len = ETYPE_ARP; 133 140 frame.data = pdata; -
uspace/srv/net/ethip/arp.h
redf0d27 r02a09ed 39 39 40 40 #include <inet/iplink_srv.h> 41 #include <inet/addr.h> 41 42 #include "ethip.h" 42 43 43 44 extern void arp_received(ethip_nic_t *, eth_frame_t *); 44 extern int arp_translate(ethip_nic_t *, uint32_t, uint32_t, mac48_addr_t *);45 extern int arp_translate(ethip_nic_t *, addr32_t, addr32_t, addr48_t); 45 46 46 47 #endif -
uspace/srv/net/ethip/atrans.c
redf0d27 r02a09ed 62 62 } 63 63 64 int atrans_add(uint32_t ip_addr, mac48_addr_t *mac_addr)64 int atrans_add(uint32_t ip_addr, addr48_t mac_addr) 65 65 { 66 66 ethip_atrans_t *atrans; … … 72 72 73 73 atrans->ip_addr = ip_addr; 74 a trans->mac_addr = *mac_addr;74 addr48(mac_addr, atrans->mac_addr); 75 75 76 76 fibril_mutex_lock(&atrans_list_lock); … … 106 106 } 107 107 108 int atrans_lookup(uint32_t ip_addr, mac48_addr_t *mac_addr)108 int atrans_lookup(uint32_t ip_addr, addr48_t mac_addr) 109 109 { 110 ethip_atrans_t *atrans;111 112 110 fibril_mutex_lock(&atrans_list_lock); 113 atrans = atrans_find(ip_addr);111 ethip_atrans_t *atrans = atrans_find(ip_addr); 114 112 if (atrans == NULL) { 115 113 fibril_mutex_unlock(&atrans_list_lock); 116 114 return ENOENT; 117 115 } 118 116 119 117 fibril_mutex_unlock(&atrans_list_lock); 120 *mac_addr = atrans->mac_addr;118 addr48(atrans->mac_addr, mac_addr); 121 119 return EOK; 122 120 } -
uspace/srv/net/ethip/atrans.h
redf0d27 r02a09ed 39 39 40 40 #include <inet/iplink_srv.h> 41 #include <inet/addr.h> 41 42 #include "ethip.h" 42 43 43 extern int atrans_add(uint32_t, mac48_addr_t *);44 extern int atrans_add(uint32_t, addr48_t); 44 45 extern int atrans_remove(uint32_t); 45 extern int atrans_lookup(uint32_t, mac48_addr_t *);46 extern int atrans_lookup(uint32_t, addr48_t); 46 47 extern int atrans_wait_timeout(suseconds_t); 47 48 -
uspace/srv/net/ethip/ethip.c
redf0d27 r02a09ed 44 44 #include <stdio.h> 45 45 #include <stdlib.h> 46 46 #include <net/socket_codes.h> 47 47 #include "arp.h" 48 48 #include "ethip.h" … … 55 55 static int ethip_open(iplink_srv_t *srv); 56 56 static int ethip_close(iplink_srv_t *srv); 57 static int ethip_send(iplink_srv_t *srv, iplink_s rv_sdu_t *sdu);57 static int ethip_send(iplink_srv_t *srv, iplink_sdu_t *sdu); 58 58 static int ethip_get_mtu(iplink_srv_t *srv, size_t *mtu); 59 static int ethip_addr_add(iplink_srv_t *srv, uint32_taddr);60 static int ethip_addr_remove(iplink_srv_t *srv, uint32_taddr);59 static int ethip_addr_add(iplink_srv_t *srv, inet_addr_t *addr); 60 static int ethip_addr_remove(iplink_srv_t *srv, inet_addr_t *addr); 61 61 62 62 static void ethip_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg); … … 164 164 } 165 165 166 static int ethip_send(iplink_srv_t *srv, iplink_srv_sdu_t *sdu) 167 { 168 ethip_nic_t *nic = (ethip_nic_t *)srv->arg; 166 static int ethip_send(iplink_srv_t *srv, iplink_sdu_t *sdu) 167 { 168 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_send()"); 169 170 ethip_nic_t *nic = (ethip_nic_t *) srv->arg; 171 172 addr32_t src_v4; 173 addr128_t src_v6; 174 uint16_t src_af = inet_addr_get(&sdu->src, &src_v4, &src_v6); 175 176 addr32_t dest_v4; 177 addr128_t dest_v6; 178 uint16_t dest_af = inet_addr_get(&sdu->dest, &dest_v4, &dest_v6); 179 180 if (src_af != dest_af) 181 return EINVAL; 182 183 int rc; 169 184 eth_frame_t frame; 185 186 switch (src_af) { 187 case AF_INET: 188 rc = arp_translate(nic, src_v4, dest_v4, frame.dest); 189 if (rc != EOK) { 190 log_msg(LOG_DEFAULT, LVL_WARN, "Failed to look up IPv4 address 0x%" 191 PRIx32, dest_v4); 192 return rc; 193 } 194 195 addr48(nic->mac_addr, frame.src); 196 frame.etype_len = ETYPE_IP; 197 frame.data = sdu->data; 198 frame.size = sdu->size; 199 200 break; 201 case AF_INET6: 202 // FIXME TODO 203 return ENOTSUP; 204 default: 205 return EINVAL; 206 } 207 170 208 void *data; 171 209 size_t size; 172 mac48_addr_t dest_mac_addr;173 int rc;174 175 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_send()");176 177 rc = arp_translate(nic, sdu->lsrc, sdu->ldest, &dest_mac_addr);178 if (rc != EOK) {179 log_msg(LOG_DEFAULT, LVL_WARN, "Failed to look up IP address 0x%" PRIx32,180 sdu->ldest);181 return rc;182 }183 184 frame.dest = dest_mac_addr;185 frame.src = nic->mac_addr;186 frame.etype_len = ETYPE_IP;187 frame.data = sdu->data;188 frame.size = sdu->size;189 190 210 rc = eth_pdu_encode(&frame, &data, &size); 191 211 if (rc != EOK) … … 201 221 { 202 222 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_received(): srv=%p", srv); 203 ethip_nic_t *nic = (ethip_nic_t *)srv->arg; 223 ethip_nic_t *nic = (ethip_nic_t *) srv->arg; 224 225 log_msg(LOG_DEFAULT, LVL_DEBUG, " - eth_pdu_decode"); 226 204 227 eth_frame_t frame; 205 iplink_srv_sdu_t sdu; 206 int rc; 207 208 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_received()"); 209 210 log_msg(LOG_DEFAULT, LVL_DEBUG, " - eth_pdu_decode"); 211 rc = eth_pdu_decode(data, size, &frame); 228 int rc = eth_pdu_decode(data, size, &frame); 212 229 if (rc != EOK) { 213 230 log_msg(LOG_DEFAULT, LVL_DEBUG, " - eth_pdu_decode failed"); 214 231 return rc; 215 232 } 216 233 234 iplink_recv_sdu_t sdu; 235 217 236 switch (frame.etype_len) { 218 237 case ETYPE_ARP: … … 221 240 case ETYPE_IP: 222 241 log_msg(LOG_DEFAULT, LVL_DEBUG, " - construct SDU"); 223 sdu.lsrc = 0;224 sdu.ldest = 0;225 242 sdu.data = frame.data; 226 243 sdu.size = frame.size; 227 244 log_msg(LOG_DEFAULT, LVL_DEBUG, " - call iplink_ev_recv"); 228 rc = iplink_ev_recv(&nic->iplink, &sdu );245 rc = iplink_ev_recv(&nic->iplink, &sdu, AF_INET); 229 246 break; 230 247 default: … … 244 261 } 245 262 246 static int ethip_addr_add(iplink_srv_t *srv, uint32_t addr) 247 { 248 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_addr_add(0x%" PRIx32 ")", addr); 249 263 static int ethip_addr_add(iplink_srv_t *srv, inet_addr_t *addr) 264 { 250 265 ethip_nic_t *nic = (ethip_nic_t *) srv->arg; 251 266 … … 253 268 } 254 269 255 static int ethip_addr_remove(iplink_srv_t *srv, uint32_t addr) 256 { 257 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_addr_remove(0x%" PRIx32 ")", addr); 258 259 ethip_nic_t *nic = (ethip_nic_t *)srv->arg; 260 261 return ethip_nic_addr_add(nic, addr); 270 static int ethip_addr_remove(iplink_srv_t *srv, inet_addr_t *addr) 271 { 272 ethip_nic_t *nic = (ethip_nic_t *) srv->arg; 273 274 return ethip_nic_addr_remove(nic, addr); 262 275 } 263 276 -
uspace/srv/net/ethip/ethip.h
redf0d27 r02a09ed 41 41 #include <async.h> 42 42 #include <inet/iplink_srv.h> 43 #include <inet/addr.h> 43 44 #include <loc.h> 44 45 #include <sys/types.h> 45 46 46 #define MAC48_BROADCAST 0xffffffffffff47 48 47 typedef struct { 49 48 link_t addr_list; 50 uint32_t addr;49 inet_addr_t addr; 51 50 } ethip_link_addr_t; 52 53 /** IEEE MAC-48 identifier */54 typedef struct {55 /** MAC Address (in lowest 48 bits) */56 uint64_t addr;57 } mac48_addr_t;58 51 59 52 typedef struct ethip_nic { … … 67 60 68 61 /** MAC address */ 69 mac48_addr_t mac_addr;62 addr48_t mac_addr; 70 63 /** List of IP addresses configured on this link */ 71 64 list_t addr_list; /* of ethip_link_addr_t */ … … 75 68 typedef struct { 76 69 /** Destination Address */ 77 mac48_addr_t dest;70 addr48_t dest; 78 71 /** Source Address */ 79 mac48_addr_t src;72 addr48_t src; 80 73 /** Ethertype or Length */ 81 74 uint16_t etype_len; … … 102 95 arp_opcode_t opcode; 103 96 /** Sender hardware address */ 104 mac48_addr_t sender_hw_addr;97 addr48_t sender_hw_addr; 105 98 /** Sender protocol address */ 106 99 uint32_t sender_proto_addr; 107 100 /** Target hardware address */ 108 mac48_addr_t target_hw_addr;101 addr48_t target_hw_addr; 109 102 /** Target protocol address */ 110 103 uint32_t target_proto_addr; … … 115 108 link_t atrans_list; 116 109 uint32_t ip_addr; 117 mac48_addr_t mac_addr;110 addr48_t mac_addr; 118 111 } ethip_atrans_t; 119 112 -
uspace/srv/net/ethip/ethip_nic.c
redf0d27 r02a09ed 121 121 } 122 122 123 static ethip_link_addr_t *ethip_nic_addr_new( uint32_taddr)123 static ethip_link_addr_t *ethip_nic_addr_new(inet_addr_t *addr) 124 124 { 125 125 ethip_link_addr_t *laddr = calloc(1, sizeof(ethip_link_addr_t)); … … 131 131 132 132 link_initialize(&laddr->addr_list); 133 laddr->addr = addr;133 laddr->addr = *addr; 134 134 135 135 return laddr; … … 193 193 goto error; 194 194 } 195 196 mac48_decode(nic_address.address, &nic->mac_addr);195 196 addr48(nic_address.address, nic->mac_addr); 197 197 198 198 rc = nic_set_state(nic->sess, NIC_STATE_ACTIVE); … … 203 203 } 204 204 205 log_msg(LOG_DEFAULT, LVL_DEBUG, "Initialized IP link service, MAC = 0x%" PRIx64, 206 nic->mac_addr.addr); 205 log_msg(LOG_DEFAULT, LVL_DEBUG, "Initialized IP link service,"); 207 206 208 207 return EOK; … … 335 334 } 336 335 337 int ethip_nic_addr_add(ethip_nic_t *nic, uint32_taddr)336 int ethip_nic_addr_add(ethip_nic_t *nic, inet_addr_t *addr) 338 337 { 339 338 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_add()"); … … 347 346 } 348 347 349 int ethip_nic_addr_remove(ethip_nic_t *nic, uint32_taddr)348 int ethip_nic_addr_remove(ethip_nic_t *nic, inet_addr_t *addr) 350 349 { 351 350 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_remove()"); … … 361 360 362 361 ethip_link_addr_t *ethip_nic_addr_find(ethip_nic_t *nic, 363 uint32_taddr)362 inet_addr_t *addr) 364 363 { 365 364 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_find()"); … … 369 368 ethip_link_addr_t, addr_list); 370 369 371 if ( addr == laddr->addr)370 if (inet_addr_compare(addr, &laddr->addr)) 372 371 return laddr; 373 372 } -
uspace/srv/net/ethip/ethip_nic.h
redf0d27 r02a09ed 39 39 40 40 #include <ipc/loc.h> 41 #include <inet/addr.h> 41 42 #include "ethip.h" 42 43 … … 44 45 extern ethip_nic_t *ethip_nic_find_by_iplink_sid(service_id_t); 45 46 extern int ethip_nic_send(ethip_nic_t *, void *, size_t); 46 extern int ethip_nic_addr_add(ethip_nic_t *, uint32_t);47 extern int ethip_nic_addr_remove(ethip_nic_t *, uint32_t);48 extern ethip_link_addr_t *ethip_nic_addr_find(ethip_nic_t *, uint32_t);47 extern int ethip_nic_addr_add(ethip_nic_t *, inet_addr_t *); 48 extern int ethip_nic_addr_remove(ethip_nic_t *, inet_addr_t *); 49 extern ethip_link_addr_t *ethip_nic_addr_find(ethip_nic_t *, inet_addr_t *); 49 50 50 51 #endif -
uspace/srv/net/ethip/pdu.c
redf0d27 r02a09ed 62 62 63 63 hdr = (eth_header_t *)data; 64 mac48_encode(&frame->src, hdr->src);65 mac48_encode(&frame->dest, hdr->dest);64 addr48(frame->src, hdr->src); 65 addr48(frame->dest, hdr->dest); 66 66 hdr->etype_len = host2uint16_t_be(frame->etype_len); 67 67 … … 69 69 frame->size); 70 70 71 log_msg(LOG_DEFAULT, LVL_DEBUG, "Encoding Ethernet frame "72 "src=%" PRIx64 " dest=%" PRIx64 " etype=%x",73 frame->src.addr, frame->dest.addr, frame->etype_len);74 71 log_msg(LOG_DEFAULT, LVL_DEBUG, "Encoded Ethernet frame (%zu bytes)", size); 75 72 … … 98 95 return ENOMEM; 99 96 100 mac48_decode(hdr->src, &frame->src);101 mac48_decode(hdr->dest, &frame->dest);97 addr48(hdr->src, frame->src); 98 addr48(hdr->dest, frame->dest); 102 99 frame->etype_len = uint16_t_be2host(hdr->etype_len); 103 100 … … 105 102 frame->size); 106 103 107 log_msg(LOG_DEFAULT, LVL_DEBUG, "Decoding Ethernet frame "108 "src=%" PRIx64 " dest=%" PRIx64 " etype=%x",109 frame->src.addr, frame->dest.addr, frame->etype_len);110 104 log_msg(LOG_DEFAULT, LVL_DEBUG, "Decoded Ethernet frame payload (%zu bytes)", frame->size); 111 105 112 106 return EOK; 113 }114 115 void mac48_encode(mac48_addr_t *addr, void *buf)116 {117 uint64_t val;118 uint8_t *bbuf = (uint8_t *)buf;119 int i;120 121 val = addr->addr;122 for (i = 0; i < MAC48_BYTES; i++)123 bbuf[i] = (val >> (8 * (MAC48_BYTES - i - 1))) & 0xff;124 }125 126 void mac48_decode(void *data, mac48_addr_t *addr)127 {128 uint64_t val;129 uint8_t *bdata = (uint8_t *)data;130 int i;131 132 val = 0;133 for (i = 0; i < MAC48_BYTES; i++)134 val |= (uint64_t)bdata[i] << (8 * (MAC48_BYTES - i - 1));135 136 addr->addr = val;137 107 } 138 108 … … 168 138 pfmt->proto_addr_size = IPV4_ADDR_SIZE; 169 139 pfmt->opcode = host2uint16_t_be(fopcode); 170 mac48_encode(&packet->sender_hw_addr, pfmt->sender_hw_addr);140 addr48(packet->sender_hw_addr, pfmt->sender_hw_addr); 171 141 pfmt->sender_proto_addr = 172 142 host2uint32_t_be(packet->sender_proto_addr); 173 mac48_encode(&packet->target_hw_addr, pfmt->target_hw_addr);143 addr48(packet->target_hw_addr, pfmt->target_hw_addr); 174 144 pfmt->target_proto_addr = 175 145 host2uint32_t_be(packet->target_proto_addr); … … 227 197 } 228 198 229 mac48_decode(pfmt->sender_hw_addr, &packet->sender_hw_addr);199 addr48(pfmt->sender_hw_addr, packet->sender_hw_addr); 230 200 packet->sender_proto_addr = 231 201 uint32_t_be2host(pfmt->sender_proto_addr); 232 mac48_decode(pfmt->target_hw_addr, &packet->target_hw_addr);202 addr48(pfmt->target_hw_addr, packet->target_hw_addr); 233 203 packet->target_proto_addr = 234 204 uint32_t_be2host(pfmt->target_proto_addr); -
uspace/srv/net/ethip/pdu.h
redf0d27 r02a09ed 42 42 extern int eth_pdu_encode(eth_frame_t *, void **, size_t *); 43 43 extern int eth_pdu_decode(void *, size_t, eth_frame_t *); 44 extern void mac48_encode(mac48_addr_t *, void *);45 extern void mac48_decode(void *, mac48_addr_t *);46 44 extern int arp_pdu_encode(arp_eth_packet_t *, void **, size_t *); 47 45 extern int arp_pdu_decode(void *, size_t, arp_eth_packet_t *); -
uspace/srv/net/ethip/std.h
redf0d27 r02a09ed 39 39 40 40 #include <sys/types.h> 41 #include <inet/addr.h> 41 42 42 43 #define ETH_ADDR_SIZE 6 … … 47 48 typedef struct { 48 49 /** Destination Address */ 49 uint8_t dest[ETH_ADDR_SIZE];50 addr48_t dest; 50 51 /** Source Address */ 51 uint8_t src[ETH_ADDR_SIZE];52 addr48_t src; 52 53 /** Ethertype or Length */ 53 54 uint16_t etype_len; … … 67 68 uint16_t opcode; 68 69 /** Sender hardware address */ 69 uint8_t sender_hw_addr[ETH_ADDR_SIZE];70 addr48_t sender_hw_addr; 70 71 /** Sender protocol address */ 71 72 uint32_t sender_proto_addr; 72 73 /** Target hardware address */ 73 uint8_t target_hw_addr[ETH_ADDR_SIZE];74 addr48_t target_hw_addr; 74 75 /** Target protocol address */ 75 76 uint32_t target_proto_addr; … … 87 88 /** IP Ethertype */ 88 89 enum ether_type { 89 ETYPE_ARP = 0x0806, 90 ETYPE_IP = 0x0800 90 ETYPE_ARP = 0x0806, 91 ETYPE_IP = 0x0800, 92 ETYPE_IPV6 = 0x86DD 91 93 }; 92 94 -
uspace/srv/net/inetsrv/Makefile
redf0d27 r02a09ed 35 35 inetsrv.c \ 36 36 inet_link.c \ 37 inet_util.c \38 37 inetcfg.c \ 39 38 inetping.c \ -
uspace/srv/net/inetsrv/addrobj.c
redf0d27 r02a09ed 42 42 #include <stdlib.h> 43 43 #include <str.h> 44 45 44 #include "addrobj.h" 46 45 #include "inetsrv.h" 47 46 #include "inet_link.h" 48 #include "inet_util.h"49 47 50 48 static inet_addrobj_t *inet_addrobj_find_by_name_locked(const char *, inet_link_t *); … … 106 104 /** Find address object matching address @a addr. 107 105 * 108 * @param addr Address 109 * @oaram find iaf_net to find network (using mask), 110 * iaf_addr to find local address (exact match) 106 * @param addr Address 107 * @oaram find iaf_net to find network (using mask), 108 * iaf_addr to find local address (exact match) 109 * 111 110 */ 112 111 inet_addrobj_t *inet_addrobj_find(inet_addr_t *addr, inet_addrobj_find_t find) 113 112 { 114 uint32_t addr_addr;115 int rc = inet_addr_pack(addr, &addr_addr);116 if (rc != EOK)117 return NULL;118 119 113 fibril_mutex_lock(&addr_list_lock); 120 114 … … 123 117 inet_addrobj_t, addr_list); 124 118 125 uint32_t naddr_addr; 126 uint8_t naddr_bits; 127 rc = inet_naddr_pack(&naddr->naddr, &naddr_addr, &naddr_bits); 128 if (rc != EOK) 129 continue; 130 131 uint32_t mask = inet_netmask(naddr_bits); 132 if ((naddr_addr & mask) == (addr_addr & mask)) { 119 if (inet_naddr_compare_mask(&naddr->naddr, addr)) { 133 120 fibril_mutex_unlock(&addr_list_lock); 134 121 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_addrobj_find: found %p", … … 227 214 inet_naddr_addr(&addr->naddr, &lsrc_addr); 228 215 229 inet_addr_t *ldest_addr = ldest; 230 231 return inet_link_send_dgram(addr->ilink, &lsrc_addr, ldest_addr, dgram, 216 return inet_link_send_dgram(addr->ilink, &lsrc_addr, ldest, dgram, 232 217 proto, ttl, df); 233 218 } -
uspace/srv/net/inetsrv/icmp.c
redf0d27 r02a09ed 40 40 #include <mem.h> 41 41 #include <stdlib.h> 42 42 #include <net/socket_codes.h> 43 43 #include "icmp.h" 44 44 #include "icmp_std.h" … … 129 129 inetping_sdu_t sdu; 130 130 131 int rc = inet_addr_pack(&dgram->src, &sdu.src);132 if ( rc != EOK)133 return rc;131 uint16_t family = inet_addr_get(&dgram->src, &sdu.src, NULL); 132 if (family != AF_INET) 133 return EINVAL; 134 134 135 rc = inet_addr_pack(&dgram->dest, &sdu.dest);136 if ( rc != EOK)137 return rc;135 family = inet_addr_get(&dgram->dest, &sdu.dest, NULL); 136 if (family != AF_INET) 137 return EINVAL; 138 138 139 139 sdu.seq_no = uint16_t_be2host(reply->seq_no); … … 168 168 inet_dgram_t dgram; 169 169 170 inet_addr_ unpack(sdu->src, &dgram.src);171 inet_addr_ unpack(sdu->dest, &dgram.dest);170 inet_addr_set(sdu->src, &dgram.src); 171 inet_addr_set(sdu->dest, &dgram.dest); 172 172 173 173 dgram.tos = ICMP_TOS; -
uspace/srv/net/inetsrv/inet_link.c
redf0d27 r02a09ed 43 43 #include <stdlib.h> 44 44 #include <str.h> 45 45 #include <net/socket_codes.h> 46 46 #include "addrobj.h" 47 47 #include "inetsrv.h" … … 49 49 #include "pdu.h" 50 50 51 static int inet_link_open(service_id_t sid);52 static int inet_iplink_recv(iplink_t * ilink, iplink_sdu_t *sdu);51 static int inet_link_open(service_id_t); 52 static int inet_iplink_recv(iplink_t *, iplink_recv_sdu_t *, uint16_t); 53 53 54 54 static iplink_ev_ops_t inet_iplink_ev_ops = { … … 59 59 static FIBRIL_MUTEX_INITIALIZE(inet_discovery_lock); 60 60 61 static int inet_iplink_recv(iplink_t *iplink, iplink_sdu_t *sdu) 62 { 61 static int inet_iplink_recv(iplink_t *iplink, iplink_recv_sdu_t *sdu, uint16_t af) 62 { 63 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_iplink_recv()"); 64 65 int rc; 63 66 inet_packet_t packet; 64 int rc; 65 66 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_iplink_recv()"); 67 rc = inet_pdu_decode(sdu->data, sdu->size, &packet); 67 68 switch (af) { 69 case AF_INET: 70 rc = inet_pdu_decode(sdu->data, sdu->size, &packet); 71 break; 72 case AF_INET6: 73 // FIXME TODO 74 return ENOTSUP; 75 default: 76 log_msg(LOG_DEFAULT, LVL_DEBUG, "invalid address family"); 77 return EINVAL; 78 } 79 68 80 if (rc != EOK) { 69 81 log_msg(LOG_DEFAULT, LVL_DEBUG, "failed decoding PDU"); … … 228 240 229 241 return EOK; 230 242 231 243 error: 232 244 if (ilink->iplink != NULL) … … 277 289 278 290 iplink_sdu_t sdu; 279 280 int rc = inet_addr_pack(lsrc, &sdu.lsrc);281 if (rc != EOK)282 return rc;283 284 rc = inet_addr_pack(ldest, &sdu.ldest);285 if (rc != EOK)286 return rc;287 288 291 size_t offs = 0; 292 int rc; 293 294 sdu.src = *lsrc; 295 sdu.dest = *ldest; 296 289 297 do { 298 /* Encode one fragment */ 290 299 size_t roffs; 291 292 /* Encode one fragment */293 300 rc = inet_pdu_encode(&packet, offs, ilink->def_mtu, &sdu.data, 294 301 &sdu.size, &roffs); -
uspace/srv/net/inetsrv/inetcfg.c
redf0d27 r02a09ed 236 236 } 237 237 238 static void inetcfg_addr_create_static_srv(ipc_callid_t callid, 239 ipc_call_t *call) 240 { 238 static void inetcfg_addr_create_static_srv(ipc_callid_t iid, 239 ipc_call_t *icall) 240 { 241 log_msg(LOG_DEFAULT, LVL_DEBUG, "inetcfg_addr_create_static_srv()"); 242 243 sysarg_t link_id = IPC_GET_ARG1(*icall); 244 245 ipc_callid_t callid; 246 size_t size; 247 if (!async_data_write_receive(&callid, &size)) { 248 async_answer_0(callid, EINVAL); 249 async_answer_0(iid, EINVAL); 250 return; 251 } 252 253 if (size != sizeof(inet_naddr_t)) { 254 async_answer_0(callid, EINVAL); 255 async_answer_0(iid, EINVAL); 256 return; 257 } 258 259 inet_naddr_t naddr; 260 int rc = async_data_write_finalize(callid, &naddr, size); 261 if (rc != EOK) { 262 async_answer_0(callid, rc); 263 async_answer_0(iid, rc); 264 return; 265 } 266 241 267 char *name; 242 inet_naddr_t naddr;243 sysarg_t link_id;244 sysarg_t addr_id;245 int rc;246 247 log_msg(LOG_DEFAULT, LVL_DEBUG, "inetcfg_addr_create_static_srv()");248 249 268 rc = async_data_write_accept((void **) &name, true, 0, LOC_NAME_MAXLEN, 250 269 0, NULL); 251 270 if (rc != EOK) { 252 async_answer_0(callid, rc); 253 return; 254 } 255 256 inet_naddr_unpack(IPC_GET_ARG1(*call), IPC_GET_ARG2(*call), &naddr); 257 link_id = IPC_GET_ARG3(*call); 258 259 addr_id = 0; 271 async_answer_0(iid, rc); 272 return; 273 } 274 275 sysarg_t addr_id = 0; 260 276 rc = inetcfg_addr_create_static(name, &naddr, link_id, &addr_id); 261 277 free(name); 262 async_answer_1( callid, rc, addr_id);278 async_answer_1(iid, rc, addr_id); 263 279 } 264 280 … … 276 292 } 277 293 278 static void inetcfg_addr_get_srv(ipc_callid_t callid, ipc_call_t *call)294 static void inetcfg_addr_get_srv(ipc_callid_t iid, ipc_call_t *icall) 279 295 { 280 296 log_msg(LOG_DEFAULT, LVL_DEBUG, "inetcfg_addr_get_srv()"); 281 297 282 sysarg_t addr_id = IPC_GET_ARG1(* call);298 sysarg_t addr_id = IPC_GET_ARG1(*icall); 283 299 284 300 inet_addr_info_t ainfo; … … 288 304 ainfo.name = NULL; 289 305 290 ipc_callid_t rcallid;291 size_t max_size;292 293 if (!async_data_read_receive(&rcallid, &max_size)) {294 async_answer_0(rcallid, EREFUSED);295 async_answer_0(callid, EREFUSED);296 return;297 }298 299 306 int rc = inetcfg_addr_get(addr_id, &ainfo); 300 307 if (rc != EOK) { 301 async_answer_0(callid, rc); 302 return; 303 } 304 305 uint32_t naddr_addr; 306 uint8_t naddr_bits; 307 rc = inet_naddr_pack(&ainfo.naddr, &naddr_addr, &naddr_bits); 308 if (rc != EOK) { 309 async_answer_0(callid, rc); 310 return; 311 } 312 313 sysarg_t retval = async_data_read_finalize(rcallid, ainfo.name, 314 min(max_size, str_size(ainfo.name))); 308 async_answer_0(iid, rc); 309 return; 310 } 311 312 ipc_callid_t callid; 313 size_t size; 314 if (!async_data_read_receive(&callid, &size)) { 315 async_answer_0(callid, EREFUSED); 316 async_answer_0(iid, EREFUSED); 317 return; 318 } 319 320 if (size != sizeof(inet_naddr_t)) { 321 async_answer_0(callid, EINVAL); 322 async_answer_0(iid, EINVAL); 323 return; 324 } 325 326 rc = async_data_read_finalize(callid, &ainfo.naddr, size); 327 if (rc != EOK) { 328 async_answer_0(callid, rc); 329 async_answer_0(iid, rc); 330 return; 331 } 332 333 if (!async_data_read_receive(&callid, &size)) { 334 async_answer_0(callid, EREFUSED); 335 async_answer_0(iid, EREFUSED); 336 return; 337 } 338 339 rc = async_data_read_finalize(callid, ainfo.name, 340 min(size, str_size(ainfo.name))); 315 341 free(ainfo.name); 316 342 317 async_answer_3(callid, retval, (sysarg_t) naddr_addr, 318 (sysarg_t) naddr_bits, ainfo.ilink); 343 if (rc != EOK) { 344 async_answer_0(callid, rc); 345 async_answer_0(iid, rc); 346 return; 347 } 348 349 async_answer_1(iid, (sysarg_t) rc, ainfo.ilink); 319 350 } 320 351 … … 477 508 } 478 509 479 static void inetcfg_sroute_create_srv(ipc_callid_t callid,480 ipc_call_t * call)510 static void inetcfg_sroute_create_srv(ipc_callid_t iid, 511 ipc_call_t *icall) 481 512 { 482 513 log_msg(LOG_DEFAULT, LVL_DEBUG, "inetcfg_sroute_create_srv()"); 514 515 ipc_callid_t callid; 516 size_t size; 517 if (!async_data_write_receive(&callid, &size)) { 518 async_answer_0(callid, EINVAL); 519 async_answer_0(iid, EINVAL); 520 return; 521 } 522 523 if (size != sizeof(inet_naddr_t)) { 524 async_answer_0(callid, EINVAL); 525 async_answer_0(iid, EINVAL); 526 return; 527 } 528 529 inet_naddr_t dest; 530 int rc = async_data_write_finalize(callid, &dest, size); 531 if (rc != EOK) { 532 async_answer_0(callid, rc); 533 async_answer_0(iid, rc); 534 return; 535 } 536 537 if (!async_data_write_receive(&callid, &size)) { 538 async_answer_0(callid, EINVAL); 539 async_answer_0(iid, EINVAL); 540 return; 541 } 542 543 if (size != sizeof(inet_addr_t)) { 544 async_answer_0(callid, EINVAL); 545 async_answer_0(iid, EINVAL); 546 return; 547 } 548 549 inet_addr_t router; 550 rc = async_data_write_finalize(callid, &router, size); 551 if (rc != EOK) { 552 async_answer_0(callid, rc); 553 async_answer_0(iid, rc); 554 return; 555 } 483 556 484 557 char *name; 485 intrc = async_data_write_accept((void **) &name, true, 0, LOC_NAME_MAXLEN,558 rc = async_data_write_accept((void **) &name, true, 0, LOC_NAME_MAXLEN, 486 559 0, NULL); 487 560 if (rc != EOK) { 488 async_answer_0(callid, rc); 489 return; 490 } 491 492 inet_naddr_t dest; 493 inet_addr_t router; 494 495 inet_naddr_unpack(IPC_GET_ARG1(*call), IPC_GET_ARG2(*call), &dest); 496 inet_addr_unpack(IPC_GET_ARG3(*call), &router); 561 async_answer_0(iid, rc); 562 return; 563 } 497 564 498 565 sysarg_t sroute_id = 0; 499 566 rc = inetcfg_sroute_create(name, &dest, &router, &sroute_id); 500 567 free(name); 501 async_answer_1( callid, rc, sroute_id);568 async_answer_1(iid, rc, sroute_id); 502 569 } 503 570 … … 515 582 } 516 583 517 static void inetcfg_sroute_get_srv(ipc_callid_t callid, ipc_call_t *call)584 static void inetcfg_sroute_get_srv(ipc_callid_t iid, ipc_call_t *icall) 518 585 { 519 586 log_msg(LOG_DEFAULT, LVL_DEBUG, "inetcfg_sroute_get_srv()"); 520 587 521 sysarg_t sroute_id = IPC_GET_ARG1(* call);588 sysarg_t sroute_id = IPC_GET_ARG1(*icall); 522 589 523 590 inet_sroute_info_t srinfo; … … 527 594 srinfo.name = NULL; 528 595 529 ipc_callid_t rcallid;530 size_t max_size;531 if (!async_data_read_receive(&rcallid, &max_size)) {532 async_answer_0(rcallid, EREFUSED);533 async_answer_0(callid, EREFUSED);534 return;535 }536 537 596 int rc = inetcfg_sroute_get(sroute_id, &srinfo); 538 597 if (rc != EOK) { 539 async_answer_0(callid, rc); 540 return; 541 } 542 543 uint32_t dest_addr; 544 uint8_t dest_bits; 545 rc = inet_naddr_pack(&srinfo.dest, &dest_addr, &dest_bits); 546 if (rc != EOK) { 547 async_answer_0(callid, rc); 548 return; 549 } 550 551 uint32_t router_addr; 552 rc = inet_addr_pack(&srinfo.router, &router_addr); 553 if (rc != EOK) { 554 async_answer_0(callid, rc); 555 return; 556 } 557 558 sysarg_t retval = async_data_read_finalize(rcallid, srinfo.name, 559 min(max_size, str_size(srinfo.name))); 598 async_answer_0(iid, rc); 599 return; 600 } 601 602 ipc_callid_t callid; 603 size_t size; 604 if (!async_data_read_receive(&callid, &size)) { 605 async_answer_0(callid, EREFUSED); 606 async_answer_0(iid, EREFUSED); 607 return; 608 } 609 610 if (size != sizeof(inet_naddr_t)) { 611 async_answer_0(callid, EINVAL); 612 async_answer_0(iid, EINVAL); 613 return; 614 } 615 616 rc = async_data_read_finalize(callid, &srinfo.dest, size); 617 if (rc != EOK) { 618 async_answer_0(callid, rc); 619 async_answer_0(iid, rc); 620 return; 621 } 622 623 if (!async_data_read_receive(&callid, &size)) { 624 async_answer_0(callid, EREFUSED); 625 async_answer_0(iid, EREFUSED); 626 return; 627 } 628 629 if (size != sizeof(inet_addr_t)) { 630 async_answer_0(callid, EINVAL); 631 async_answer_0(iid, EINVAL); 632 return; 633 } 634 635 rc = async_data_read_finalize(callid, &srinfo.router, size); 636 if (rc != EOK) { 637 async_answer_0(callid, rc); 638 async_answer_0(iid, rc); 639 return; 640 } 641 642 if (!async_data_read_receive(&callid, &size)) { 643 async_answer_0(callid, EREFUSED); 644 async_answer_0(iid, EREFUSED); 645 return; 646 } 647 648 rc = async_data_read_finalize(callid, srinfo.name, 649 min(size, str_size(srinfo.name))); 560 650 free(srinfo.name); 561 651 562 async_answer_3(callid, retval, (sysarg_t) dest_addr, 563 (sysarg_t) dest_bits, (sysarg_t) router_addr); 652 async_answer_0(iid, (sysarg_t) rc); 564 653 } 565 654 -
uspace/srv/net/inetsrv/inetping.c
redf0d27 r02a09ed 43 43 #include <stdlib.h> 44 44 #include <sys/types.h> 45 45 #include <net/socket_codes.h> 46 46 #include "icmp.h" 47 47 #include "icmp_std.h" … … 55 55 static uint16_t inetping_ident = 0; 56 56 57 static inetping_client_t *inetping_client_find(uint16_t);58 59 57 static int inetping_send(inetping_client_t *client, inetping_sdu_t *sdu) 60 58 { … … 62 60 } 63 61 64 static int inetping_get_srcaddr(inetping_client_t *client, uint32_t remote,65 uint32_t *local)62 static int inetping_get_srcaddr(inetping_client_t *client, addr32_t remote, 63 addr32_t *local) 66 64 { 67 65 inet_addr_t remote_addr; 68 inet_addr_ unpack(remote, &remote_addr);66 inet_addr_set(remote, &remote_addr); 69 67 70 68 inet_addr_t local_addr; … … 73 71 return rc; 74 72 75 return inet_addr_pack(&local_addr, local); 73 uint16_t family = inet_addr_get(&local_addr, local, NULL); 74 if (family != AF_INET) 75 return EINVAL; 76 77 return EOK; 78 } 79 80 static inetping_client_t *inetping_client_find(uint16_t ident) 81 { 82 fibril_mutex_lock(&client_list_lock); 83 84 list_foreach(client_list, link) { 85 inetping_client_t *client = list_get_instance(link, 86 inetping_client_t, client_list); 87 88 if (client->ident == ident) { 89 fibril_mutex_unlock(&client_list_lock); 90 return client; 91 } 92 } 93 94 fibril_mutex_unlock(&client_list_lock); 95 return NULL; 76 96 } 77 97 78 98 int inetping_recv(uint16_t ident, inetping_sdu_t *sdu) 79 99 { 80 inetping_client_t *client; 81 async_exch_t *exch; 82 ipc_call_t answer; 83 84 client = inetping_client_find(ident); 100 inetping_client_t *client = inetping_client_find(ident); 85 101 if (client == NULL) { 86 102 log_msg(LOG_DEFAULT, LVL_DEBUG, "Unknown ICMP ident. Dropping."); 87 103 return ENOENT; 88 104 } 89 90 exch = async_exchange_begin(client->sess); 91 105 106 async_exch_t *exch = async_exchange_begin(client->sess); 107 108 ipc_call_t answer; 92 109 aid_t req = async_send_3(exch, INETPING_EV_RECV, (sysarg_t) sdu->src, 93 110 (sysarg_t) sdu->dest, sdu->seq_no, &answer); 94 111 int rc = async_data_write_start(exch, sdu->data, sdu->size); 112 95 113 async_exchange_end(exch); 96 114 … … 170 188 } 171 189 172 static inetping_client_t *inetping_client_find(uint16_t ident)173 {174 fibril_mutex_lock(&client_list_lock);175 176 list_foreach(client_list, link) {177 inetping_client_t *client = list_get_instance(link,178 inetping_client_t, client_list);179 180 if (client->ident == ident) {181 fibril_mutex_unlock(&client_list_lock);182 return client;183 }184 }185 186 fibril_mutex_unlock(&client_list_lock);187 return NULL;188 }189 190 190 void inetping_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg) 191 191 { 192 inetping_client_t client;193 int rc;194 195 192 log_msg(LOG_DEFAULT, LVL_DEBUG, "inetping_conn()"); 196 193 197 194 /* Accept the connection */ 198 195 async_answer_0(iid, EOK); 199 200 rc = inetping_client_init(&client); 196 197 inetping_client_t client; 198 int rc = inetping_client_init(&client); 201 199 if (rc != EOK) 202 200 return; -
uspace/srv/net/inetsrv/inetsrv.c
redf0d27 r02a09ed 46 46 #include <stdlib.h> 47 47 #include <sys/types.h> 48 48 #include <net/socket_codes.h> 49 49 #include "addrobj.h" 50 50 #include "icmp.h" … … 202 202 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_get_srcaddr_srv()"); 203 203 204 addr32_t remote_v4 = IPC_GET_ARG1(*call); 205 uint8_t tos = IPC_GET_ARG2(*call); 206 204 207 inet_addr_t remote; 205 inet_addr_unpack(IPC_GET_ARG1(*call), &remote); 206 uint8_t tos = IPC_GET_ARG2(*call); 208 inet_addr_set(remote_v4, &remote); 207 209 208 210 inet_addr_t local; … … 213 215 } 214 216 215 uint32_t local_addr;216 rc = inet_addr_pack(&local, &local_addr);217 if ( rc != EOK) {218 async_answer_0(callid, rc);217 addr32_t local_v4; 218 uint16_t family = inet_addr_get(&local, &local_v4, NULL); 219 if (family != AF_INET) { 220 async_answer_0(callid, EINVAL); 219 221 return; 220 222 } 221 223 222 async_answer_1(callid, rc, (sysarg_t) local_ addr);224 async_answer_1(callid, rc, (sysarg_t) local_v4); 223 225 } 224 226 … … 228 230 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_send_srv()"); 229 231 232 addr32_t src_v4 = IPC_GET_ARG1(*call); 233 addr32_t dest_v4 = IPC_GET_ARG2(*call); 234 230 235 inet_dgram_t dgram; 231 236 232 inet_addr_ unpack(IPC_GET_ARG1(*call), &dgram.src);233 inet_addr_ unpack(IPC_GET_ARG2(*call), &dgram.dest);237 inet_addr_set(src_v4, &dgram.src); 238 inet_addr_set(dest_v4, &dgram.dest); 234 239 dgram.tos = IPC_GET_ARG3(*call); 235 240 … … 371 376 int inet_ev_recv(inet_client_t *client, inet_dgram_t *dgram) 372 377 { 373 uint32_t src;374 int rc = inet_addr_pack(&dgram->src, &src);375 if (rc != EOK)376 return rc;377 378 uint32_t dest;379 rc = inet_addr_pack(&dgram->dest, &dest);380 if (rc != EOK)381 return rc;382 383 378 async_exch_t *exch = async_exchange_begin(client->sess); 384 379 385 380 ipc_call_t answer; 386 aid_t req = async_send_3(exch, INET_EV_RECV, (sysarg_t) src, 387 (sysarg_t) dest, dgram->tos, &answer); 388 rc = async_data_write_start(exch, dgram->data, dgram->size); 389 async_exchange_end(exch); 390 391 if (rc != EOK) { 381 aid_t req = async_send_1(exch, INET_EV_RECV, dgram->tos, &answer); 382 383 int rc = async_data_write_start(exch, &dgram->src, sizeof(inet_addr_t)); 384 if (rc != EOK) { 385 async_exchange_end(exch); 392 386 async_forget(req); 393 387 return rc; 394 388 } 395 389 390 rc = async_data_write_start(exch, &dgram->dest, sizeof(inet_addr_t)); 391 if (rc != EOK) { 392 async_exchange_end(exch); 393 async_forget(req); 394 return rc; 395 } 396 397 rc = async_data_write_start(exch, dgram->data, dgram->size); 398 399 async_exchange_end(exch); 400 401 if (rc != EOK) { 402 async_forget(req); 403 return rc; 404 } 405 396 406 sysarg_t retval; 397 407 async_wait_for(req, &retval); 398 if (retval != EOK) 399 return retval; 400 401 return EOK; 408 409 return (int) retval; 402 410 } 403 411 -
uspace/srv/net/inetsrv/pdu.c
redf0d27 r02a09ed 44 44 #include <mem.h> 45 45 #include <stdlib.h> 46 46 #include <net/socket_codes.h> 47 47 #include "inetsrv.h" 48 48 #include "inet_std.h" … … 106 106 void **rdata, size_t *rsize, size_t *roffs) 107 107 { 108 uint32_t src_addr; 109 int rc = inet_addr_pack(&packet->src, &src_addr); 110 if (rc != EOK) 111 return rc; 112 113 uint32_t dest_addr; 114 rc = inet_addr_pack(&packet->dest, &dest_addr); 115 if (rc != EOK) 116 return rc; 108 addr32_t src_v4; 109 addr128_t src_v6; 110 uint16_t src_af = inet_addr_get(&packet->src, &src_v4, &src_v6); 111 112 addr32_t dest_v4; 113 addr128_t dest_v6; 114 uint16_t dest_af = inet_addr_get(&packet->dest, &dest_v4, &dest_v6); 115 116 if (src_af != dest_af) 117 return EINVAL; 117 118 118 119 /* Upper bound for fragment offset field */ … … 123 124 return ELIMIT; 124 125 125 size_t hdr_size = sizeof(ip_header_t); 126 size_t hdr_size; 127 128 switch (src_af) { 129 case AF_INET: 130 hdr_size = sizeof(ip_header_t); 131 break; 132 case AF_INET6: 133 // FIXME TODO 134 assert(false); 135 default: 136 assert(false); 137 } 138 126 139 size_t data_offs = ROUND_UP(hdr_size, 4); 127 140 … … 164 177 165 178 /* Encode header fields */ 166 ip_header_t *hdr = (ip_header_t *) data; 167 168 hdr->ver_ihl = (4 << VI_VERSION_l) | (hdr_size / sizeof(uint32_t)); 169 hdr->tos = packet->tos; 170 hdr->tot_len = host2uint16_t_be(size); 171 hdr->id = host2uint16_t_be(ident); 172 hdr->flags_foff = host2uint16_t_be(flags_foff); 173 hdr->ttl = packet->ttl; 174 hdr->proto = packet->proto; 175 hdr->chksum = 0; 176 hdr->src_addr = host2uint32_t_be(src_addr); 177 hdr->dest_addr = host2uint32_t_be(dest_addr); 178 179 /* Compute checksum */ 180 uint16_t chksum = inet_checksum_calc(INET_CHECKSUM_INIT, (void *) hdr, 181 hdr_size); 182 hdr->chksum = host2uint16_t_be(chksum); 179 ip_header_t *hdr; 180 181 switch (src_af) { 182 case AF_INET: 183 hdr = (ip_header_t *) data; 184 185 hdr->ver_ihl = 186 (4 << VI_VERSION_l) | (hdr_size / sizeof(uint32_t)); 187 hdr->tos = packet->tos; 188 hdr->tot_len = host2uint16_t_be(size); 189 hdr->id = host2uint16_t_be(ident); 190 hdr->flags_foff = host2uint16_t_be(flags_foff); 191 hdr->ttl = packet->ttl; 192 hdr->proto = packet->proto; 193 hdr->chksum = 0; 194 hdr->src_addr = host2uint32_t_be(src_v4); 195 hdr->dest_addr = host2uint32_t_be(dest_v4); 196 197 /* Compute checksum */ 198 uint16_t chksum = inet_checksum_calc(INET_CHECKSUM_INIT, 199 (void *) hdr, hdr_size); 200 hdr->chksum = host2uint16_t_be(chksum); 201 202 break; 203 case AF_INET6: 204 // FIXME TODO 205 return ENOTSUP; 206 default: 207 assert(false); 208 } 183 209 184 210 /* Copy payload */ … … 194 220 int inet_pdu_decode(void *data, size_t size, inet_packet_t *packet) 195 221 { 196 ip_header_t *hdr;197 size_t tot_len;198 size_t data_offs;199 uint8_t version;200 uint16_t ident;201 uint16_t flags_foff;202 uint16_t foff;203 204 222 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_pdu_decode()"); 205 223 … … 208 226 return EINVAL; 209 227 } 210 211 hdr = (ip_header_t *)data;212 213 version = BIT_RANGE_EXTRACT(uint8_t, VI_VERSION_h, VI_VERSION_l,214 hdr->ver_ihl);228 229 ip_header_t *hdr = (ip_header_t *) data; 230 231 uint8_t version = BIT_RANGE_EXTRACT(uint8_t, VI_VERSION_h, 232 VI_VERSION_l, hdr->ver_ihl); 215 233 if (version != 4) { 216 234 log_msg(LOG_DEFAULT, LVL_DEBUG, "Version (%d) != 4", version); 217 235 return EINVAL; 218 236 } 219 220 tot_len = uint16_t_be2host(hdr->tot_len);237 238 size_t tot_len = uint16_t_be2host(hdr->tot_len); 221 239 if (tot_len < sizeof(ip_header_t)) { 222 240 log_msg(LOG_DEFAULT, LVL_DEBUG, "Total Length too small (%zu)", tot_len); … … 229 247 return EINVAL; 230 248 } 231 232 ident = uint16_t_be2host(hdr->id);233 flags_foff = uint16_t_be2host(hdr->flags_foff);234 foff = BIT_RANGE_EXTRACT(uint16_t, FF_FRAGOFF_h, FF_FRAGOFF_l,249 250 uint16_t ident = uint16_t_be2host(hdr->id); 251 uint16_t flags_foff = uint16_t_be2host(hdr->flags_foff); 252 uint16_t foff = BIT_RANGE_EXTRACT(uint16_t, FF_FRAGOFF_h, FF_FRAGOFF_l, 235 253 flags_foff); 236 254 /* XXX Checksum */ 237 238 inet_addr_ unpack(uint32_t_be2host(hdr->src_addr), &packet->src);239 inet_addr_ unpack(uint32_t_be2host(hdr->dest_addr), &packet->dest);255 256 inet_addr_set(uint32_t_be2host(hdr->src_addr), &packet->src); 257 inet_addr_set(uint32_t_be2host(hdr->dest_addr), &packet->dest); 240 258 packet->tos = hdr->tos; 241 259 packet->proto = hdr->proto; … … 248 266 249 267 /* XXX IP options */ 250 data_offs = sizeof(uint32_t) * BIT_RANGE_EXTRACT(uint8_t, VI_IHL_h,251 VI_IHL_l, hdr->ver_ihl);252 268 size_t data_offs = sizeof(uint32_t) * 269 BIT_RANGE_EXTRACT(uint8_t, VI_IHL_h, VI_IHL_l, hdr->ver_ihl); 270 253 271 packet->size = tot_len - data_offs; 254 272 packet->data = calloc(packet->size, 1); -
uspace/srv/net/inetsrv/sroute.c
redf0d27 r02a09ed 42 42 #include <stdlib.h> 43 43 #include <str.h> 44 45 44 #include "sroute.h" 46 45 #include "inetsrv.h" 47 46 #include "inet_link.h" 48 #include "inet_util.h"49 47 50 48 static FIBRIL_MUTEX_INITIALIZE(sroute_list_lock); … … 97 95 inet_sroute_t *inet_sroute_find(inet_addr_t *addr) 98 96 { 99 uint32_t addr_addr; 100 int rc = inet_addr_pack(addr, &addr_addr); 101 if (rc != EOK) 102 return NULL; 97 uint16_t addr_af = inet_addr_get(addr, NULL, NULL); 103 98 104 99 inet_sroute_t *best = NULL; … … 111 106 inet_sroute_t, sroute_list); 112 107 113 uint32_t dest_addr;114 108 uint8_t dest_bits; 115 rc = inet_naddr_pack(&sroute->dest, &dest_addr, &dest_bits); 116 if (rc != EOK) 109 uint16_t dest_af = inet_naddr_get(&sroute->dest, NULL, NULL, 110 &dest_bits); 111 112 /* Skip comparison with different address family */ 113 if (addr_af != dest_af) 117 114 continue; 118 115 … … 121 118 continue; 122 119 123 uint32_t mask = inet_netmask(dest_bits); 124 if ((dest_addr & mask) == (addr_addr & mask)) { 120 if (inet_naddr_compare_mask(&sroute->dest, addr)) { 125 121 log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_sroute_find: found candidate %p", 126 122 sroute); -
uspace/srv/net/loopip/loopip.c
redf0d27 r02a09ed 39 39 #include <errno.h> 40 40 #include <inet/iplink_srv.h> 41 #include <inet/addr.h> 41 42 #include <io/log.h> 42 43 #include <loc.h> … … 48 49 static int loopip_open(iplink_srv_t *srv); 49 50 static int loopip_close(iplink_srv_t *srv); 50 static int loopip_send(iplink_srv_t *srv, iplink_s rv_sdu_t *sdu);51 static int loopip_send(iplink_srv_t *srv, iplink_sdu_t *sdu); 51 52 static int loopip_get_mtu(iplink_srv_t *srv, size_t *mtu); 52 static int loopip_addr_add(iplink_srv_t *srv, uint32_taddr);53 static int loopip_addr_remove(iplink_srv_t *srv, uint32_taddr);53 static int loopip_addr_add(iplink_srv_t *srv, inet_addr_t *addr); 54 static int loopip_addr_remove(iplink_srv_t *srv, inet_addr_t *addr); 54 55 55 56 static void loopip_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg); … … 69 70 typedef struct { 70 71 link_t link; 71 iplink_srv_sdu_t sdu; 72 73 uint16_t af; 74 iplink_recv_sdu_t sdu; 72 75 } rqueue_entry_t; 73 76 … … 77 80 log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip_recv_fibril(): Wait for one item"); 78 81 link_t *link = prodcons_consume(&loopip_rcv_queue); 79 rqueue_entry_t *rqe = list_get_instance(link, rqueue_entry_t, link); 80 81 (void) iplink_ev_recv(&loopip_iplink, &rqe->sdu); 82 rqueue_entry_t *rqe = 83 list_get_instance(link, rqueue_entry_t, link); 84 85 (void) iplink_ev_recv(&loopip_iplink, &rqe->sdu, rqe->af); 86 87 free(rqe->sdu.data); 88 free(rqe); 82 89 } 83 90 … … 87 94 static int loopip_init(void) 88 95 { 89 int rc;90 service_id_t sid;91 category_id_t iplink_cat;92 const char *svc_name = "net/loopback";93 94 96 async_set_client_connection(loopip_client_conn); 95 97 96 rc = loc_server_register(NAME);98 int rc = loc_server_register(NAME); 97 99 if (rc != EOK) { 98 100 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server."); … … 105 107 106 108 prodcons_initialize(&loopip_rcv_queue); 107 109 110 const char *svc_name = "net/loopback"; 111 service_id_t sid; 108 112 rc = loc_service_register(svc_name, &sid); 109 113 if (rc != EOK) { 110 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service %s.", svc_name); 111 return rc; 112 } 113 114 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service %s.", 115 svc_name); 116 return rc; 117 } 118 119 category_id_t iplink_cat; 114 120 rc = loc_category_get_id("iplink", &iplink_cat, IPC_FLAG_BLOCKING); 115 121 if (rc != EOK) { … … 120 126 rc = loc_service_add_to_cat(sid, iplink_cat); 121 127 if (rc != EOK) { 122 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding %s to category.", svc_name); 128 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed adding %s to category.", 129 svc_name); 123 130 return rc; 124 131 } … … 151 158 } 152 159 153 static int loopip_send(iplink_srv_t *srv, iplink_srv_sdu_t *sdu) 154 { 155 rqueue_entry_t *rqe; 156 160 static int loopip_send(iplink_srv_t *srv, iplink_sdu_t *sdu) 161 { 157 162 log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip_send()"); 158 159 rqe = calloc(1, sizeof(rqueue_entry_t)); 163 164 addr32_t src_v4; 165 addr128_t src_v6; 166 uint16_t src_af = inet_addr_get(&sdu->src, &src_v4, &src_v6); 167 168 addr32_t dest_v4; 169 addr128_t dest_v6; 170 uint16_t dest_af = inet_addr_get(&sdu->dest, &dest_v4, &dest_v6); 171 172 if (src_af != dest_af) 173 return EINVAL; 174 175 rqueue_entry_t *rqe = calloc(1, sizeof(rqueue_entry_t)); 160 176 if (rqe == NULL) 161 177 return ENOMEM; … … 164 180 * Clone SDU 165 181 */ 166 rqe->sdu.lsrc = sdu->ldest; 167 rqe->sdu.ldest = sdu->lsrc; 182 rqe->af = src_af; 168 183 rqe->sdu.data = malloc(sdu->size); 169 184 if (rqe->sdu.data == NULL) { … … 190 205 } 191 206 192 static int loopip_addr_add(iplink_srv_t *srv, uint32_t addr) 193 { 194 log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip_addr_add(0x%" PRIx32 ")", addr); 195 return EOK; 196 } 197 198 static int loopip_addr_remove(iplink_srv_t *srv, uint32_t addr) 199 { 200 log_msg(LOG_DEFAULT, LVL_DEBUG, "loopip_addr_remove(0x%" PRIx32 ")", addr); 207 static int loopip_addr_add(iplink_srv_t *srv, inet_addr_t *addr) 208 { 209 return EOK; 210 } 211 212 static int loopip_addr_remove(iplink_srv_t *srv, inet_addr_t *addr) 213 { 201 214 return EOK; 202 215 } … … 204 217 int main(int argc, char *argv[]) 205 218 { 206 int rc; 207 208 printf(NAME ": HelenOS loopback IP link provider\n"); 209 210 if (log_init(NAME) != EOK) { 211 printf(NAME ": Failed to initialize logging.\n"); 212 return 1; 219 printf("%s: HelenOS loopback IP link provider\n", NAME); 220 221 int rc = log_init(NAME); 222 if (rc != EOK) { 223 printf("%s: Failed to initialize logging.\n", NAME); 224 return rc; 213 225 } 214 226 215 227 rc = loopip_init(); 216 228 if (rc != EOK) 217 return 1;218 219 printf( NAME ": Accepting connections.\n");229 return rc; 230 231 printf("%s: Accepting connections.\n", NAME); 220 232 task_retval(0); 221 233 async_manager(); -
uspace/srv/net/slip/slip.c
redf0d27 r02a09ed 38 38 #include <stdint.h> 39 39 #include <loc.h> 40 #include <net/socket_codes.h> 41 #include <inet/addr.h> 40 42 #include <inet/iplink_srv.h> 41 43 #include <device/char_dev.h> … … 50 52 #define SLIP_END 0300 51 53 #define SLIP_ESC 0333 52 #define 54 #define SLIP_ESC_END 0334 53 55 #define SLIP_ESC_ESC 0335 54 56 55 57 static int slip_open(iplink_srv_t *); 56 58 static int slip_close(iplink_srv_t *); 57 static int slip_send(iplink_srv_t *, iplink_s rv_sdu_t *);59 static int slip_send(iplink_srv_t *, iplink_sdu_t *); 58 60 static int slip_get_mtu(iplink_srv_t *, size_t *); 59 static int slip_addr_add(iplink_srv_t *, uint32_t);60 static int slip_addr_remove(iplink_srv_t *, uint32_t);61 static int slip_addr_add(iplink_srv_t *, inet_addr_t *); 62 static int slip_addr_remove(iplink_srv_t *, inet_addr_t *); 61 63 62 64 static iplink_srv_t slip_iplink; … … 118 120 } 119 121 120 int slip_send(iplink_srv_t *srv, iplink_s rv_sdu_t *sdu)122 int slip_send(iplink_srv_t *srv, iplink_sdu_t *sdu) 121 123 { 122 124 async_sess_t *sess = (async_sess_t *) srv->arg; … … 137 139 case SLIP_END: 138 140 write_buffered(sess, SLIP_ESC); 139 write_buffered(sess, SLIP_ESC_END); 141 write_buffered(sess, SLIP_ESC_END); 140 142 break; 141 143 case SLIP_ESC: … … 161 163 } 162 164 163 int slip_addr_add(iplink_srv_t *srv, uint32_taddr)165 int slip_addr_add(iplink_srv_t *srv, inet_addr_t *addr) 164 166 { 165 167 log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_addr_add()"); … … 167 169 } 168 170 169 int slip_addr_remove(iplink_srv_t *srv, uint32_taddr)171 int slip_addr_remove(iplink_srv_t *srv, inet_addr_t *addr) 170 172 { 171 173 log_msg(LOG_DEFAULT, LVL_DEBUG, "slip_addr_remove()"); … … 207 209 async_sess_t *sess = (async_sess_t *) arg; 208 210 static uint8_t recv_final[SLIP_MTU]; 209 iplink_ srv_sdu_t sdu;211 iplink_recv_sdu_t sdu; 210 212 uint8_t ch; 211 213 int rc; 212 214 213 sdu.lsrc = 0;214 sdu.ldest = 0;215 215 sdu.data = recv_final; 216 216 … … 222 222 if (sdu.size == 0) { 223 223 /* 224 225 224 * Discard the empty SLIP datagram. 225 */ 226 226 break; 227 227 } … … 260 260 261 261 pass: 262 rc = iplink_ev_recv(&slip_iplink, &sdu );262 rc = iplink_ev_recv(&slip_iplink, &sdu, AF_INET); 263 263 if (rc != EOK) { 264 264 log_msg(LOG_DEFAULT, LVL_ERROR, -
uspace/srv/net/tcp/pdu.c
redf0d27 r02a09ed 40 40 #include <mem.h> 41 41 #include <stdlib.h> 42 #include <net/socket_codes.h> 42 43 #include "pdu.h" 43 44 #include "segment.h" … … 144 145 } 145 146 146 static void tcp_phdr_setup(tcp_pdu_t *pdu, tcp_phdr_t *phdr) 147 { 148 // FIXME: Check for correctness 149 150 uint32_t src_addr; 151 inet_addr_pack(&pdu->src_addr, &src_addr); 152 153 uint32_t dest_addr; 154 inet_addr_pack(&pdu->dest_addr, &dest_addr); 155 156 phdr->src_addr = host2uint32_t_be(src_addr); 157 phdr->dest_addr = host2uint32_t_be(dest_addr); 158 phdr->zero = 0; 159 phdr->protocol = 6; /* XXX Magic number */ 160 phdr->tcp_length = host2uint16_t_be(pdu->header_size + pdu->text_size); 147 static uint16_t tcp_phdr_setup(tcp_pdu_t *pdu, tcp_phdr_t *phdr) 148 { 149 addr32_t src_v4; 150 addr128_t src_v6; 151 uint16_t src_af = inet_addr_get(&pdu->src, &src_v4, &src_v6); 152 153 addr32_t dest_v4; 154 addr128_t dest_v6; 155 uint16_t dest_af = inet_addr_get(&pdu->dest, &dest_v4, &dest_v6); 156 157 assert(src_af == dest_af); 158 159 switch (src_af) { 160 case AF_INET: 161 phdr->src = host2uint32_t_be(src_v4); 162 phdr->dest = host2uint32_t_be(dest_v4); 163 phdr->zero = 0; 164 phdr->protocol = IP_PROTO_TCP; 165 phdr->tcp_length = 166 host2uint16_t_be(pdu->header_size + pdu->text_size); 167 break; 168 case AF_INET6: 169 // FIXME TODO 170 assert(false); 171 default: 172 assert(false); 173 } 174 175 return src_af; 161 176 } 162 177 … … 243 258 uint16_t cs_phdr; 244 259 uint16_t cs_headers; 245 uint16_t cs_all;246 260 tcp_phdr_t phdr; 247 248 tcp_phdr_setup(pdu, &phdr); 249 cs_phdr = tcp_checksum_calc(TCP_CHECKSUM_INIT, (void *)&phdr, 250 sizeof(tcp_phdr_t)); 261 262 uint16_t af = tcp_phdr_setup(pdu, &phdr); 263 switch (af) { 264 case AF_INET: 265 cs_phdr = tcp_checksum_calc(TCP_CHECKSUM_INIT, (void *) &phdr, 266 sizeof(tcp_phdr_t)); 267 break; 268 case AF_INET6: 269 // FIXME TODO 270 assert(false); 271 default: 272 assert(false); 273 } 274 251 275 cs_headers = tcp_checksum_calc(cs_phdr, pdu->header, pdu->header_size); 252 cs_all = tcp_checksum_calc(cs_headers, pdu->text, pdu->text_size); 253 254 return cs_all; 276 return tcp_checksum_calc(cs_headers, pdu->text, pdu->text_size); 255 277 } 256 278 … … 279 301 280 302 sp->local.port = uint16_t_be2host(hdr->dest_port); 281 sp->local.addr = pdu->dest _addr;303 sp->local.addr = pdu->dest; 282 304 sp->foreign.port = uint16_t_be2host(hdr->src_port); 283 sp->foreign.addr = pdu->src _addr;305 sp->foreign.addr = pdu->src; 284 306 285 307 *seg = nseg; … … 298 320 return ENOMEM; 299 321 300 npdu->src _addr= sp->local.addr;301 npdu->dest _addr= sp->foreign.addr;322 npdu->src = sp->local.addr; 323 npdu->dest = sp->foreign.addr; 302 324 tcp_header_encode(sp, seg, &npdu->header, &npdu->header_size); 303 325 -
uspace/srv/net/tcp/sock.c
redf0d27 r02a09ed 354 354 } 355 355 356 static void tcp_sock_connect(tcp_client_t *client, ipc_callid_t callid, ipc_call_t call) 357 { 358 int rc; 359 struct sockaddr_in *addr; 360 int socket_id; 356 static void tcp_sock_connect(tcp_client_t *client, ipc_callid_t callid, 357 ipc_call_t call) 358 { 359 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_connect()"); 360 361 struct sockaddr_in6 *addr6 = NULL; 361 362 size_t addr_len; 362 socket_core_t *sock_core; 363 tcp_sockdata_t *socket; 364 tcp_error_t trc; 365 tcp_sock_t lsocket; 366 tcp_sock_t fsocket; 367 368 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_connect()"); 369 370 rc = async_data_write_accept((void **) &addr, false, 0, 0, 0, &addr_len); 371 if (rc != EOK || addr_len != sizeof(struct sockaddr_in)) { 363 int rc = async_data_write_accept((void **) &addr6, false, 0, 0, 0, &addr_len); 364 if (rc != EOK) { 372 365 async_answer_0(callid, rc); 373 366 return; 374 367 } 375 376 socket_id = SOCKET_GET_SOCKET_ID(call); 377 378 sock_core = socket_cores_find(&client->sockets, socket_id); 368 369 if ((addr_len != sizeof(struct sockaddr_in)) && 370 (addr_len != sizeof(struct sockaddr_in6))) { 371 async_answer_0(callid, EINVAL); 372 goto out; 373 } 374 375 struct sockaddr_in *addr = (struct sockaddr_in *) addr6; 376 377 int socket_id = SOCKET_GET_SOCKET_ID(call); 378 socket_core_t *sock_core = socket_cores_find(&client->sockets, 379 socket_id); 379 380 if (sock_core == NULL) { 380 381 async_answer_0(callid, ENOTSOCK); 381 return; 382 } 383 384 socket = (tcp_sockdata_t *)sock_core->specific_data; 382 goto out; 383 } 384 385 tcp_sockdata_t *socket = 386 (tcp_sockdata_t *) sock_core->specific_data; 387 385 388 if (sock_core->port <= 0) { 386 389 rc = socket_bind_free_port(&gsock, sock_core, … … 389 392 if (rc != EOK) { 390 393 async_answer_0(callid, rc); 391 return;394 goto out; 392 395 } 393 396 394 397 last_used_port = sock_core->port; 395 398 } 396 399 397 400 fibril_mutex_lock(&socket->lock); 398 401 399 402 if (inet_addr_is_any(&socket->laddr)) { 400 403 /* Determine local IP address */ … … 402 405 inet_addr_t rem_addr; 403 406 404 inet_addr_unpack(uint32_t_be2host(addr->sin_addr.s_addr), 405 &rem_addr); 407 switch (addr->sin_family) { 408 case AF_INET: 409 inet_sockaddr_in_addr(addr, &rem_addr); 410 break; 411 case AF_INET6: 412 inet_sockaddr_in6_addr(addr6, &rem_addr); 413 break; 414 default: 415 fibril_mutex_unlock(&socket->lock); 416 async_answer_0(callid, EINVAL); 417 goto out; 418 } 419 406 420 rc = inet_get_srcaddr(&rem_addr, 0, &loc_addr); 407 421 if (rc != EOK) { … … 410 424 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_connect: Failed to " 411 425 "determine local address."); 412 return;426 goto out; 413 427 } 414 428 … … 416 430 } 417 431 432 tcp_sock_t lsocket; 433 tcp_sock_t fsocket; 434 418 435 lsocket.addr = socket->laddr; 419 436 lsocket.port = sock_core->port; 420 437 421 inet_addr_unpack(uint32_t_be2host(addr->sin_addr.s_addr), 422 &fsocket.addr); 438 switch (addr->sin_family) { 439 case AF_INET: 440 inet_sockaddr_in_addr(addr, &fsocket.addr); 441 break; 442 case AF_INET6: 443 inet_sockaddr_in6_addr(addr6, &fsocket.addr); 444 break; 445 default: 446 fibril_mutex_unlock(&socket->lock); 447 async_answer_0(callid, EINVAL); 448 goto out; 449 } 450 423 451 fsocket.port = uint16_t_be2host(addr->sin_port); 424 425 trc = tcp_uc_open(&lsocket, &fsocket, ap_active, 0, &socket->conn); 426 452 453 tcp_error_t trc = tcp_uc_open(&lsocket, &fsocket, ap_active, 0, 454 &socket->conn); 455 427 456 if (socket->conn != NULL) 428 socket->conn->name = (char *) "C";429 457 socket->conn->name = (char *) "C"; 458 430 459 fibril_mutex_unlock(&socket->lock); 431 460 … … 445 474 446 475 async_answer_0(callid, rc); 476 477 out: 478 if (addr6 != NULL) 479 free(addr6); 447 480 } 448 481 … … 662 695 static void tcp_sock_recvfrom(tcp_client_t *client, ipc_callid_t callid, ipc_call_t call) 663 696 { 664 int socket_id;665 int flags;666 size_t addr_length, length;667 socket_core_t *sock_core;668 tcp_sockdata_t *socket;669 ipc_call_t answer;670 ipc_callid_t rcallid;671 size_t data_len;672 struct sockaddr_in addr;673 tcp_sock_t *rsock;674 int rc;675 676 697 log_msg(LOG_DEFAULT, LVL_DEBUG, "%p: tcp_sock_recv[from]()", client); 677 678 socket_id = SOCKET_GET_SOCKET_ID(call);679 flags = SOCKET_GET_FLAGS(call);680 681 sock_core =socket_cores_find(&client->sockets, socket_id);698 699 int socket_id = SOCKET_GET_SOCKET_ID(call); 700 701 socket_core_t *sock_core = 702 socket_cores_find(&client->sockets, socket_id); 682 703 if (sock_core == NULL) { 683 704 async_answer_0(callid, ENOTSOCK); 684 705 return; 685 706 } 686 687 socket = (tcp_sockdata_t *)sock_core->specific_data; 707 708 tcp_sockdata_t *socket = 709 (tcp_sockdata_t *) sock_core->specific_data; 710 688 711 fibril_mutex_lock(&socket->lock); 689 712 … … 693 716 return; 694 717 } 695 696 (void)flags; 697 718 698 719 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_recvfrom(): lock recv_buffer_lock"); 720 699 721 fibril_mutex_lock(&socket->recv_buffer_lock); 700 while (socket->recv_buffer_used == 0 && socket->recv_error == TCP_EOK) { 722 while ((socket->recv_buffer_used == 0) && 723 (socket->recv_error == TCP_EOK)) { 701 724 log_msg(LOG_DEFAULT, LVL_DEBUG, "wait for recv_buffer_cv + recv_buffer_used != 0"); 702 725 fibril_condvar_wait(&socket->recv_buffer_cv, … … 705 728 706 729 log_msg(LOG_DEFAULT, LVL_DEBUG, "Got data in sock recv_buffer"); 707 708 data_len = socket->recv_buffer_used; 709 rc = socket->recv_error; 710 711 switch (socket->recv_error) { 730 731 size_t data_len = socket->recv_buffer_used; 732 tcp_error_t trc = socket->recv_error; 733 int rc; 734 735 switch (trc) { 712 736 case TCP_EOK: 713 737 rc = EOK; … … 732 756 return; 733 757 } 734 758 759 ipc_callid_t rcallid; 760 735 761 if (IPC_GET_IMETHOD(call) == NET_SOCKET_RECVFROM) { 736 762 /* Fill address */ 737 rsock = &socket->conn->ident.foreign; 763 tcp_sock_t *rsock = &socket->conn->ident.foreign; 764 struct sockaddr_in addr; 765 struct sockaddr_in6 addr6; 766 size_t addr_length; 738 767 739 uint32_t rsock_addr; 740 int rc = inet_addr_pack(&rsock->addr, &rsock_addr); 741 if (rc != EOK) { 742 fibril_mutex_unlock(&socket->recv_buffer_lock); 743 fibril_mutex_unlock(&socket->lock); 744 async_answer_0(callid, rc); 745 return; 746 } 768 uint16_t addr_af = inet_addr_sockaddr_in(&rsock->addr, &addr, 769 &addr6); 747 770 748 addr.sin_family = AF_INET; 749 addr.sin_addr.s_addr = host2uint32_t_be(rsock_addr); 750 addr.sin_port = host2uint16_t_be(rsock->port); 751 752 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read receive"); 753 if (!async_data_read_receive(&rcallid, &addr_length)) { 771 switch (addr_af) { 772 case AF_INET: 773 addr.sin_port = host2uint16_t_be(rsock->port); 774 775 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read receive"); 776 if (!async_data_read_receive(&rcallid, &addr_length)) { 777 fibril_mutex_unlock(&socket->recv_buffer_lock); 778 fibril_mutex_unlock(&socket->lock); 779 async_answer_0(callid, EINVAL); 780 return; 781 } 782 783 if (addr_length > sizeof(addr)) 784 addr_length = sizeof(addr); 785 786 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read finalize"); 787 rc = async_data_read_finalize(rcallid, &addr, addr_length); 788 if (rc != EOK) { 789 fibril_mutex_unlock(&socket->recv_buffer_lock); 790 fibril_mutex_unlock(&socket->lock); 791 async_answer_0(callid, EINVAL); 792 return; 793 } 794 795 break; 796 case AF_INET6: 797 addr6.sin6_port = host2uint16_t_be(rsock->port); 798 799 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr6 read receive"); 800 if (!async_data_read_receive(&rcallid, &addr_length)) { 801 fibril_mutex_unlock(&socket->recv_buffer_lock); 802 fibril_mutex_unlock(&socket->lock); 803 async_answer_0(callid, EINVAL); 804 return; 805 } 806 807 if (addr_length > sizeof(addr6)) 808 addr_length = sizeof(addr6); 809 810 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr6 read finalize"); 811 rc = async_data_read_finalize(rcallid, &addr6, addr_length); 812 if (rc != EOK) { 813 fibril_mutex_unlock(&socket->recv_buffer_lock); 814 fibril_mutex_unlock(&socket->lock); 815 async_answer_0(callid, EINVAL); 816 return; 817 } 818 819 break; 820 default: 754 821 fibril_mutex_unlock(&socket->recv_buffer_lock); 755 822 fibril_mutex_unlock(&socket->lock); … … 757 824 return; 758 825 } 759 760 if (addr_length > sizeof(addr))761 addr_length = sizeof(addr);762 763 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read finalize");764 rc = async_data_read_finalize(rcallid, &addr, addr_length);765 if (rc != EOK) {766 fibril_mutex_unlock(&socket->recv_buffer_lock);767 fibril_mutex_unlock(&socket->lock);768 async_answer_0(callid, EINVAL);769 return;770 }771 826 } 772 827 773 828 log_msg(LOG_DEFAULT, LVL_DEBUG, "data read receive"); 829 830 size_t length; 774 831 if (!async_data_read_receive(&rcallid, &length)) { 775 832 fibril_mutex_unlock(&socket->recv_buffer_lock); … … 783 840 784 841 log_msg(LOG_DEFAULT, LVL_DEBUG, "data read finalize"); 842 785 843 rc = async_data_read_finalize(rcallid, socket->recv_buffer, length); 786 844 787 845 socket->recv_buffer_used -= length; 846 788 847 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_recvfrom: %zu left in buffer", 789 848 socket->recv_buffer_used); 849 790 850 if (socket->recv_buffer_used > 0) { 791 851 memmove(socket->recv_buffer, socket->recv_buffer + length, … … 795 855 796 856 fibril_condvar_broadcast(&socket->recv_buffer_cv); 797 798 if ( length < data_len && rc == EOK)857 858 if ((length < data_len) && (rc == EOK)) 799 859 rc = EOVERFLOW; 800 860 861 ipc_call_t answer; 862 801 863 SOCKET_SET_READ_DATA_LENGTH(answer, length); 802 864 async_answer_1(callid, EOK, IPC_GET_ARG1(answer)); -
uspace/srv/net/tcp/std.h
redf0d27 r02a09ed 39 39 40 40 #include <sys/types.h> 41 #include <inet/addr.h> 42 43 #define IP_PROTO_TCP 6 41 44 42 45 /** TCP Header (fixed part) */ … … 75 78 typedef struct { 76 79 /** Source address */ 77 uint32_t src _addr;80 uint32_t src; 78 81 /** Destination address */ 79 uint32_t dest _addr;82 uint32_t dest; 80 83 /** Zero */ 81 84 uint8_t zero; -
uspace/srv/net/tcp/tcp.c
redf0d27 r02a09ed 115 115 } 116 116 117 pdu->src _addr= dgram->src;118 pdu->dest _addr= dgram->dest;117 pdu->src = dgram->src; 118 pdu->dest = dgram->dest; 119 119 120 120 tcp_received_pdu(pdu); … … 143 143 pdu->text_size); 144 144 145 dgram.src = pdu->src _addr;146 dgram.dest = pdu->dest _addr;145 dgram.src = pdu->src; 146 dgram.dest = pdu->dest; 147 147 dgram.tos = 0; 148 148 dgram.data = pdu_raw; -
uspace/srv/net/tcp/tcp_type.h
redf0d27 r02a09ed 306 306 typedef struct { 307 307 /** Source address */ 308 inet_addr_t src _addr;308 inet_addr_t src; 309 309 /** Destination address */ 310 inet_addr_t dest_addr; 311 310 inet_addr_t dest; 312 311 /** Encoded header */ 313 312 void *header; -
uspace/srv/net/udp/pdu.c
redf0d27 r02a09ed 40 40 #include <mem.h> 41 41 #include <stdlib.h> 42 42 #include <inet/addr.h> 43 #include <net/socket_codes.h> 43 44 #include "msg.h" 44 45 #include "pdu.h" … … 84 85 } 85 86 86 static void udp_phdr_setup(udp_pdu_t *pdu, udp_phdr_t *phdr) 87 { 88 // FIXME: Check for correctness 89 90 uint32_t src; 91 inet_addr_pack(&pdu->src, &src); 92 93 uint32_t dest; 94 inet_addr_pack(&pdu->dest, &dest); 95 96 phdr->src_addr = host2uint32_t_be(src); 97 phdr->dest_addr = host2uint32_t_be(dest); 98 phdr->zero = 0; 99 phdr->protocol = IP_PROTO_UDP; 100 phdr->udp_length = host2uint16_t_be(pdu->data_size); 87 static uint16_t udp_phdr_setup(udp_pdu_t *pdu, udp_phdr_t *phdr) 88 { 89 addr32_t src_v4; 90 addr128_t src_v6; 91 uint16_t src_af = inet_addr_get(&pdu->src, &src_v4, &src_v6); 92 93 addr32_t dest_v4; 94 addr128_t dest_v6; 95 uint16_t dest_af = inet_addr_get(&pdu->dest, &dest_v4, &dest_v6); 96 97 assert(src_af == dest_af); 98 99 switch (src_af) { 100 case AF_INET: 101 phdr->src_addr = host2uint32_t_be(src_v4); 102 phdr->dest_addr = host2uint32_t_be(dest_v4); 103 phdr->zero = 0; 104 phdr->protocol = IP_PROTO_UDP; 105 phdr->udp_length = host2uint16_t_be(pdu->data_size); 106 break; 107 case AF_INET6: 108 // FIXME TODO 109 assert(false); 110 default: 111 assert(false); 112 } 113 114 return src_af; 101 115 } 102 116 … … 115 129 { 116 130 uint16_t cs_phdr; 117 uint16_t cs_all;118 131 udp_phdr_t phdr; 119 120 udp_phdr_setup(pdu, &phdr); 121 cs_phdr = udp_checksum_calc(UDP_CHECKSUM_INIT, (void *)&phdr, 122 sizeof(udp_phdr_t)); 123 cs_all = udp_checksum_calc(cs_phdr, pdu->data, pdu->data_size); 124 125 return cs_all; 132 133 uint16_t af = udp_phdr_setup(pdu, &phdr); 134 switch (af) { 135 case AF_INET: 136 cs_phdr = udp_checksum_calc(UDP_CHECKSUM_INIT, (void *) &phdr, 137 sizeof(udp_phdr_t)); 138 break; 139 case AF_INET6: 140 // FIXME TODO 141 assert(false); 142 default: 143 assert(false); 144 } 145 146 return udp_checksum_calc(cs_phdr, pdu->data, pdu->data_size); 126 147 } 127 148 -
uspace/srv/net/udp/sock.c
redf0d27 r02a09ed 159 159 static void udp_sock_bind(udp_client_t *client, ipc_callid_t callid, ipc_call_t call) 160 160 { 161 int rc;162 struct sockaddr_in *addr;163 size_t addr_size;164 socket_core_t *sock_core;165 udp_sockdata_t *socket;166 udp_sock_t fsock;167 udp_error_t urc;168 169 161 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_bind()"); 170 162 log_msg(LOG_DEFAULT, LVL_DEBUG, " - async_data_write_accept"); 171 172 addr= NULL;173 174 rc = async_data_write_accept((void **) &addr, false, 0, 0, 0, &addr_size);163 164 struct sockaddr_in6 *addr6 = NULL; 165 size_t addr_len; 166 int rc = async_data_write_accept((void **) &addr6, false, 0, 0, 0, &addr_len); 175 167 if (rc != EOK) { 176 168 async_answer_0(callid, rc); 177 goto out; 178 } 179 180 if (addr_size != sizeof(struct sockaddr_in)) { 169 return; 170 } 171 172 if ((addr_len != sizeof(struct sockaddr_in)) && 173 (addr_len != sizeof(struct sockaddr_in6))) { 181 174 async_answer_0(callid, EINVAL); 182 175 goto out; 183 176 } 184 177 178 struct sockaddr_in *addr = (struct sockaddr_in *) addr6; 179 185 180 log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_bind"); 181 186 182 rc = socket_bind(&client->sockets, &gsock, SOCKET_GET_SOCKET_ID(call), 187 addr , addr_size, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,183 addr6, addr_len, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, 188 184 last_used_port); 189 185 if (rc != EOK) { … … 193 189 194 190 log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_cores_find"); 195 sock_core = socket_cores_find(&client->sockets, SOCKET_GET_SOCKET_ID(call)); 191 192 socket_core_t *sock_core = socket_cores_find(&client->sockets, 193 SOCKET_GET_SOCKET_ID(call)); 196 194 if (sock_core == NULL) { 197 195 async_answer_0(callid, ENOENT); 198 196 goto out; 199 197 } 200 201 socket = (udp_sockdata_t *) sock_core->specific_data; 202 203 inet_addr_unpack(uint32_t_be2host(addr->sin_addr.s_addr), 204 &fsock.addr); 205 fsock.port = sock_core->port; 206 urc = udp_uc_set_local(socket->assoc, &fsock); 207 198 199 udp_sockdata_t *socket = 200 (udp_sockdata_t *) sock_core->specific_data; 201 202 udp_sock_t fsocket; 203 204 fsocket.port = sock_core->port; 205 206 switch (addr->sin_family) { 207 case AF_INET: 208 inet_sockaddr_in_addr(addr, &fsocket.addr); 209 break; 210 case AF_INET6: 211 inet_sockaddr_in6_addr(addr6, &fsocket.addr); 212 break; 213 default: 214 async_answer_0(callid, EINVAL); 215 goto out; 216 } 217 218 udp_error_t urc = udp_uc_set_local(socket->assoc, &fsocket); 219 208 220 switch (urc) { 209 221 case UDP_EOK: … … 225 237 log_msg(LOG_DEFAULT, LVL_DEBUG, " - success"); 226 238 async_answer_0(callid, rc); 239 227 240 out: 228 if (addr != NULL)229 free(addr );241 if (addr6 != NULL) 242 free(addr6); 230 243 } 231 244 … … 252 265 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_send()"); 253 266 254 struct sockaddr_in *addr = NULL; 255 udp_sock_t fsock; 256 udp_sock_t *fsock_ptr; 267 struct sockaddr_in6 *addr6 = NULL; 268 struct sockaddr_in *addr; 269 udp_sock_t fsocket; 270 udp_sock_t *fsocket_ptr; 257 271 258 272 if (IPC_GET_IMETHOD(call) == NET_SOCKET_SENDTO) { 259 size_t addr_ size;260 int rc = async_data_write_accept((void **) &addr , false,261 0, 0, 0, &addr_ size);273 size_t addr_len; 274 int rc = async_data_write_accept((void **) &addr6, false, 275 0, 0, 0, &addr_len); 262 276 if (rc != EOK) { 263 277 async_answer_0(callid, rc); 264 goto out; 265 } 266 267 if (addr_size != sizeof(struct sockaddr_in)) { 278 return; 279 } 280 281 if ((addr_len != sizeof(struct sockaddr_in)) && 282 (addr_len != sizeof(struct sockaddr_in6))) { 268 283 async_answer_0(callid, EINVAL); 269 284 goto out; 270 285 } 271 286 272 inet_addr_unpack(uint32_t_be2host(addr->sin_addr.s_addr), 273 &fsock.addr); 274 fsock.port = uint16_t_be2host(addr->sin_port); 275 fsock_ptr = &fsock; 287 addr = (struct sockaddr_in *) addr6; 288 289 switch (addr->sin_family) { 290 case AF_INET: 291 inet_sockaddr_in_addr(addr, &fsocket.addr); 292 break; 293 case AF_INET6: 294 inet_sockaddr_in6_addr(addr6, &fsocket.addr); 295 break; 296 default: 297 async_answer_0(callid, EINVAL); 298 goto out; 299 } 300 301 fsocket.port = uint16_t_be2host(addr->sin_port); 302 fsocket_ptr = &fsocket; 276 303 } else 277 fsock _ptr = NULL;304 fsocket_ptr = NULL; 278 305 279 306 int socket_id = SOCKET_GET_SOCKET_ID(call); … … 321 348 inet_addr_t rem_addr; 322 349 323 rem_addr = fsock _ptr ? fsock.addr :350 rem_addr = fsocket_ptr ? fsocket.addr : 324 351 socket->assoc->ident.foreign.addr; 325 352 … … 361 388 362 389 udp_error_t urc = 363 udp_uc_send(socket->assoc, fsock _ptr, buffer, length, 0);390 udp_uc_send(socket->assoc, fsocket_ptr, buffer, length, 0); 364 391 365 392 switch (urc) { … … 396 423 397 424 out: 398 if (addr != NULL)399 free(addr );425 if (addr6 != NULL) 426 free(addr6); 400 427 } 401 428 402 429 static void udp_sock_recvfrom(udp_client_t *client, ipc_callid_t callid, ipc_call_t call) 403 430 { 404 int socket_id;405 int flags;406 size_t addr_length, length;407 socket_core_t *sock_core;408 udp_sockdata_t *socket;409 ipc_call_t answer;410 ipc_callid_t rcallid;411 size_t data_len;412 udp_error_t urc;413 udp_sock_t *rsock;414 struct sockaddr_in addr;415 int rc;416 417 431 log_msg(LOG_DEFAULT, LVL_DEBUG, "%p: udp_sock_recv[from]()", client); 418 419 socket_id = SOCKET_GET_SOCKET_ID(call);420 flags = SOCKET_GET_FLAGS(call);421 422 sock_core =socket_cores_find(&client->sockets, socket_id);432 433 int socket_id = SOCKET_GET_SOCKET_ID(call); 434 435 socket_core_t *sock_core = 436 socket_cores_find(&client->sockets, socket_id); 423 437 if (sock_core == NULL) { 424 438 async_answer_0(callid, ENOTSOCK); 425 439 return; 426 440 } 427 428 socket = (udp_sockdata_t *)sock_core->specific_data; 441 442 udp_sockdata_t *socket = 443 (udp_sockdata_t *) sock_core->specific_data; 444 429 445 fibril_mutex_lock(&socket->lock); 430 446 … … 434 450 return; 435 451 } 436 437 (void)flags; 438 452 439 453 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recvfrom(): lock recv_buffer lock"); 454 440 455 fibril_mutex_lock(&socket->recv_buffer_lock); 441 while (socket->recv_buffer_used == 0 && socket->recv_error == UDP_EOK) { 456 457 while ((socket->recv_buffer_used == 0) && 458 (socket->recv_error == UDP_EOK)) { 442 459 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recvfrom(): wait for cv"); 443 460 fibril_condvar_wait(&socket->recv_buffer_cv, … … 446 463 447 464 log_msg(LOG_DEFAULT, LVL_DEBUG, "Got data in sock recv_buffer"); 448 449 rsock = &socket->recv_fsock; 450 data_len = socket->recv_buffer_used; 451 urc = socket->recv_error; 452 465 466 size_t data_len = socket->recv_buffer_used; 467 udp_error_t urc = socket->recv_error; 468 453 469 log_msg(LOG_DEFAULT, LVL_DEBUG, "**** recv data_len=%zu", data_len); 454 470 471 int rc; 472 455 473 switch (urc) { 456 474 case UDP_EOK: … … 476 494 return; 477 495 } 478 496 497 ipc_callid_t rcallid; 498 size_t addr_size = 0; 499 479 500 if (IPC_GET_IMETHOD(call) == NET_SOCKET_RECVFROM) { 480 501 /* Fill address */ 481 uint32_t rsock_addr; 482 int rc = inet_addr_pack(&rsock->addr, &rsock_addr); 483 if (rc != EOK) { 484 fibril_mutex_unlock(&socket->recv_buffer_lock); 485 fibril_mutex_unlock(&socket->lock); 486 async_answer_0(callid, rc); 487 return; 488 } 489 490 addr.sin_family = AF_INET; 491 addr.sin_addr.s_addr = host2uint32_t_be(rsock_addr); 492 addr.sin_port = host2uint16_t_be(rsock->port); 493 494 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read receive"); 495 if (!async_data_read_receive(&rcallid, &addr_length)) { 502 udp_sock_t *rsock = &socket->recv_fsock; 503 struct sockaddr_in addr; 504 struct sockaddr_in6 addr6; 505 size_t addr_length; 506 507 uint16_t addr_af = inet_addr_sockaddr_in(&rsock->addr, &addr, 508 &addr6); 509 510 switch (addr_af) { 511 case AF_INET: 512 addr.sin_port = host2uint16_t_be(rsock->port); 513 514 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read receive"); 515 if (!async_data_read_receive(&rcallid, &addr_length)) { 516 fibril_mutex_unlock(&socket->recv_buffer_lock); 517 fibril_mutex_unlock(&socket->lock); 518 async_answer_0(callid, EINVAL); 519 return; 520 } 521 522 if (addr_length > sizeof(addr)) 523 addr_length = sizeof(addr); 524 525 addr_size = sizeof(addr); 526 527 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read finalize"); 528 rc = async_data_read_finalize(rcallid, &addr, addr_length); 529 if (rc != EOK) { 530 fibril_mutex_unlock(&socket->recv_buffer_lock); 531 fibril_mutex_unlock(&socket->lock); 532 async_answer_0(callid, EINVAL); 533 return; 534 } 535 536 break; 537 case AF_INET6: 538 addr6.sin6_port = host2uint16_t_be(rsock->port); 539 540 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr6 read receive"); 541 if (!async_data_read_receive(&rcallid, &addr_length)) { 542 fibril_mutex_unlock(&socket->recv_buffer_lock); 543 fibril_mutex_unlock(&socket->lock); 544 async_answer_0(callid, EINVAL); 545 return; 546 } 547 548 if (addr_length > sizeof(addr6)) 549 addr_length = sizeof(addr6); 550 551 addr_size = sizeof(addr6); 552 553 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr6 read finalize"); 554 rc = async_data_read_finalize(rcallid, &addr6, addr_length); 555 if (rc != EOK) { 556 fibril_mutex_unlock(&socket->recv_buffer_lock); 557 fibril_mutex_unlock(&socket->lock); 558 async_answer_0(callid, EINVAL); 559 return; 560 } 561 562 break; 563 default: 496 564 fibril_mutex_unlock(&socket->recv_buffer_lock); 497 565 fibril_mutex_unlock(&socket->lock); … … 499 567 return; 500 568 } 501 502 if (addr_length > sizeof(addr)) 503 addr_length = sizeof(addr); 504 505 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read finalize"); 506 rc = async_data_read_finalize(rcallid, &addr, addr_length); 507 if (rc != EOK) { 508 fibril_mutex_unlock(&socket->recv_buffer_lock); 509 fibril_mutex_unlock(&socket->lock); 510 async_answer_0(callid, EINVAL); 511 return; 512 } 513 } 514 569 } 570 515 571 log_msg(LOG_DEFAULT, LVL_DEBUG, "data read receive"); 572 573 size_t length; 516 574 if (!async_data_read_receive(&rcallid, &length)) { 517 575 fibril_mutex_unlock(&socket->recv_buffer_lock); … … 525 583 526 584 log_msg(LOG_DEFAULT, LVL_DEBUG, "data read finalize"); 585 527 586 rc = async_data_read_finalize(rcallid, socket->recv_buffer, length); 528 529 if ( length < data_len && rc == EOK)587 588 if ((length < data_len) && (rc == EOK)) 530 589 rc = EOVERFLOW; 531 590 532 591 log_msg(LOG_DEFAULT, LVL_DEBUG, "read_data_length <- %zu", length); 592 593 ipc_call_t answer; 594 533 595 IPC_SET_ARG2(answer, 0); 534 596 SOCKET_SET_READ_DATA_LENGTH(answer, length); 535 SOCKET_SET_ADDRESS_LENGTH(answer, sizeof(addr));597 SOCKET_SET_ADDRESS_LENGTH(answer, addr_size); 536 598 async_answer_3(callid, EOK, IPC_GET_ARG1(answer), 537 599 IPC_GET_ARG2(answer), IPC_GET_ARG3(answer)); … … 634 696 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] urc != UDP_EOK, break"); 635 697 fibril_condvar_broadcast(&sock->recv_buffer_cv); 698 fibril_mutex_unlock(&sock->recv_buffer_lock); 636 699 break; 637 700 }
Note:
See TracChangeset
for help on using the changeset viewer.