]> git.sesse.net Git - vlc/blob - modules/access_output/udp.c
- b_rtpts should really b a boolean (!)
[vlc] / modules / access_output / udp.c
1 /*****************************************************************************
2  * udp.c
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Eric Petit <titer@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <fcntl.h>
34
35 #include <vlc/vlc.h>
36 #include <vlc_sout.h>
37 #include <vlc_block.h>
38
39 #ifdef HAVE_UNISTD_H
40 #   include <unistd.h>
41 #endif
42
43 #ifdef WIN32
44 #   include <winsock2.h>
45 #   include <ws2tcpip.h>
46 #else
47 #   include <sys/socket.h>
48 #endif
49
50 #include <vlc_network.h>
51
52 #if defined (HAVE_NETINET_UDPLITE_H)
53 # include <netinet/udplite.h>
54 #elif defined (__linux__)
55 # define UDPLITE_SEND_CSCOV     10
56 # define UDPLITE_RECV_CSCOV     11
57 #endif
58
59 #ifndef IPPROTO_UDPLITE
60 # define IPPROTO_UDPLITE 136 /* from IANA */
61 #endif
62 #ifndef SOL_UDPLITE
63 # define SOL_UDPLITE IPPROTO_UDPLITE
64 #endif
65
66 #define MAX_EMPTY_BLOCKS 200
67
68 #if defined(WIN32) || defined(UNDER_CE)
69 # define WINSOCK_STRERROR_SIZE 20
70 static const char *winsock_strerror( char *buf )
71 {
72     snprintf( buf, WINSOCK_STRERROR_SIZE, "Winsock error %d",
73               WSAGetLastError( ) );
74     buf[WINSOCK_STRERROR_SIZE - 1] = '\0';
75     return buf;
76 }
77 #endif
78
79 /*****************************************************************************
80  * Module descriptor
81  *****************************************************************************/
82 static int  Open ( vlc_object_t * );
83 static void Close( vlc_object_t * );
84
85 #define SOUT_CFG_PREFIX "sout-udp-"
86
87 #define CACHING_TEXT N_("Caching value (ms)")
88 #define CACHING_LONGTEXT N_( \
89     "Default caching value for outbound UDP streams. This " \
90     "value should be set in milliseconds." )
91
92 #define GROUP_TEXT N_("Group packets")
93 #define GROUP_LONGTEXT N_("Packets can be sent one by one at the right time " \
94                           "or by groups. You can choose the number " \
95                           "of packets that will be sent at a time. It " \
96                           "helps reducing the scheduling load on " \
97                           "heavily-loaded systems." )
98 #define RAW_TEXT N_("Raw write")
99 #define RAW_LONGTEXT N_("Packets will be sent " \
100                        "directly, without trying to fill the MTU (ie, " \
101                        "without trying to make the biggest possible packets " \
102                        "in order to improve streaming)." )
103 #define AUTO_MCAST_TEXT N_("Automatic multicast streaming")
104 #define AUTO_MCAST_LONGTEXT N_("Allocates an outbound multicast address " \
105                                "automatically.")
106
107 vlc_module_begin();
108     set_description( _("UDP stream output") );
109     set_shortname( "UDP" );
110     set_category( CAT_SOUT );
111     set_subcategory( SUBCAT_SOUT_ACO );
112     add_integer( SOUT_CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
113     add_integer( SOUT_CFG_PREFIX "group", 1, NULL, GROUP_TEXT, GROUP_LONGTEXT,
114                                  VLC_TRUE );
115     add_suppressed_integer( SOUT_CFG_PREFIX "late" );
116     add_bool( SOUT_CFG_PREFIX "raw",  0, NULL, RAW_TEXT, RAW_LONGTEXT,
117                                  VLC_TRUE );
118     add_bool( SOUT_CFG_PREFIX "auto-mcast", 0, NULL, AUTO_MCAST_TEXT,
119               AUTO_MCAST_LONGTEXT, VLC_TRUE );
120
121     set_capability( "sout access", 100 );
122     add_shortcut( "udp" );
123     add_shortcut( "rtp" ); // Will work only with ts muxer
124     add_shortcut( "udplite" );
125     set_callbacks( Open, Close );
126 vlc_module_end();
127
128 /*****************************************************************************
129  * Exported prototypes
130  *****************************************************************************/
131
132 static const char *ppsz_sout_options[] = {
133     "auto-mcast",
134     "caching",
135     "group",
136     "raw",
137     NULL
138 };
139
140 /* Options handled by the libvlc network core */
141 static const char *ppsz_core_options[] = {
142     "dscp",
143     "ttl",
144     "miface",
145     "miface-addr",
146     NULL
147 };
148
149 static int  Write   ( sout_access_out_t *, block_t * );
150 static int  WriteRaw( sout_access_out_t *, block_t * );
151 static int  Seek    ( sout_access_out_t *, off_t  );
152
153 static void ThreadWrite( vlc_object_t * );
154 static block_t *NewUDPPacket( sout_access_out_t *, mtime_t );
155 static const char *MakeRandMulticast (int family, char *buf, size_t buflen);
156
157 typedef struct sout_access_thread_t
158 {
159     VLC_COMMON_MEMBERS
160
161     sout_instance_t *p_sout;
162
163     block_fifo_t *p_fifo;
164
165     int         i_handle;
166
167     int64_t     i_caching;
168     int         i_group;
169
170     block_fifo_t *p_empty_blocks;
171
172 } sout_access_thread_t;
173
174 struct sout_access_out_sys_t
175 {
176     int                 i_mtu;
177
178     vlc_bool_t          b_rtpts;  // 1 if add rtp/ts header
179     vlc_bool_t          b_mtu_warning;
180     uint16_t            i_sequence_number;
181     uint32_t            i_ssrc;
182
183     block_t             *p_buffer;
184
185     sout_access_thread_t *p_thread;
186
187 };
188
189 #define DEFAULT_PORT 1234
190 #define RTP_HEADER_LENGTH 12
191
192 /*****************************************************************************
193  * Open: open the file
194  *****************************************************************************/
195 static int Open( vlc_object_t *p_this )
196 {
197     sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
198     sout_access_out_sys_t   *p_sys;
199
200     char                *psz_dst_addr = NULL;
201     int                 i_dst_port, proto = IPPROTO_UDP, cscov = 8;
202     const char          *protoname = "UDP";
203
204     int                 i_handle;
205
206     vlc_value_t         val;
207
208     config_ChainParse( p_access, SOUT_CFG_PREFIX,
209                        ppsz_sout_options, p_access->p_cfg );
210     config_ChainParse( p_access, "",
211                        ppsz_core_options, p_access->p_cfg );
212
213     if( !( p_sys = calloc ( 1, sizeof( sout_access_out_sys_t ) ) ) )
214     {
215         msg_Err( p_access, "not enough memory" );
216         return VLC_EGENERIC;
217     }
218     p_access->p_sys = p_sys;
219
220     if( p_access->psz_access != NULL )
221     {
222         if (strcmp (p_access->psz_access, "rtp") == 0)
223             p_sys->b_rtpts = VLC_TRUE;
224         if (strcmp (p_access->psz_access, "udplite") == 0)
225         {
226             protoname = "UDP-Lite";
227             proto = IPPROTO_UDPLITE;
228             p_sys->b_rtpts = VLC_TRUE;
229         }
230     }
231     if (p_sys->b_rtpts)
232         cscov += RTP_HEADER_LENGTH;
233
234     i_dst_port = DEFAULT_PORT;
235     if (var_CreateGetBool (p_access, SOUT_CFG_PREFIX"auto-mcast"))
236     {
237         char buf[INET6_ADDRSTRLEN];
238         if (MakeRandMulticast (AF_INET, buf, sizeof (buf)) != NULL)
239             psz_dst_addr = strdup (buf);
240     }
241     else
242     {
243         char *psz_parser = psz_dst_addr = strdup( p_access->psz_path );
244
245         if (psz_parser[0] == '[')
246             psz_parser = strchr (psz_parser, ']');
247
248         psz_parser = strchr (psz_parser, ':');
249         if (psz_parser != NULL)
250         {
251             *psz_parser++ = '\0';
252             i_dst_port = atoi (psz_parser);
253         }
254     }
255
256     if (var_Create (p_access, "dst-port", VLC_VAR_INTEGER)
257      || var_Create (p_access, "src-port", VLC_VAR_INTEGER)
258      || var_Create (p_access, "dst-addr", VLC_VAR_STRING)
259      || var_Create (p_access, "src-addr", VLC_VAR_STRING))
260         return VLC_ENOMEM;
261
262     p_sys->p_thread =
263         vlc_object_create( p_access, sizeof( sout_access_thread_t ) );
264     if( !p_sys->p_thread )
265     {
266         msg_Err( p_access, "out of memory" );
267         return VLC_ENOMEM;
268     }
269
270     vlc_object_attach( p_sys->p_thread, p_access );
271     p_sys->p_thread->p_sout = p_access->p_sout;
272     p_sys->p_thread->b_die  = 0;
273     p_sys->p_thread->b_error= 0;
274     p_sys->p_thread->p_fifo = block_FifoNew( p_access );
275     p_sys->p_thread->p_empty_blocks = block_FifoNew( p_access );
276
277     i_handle = net_ConnectDgram( p_this, psz_dst_addr, i_dst_port, -1, proto );
278     if( i_handle == -1 )
279     {
280          msg_Err( p_access, "failed to create %s socket", protoname );
281          return VLC_EGENERIC;
282     }
283     else
284     {
285         char addr[NI_MAXNUMERICHOST];
286         int port;
287
288         if (net_GetSockAddress (i_handle, addr, &port) == 0)
289         {
290             msg_Dbg (p_access, "source: %s port %d", addr, port);
291             var_SetString (p_access, "src-addr", addr);
292             var_SetInteger (p_access, "src-port", port);
293         }
294
295         if (net_GetPeerAddress (i_handle, addr, &port) == 0)
296         {
297             msg_Dbg (p_access, "destination: %s port %d", addr, port);
298             var_SetString (p_access, "dst-addr", addr);
299             var_SetInteger (p_access, "dst-port", port);
300         }
301     }
302     p_sys->p_thread->i_handle = i_handle;
303     net_StopRecv( i_handle );
304
305 #ifdef UDPLITE_SEND_CSCOV
306     if (proto == IPPROTO_UDPLITE)
307         setsockopt (i_handle, SOL_UDPLITE, UDPLITE_SEND_CSCOV,
308                     &cscov, sizeof (cscov));
309 #endif
310
311     var_Get( p_access, SOUT_CFG_PREFIX "caching", &val );
312     p_sys->p_thread->i_caching = (int64_t)val.i_int * 1000;
313
314     var_Get( p_access, SOUT_CFG_PREFIX "group", &val );
315     p_sys->p_thread->i_group = val.i_int;
316
317     p_sys->i_mtu = var_CreateGetInteger( p_this, "mtu" );
318
319     if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
320                            VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
321     {
322         msg_Err( p_access->p_sout, "cannot spawn sout access thread" );
323         vlc_object_destroy( p_sys->p_thread );
324         return VLC_EGENERIC;
325     }
326
327     srand( (uint32_t)mdate());
328     p_sys->p_buffer          = NULL;
329     p_sys->i_sequence_number = rand()&0xffff;
330     p_sys->i_ssrc            = rand()&0xffffffff;
331
332     var_Get( p_access, SOUT_CFG_PREFIX "raw", &val );
333     if( val.b_bool )  p_access->pf_write = WriteRaw;
334     else p_access->pf_write = Write;
335
336     p_access->pf_seek = Seek;
337
338     free( psz_dst_addr );
339
340     /* update p_sout->i_out_pace_nocontrol */
341     p_access->p_sout->i_out_pace_nocontrol++;
342
343     return VLC_SUCCESS;
344 }
345
346 /*****************************************************************************
347  * Close: close the target
348  *****************************************************************************/
349 static void Close( vlc_object_t * p_this )
350 {
351     sout_access_out_t     *p_access = (sout_access_out_t*)p_this;
352     sout_access_out_sys_t *p_sys = p_access->p_sys;
353     int i;
354
355     p_sys->p_thread->b_die = 1;
356     for( i = 0; i < 10; i++ )
357     {
358         block_t *p_dummy = block_New( p_access, p_sys->i_mtu );
359         p_dummy->i_dts = 0;
360         p_dummy->i_pts = 0;
361         p_dummy->i_length = 0;
362         memset( p_dummy->p_buffer, 0, p_dummy->i_buffer );
363         block_FifoPut( p_sys->p_thread->p_fifo, p_dummy );
364     }
365     vlc_thread_join( p_sys->p_thread );
366
367     block_FifoRelease( p_sys->p_thread->p_fifo );
368     block_FifoRelease( p_sys->p_thread->p_empty_blocks );
369
370     if( p_sys->p_buffer ) block_Release( p_sys->p_buffer );
371
372     net_Close( p_sys->p_thread->i_handle );
373
374     vlc_object_detach( p_sys->p_thread );
375     vlc_object_destroy( p_sys->p_thread );
376     /* update p_sout->i_out_pace_nocontrol */
377     p_access->p_sout->i_out_pace_nocontrol--;
378
379     msg_Dbg( p_access, "UDP access output closed" );
380     free( p_sys );
381 }
382
383 /*****************************************************************************
384  * Write: standard write on a file descriptor.
385  *****************************************************************************/
386 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
387 {
388     sout_access_out_sys_t *p_sys = p_access->p_sys;
389     int i_len = 0;
390
391     while( p_buffer )
392     {
393         block_t *p_next;
394         int i_packets = 0;
395
396         if( !p_sys->b_mtu_warning && p_buffer->i_buffer > p_sys->i_mtu )
397         {
398             msg_Warn( p_access, "packet size > MTU, you should probably "
399                       "increase the MTU" );
400             p_sys->b_mtu_warning = VLC_TRUE;
401         }
402
403         /* Check if there is enough space in the buffer */
404         if( p_sys->p_buffer &&
405             p_sys->p_buffer->i_buffer + p_buffer->i_buffer > p_sys->i_mtu )
406         {
407             if( p_sys->p_buffer->i_dts + p_sys->p_thread->i_caching < mdate() )
408             {
409                 msg_Dbg( p_access, "late packet for UDP input (" I64Fd ")",
410                          mdate() - p_sys->p_buffer->i_dts
411                           - p_sys->p_thread->i_caching );
412             }
413             block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
414             p_sys->p_buffer = NULL;
415         }
416
417         i_len += p_buffer->i_buffer;
418         while( p_buffer->i_buffer )
419         {
420             int i_payload_size = p_sys->i_mtu;
421             if( p_sys->b_rtpts )
422                 i_payload_size -= RTP_HEADER_LENGTH;
423
424             int i_write = __MIN( p_buffer->i_buffer, i_payload_size );
425
426             i_packets++;
427
428             if( !p_sys->p_buffer )
429             {
430                 p_sys->p_buffer = NewUDPPacket( p_access, p_buffer->i_dts );
431                 if( !p_sys->p_buffer ) break;
432             }
433
434             memcpy( p_sys->p_buffer->p_buffer + p_sys->p_buffer->i_buffer,
435                     p_buffer->p_buffer, i_write );
436
437             p_sys->p_buffer->i_buffer += i_write;
438             p_buffer->p_buffer += i_write;
439             p_buffer->i_buffer -= i_write;
440             if ( p_buffer->i_flags & BLOCK_FLAG_CLOCK )
441             {
442                 if ( p_sys->p_buffer->i_flags & BLOCK_FLAG_CLOCK )
443                     msg_Warn( p_access, "putting two PCRs at once" );
444                 p_sys->p_buffer->i_flags |= BLOCK_FLAG_CLOCK;
445             }
446
447             if( p_sys->p_buffer->i_buffer == p_sys->i_mtu || i_packets > 1 )
448             {
449                 /* Flush */
450                 if( p_sys->p_buffer->i_dts + p_sys->p_thread->i_caching
451                       < mdate() )
452                 {
453                     msg_Dbg( p_access, "late packet for udp input (" I64Fd ")",
454                              mdate() - p_sys->p_buffer->i_dts
455                               - p_sys->p_thread->i_caching );
456                 }
457                 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
458                 p_sys->p_buffer = NULL;
459             }
460         }
461
462         p_next = p_buffer->p_next;
463         block_Release( p_buffer );
464         p_buffer = p_next;
465     }
466
467     return( p_sys->p_thread->b_error ? -1 : i_len );
468 }
469
470 /*****************************************************************************
471  * WriteRaw: write p_buffer without trying to fill mtu
472  *****************************************************************************/
473 static int WriteRaw( sout_access_out_t *p_access, block_t *p_buffer )
474 {
475     sout_access_out_sys_t   *p_sys = p_access->p_sys;
476     block_t *p_buf;
477     int i_len;
478
479     while ( p_sys->p_thread->p_empty_blocks->i_depth >= MAX_EMPTY_BLOCKS )
480     {
481         p_buf = block_FifoGet(p_sys->p_thread->p_empty_blocks);
482         block_Release( p_buf );
483     }
484
485     i_len = p_buffer->i_buffer;
486     block_FifoPut( p_sys->p_thread->p_fifo, p_buffer );
487
488     return( p_sys->p_thread->b_error ? -1 : i_len );
489 }
490
491 /*****************************************************************************
492  * Seek: seek to a specific location in a file
493  *****************************************************************************/
494 static int Seek( sout_access_out_t *p_access, off_t i_pos )
495 {
496     msg_Err( p_access, "UDP sout access cannot seek" );
497     return -1;
498 }
499
500 /*****************************************************************************
501  * NewUDPPacket: allocate a new UDP packet of size p_sys->i_mtu
502  *****************************************************************************/
503 static block_t *NewUDPPacket( sout_access_out_t *p_access, mtime_t i_dts)
504 {
505     sout_access_out_sys_t *p_sys = p_access->p_sys;
506     block_t *p_buffer;
507
508     while ( p_sys->p_thread->p_empty_blocks->i_depth > MAX_EMPTY_BLOCKS )
509     {
510         p_buffer = block_FifoGet( p_sys->p_thread->p_empty_blocks );
511         block_Release( p_buffer );
512     }
513
514     if( p_sys->p_thread->p_empty_blocks->i_depth == 0 )
515     {
516         p_buffer = block_New( p_access->p_sout, p_sys->i_mtu );
517     }
518     else
519     {
520         p_buffer = block_FifoGet(p_sys->p_thread->p_empty_blocks );       
521         p_buffer->i_flags = 0;
522         p_buffer = block_Realloc( p_buffer, 0, p_sys->i_mtu );
523     }
524
525     p_buffer->i_dts = i_dts;
526     p_buffer->i_buffer = 0;
527
528     if( p_sys->b_rtpts )
529     {
530         mtime_t i_timestamp = p_buffer->i_dts * 9 / 100;
531
532         /* add rtp/ts header */
533         p_buffer->p_buffer[0] = 0x80;
534         p_buffer->p_buffer[1] = 0x21; // mpeg2-ts
535
536         SetWLE( p_buffer->p_buffer + 2, p_sys->i_sequence_number );
537         p_sys->i_sequence_number++;
538         SetDWLE( p_buffer->p_buffer + 4, i_timestamp );
539         SetDWLE( p_buffer->p_buffer + 8, p_sys->i_ssrc );
540
541         p_buffer->i_buffer = RTP_HEADER_LENGTH;
542     }
543
544     return p_buffer;
545 }
546
547 /*****************************************************************************
548  * ThreadWrite: Write a packet on the network at the good time.
549  *****************************************************************************/
550 static void ThreadWrite( vlc_object_t *p_this )
551 {
552     sout_access_thread_t *p_thread = (sout_access_thread_t*)p_this;
553     mtime_t              i_date_last = -1;
554     mtime_t              i_to_send = p_thread->i_group;
555     int                  i_dropped_packets = 0;
556 #if defined(WIN32) || defined(UNDER_CE)
557     char strerror_buf[WINSOCK_STRERROR_SIZE];
558 # define strerror( x ) winsock_strerror( strerror_buf )
559 #endif
560
561     while( !p_thread->b_die )
562     {
563         block_t *p_pk;
564         mtime_t       i_date, i_sent;
565 #if 0
566         if( (i++ % 1000)==0 ) {
567           int i = 0;
568           int j = 0;
569           block_t *p_tmp = p_thread->p_empty_blocks->p_first;
570           while( p_tmp ) { p_tmp = p_tmp->p_next; i++;}
571           p_tmp = p_thread->p_fifo->p_first;
572           while( p_tmp ) { p_tmp = p_tmp->p_next; j++;}
573           msg_Dbg( p_thread, "fifo depth: %d/%d, empty blocks: %d/%d",
574                    p_thread->p_fifo->i_depth, j,p_thread->p_empty_blocks->i_depth,i );
575         }
576 #endif
577         p_pk = block_FifoGet( p_thread->p_fifo );
578
579         i_date = p_thread->i_caching + p_pk->i_dts;
580         if( i_date_last > 0 )
581         {
582             if( i_date - i_date_last > 2000000 )
583             {
584                 if( !i_dropped_packets )
585                     msg_Dbg( p_thread, "mmh, hole ("I64Fd" > 2s) -> drop",
586                              i_date - i_date_last );
587
588                 block_FifoPut( p_thread->p_empty_blocks, p_pk );
589
590                 i_date_last = i_date;
591                 i_dropped_packets++;
592                 continue;
593             }
594             else if( i_date - i_date_last < -1000 )
595             {
596                 if( !i_dropped_packets )
597                     msg_Dbg( p_thread, "mmh, packets in the past ("I64Fd")",
598                              i_date_last - i_date );
599             }
600         }
601
602         i_to_send--;
603         if( !i_to_send || (p_pk->i_flags & BLOCK_FLAG_CLOCK) )
604         {
605             mwait( i_date );
606             i_to_send = p_thread->i_group;
607         }
608         if( send( p_thread->i_handle, p_pk->p_buffer, p_pk->i_buffer, 0 )
609               == -1 )
610         {
611             msg_Warn( p_thread, "send error: %s", strerror(errno) );
612         }
613
614         if( i_dropped_packets )
615         {
616             msg_Dbg( p_thread, "dropped %i packets", i_dropped_packets );
617             i_dropped_packets = 0;
618         }
619
620 #if 1
621         i_sent = mdate();
622         if ( i_sent > i_date + 20000 )
623         {
624             msg_Dbg( p_thread, "packet has been sent too late (" I64Fd ")",
625                      i_sent - i_date );
626         }
627 #endif
628
629         block_FifoPut( p_thread->p_empty_blocks, p_pk );
630
631         i_date_last = i_date;
632     }
633 }
634
635
636 static const char *MakeRandMulticast (int family, char *buf, size_t buflen)
637 {
638     uint32_t rand = (getpid() & 0xffff)
639                   | (uint32_t)(((mdate () >> 10) & 0xffff) << 16);
640
641     switch (family)
642     {
643 #ifdef AF_INET6
644         case AF_INET6:
645         {
646             struct in6_addr addr;
647             memcpy (&addr, "\xff\x38\x00\x00" "\x00\x00\x00\x00"
648                            "\x00\x00\x00\x00", 12);
649             rand |= 0x80000000;
650             memcpy (addr.s6_addr + 12, &(uint32_t){ htonl (rand) }, 4);
651             return inet_ntop (family, &addr, buf, buflen);
652         }
653 #endif
654
655         case AF_INET:
656         {
657             struct in_addr addr;
658             addr.s_addr = htonl ((rand & 0xffffff) | 0xe8000000);
659             return inet_ntop (family, &addr, buf, buflen);
660         }
661     }
662 #ifdef EAFNOSUPPORT
663     errno = EAFNOSUPPORT;
664 #endif
665     return NULL;
666 }