Changeset 14548


Ignore:
Timestamp:
Jul 1, 2015, 12:54:41 AM (8 years ago)
Author:
mikedld
Message:

Use sockerrno and tr_net_strerror for most of network-related errors

This ensures proper network errors formatting on Windows.

Also, disable IP_TOS socket option modification attempts on Windows
since it's not supported there and is considered deprecated: "Do not
use. Type of Service (TOS) settings should only be set using the
Quality of Service API" (c) MSDN. Using QoS API is a subject for
separate commit(s).

Location:
trunk/libtransmission
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/fdlimit.c

    r14479 r14548  
    519519    if ((s = socket (domain, type, 0)) == TR_BAD_SOCKET)
    520520      if (sockerrno != EAFNOSUPPORT)
    521         tr_logAddError (_("Couldn't create socket: %s"), tr_strerror (sockerrno));
     521        {
     522          char err_buf[512];
     523          tr_logAddError (_("Couldn't create socket: %s"),
     524                          tr_net_strerror (err_buf, sizeof (err_buf), sockerrno));
     525        }
    522526
    523527  if (s != TR_BAD_SOCKET)
  • trunk/libtransmission/log.c

    r14476 r14548  
    230230
    231231  OutputDebugStringA (buf);
     232  OutputDebugStringA (TR_NATIVE_EOL_STR);
    232233
    233234  if (*buf)
  • trunk/libtransmission/net.c

    r14527 r14548  
    7777#ifdef _WIN32
    7878    FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0, buf, buflen, NULL);
     79    while (len > 0 && buf[len - 1] >= '\0' && buf[len - 1] <= ' ')
     80      buf[--len] = '\0';
    7981#else
    8082    tr_strlcpy (buf, tr_strerror (err), buflen);
     
    149151              int         tos)
    150152{
    151 #ifdef IP_TOS
    152     if (setsockopt (s, IPPROTO_IP, IP_TOS, (const void *) &tos, sizeof (tos)) != -1)
    153         return;
     153#if defined (IP_TOS) && !defined (_WIN32)
     154    if (setsockopt (s, IPPROTO_IP, IP_TOS, (const void *) &tos, sizeof (tos)) == -1)
     155    {
     156        char err_buf[512];
     157        tr_logAddNamedInfo ("Net", "Can't set TOS '%d': %s", tos,
     158                            tr_net_strerror (err_buf, sizeof (err_buf), sockerrno));
     159    }
    154160#else
    155161    (void) s;
    156     errno = ENOSYS;
    157 #endif
    158 
    159     tr_logAddNamedInfo ("Net", "Can't set TOS '%d': %s", tos, tr_strerror (errno));
     162    (void) tos;
     163#endif
    160164}
    161165
     
    166170#ifdef TCP_CONGESTION
    167171    if (setsockopt (s, IPPROTO_TCP, TCP_CONGESTION,
    168                     (const void *) algorithm, strlen (algorithm) + 1) != -1)
    169         return;
    170 
     172                    (const void *) algorithm, strlen (algorithm) + 1) == -1)
     173    {
     174        char err_buf[512];
     175        tr_logAddNamedInfo ("Net", "Can't set congestion control algorithm '%s': %s",
     176                            algorithm, tr_net_strerror (err_buf, sizeof (err_buf), sockerrno));
     177    }
    171178#else
    172179    (void) s;
    173     errno = ENOSYS;
    174 #endif
    175 
    176     tr_logAddNamedInfo ("Net", "Can't set congestion control algorithm '%s': %s",
    177                         algorithm, tr_strerror (errno));
     180    (void) algorithm;
     181#endif
    178182}
    179183
     
    247251    socklen_t               sourcelen;
    248252    struct sockaddr_storage source_sock;
     253    char err_buf[512];
    249254
    250255    assert (tr_address_is_valid (addr));
     
    261266        int n = 8192;
    262267        if (setsockopt (s, SOL_SOCKET, SO_RCVBUF, (const void *) &n, sizeof (n)))
    263             tr_logAddInfo ("Unable to set SO_RCVBUF on socket %"TR_PRI_SOCK": %s", s, tr_strerror (sockerrno));
     268            tr_logAddInfo ("Unable to set SO_RCVBUF on socket %"TR_PRI_SOCK": %s", s,
     269                           tr_net_strerror (err_buf, sizeof (err_buf), sockerrno));
    264270    }
    265271
     
    278284    {
    279285        tr_logAddError (_("Couldn't set source address %s on %"TR_PRI_SOCK": %s"),
    280                 tr_address_to_string (source_addr), s, tr_strerror (errno));
     286                        tr_address_to_string (source_addr), s,
     287                        tr_net_strerror (err_buf, sizeof (err_buf), sockerrno));
    281288        tr_netClose (session, s);
    282289        return TR_BAD_SOCKET; /* -errno */
     
    292299        int tmperrno;
    293300        tmperrno = sockerrno;
    294         if ((tmperrno != ENETUNREACH && tmperrno != EHOSTUNREACH)
    295                 || addr->type == TR_AF_INET)
     301        if ((tmperrno != ENETUNREACH && tmperrno != EHOSTUNREACH) || addr->type == TR_AF_INET)
     302        {
    296303            tr_logAddError (_("Couldn't connect socket %"TR_PRI_SOCK" to %s, port %d (errno %d - %s)"),
    297                     s, tr_address_to_string (addr), (int)ntohs (port), tmperrno,
    298                     tr_strerror (tmperrno));
     304                            s, tr_address_to_string (addr), (int)ntohs (port), tmperrno,
     305                            tr_net_strerror (err_buf, sizeof (err_buf), tmperrno));
     306        }
    299307        tr_netClose (session, s);
    300308        s = TR_BAD_SOCKET; /* -tmperrno */
     
    372380            const char * fmt;
    373381            const char * hint;
     382            char err_buf[512];
    374383
    375384            if (err == EADDRINUSE)
     
    383392                fmt = _("Couldn't bind port %d on %s: %s (%s)");
    384393
    385             tr_logAddError (fmt, port, tr_address_to_string (addr), tr_strerror (err), hint);
     394            tr_logAddError (fmt, port, tr_address_to_string (addr),
     395                            tr_net_strerror (err_buf, sizeof (err_buf), err), hint);
    386396        }
    387397        tr_netCloseSocket (fd);
  • trunk/libtransmission/peer-io.c

    r14545 r14548  
    12561256        {
    12571257            int e;
     1258            char err_buf[512];
    12581259
    12591260            EVUTIL_SET_SOCKET_ERROR (0);
     
    12611262            e = EVUTIL_SOCKET_ERROR ();
    12621263
    1263             dbgmsg (io, "read %d from peer (%s)", res, (res==-1?tr_strerror (e):""));
     1264            dbgmsg (io, "read %d from peer (%s)", res,
     1265                    (res==-1?tr_net_strerror (err_buf, sizeof (err_buf), e):""));
    12641266
    12651267            if (evbuffer_get_length (io->inbuf))
     
    12681270            if ((res <= 0) && (io->gotError) && (e != EAGAIN) && (e != EINTR) && (e != EINPROGRESS))
    12691271            {
    1270                 char errstr[512];
    12711272                short what = BEV_EVENT_READING | BEV_EVENT_ERROR;
    12721273                if (res == 0)
    12731274                    what |= BEV_EVENT_EOF;
    12741275                dbgmsg (io, "tr_peerIoTryRead got an error. res is %d, what is %hd, errno is %d (%s)",
    1275                         res, what, e, tr_net_strerror (errstr, sizeof (errstr), e));
     1276                        res, what, e, tr_net_strerror (err_buf, sizeof (err_buf), e));
    12761277                io->gotError (io, what, io->userData);
    12771278            }
  • trunk/libtransmission/tr-udp.c

    r14479 r14548  
    5757    int size, rbuf, sbuf, rc;
    5858    socklen_t rbuf_len = sizeof (rbuf), sbuf_len = sizeof (sbuf);
     59    char err_buf[512];
    5960
    6061    size = large ? RECV_BUFFER_SIZE : SMALL_BUFFER_SIZE;
     
    6263    if (rc < 0)
    6364        tr_logAddNamedError ("UDP", "Failed to set receive buffer: %s",
    64                 tr_strerror (errno));
     65                             tr_net_strerror (err_buf, sizeof (err_buf), sockerrno));
    6566
    6667    size = large ? SEND_BUFFER_SIZE : SMALL_BUFFER_SIZE;
     
    6869    if (rc < 0)
    6970        tr_logAddNamedError ("UDP", "Failed to set send buffer: %s",
    70                 tr_strerror (errno));
     71                             tr_net_strerror (err_buf, sizeof (err_buf), sockerrno));
    7172
    7273    if (large) {
Note: See TracChangeset for help on using the changeset viewer.