Changeset aa935693f83221d868cd51522152ef3845222fd4

Show
Ignore:
Timestamp:
17/07/03 16:10:08 (5 years ago)
Author:
Sam Hocevar <sam@videolan.org>
git-committer:
Sam Hocevar <sam@videolan.org> 1058451008 +0000
git-parent:

[8c7b5a88af84044ec88e45fdc2bd899ddc524bea]

git-author:
Sam Hocevar <sam@videolan.org> 1058451008 +0000
Message:
  • src/stream_output/announce.c:
    + Coding style fixes.
    + Use memcpy() instead of for() loops.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • include/announce.h

    rb861cc0 raa93569  
    33 ***************************************************************************** 
    44 * Copyright (C) 2002 VideoLAN 
    5  * $Id: announce.h,v 1.6 2003/07/05 15:00:28 zorglub Exp $ 
     5 * $Id: announce.h,v 1.7 2003/07/17 14:10:08 sam Exp $ 
    66 * 
    77 * Authors: Cl�nt Stenac <zorglub@via.ecp.fr> 
     
    5252        char psz_port[8]; 
    5353        module_t p_network; 
    54         unsigned int socket; 
    55         unsigned int sendnow
     54        unsigned int i_socket; 
     55        unsigned int i_calls
    5656        int i_ip_version; 
    5757}; 
  • src/stream_output/announce.c

    rb861cc0 raa93569  
    5959    unsigned int i_count = 0; /*pos in input string*/ 
    6060    unsigned int i_pos1  = 0; /*pos in out2 string */ 
    61     unsigned int i_pos2  = 0;  
     61    unsigned int i_pos2  = 0; 
    6262    char *psz_cur; /*store the pos of the first delim found */ 
    63      
     63 
    6464    /*skip spaces at the beginning*/ 
    6565    while(psz_in[i_count] == ' ' && i_count < strlen(psz_in)) 
     
    6969    if(i_count == strlen(psz_in)) 
    7070        return NULL; 
    71      
     71 
    7272    /*Look for delim*/ 
    7373    while(psz_in[i_count] != delim && i_count < strlen(psz_in)) 
     
    7979    /* Mark the end of out1 */ 
    8080    psz_out1[i_pos1] = 0; 
    81      
     81 
    8282    if(i_count == strlen(psz_in)) 
    8383        return NULL; 
    84      
     84 
    8585    /*store pos of the first delim*/ 
    8686    psz_cur = &psz_in[i_count]; 
    87      
    88      
    89      
     87 
     88 
     89 
    9090    /*skip all delim and all spaces*/ 
    91     while( (psz_in[i_count] == ' ' ||  
    92             psz_in[i_count] == delim)  
     91    while( (psz_in[i_count] == ' ' || 
     92            psz_in[i_count] == delim) 
    9393           && i_count < strlen(psz_in)) 
    9494    { 
    9595        i_count++; 
    9696    } 
    97      
     97 
    9898    if(i_count == strlen(psz_in)) 
    9999        return psz_cur; 
    100      
     100 
    101101    /*Store the second string*/ 
    102102    while(i_count < strlen(psz_in)) 
     
    107107    } 
    108108    psz_out2[i_pos2] = 0; 
    109      
     109 
    110110    return psz_cur; 
    111111} 
     
    114114 * sout_SAPNew: Creates a SAP Session 
    115115 *****************************************************************************/ 
    116 sap_session_t * sout_SAPNew ( sout_instance_t *p_sout
     116sap_session_t * sout_SAPNew ( sout_instance_t *p_sout
    117117                char * psz_url_arg, 
    118118                char * psz_name_arg, int ip_version, 
     
    126126    char                *psz_eol; /* Used to parse IPv6 URIs */ 
    127127    int                 i_port; /* Port in numerical format */ 
    128      
     128 
    129129    /* Allocate the SAP structure */ 
    130     p_new = (sap_session_t *)malloc( sizeof ( sap_session_t ) ) ; 
     130    p_new = (sap_session_t *) malloc( sizeof ( sap_session_t ) ) ; 
    131131    if ( !p_new ) 
    132132    { 
    133         msg_Err( p_sout, "No memory left" ); 
     133        msg_Err( p_sout, "out of memory" ); 
    134134        return NULL; 
    135135    } 
    136      
     136 
    137137    /* Fill the information in the structure */ 
    138     if( strstr( psz_url_arg , "[" ) ) 
     138    if( strstr( psz_url_arg, "[" ) ) 
    139139    {      /* We have an IPv6 address. Do not use ':' as the port separator */ 
    140140        psz_eol = strchr( psz_url_arg, ']' ); 
    141141        if( !psz_eol ) /* No matching ] ! Aborting */ 
    142142        { 
    143             msg_Warn( p_sout , "No matching ]. Unable to parse URI"); 
    144             return NULL; 
    145         } 
    146         if(!psz_eol++
    147         { 
    148               sprintf (p_new->psz_url, "%s", psz_url_arg); 
    149               sprintf (p_new->psz_port, "%s", DEFAULT_PORT); 
     143            msg_Warn( p_sout, "no matching ], unable to parse URI"); 
     144            return NULL; 
     145        } 
     146        if( !psz_eol++
     147        { 
     148              sprintf( p_new->psz_url, "%s", psz_url_arg ); 
     149              sprintf( p_new->psz_port, "%s", DEFAULT_PORT ); 
    150150        } 
    151151        else 
    152152        { 
    153153            *psz_eol = '\0'; 
    154             sprintf (p_new->psz_url, "%s", psz_url_arg); 
     154            sprintf( p_new->psz_url, "%s", psz_url_arg ); 
    155155            psz_eol++; 
    156             if(psz_eol
     156            if( psz_eol
    157157            { 
    158                   sprintf (p_new->psz_port, "%s", psz_eol); 
     158                sprintf( p_new->psz_port, "%s", psz_eol ); 
    159159            } 
    160         }     
     160        } 
    161161    } 
    162162    else 
    163163    { 
    164         split(psz_url_arg,p_new->psz_url,p_new->psz_port,':');    
    165     } 
    166    
     164        split( psz_url_arg, p_new->psz_url, p_new->psz_port, ':' ); 
     165    } 
     166 
    167167    /* Check if we have a port */ 
    168     if( !strlen(p_new->psz_port) ) 
    169     { 
    170         sprintf (p_new->psz_port, "%s", DEFAULT_PORT); 
    171     } 
    172    
     168    if( !strlen( p_new->psz_port ) ) 
     169    { 
     170        sprintf( p_new->psz_port, "%s", DEFAULT_PORT ); 
     171    } 
     172 
    173173    /* Make sure our port is valid and atoi it*/ 
    174174    i_port = atoi( p_new->psz_port ); 
    175      
     175 
    176176    if( !i_port ) 
    177177    { 
    178         sprintf (p_new->psz_port, "%s", DEFAULT_PORT); 
     178        sprintf( p_new->psz_port, "%s", DEFAULT_PORT ); 
    179179    } 
    180180    else 
    181181    { 
    182        sprintf (p_new->psz_port, "%i", i_port);  
    183     } 
    184  
    185     /* The name that we send */  
    186     sprintf ( p_new->psz_name , "%s" , psz_name_arg ); 
     182        sprintf( p_new->psz_port, "%i", i_port ); 
     183    } 
     184 
     185    /* The name that we send */ 
     186    sprintf( p_new->psz_name, "%s", psz_name_arg ); 
    187187 
    188188    p_new->i_ip_version = ip_version; 
     
    191191    if( ip_version != 6 ) 
    192192    { 
    193         msg_Dbg( p_sout , "Creating IPv4 SAP socket" ); 
     193        msg_Dbg( p_sout, "creating IPv4 SAP socket" ); 
    194194 
    195195        /* Fill the socket descriptor */ 
     
    203203        /* Call the network module */ 
    204204        sprintf ( psz_network, "ipv4" ); 
    205         p_sout->p_private=(void*) &socket_desc; 
     205        p_sout->p_private = (void*) &socket_desc; 
    206206        if( !( p_network = module_Need( p_sout, "network", psz_network ) ) ) 
    207207        { 
     
    211211        module_Unneed( p_sout, p_network ); 
    212212 
    213         p_new->socket   =      socket_desc.i_handle; 
    214         if(p_new->socket <= 0 ) 
    215         { 
    216             msg_Warn( p_sout, "Unable to initialize SAP" ); 
     213        p_new->i_socket = socket_desc.i_handle; 
     214        if( p_new->i_socket < 0 ) 
     215        { 
     216            msg_Warn( p_sout, "unable to initialize SAP" ); 
    217217            return NULL; 
    218218        } 
     
    220220    else 
    221221    { 
    222         msg_Dbg(p_sout , "Creating IPv6 SAP socket with scope %s" 
    223                         , psz_v6_scope ); 
     222        msg_Dbg( p_sout, "creating IPv6 SAP socket with scope %s", 
     223                        psz_v6_scope ); 
    224224 
    225225        /* Initialize and build the IPv6 address to broadcast to */ 
    226         sap_ipv6_addr = (char *)malloc(28*sizeof(char)); 
     226        sap_ipv6_addr = (char *) malloc( 28 * sizeof(char) ); 
    227227        if ( !sap_ipv6_addr ) 
    228228        { 
    229             msg_Err( p_sout, "No memory left" ); 
    230             return NULL; 
    231         } 
    232         sprintf(sap_ipv6_addr,"%s%c%s", 
    233                          SAP_IPV6_ADDR_1, 
    234                          psz_v6_scope[0], 
    235                          SAP_IPV6_ADDR_2); 
     229            msg_Err( p_sout, "out of memory" ); 
     230            return NULL; 
     231        } 
     232        sprintf( sap_ipv6_addr, "%s%c%s", 
     233                 SAP_IPV6_ADDR_1, psz_v6_scope[0], SAP_IPV6_ADDR_2 ); 
    236234 
    237235        /* Fill the socket descriptor */ 
    238         socket_desc.i_type        = NETWORK_UDP; 
    239         socket_desc.psz_bind_addr = ""; 
    240         socket_desc.i_bind_port   = 0; 
     236        socket_desc.i_type          = NETWORK_UDP; 
     237        socket_desc.psz_bind_addr   = ""; 
     238        socket_desc.i_bind_port     = 0; 
    241239        socket_desc.psz_server_addr = sap_ipv6_addr; 
    242         socket_desc.i_server_port     = SAP_PORT; 
    243         socket_desc.i_handle          = 0; 
     240        socket_desc.i_server_port   = SAP_PORT; 
     241        socket_desc.i_handle        = 0; 
    244242 
    245243        sprintf ( psz_network, "ipv6" ); 
    246244 
    247245        /* Call the network module */ 
    248         p_sout->p_private=(void*) &socket_desc; 
     246        p_sout->p_private = (void *) &socket_desc; 
    249247        if( !( p_network = module_Need( p_sout, "network", psz_network ) ) ) 
    250248        { 
     
    254252        module_Unneed( p_sout, p_network ); 
    255253 
    256         p_new->socket   =       socket_desc.i_handle; 
    257  
    258         if(p_new->socket <= 0 ) 
    259         { 
    260             msg_Warn( p_sout, "Unable to initialize SAP" ); 
     254        p_new->i_socket = socket_desc.i_handle; 
     255        if( p_new->i_socket <= 0 ) 
     256        { 
     257            msg_Warn( p_sout, "unable to initialize SAP" ); 
    261258            return NULL; 
    262259        } 
    263260 
    264261        /* Free what we allocated */ 
    265         if( sap_ipv6_addr ) free(sap_ipv6_addr); 
    266     } 
    267  
    268     msg_Dbg (p_sout,"SAP initialization complete"); 
     262        if( sap_ipv6_addr ) 
     263        { 
     264            free( sap_ipv6_addr ); 
     265        } 
     266    } 
     267 
     268    msg_Dbg( p_sout, "SAP initialization complete" ); 
    269269 
    270270    return(p_new); 
     
    274274 * sout_SAPDelete: Deletes a SAP Session 
    275275 *****************************************************************************/ 
    276 void sout_SAPDelete( sout_instance_t *p_sout , sap_session_t * p_this ) 
     276void sout_SAPDelete( sout_instance_t *p_sout, sap_session_t * p_this ) 
    277277{ 
    278     if( close( p_this->socket ) ) 
    279     { 
    280         msg_Err ( p_sout, "Unable to close SAP socket"); 
    281     } 
    282  
    283     if( p_this ) free( p_this ); 
     278    if( close( p_this->i_socket ) ) 
     279    { 
     280        msg_Err( p_sout, "unable to close SAP socket" ); 
     281    } 
     282 
     283    free( p_this ); 
    284284} 
    285285 
     
    289289void sout_SAPSend( sout_instance_t *p_sout, sap_session_t * p_this) 
    290290{ 
    291     char *sap_head;                         /* SAP header */ 
    292     char sap_msg[1000];                     /* SDP content */ 
    293     char *sap_send;                         /* What we send */ 
    294     char *payload_type="application/sdp"; 
    295     int i_send_result=0;                    /* Result of send */ 
    296     int i; 
     291    char psz_msg[1000];                     /* SDP content */ 
     292    char *psz_head;                         /* SAP header */ 
     293    char *psz_send;                         /* What we send */ 
     294    char *psz_type = "application/sdp"; 
    297295    int i_header_size;                      /* SAP header size */ 
    298296    int i_msg_size;                         /* SDP content size */ 
    299297    int i_size;                             /* Total size */ 
     298    int i_ret = 0; 
    300299 
    301300    /* We send a packet every 24 calls to the function */ 
    302     if( p_this->sendnow == 24 ) 
    303     { 
    304         i_header_size = 9 + strlen( payload_type ); 
    305         sap_head = ( char * )malloc( i_header_size * sizeof( char ) ); 
    306  
    307         if( ! sap_head ) 
    308         { 
    309             msg_Warn( p_sout , "No memory left"); 
    310             return; 
    311         } 
    312  
    313         /* Create the SAP headers */ 
    314         sap_head[0]=0x20; /* Means IPv4, not encrypted, not compressed */ 
    315         sap_head[1]=0x00; /* No authentification */ 
    316         sap_head[2]=0x42; /* Version */ 
    317         sap_head[3]=0x12; /* Version */ 
    318  
    319         sap_head[4]=0x01; /* Source IP  FIXME: we should get the real address */ 
    320         sap_head[5]=0x02; /* idem */ 
    321         sap_head[6]=0x03; /* idem */ 
    322         sap_head[7]=0x04; /* idem */ 
    323  
    324         strncpy( sap_head+8 , payload_type , 15 ); 
    325         sap_head[ i_header_size-1 ] = '\0'; 
    326  
    327         /* Create the SDP content */ 
    328         /* Do not add spaces at beginning of the lines ! */ 
    329         sprintf( sap_msg, "v=0\n" 
    330                           "o=VideoLAN 3247692199 3247895918 IN IP4 VideoLAN\n" 
    331                           "s=%s\n" 
    332                           "u=VideoLAN\n" 
    333                           "t=0 0\n" 
    334                           "m=audio %s udp 14\n" 
    335                           "c=IN IP4 %s/15\n" 
    336                           "a=type:test\n", 
    337                  p_this->psz_name , p_this->psz_port , p_this->psz_url ); 
    338          
    339         i_msg_size = strlen( sap_msg ); 
    340         i_size = i_msg_size + i_header_size; 
    341  
    342         /* Create the message */ 
    343         sap_send = ( char* )malloc( i_size*sizeof(char) ); 
    344         if( !sap_send ) 
    345         { 
    346             msg_Err( p_sout ,  "No memory left") ; 
    347             return; 
    348         } 
    349  
    350         for( i = 0 ; i < i_header_size ; i++ ) 
    351         { 
    352             sap_send[i] = sap_head[i]; 
    353         } 
    354  
    355         for( ;  i < i_size ; i++ ) 
    356         { 
    357             sap_send[i] = sap_msg[i-i_header_size]; 
    358         } 
    359  
    360         if( i_size < 1024 ) /* We mustn't send packets larger than 1024B */ 
    361         { 
    362             if( p_this->i_ip_version == 6) 
    363             { 
    364                 i_send_result =  send( p_this->socket, sap_send, i_size, 0 ); 
    365             } 
    366             else 
    367             { 
    368                 i_send_result =  send( p_this->socket, sap_send, i_size, 0 ); 
    369             } 
    370         } 
    371  
    372         if( i_send_result == -1 ) 
    373         { 
    374             msg_Warn(p_sout, "SAP send failed on socket %i", p_this->socket ); 
    375             perror("sendto"); 
    376         } 
    377  
    378         p_this->sendnow = 0; 
    379  
    380         /* Free what we allocated */ 
    381         if(sap_send) free(sap_send); 
    382         if(sap_head) free(sap_head); 
    383     } 
    384  
    385     p_this->sendnow++; 
     301    if( p_this->i_calls++ < 24 ) 
     302    { 
     303        return; 
     304    } 
     305 
     306    i_header_size = 8 + strlen( psz_type ) + 1; 
     307    psz_head = (char *) malloc( i_header_size * sizeof( char ) ); 
     308 
     309    if( ! psz_head ) 
     310    { 
     311        msg_Err( p_sout, "out of memory" ); 
     312        return; 
     313    } 
     314 
     315    /* Create the SAP headers */ 
     316    psz_head[0] = 0x20; /* Means IPv4, not encrypted, not compressed */ 
     317    psz_head[1] = 0x00; /* No authentification */ 
     318    psz_head[2] = 0x42; /* Version */ 
     319    psz_head[3] = 0x12; /* Version */ 
     320 
     321    psz_head[4] = 0x01; /* Source IP  FIXME: we should get the real address */ 
     322    psz_head[5] = 0x02; /* idem */ 
     323    psz_head[6] = 0x03; /* idem */ 
     324    psz_head[7] = 0x04; /* idem */ 
     325 
     326    strncpy( psz_head + 8, psz_type, 15 ); 
     327    psz_head[ i_header_size-1 ] = '\0'; 
     328 
     329    /* Create the SDP content */ 
     330    /* Do not add spaces at beginning of the lines ! */ 
     331    sprintf( psz_msg, "v=0\n" 
     332                      "o=VideoLAN 3247692199 3247895918 IN IP4 VideoLAN\n" 
     333                      "s=%s\n" 
     334                      "u=VideoLAN\n" 
     335                      "t=0 0\n" 
     336                      "m=audio %s udp 14\n" 
     337                      "c=IN IP4 %s/15\n" 
     338                      "a=type:test\n", 
     339             p_this->psz_name, p_this->psz_port, p_this->psz_url ); 
     340 
     341    i_msg_size = strlen( psz_msg ); 
     342    i_size = i_msg_size + i_header_size; 
     343 
     344    /* Create the message */ 
     345    psz_send = (char *) malloc( i_size*sizeof(char) ); 
     346    if( !psz_send ) 
     347    { 
     348        msg_Err( p_sout, "out of memory" ); 
     349        return; 
     350    } 
     351 
     352    memcpy( psz_send, psz_head, i_header_size ); 
     353    memcpy( psz_send + i_header_size, psz_msg, i_msg_size ); 
     354 
     355    if( i_size < 1024 ) /* We mustn't send packets larger than 1024B */ 
     356    { 
     357        if( p_this->i_ip_version == 6 ) 
     358        { 
     359            i_ret = send( p_this->i_socket, psz_send, i_size, 0 ); 
     360        } 
     361        else 
     362        { 
     363            i_ret = send( p_this->i_socket, psz_send, i_size, 0 ); 
     364        } 
     365    } 
     366 
     367    if( i_ret <= 0 ) 
     368    { 
     369        msg_Warn( p_sout, "SAP send failed on socket %i (%s)", 
     370                          p_this->i_socket, strerror(errno) ); 
     371    } 
     372 
     373    p_this->i_calls = 0; 
     374 
     375    /* Free what we allocated */ 
     376    free( psz_send ); 
     377    free( psz_head ); 
    386378} 
     379