MCPcopy Create free account
hub / github.com/acl-dev/acl / udp_request

Function udp_request

lib_fiber/c/src/dns/resolver.c:396–454  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

394static int __wait_timeout = 5000;
395
396static int udp_request(const char *ip, unsigned short port,
397 const char *data, size_t dlen, char *buf, size_t size)
398{
399 int ret;
400 struct sockaddr_in addr, from_addr;
401 socklen_t len;
402 socket_t sock = acl_fiber_socket(AF_INET, SOCK_DGRAM, 0);
403
404 if (sock == INVALID_SOCKET) {
405 msg_error("%s(%d): create socket error %s",
406 __FUNCTION__ , __LINE__, last_serror());
407 return -1;
408 }
409
410 memset(&addr, 0, sizeof(addr));
411 addr.sin_family = AF_INET;
412 addr.sin_port = htons(port);
413 addr.sin_addr.s_addr = inet_addr(ip);
414
415#if defined(_WIN32) || defined(_WIN64)
416 ret = acl_fiber_sendto(sock, data, (int) dlen, 0, (struct sockaddr *) &addr,
417 (socklen_t) sizeof(addr));
418#else
419 ret = (int) acl_fiber_sendto(sock, data, dlen, 0, (struct sockaddr *) &addr,
420 (socklen_t) sizeof(addr));
421#endif
422
423 if (ret < 0) {
424 msg_error("%s(%d): send error %s",
425 __FUNCTION__ , __LINE__, last_serror());
426 acl_fiber_close(sock);
427 return -1;
428 }
429
430 if (read_wait(sock, __wait_timeout) < 0) {
431 acl_fiber_close(sock);
432 msg_warn("%s(%d), %s: read timeout",
433 __FILE__, __LINE__, __FUNCTION__);
434 return -1;
435 }
436
437 len = (socklen_t) sizeof(from_addr);
438
439#if defined(_WIN32) || defined(_WIN64)
440 ret = acl_fiber_recvfrom(sock, buf, (int) size, 0,
441 (struct sockaddr*) &from_addr, &len);
442#else
443 ret = (int) acl_fiber_recvfrom(sock, buf, size, 0,
444 (struct sockaddr*) &from_addr, &len);
445#endif
446
447 acl_fiber_close(sock);
448 if (ret <= 0) {
449 msg_error("%s(%d): read error %s",
450 __FUNCTION__ , __LINE__, last_serror());
451 return -1;
452 }
453 return ret;

Callers 1

ns_lookupFunction · 0.85

Calls 8

acl_fiber_socketFunction · 0.85
acl_fiber_sendtoFunction · 0.85
acl_fiber_closeFunction · 0.85
msg_warnFunction · 0.85
acl_fiber_recvfromFunction · 0.85
msg_errorFunction · 0.50
last_serrorFunction · 0.50
read_waitFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…