Changeset 92b42442 in mainline
- Timestamp:
- 2012-04-05T21:16:00Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 7094e196
- Parents:
- ee603c4
- Location:
- uspace
- Files:
-
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/netecho/netecho.c
ree603c4 r92b42442 373 373 address_in.sin_family = AF_INET; 374 374 address_in.sin_port = htons(port); 375 address_in.sin_addr.s_addr = INADDR_ANY; 375 376 address = (struct sockaddr *) &address_in; 376 377 addrlen = sizeof(address_in); -
uspace/lib/c/include/net/in.h
ree603c4 r92b42442 45 45 #define INET_ADDRSTRLEN (4 * 3 + 3 + 1) 46 46 47 #define INADDR_ANY 0 48 47 49 /** Type definition of the INET address. 48 50 * @see in_addr -
uspace/srv/tcp/conn.c
ree603c4 r92b42442 309 309 } 310 310 311 /** Compare two sockets. 312 * 313 * Two sockets are equal if the address is equal and the port number 314 * is equal. 315 */ 311 /** Match socket with pattern. */ 316 312 static bool tcp_socket_match(tcp_sock_t *sock, tcp_sock_t *patt) 317 313 { … … 332 328 } 333 329 334 /** Match socket with pattern. */330 /** Match socket pair with pattern. */ 335 331 static bool tcp_sockpair_match(tcp_sockpair_t *sp, tcp_sockpair_t *pattern) 336 332 { … … 357 353 tcp_conn_t *tcp_conn_find_ref(tcp_sockpair_t *sp) 358 354 { 359 log_msg(LVL_DEBUG, "tcp_conn_find (%p)", sp);355 log_msg(LVL_DEBUG, "tcp_conn_find_ref(%p)", sp); 360 356 361 357 fibril_mutex_lock(&conn_list_lock); -
uspace/srv/udp/assoc.c
ree603c4 r92b42442 51 51 FIBRIL_MUTEX_INITIALIZE(assoc_list_lock); 52 52 53 static udp_assoc_t *udp_assoc_find_ref(udp_sockpair_t *); 54 static int udp_assoc_queue_msg(udp_assoc_t *, udp_sockpair_t *, udp_msg_t *); 55 static bool udp_socket_match(udp_sock_t *, udp_sock_t *); 56 static bool udp_sockpair_match(udp_sockpair_t *, udp_sockpair_t *); 57 53 58 /** Create new association structure. 54 59 * … … 61 66 udp_assoc_t *assoc = NULL; 62 67 63 /* Allocate connection structure */68 /* Allocate association structure */ 64 69 assoc = calloc(1, sizeof(udp_assoc_t)); 65 70 if (assoc == NULL) … … 95 100 * as an extra reference. 96 101 * 97 * @param conn Connection102 * @param assoc Association 98 103 */ 99 104 static void udp_assoc_free(udp_assoc_t *assoc) … … 103 108 while (!list_empty(&assoc->rcv_queue)) { 104 109 link_t *link = list_first(&assoc->rcv_queue); 105 // ....; 110 udp_rcv_queue_entry_t *rqe = list_get_instance(link, 111 udp_rcv_queue_entry_t, link); 106 112 list_remove(link); 113 114 udp_msg_delete(rqe->msg); 115 free(rqe); 107 116 } 108 117 … … 219 228 int rc; 220 229 230 log_msg(LVL_DEBUG, "udp_assoc_send(%p, %p, %p)", 231 assoc, fsock, msg); 232 221 233 /* @a fsock can be used to override the foreign socket */ 222 234 sp = assoc->ident; … … 240 252 } 241 253 254 /** Get a received message. 255 * 256 * Pull one message from the association's receive queue. 257 */ 258 int udp_assoc_recv(udp_assoc_t *assoc, udp_msg_t **msg, udp_sock_t *fsock) 259 { 260 link_t *link; 261 udp_rcv_queue_entry_t *rqe; 262 263 log_msg(LVL_DEBUG, "udp_assoc_recv()"); 264 265 fibril_mutex_lock(&assoc->lock); 266 while (list_empty(&assoc->rcv_queue)) { 267 log_msg(LVL_DEBUG, "udp_assoc_recv() - waiting"); 268 fibril_condvar_wait(&assoc->rcv_queue_cv, &assoc->lock); 269 } 270 271 log_msg(LVL_DEBUG, "udp_assoc_recv() - got a message"); 272 link = list_first(&assoc->rcv_queue); 273 rqe = list_get_instance(link, udp_rcv_queue_entry_t, link); 274 list_remove(link); 275 fibril_mutex_unlock(&assoc->lock); 276 277 *msg = rqe->msg; 278 *fsock = rqe->sp.foreign; 279 280 return EOK; 281 } 282 283 /** Message received. 284 * 285 * Find the association to which the message belongs and queue it. 286 */ 287 void udp_assoc_received(udp_sockpair_t *rsp, udp_msg_t *msg) 288 { 289 udp_assoc_t *assoc; 290 int rc; 291 292 log_msg(LVL_DEBUG, "udp_assoc_received(%p, %p)", rsp, msg); 293 294 assoc = udp_assoc_find_ref(rsp); 295 if (assoc == NULL) { 296 log_msg(LVL_DEBUG, "No association found. Message dropped."); 297 /* XXX Generate ICMP error. */ 298 /* XXX Might propagate error directly by error return. */ 299 return; 300 } 301 302 rc = udp_assoc_queue_msg(assoc, rsp, msg); 303 if (rc != EOK) { 304 log_msg(LVL_DEBUG, "Out of memory. Message dropped."); 305 /* XXX Generate ICMP error? */ 306 } 307 } 308 309 static int udp_assoc_queue_msg(udp_assoc_t *assoc, udp_sockpair_t *sp, 310 udp_msg_t *msg) 311 { 312 udp_rcv_queue_entry_t *rqe; 313 314 log_msg(LVL_DEBUG, "udp_assoc_queue_msg(%p, %p, %p)", 315 assoc, sp, msg); 316 317 rqe = calloc(1, sizeof(udp_rcv_queue_entry_t)); 318 if (rqe == NULL) 319 return ENOMEM; 320 321 link_initialize(&rqe->link); 322 rqe->sp = *sp; 323 rqe->msg = msg; 324 325 fibril_mutex_lock(&assoc->lock); 326 list_append(&rqe->link, &assoc->rcv_queue); 327 fibril_mutex_unlock(&assoc->lock); 328 329 fibril_condvar_broadcast(&assoc->rcv_queue_cv); 330 331 return EOK; 332 } 333 334 /** Match socket with pattern. */ 335 static bool udp_socket_match(udp_sock_t *sock, udp_sock_t *patt) 336 { 337 log_msg(LVL_DEBUG, "udp_socket_match(sock=(%x,%u), pat=(%x,%u))", 338 sock->addr.ipv4, sock->port, patt->addr.ipv4, patt->port); 339 340 if (patt->addr.ipv4 != UDP_IPV4_ANY && 341 patt->addr.ipv4 != sock->addr.ipv4) 342 return false; 343 344 if (patt->port != UDP_PORT_ANY && 345 patt->port != sock->port) 346 return false; 347 348 log_msg(LVL_DEBUG, " -> match"); 349 350 return true; 351 } 352 353 /** Match socket pair with pattern. */ 354 static bool udp_sockpair_match(udp_sockpair_t *sp, udp_sockpair_t *pattern) 355 { 356 log_msg(LVL_DEBUG, "udp_sockpair_match(%p, %p)", sp, pattern); 357 358 if (!udp_socket_match(&sp->local, &pattern->local)) 359 return false; 360 361 if (!udp_socket_match(&sp->foreign, &pattern->foreign)) 362 return false; 363 364 log_msg(LVL_DEBUG, "Socket pair matched."); 365 return true; 366 } 367 368 369 /** Find association structure for specified socket pair. 370 * 371 * An association is uniquely identified by a socket pair. Look up our 372 * association map and return association structure based on socket pair. 373 * The association reference count is bumped by one. 374 * 375 * @param sp Socket pair 376 * @return Association structure or NULL if not found. 377 */ 378 static udp_assoc_t *udp_assoc_find_ref(udp_sockpair_t *sp) 379 { 380 log_msg(LVL_DEBUG, "udp_assoc_find_ref(%p)", sp); 381 382 fibril_mutex_lock(&assoc_list_lock); 383 384 list_foreach(assoc_list, link) { 385 udp_assoc_t *assoc = list_get_instance(link, udp_assoc_t, link); 386 udp_sockpair_t *asp = &assoc->ident; 387 log_msg(LVL_DEBUG, "compare with assoc (f:(%x,%u), l:(%x,%u))", 388 asp->foreign.addr.ipv4, asp->foreign.port, 389 asp->local.addr.ipv4, asp->local.port); 390 391 /* Skip unbound associations */ 392 if (asp->local.port == UDP_PORT_ANY) 393 continue; 394 395 if (udp_sockpair_match(sp, asp)) { 396 log_msg(LVL_DEBUG, "Returning assoc %p", assoc); 397 udp_assoc_addref(assoc); 398 fibril_mutex_unlock(&assoc_list_lock); 399 return assoc; 400 } 401 } 402 403 fibril_mutex_unlock(&assoc_list_lock); 404 return NULL; 405 } 406 407 242 408 /** 243 409 * @} -
uspace/srv/udp/assoc.h
ree603c4 r92b42442 48 48 extern void udp_assoc_set_local(udp_assoc_t *, udp_sock_t *); 49 49 extern int udp_assoc_send(udp_assoc_t *, udp_sock_t *, udp_msg_t *); 50 extern int udp_assoc_recv(udp_assoc_t *, udp_msg_t **, udp_sock_t *); 51 extern void udp_assoc_received(udp_sockpair_t *, udp_msg_t *); 52 50 53 51 54 #endif -
uspace/srv/udp/pdu.c
ree603c4 r92b42442 93 93 } 94 94 95 staticudp_pdu_t *udp_pdu_new(void)95 udp_pdu_t *udp_pdu_new(void) 96 96 { 97 97 return calloc(1, sizeof(udp_pdu_t)); -
uspace/srv/udp/pdu.h
ree603c4 r92b42442 40 40 #include "udp_type.h" 41 41 42 extern udp_pdu_t *udp_pdu_new(void); 42 43 extern void udp_pdu_delete(udp_pdu_t *); 43 44 extern int udp_pdu_decode(udp_pdu_t *, udp_sockpair_t *, udp_msg_t **); -
uspace/srv/udp/sock.c
ree603c4 r92b42442 114 114 fibril_mutex_initialize(&sock->lock); 115 115 sock->client = client; 116 sock->laddr.ipv4 = UDP_IPV4_ANY;117 116 118 117 rc = udp_uc_create(&sock->assoc); … … 189 188 190 189 fsock.addr.ipv4 = uint32_t_be2host(addr->sin_addr.s_addr); 191 fsock.port = uint16_t_be2host(sock_core->port);190 fsock.port = sock_core->port; 192 191 urc = udp_uc_set_local(socket->assoc, &fsock); 193 192 … … 209 208 } 210 209 210 udp_sock_notify_data(sock_core); 211 211 212 log_msg(LVL_DEBUG, " - success"); 212 213 async_answer_0(callid, rc); … … 286 287 287 288 if (sock_core->port == 0) { 288 /* Implicitly bind socket */289 /* Implicitly bind socket to port */ 289 290 rc = socket_bind(&client->sockets, &gsock, SOCKET_GET_SOCKET_ID(call), 290 291 addr, addr_size, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, … … 294 295 goto out; 295 296 } 297 298 udp_sock_notify_data(sock_core); 296 299 } 297 300 298 301 socket = (udp_sockdata_t *)sock_core->specific_data; 299 302 fibril_mutex_lock(&socket->lock); 303 304 if (socket->assoc->ident.local.addr.ipv4 == UDP_IPV4_ANY) { 305 /* Determine local IP address */ 306 inet_addr_t loc_addr, rem_addr; 307 308 rem_addr.ipv4 = fsockp ? fsock.addr.ipv4 : 309 socket->assoc->ident.foreign.addr.ipv4; 310 311 rc = inet_get_srcaddr(&rem_addr, 0, &loc_addr); 312 if (rc != EOK) { 313 fibril_mutex_unlock(&socket->lock); 314 async_answer_0(callid, rc); 315 log_msg(LVL_DEBUG, "udp_sock_sendto: Failed to " 316 "determine local address."); 317 return; 318 } 319 320 socket->assoc->ident.local.addr.ipv4 = loc_addr.ipv4; 321 log_msg(LVL_DEBUG, "Local IP address is %x", 322 socket->assoc->ident.local.addr.ipv4); 323 } 324 300 325 301 326 assert(socket->assoc != NULL); … … 394 419 urc = udp_uc_receive(socket->assoc, buffer, FRAGMENT_SIZE, &data_len, 395 420 &xflags, &rsock); 396 log_msg(LVL_DEBUG, "**** udp_uc_receive done ");421 log_msg(LVL_DEBUG, "**** udp_uc_receive done, data_len=%zu", data_len); 397 422 398 423 switch (urc) { … … 459 484 rc = EOVERFLOW; 460 485 486 log_msg(LVL_DEBUG, "read_data_length <- %zu", length); 461 487 SOCKET_SET_READ_DATA_LENGTH(answer, length); 462 answer_call(callid, EOK, &answer, 1); 488 SOCKET_SET_ADDRESS_LENGTH(answer, sizeof(addr)); 489 answer_call(callid, EOK, &answer, 3); 463 490 464 491 /* Push one fragment notification to client's queue */ … … 526 553 527 554 while (true) { 555 log_msg(LVL_DEBUG, "udp_sock_connection: wait"); 528 556 callid = async_get_call(&call); 529 557 if (!IPC_GET_IMETHOD(call)) 530 558 break; 531 559 532 log_msg(LVL_DEBUG, "udp_sock_connection: METHOD=%d \n",560 log_msg(LVL_DEBUG, "udp_sock_connection: METHOD=%d", 533 561 (int)IPC_GET_IMETHOD(call)); 534 562 -
uspace/srv/udp/ucall.c
ree603c4 r92b42442 36 36 37 37 #include <io/log.h> 38 #include <macros.h> 38 39 39 40 #include "assoc.h" … … 97 98 size_t *rcvd, xflags_t *xflags, udp_sock_t *fsock) 98 99 { 99 // size_t xfer_size; 100 size_t xfer_size; 101 udp_msg_t *msg; 102 int rc; 100 103 101 104 log_msg(LVL_DEBUG, "%s: udp_uc_receive()", assoc->name); 105 rc = udp_assoc_recv(assoc, &msg, fsock); 106 switch (rc) { 107 } 108 109 xfer_size = min(size, msg->data_size); 110 memcpy(buf, msg->data, xfer_size); 111 *rcvd = xfer_size; 112 102 113 return UDP_EOK; 103 114 } -
uspace/srv/udp/udp_inet.c
ree603c4 r92b42442 43 43 #include <task.h> 44 44 45 #include "assoc.h" 46 #include "pdu.h" 45 47 #include "std.h" 46 48 #include "udp_inet.h" … … 48 50 49 51 static int udp_inet_ev_recv(inet_dgram_t *dgram); 50 //static void tcp_received_pdu(tcp_pdu_t *pdu);52 static void udp_received_pdu(udp_pdu_t *pdu); 51 53 52 54 static inet_ev_ops_t udp_inet_ev_ops = { … … 57 59 static int udp_inet_ev_recv(inet_dgram_t *dgram) 58 60 { 59 uint8_t *pdu_raw; 60 size_t pdu_raw_size; 61 udp_pdu_t *pdu; 61 62 62 63 log_msg(LVL_DEBUG, "udp_inet_ev_recv()"); 63 64 64 pdu_raw = dgram->data; 65 pdu_raw_size = dgram->size; 65 pdu = udp_pdu_new(); 66 pdu->data = dgram->data; 67 pdu->data_size = dgram->size; 66 68 67 (void)pdu_raw; 68 (void)pdu_raw_size; 69 /* Split into header and payload. */ 70 /* 71 log_msg(LVL_DEBUG, "tcp_inet_ev_recv() - split header/payload"); 69 pdu->src.ipv4 = dgram->src.ipv4; 70 pdu->dest.ipv4 = dgram->dest.ipv4; 71 log_msg(LVL_DEBUG, "src: 0x%08x, dest: 0x%08x", 72 pdu->src.ipv4, pdu->dest.ipv4); 72 73 73 tcp_pdu_t *pdu; 74 size_t hdr_size; 75 tcp_header_t *hdr; 76 uint32_t data_offset; 74 udp_received_pdu(pdu); 75 udp_pdu_delete(pdu); 77 76 78 if (pdu_raw_size < sizeof(tcp_header_t)) {79 log_msg(LVL_WARN, "pdu_raw_size = %zu < sizeof(tcp_header_t) = %zu",80 pdu_raw_size, sizeof(tcp_header_t));81 return EINVAL;82 }83 84 hdr = (tcp_header_t *)pdu_raw;85 data_offset = BIT_RANGE_EXTRACT(uint32_t, DF_DATA_OFFSET_h, DF_DATA_OFFSET_l,86 uint16_t_be2host(hdr->doff_flags));87 88 hdr_size = sizeof(uint32_t) * data_offset;89 90 if (pdu_raw_size < hdr_size) {91 log_msg(LVL_WARN, "pdu_raw_size = %zu < hdr_size = %zu",92 pdu_raw_size, hdr_size);93 return EINVAL;94 }95 96 if (hdr_size < sizeof(tcp_header_t)) {97 log_msg(LVL_WARN, "hdr_size = %zu < sizeof(tcp_header_t) = %zu",98 hdr_size, sizeof(tcp_header_t)); return EINVAL;99 }100 101 log_msg(LVL_DEBUG, "pdu_raw_size=%zu, hdr_size=%zu",102 pdu_raw_size, hdr_size);103 pdu = tcp_pdu_create(pdu_raw, hdr_size, pdu_raw + hdr_size,104 pdu_raw_size - hdr_size);105 if (pdu == NULL) {106 log_msg(LVL_WARN, "Failed creating PDU. Dropped.");107 return ENOMEM;108 }109 110 pdu->src_addr.ipv4 = dgram->src.ipv4;111 pdu->dest_addr.ipv4 = dgram->dest.ipv4;112 log_msg(LVL_DEBUG, "src: 0x%08x, dest: 0x%08x",113 pdu->src_addr.ipv4, pdu->dest_addr.ipv4);114 115 tcp_received_pdu(pdu);116 tcp_pdu_delete(pdu);117 */118 77 return EOK; 119 78 } … … 124 83 int rc; 125 84 inet_dgram_t dgram; 85 86 log_msg(LVL_DEBUG, "udp_transmit_pdu()"); 126 87 127 88 dgram.src.ipv4 = pdu->src.ipv4; … … 139 100 140 101 /** Process received PDU. */ 141 /* 142 static void tcp_received_pdu(tcp_pdu_t *pdu) 102 static void udp_received_pdu(udp_pdu_t *pdu) 143 103 { 144 tcp_segment_t *dseg;145 tcp_sockpair_t rident;104 udp_msg_t *dmsg; 105 udp_sockpair_t rident; 146 106 147 log_msg(LVL_DEBUG, " tcp_received_pdu()");107 log_msg(LVL_DEBUG, "udp_received_pdu()"); 148 108 149 if ( tcp_pdu_decode(pdu, &rident, &dseg) != EOK) {109 if (udp_pdu_decode(pdu, &rident, &dmsg) != EOK) { 150 110 log_msg(LVL_WARN, "Not enough memory. PDU dropped."); 151 111 return; 152 112 } 153 */ 154 /* Insert decoded segment into rqueue */ 155 /* tcp_rqueue_insert_seg(&rident, dseg); 113 114 /* 115 * Insert decoded message into appropriate receive queue. 116 * This transfers ownership of dmsg to the callee, we do not 117 * free it. 118 */ 119 udp_assoc_received(&rident, dmsg); 156 120 } 157 */ 121 158 122 int udp_inet_init(void) 159 123 { -
uspace/srv/udp/udp_type.h
ree603c4 r92b42442 60 60 enum netaddr { 61 61 UDP_IPV4_ANY = 0 62 }; 63 64 enum tcp_port { 65 UDP_PORT_ANY = 0 62 66 }; 63 67 … … 137 141 /** Connection */ 138 142 udp_assoc_t *assoc; 139 /** Local address */140 netaddr_t laddr;141 143 } udp_sockdata_t; 144 145 typedef struct { 146 /** Link to receive queue */ 147 link_t link; 148 /** Socket pair */ 149 udp_sockpair_t sp; 150 /** Message */ 151 udp_msg_t *msg; 152 } udp_rcv_queue_entry_t; 142 153 143 154 #endif
Note:
See TracChangeset
for help on using the changeset viewer.