~upstart-devel/upstart/trunk

« back to all changes in this revision

Viewing changes to extra/upstart-socket-bridge.c

  • Committer: James Hunt
  • Date: 2014-02-19 09:07:56 UTC
  • mfrom: (1588.1.14 ipv6)
  • Revision ID: james.hunt@ubuntu.com-20140219090756-fu7d0ez5i6gcj0u6
* Merged lp:~cameronnemo/upstart/ipv6.

Show diffs side-by-side

added added

removed removed

Lines of Context:
67
67
        NihList entry;
68
68
 
69
69
        union {
70
 
                struct sockaddr        addr;
71
 
                struct sockaddr_in sin_addr;
72
 
                struct sockaddr_un sun_addr;
 
70
                struct sockaddr         addr;
 
71
                struct sockaddr_in  sin_addr;
 
72
                struct sockaddr_in6 sin6_addr;
 
73
                struct sockaddr_un  sun_addr;
73
74
        };
74
75
        socklen_t addrlen;
75
76
 
295
296
                size_t env_len = 0;
296
297
                char *var;
297
298
                DBusPendingCall *pending_call;
 
299
                char buffer[INET6_ADDRSTRLEN];
298
300
 
299
301
                if (event[i].events & EPOLLIN)
300
302
                        nih_debug ("%p EPOLLIN", sock);
308
310
                switch (sock->addr.sa_family) {
309
311
                case AF_INET:
310
312
                        NIH_MUST (nih_str_array_add (&env, NULL, &env_len,
311
 
                                                     "PROTO=inet"));
312
 
 
313
 
                        var = NIH_MUST (nih_sprintf (NULL, "PORT=%d",
314
 
                                                     ntohs (sock->sin_addr.sin_port)));
315
 
                        NIH_MUST (nih_str_array_addp (&env, NULL, &env_len,
316
 
                                                      var));
317
 
                        nih_discard (var);
318
 
 
319
 
                        var = NIH_MUST (nih_sprintf (NULL, "ADDR=%s",
320
 
                                                     inet_ntoa (sock->sin_addr.sin_addr)));
321
 
                        NIH_MUST (nih_str_array_addp (&env, NULL, &env_len,
322
 
                                                      var));
 
313
                                                        "PROTO=inet"));
 
314
 
 
315
                        var = NIH_MUST (nih_sprintf (NULL, "PORT=%d",
 
316
                                                        ntohs (sock->sin_addr.sin_port)));
 
317
                        NIH_MUST (nih_str_array_addp (&env, NULL, &env_len,
 
318
                                                        var));
 
319
                        nih_discard (var);
 
320
 
 
321
                        var = NIH_MUST (nih_sprintf (NULL, "ADDR=%s",
 
322
                                                        inet_ntoa (sock->sin_addr.sin_addr)));
 
323
                        NIH_MUST (nih_str_array_addp (&env, NULL, &env_len,
 
324
                                                        var));
 
325
                        nih_discard (var);
 
326
                        break;
 
327
                case AF_INET6:
 
328
                        NIH_MUST (nih_str_array_add (&env, NULL, &env_len,
 
329
                                                        "PROTO=inet6"));
 
330
 
 
331
                        var = NIH_MUST (nih_sprintf (NULL, "PORT=%d",
 
332
                                                        ntohs (sock->sin6_addr.sin6_port)));
 
333
                        NIH_MUST (nih_str_array_addp (&env, NULL, &env_len,
 
334
                                                        var));
 
335
                        nih_discard (var);
 
336
 
 
337
                        var = NIH_MUST (nih_sprintf (NULL, "ADDR=%s",
 
338
                                                        inet_ntop(AF_INET6, &sock->sin6_addr.sin6_addr, buffer, INET6_ADDRSTRLEN)));
 
339
 
 
340
                        NIH_MUST (nih_str_array_addp (&env, NULL, &env_len,
 
341
                                                        var));
323
342
                        nih_discard (var);
324
343
                        break;
325
344
                case AF_UNIX:
504
523
                                sock->sin_addr.sin_family = AF_INET;
505
524
                                sock->sin_addr.sin_addr.s_addr = INADDR_ANY;
506
525
                                components = 1;
 
526
                        } else if (! strcmp (val, "inet6")) {
 
527
                                sock->addrlen = sizeof sock->sin6_addr;
 
528
                                sock->sin6_addr.sin6_family = AF_INET6;
 
529
                                sock->sin6_addr.sin6_addr = in6addr_any;
 
530
                                components = 1;
507
531
                        } else if (! strcmp (val, "unix")) {
508
532
                                sock->addrlen = sizeof sock->sun_addr;
509
533
                                sock->sun_addr.sun_family = AF_UNIX;
515
539
                        }
516
540
 
517
541
                } else if (! strncmp (*env, "PORT", name_len)
518
 
                           && (sock->sin_addr.sin_family == AF_INET)) {
 
542
                           && (sock->sin_addr.sin_family == AF_INET)) {
519
543
                        sock->sin_addr.sin_port = htons (atoi (val));
520
544
                        components--;
521
 
 
 
545
                } else if (! strncmp (*env, "PORT", name_len)
 
546
                           && (sock->sin6_addr.sin6_family == AF_INET6)) {
 
547
                        sock->sin6_addr.sin6_port = htons (atoi (val));
 
548
                        components--;
522
549
                } else if (! strncmp (*env, "ADDR", name_len)
523
 
                           && (sock->sin_addr.sin_family == AF_INET)) {
 
550
                           && (sock->sin_addr.sin_family == AF_INET)) {
524
551
                        if (inet_aton (val, &(sock->sin_addr.sin_addr)) == 0) {
525
552
                                nih_warn ("Ignored socket event with invalid ADDR=%s in %s",
526
 
                                          val, job->path);
527
 
                                goto error;
528
 
                        }
529
 
 
 
553
                                          val, job->path);
 
554
                                goto error;
 
555
                        }
 
556
                } else if (! strncmp (*env, "ADDR", name_len)
 
557
                           && (sock->sin6_addr.sin6_family == AF_INET6)) {
 
558
                        if (inet_pton (AF_INET6, val, &(sock->sin6_addr.sin6_addr)) != 1) {
 
559
                                nih_warn ("Ignored socket event with invalid ADDR=%s in %s",
 
560
                                          val, job->path);
 
561
                                goto error;
 
562
                        }
530
563
                } else if (! strncmp (*env, "PATH", name_len)
531
 
                           && (sock->sun_addr.sun_family == AF_UNIX)) {
 
564
                           && (sock->sun_addr.sun_family == AF_UNIX)) {
532
565
                        strncpy (sock->sun_addr.sun_path, val,
533
 
                                 sizeof sock->sun_addr.sun_path);
 
566
                                 sizeof sock->sun_addr.sun_path);
534
567
 
535
568
                        if (sock->sun_addr.sun_path[0] == '@')
536
569
                                sock->sun_addr.sun_path[0] = '\0';
567
600
                goto error;
568
601
        }
569
602
 
 
603
        /* If socket is ipv6, need to set IPV6_V6ONLY option */
 
604
        if (sock->sin6_addr.sin6_family == AF_INET6 && 
 
605
            setsockopt (sock->sock, SOL_IPV6, IPV6_V6ONLY,
 
606
                        &opt, sizeof opt) < 0) {
 
607
                nih_warn ("Failed to set IPV6 only option in %s: %s",
 
608
                          job->path, strerror (errno));
 
609
                goto error;
 
610
        }
 
611
 
570
612
        if (bind (sock->sock, &sock->addr, sock->addrlen) < 0) {
571
613
                nih_warn ("Failed to bind socket in %s: %s",
572
614
                          job->path, strerror (errno));