Changeset 71abe93 for lib


Ignore:
Timestamp:
2015-04-05T14:09:37Z (9 years ago)
Author:
dequis <dx@…>
Branches:
master
Children:
69982f8
Parents:
4543b6bd
git-author:
dequis <dx@…> (05-04-15 13:54:41)
git-committer:
dequis <dx@…> (05-04-15 14:09:37)
Message:

proxy: minor refactor, simplify error handling

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lib/proxy.c

    r4543b6bd r71abe93  
    6363static int proxy_connect_none(const char *host, unsigned short port_, struct PHB *phb);
    6464
    65 static gboolean gaim_io_connected(gpointer data, gint source, b_input_condition cond)
     65static gboolean phb_close(struct PHB *phb)
     66{
     67        close(phb->fd);
     68        phb->func(phb->data, -1, B_EV_IO_READ);
     69        g_free(phb->host);
     70        g_free(phb);
     71        return FALSE;
     72}
     73
     74static gboolean proxy_connected(gpointer data, gint source, b_input_condition cond)
    6675{
    6776        struct PHB *phb = data;
     
    8089                                dup2(new_fd, source);
    8190                                closesocket(new_fd);
    82                                 phb->inpa = b_input_add(source, B_EV_IO_WRITE, gaim_io_connected, phb);
     91                                phb->inpa = b_input_add(source, B_EV_IO_WRITE, proxy_connected, phb);
    8392                                return FALSE;
    8493                        }
    8594                }
    86                 freeaddrinfo(phb->gai);
    8795                closesocket(source);
    88                 b_event_remove(phb->inpa);
    89                 phb->inpa = 0;
    90                 if (phb->proxy_func) {
    91                         phb->proxy_func(phb->proxy_data, -1, B_EV_IO_READ);
    92                 } else {
    93                         phb->func(phb->data, -1, B_EV_IO_READ);
    94                         g_free(phb);
    95                 }
    96                 return FALSE;
    97         }
     96                source = -1;
     97                /* socket is dead, but continue to clean up */
     98        } else {
     99                sock_make_blocking(source);
     100        }
     101
    98102        freeaddrinfo(phb->gai);
    99         sock_make_blocking(source);
    100103        b_event_remove(phb->inpa);
    101104        phb->inpa = 0;
     
    160163                        continue;
    161164                } else {
    162                         phb->inpa = b_input_add(fd, B_EV_IO_WRITE, gaim_io_connected, phb);
     165                        phb->inpa = b_input_add(fd, B_EV_IO_WRITE, proxy_connected, phb);
    163166                        phb->fd = fd;
    164167
     
    206209        }
    207210
    208         close(source);
    209         phb->func(phb->data, -1, B_EV_IO_READ);
    210         g_free(phb->host);
    211         g_free(phb);
    212 
    213         return FALSE;
     211        return phb_close(phb);
    214212}
    215213
     
    226224        len = sizeof(error);
    227225        if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
    228                 close(source);
    229                 phb->func(phb->data, -1, B_EV_IO_READ);
    230                 g_free(phb->host);
    231                 g_free(phb);
    232                 return FALSE;
     226                return phb_close(phb);
    233227        }
    234228        sock_make_blocking(source);
     
    237231                   phb->host, phb->port);
    238232        if (send(source, cmd, strlen(cmd), 0) < 0) {
    239                 close(source);
    240                 phb->func(phb->data, -1, B_EV_IO_READ);
    241                 g_free(phb->host);
    242                 g_free(phb);
    243                 return FALSE;
     233                return phb_close(phb);
    244234        }
    245235
     
    252242                g_free(t2);
    253243                if (send(source, cmd, strlen(cmd), 0) < 0) {
    254                         close(source);
    255                         phb->func(phb->data, -1, B_EV_IO_READ);
    256                         g_free(phb->host);
    257                         g_free(phb);
    258                         return FALSE;
     244                        return phb_close(phb);
    259245                }
    260246        }
     
    262248        g_snprintf(cmd, sizeof(cmd), "\r\n");
    263249        if (send(source, cmd, strlen(cmd), 0) < 0) {
    264                 close(source);
    265                 phb->func(phb->data, -1, B_EV_IO_READ);
    266                 g_free(phb->host);
    267                 g_free(phb);
    268                 return FALSE;
     250                return phb_close(phb);
    269251        }
    270252
     
    302284        }
    303285
    304         close(source);
    305         phb->func(phb->data, -1, B_EV_IO_READ);
    306         g_free(phb->host);
    307         g_free(phb);
    308 
    309         return FALSE;
     286        return phb_close(phb);
    310287}
    311288
     
    323300        len = sizeof(error);
    324301        if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
    325                 close(source);
    326                 phb->func(phb->data, -1, B_EV_IO_READ);
    327                 g_free(phb->host);
    328                 g_free(phb);
    329                 return FALSE;
     302                return phb_close(phb);
    330303        }
    331304        sock_make_blocking(source);
     
    333306        /* XXX does socks4 not support host name lookups by the proxy? */
    334307        if (!(hp = gethostbyname(phb->host))) {
    335                 close(source);
    336                 phb->func(phb->data, -1, B_EV_IO_READ);
    337                 g_free(phb->host);
    338                 g_free(phb);
    339                 return FALSE;
     308                return phb_close(phb);
    340309        }
    341310
     
    350319        packet[8] = 0;
    351320        if (write(source, packet, 9) != 9) {
    352                 close(source);
    353                 phb->func(phb->data, -1, B_EV_IO_READ);
    354                 g_free(phb->host);
    355                 g_free(phb);
    356                 return FALSE;
     321                return phb_close(phb);
    357322        }
    358323
     
    383348
    384349        if (read(source, buf, 10) < 10) {
    385                 close(source);
    386                 phb->func(phb->data, -1, B_EV_IO_READ);
    387                 g_free(phb->host);
    388                 g_free(phb);
    389                 return FALSE;
     350                return phb_close(phb);
    390351        }
    391352        if ((buf[0] != 0x05) || (buf[1] != 0x00)) {
    392                 close(source);
    393                 phb->func(phb->data, -1, B_EV_IO_READ);
    394                 g_free(phb->host);
    395                 g_free(phb);
    396                 return FALSE;
     353                return phb_close(phb);
    397354        }
    398355
     
    420377
    421378        if (write(source, buf, (5 + strlen(phb->host) + 2)) < (5 + strlen(phb->host) + 2)) {
    422                 close(source);
    423                 phb->func(phb->data, -1, B_EV_IO_READ);
    424                 g_free(phb->host);
    425                 g_free(phb);
     379                phb_close(phb);
    426380                return;
    427381        }
     
    438392
    439393        if (read(source, buf, 2) < 2) {
    440                 close(source);
    441                 phb->func(phb->data, -1, B_EV_IO_READ);
    442                 g_free(phb->host);
    443                 g_free(phb);
    444                 return FALSE;
     394                return phb_close(phb);
    445395        }
    446396
    447397        if ((buf[0] != 0x01) || (buf[1] != 0x00)) {
    448                 close(source);
    449                 phb->func(phb->data, -1, B_EV_IO_READ);
    450                 g_free(phb->host);
    451                 g_free(phb);
    452                 return FALSE;
     398                return phb_close(phb);
    453399        }
    454400
     
    466412
    467413        if (read(source, buf, 2) < 2) {
    468                 close(source);
    469                 phb->func(phb->data, -1, B_EV_IO_READ);
    470                 g_free(phb->host);
    471                 g_free(phb);
    472                 return FALSE;
     414                return phb_close(phb);
    473415        }
    474416
    475417        if ((buf[0] != 0x05) || (buf[1] == 0xff)) {
    476                 close(source);
    477                 phb->func(phb->data, -1, B_EV_IO_READ);
    478                 g_free(phb->host);
    479                 g_free(phb);
    480                 return FALSE;
     418                return phb_close(phb);
    481419        }
    482420
     
    489427                memcpy(buf + 2 + i + 1, proxypass, j);
    490428                if (write(source, buf, 3 + i + j) < 3 + i + j) {
    491                         close(source);
    492                         phb->func(phb->data, -1, B_EV_IO_READ);
    493                         g_free(phb->host);
    494                         g_free(phb);
    495                         return FALSE;
     429                        return phb_close(phb);
    496430                }
    497431
     
    517451        len = sizeof(error);
    518452        if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
    519                 close(source);
    520                 phb->func(phb->data, -1, B_EV_IO_READ);
    521                 g_free(phb->host);
    522                 g_free(phb);
    523                 return FALSE;
     453                return phb_close(phb);
    524454        }
    525455        sock_make_blocking(source);
     
    539469
    540470        if (write(source, buf, i) < i) {
    541                 close(source);
    542                 phb->func(phb->data, -1, B_EV_IO_READ);
    543                 g_free(phb->host);
    544                 g_free(phb);
    545                 return FALSE;
     471                return phb_close(phb);
    546472        }
    547473
Note: See TracChangeset for help on using the changeset viewer.