using sendto() in iPhone - memory allocation issue
<p>i was using lib to send RTP packets. after session was established, on bad quality networks i have received this error for most packets, which was send:</p>

<pre><code>2012-02-26 05:15:53.947[492:7c3b] iovlen size:12 
2012-02-26 05:15:53.950[492:7c3b] iovlen size:160 
2012-02-26 05:15:53.955[492:7c3b] Error sending rtp packet: Cannot allocate memory ; socket=26
</code></pre>

<p>Probably sockets for iPhone on gprs have some specific issues?
In bellow code i have produces that error:</p>

<pre><code>static int rtp_sendmsg(int sock,mblk_t *m, struct sockaddr *rem_addr, int addr_len){
    int error;
    struct msghdr msg;
    struct iovec iov[MAX_IOV];
    int iovlen;
    for(iovlen=0; iovlen&lt;MAX_IOV &amp;&amp; m!=NULL; m=m-&gt;b_cont,iovlen++){
        iov[iovlen].iov_base=m-&gt;b_rptr;
        iov[iovlen].iov_len=m-&gt;b_wptr-m-&gt;b_rptr;
        //ortp_warning ("iovlen size:%d",m-&gt;b_wptr-m-&gt;b_rptr);
    }
    if (iovlen==MAX_IOV){
        ortp_error("Too long msgb, didn't fit into iov, end discarded.");
    }
    msg.msg_name=(void*)rem_addr;
    msg.msg_namelen=addr_len;
    msg.msg_iov=&amp;iov[0];
    msg.msg_iovlen=iovlen;
    msg.msg_control=NULL;
    msg.msg_controllen=0;
    msg.msg_flags=0;
    error=sendmsg(sock,&amp;msg,0);
    return error;
}
</code></pre>

<p>Anybody can suggest to refactor this code (i'm not very strong in C, so final peace of code appreciated) ,this function was called from this part of code:</p>

<pre><code>int
rtp_session_rtp_send (RtpSession * session, mblk_t * m)
{
    int error;
    int i;
    rtp_header_t *hdr;
    struct sockaddr *destaddr=(struct sockaddr*)&amp;session-&gt;rtp.rem_addr;
    socklen_t destlen=session-&gt;rtp.rem_addrlen;
    ortp_socket_t sockfd=session-&gt;rtp.socket;

    hdr = (rtp_header_t *) m-&gt;b_rptr;
    /* perform host to network conversions */
    hdr-&gt;ssrc = htonl (hdr-&gt;ssrc);
    hdr-&gt;timestamp = htonl (hdr-&gt;timestamp);
    hdr-&gt;seq_number = htons (hdr-&gt;seq_number);
    for (i = 0; i &lt; hdr-&gt;cc; i++)
        hdr-&gt;csrc[i] = htonl (hdr-&gt;csrc[i]);

    if (session-&gt;flags &amp; RTP_SOCKET_CONNECTED) {
        destaddr=NULL;
        destlen=0;
    }

    if (rtp_session_using_transport(session, rtp)){
        error = (session-&gt;rtp.tr-&gt;t_sendto) (session-&gt;rtp.tr,m,0,destaddr,destlen);
        //ortp_warning ("rtp_session_using_transport passed");
    }else{
        //ortp_warning ("rtp_session_using_transport NOT passed");

#ifdef USE_SENDMSG
        error=rtp_sendmsg(sockfd,m,destaddr,destlen);
#else
        if (m-&gt;b_cont!=NULL)
            msgpullup(m,-1);
        error = sendto (sockfd, (char*)m-&gt;b_rptr, (int) (m-&gt;b_wptr - m-&gt;b_rptr),
             0,destaddr,destlen);
#endif
    }
    if (error &lt; 0){
        if (session-&gt;on_network_error.count&gt;0){
            rtp_signal_table_emit3(&amp;session-&gt;on_network_error,(long)"Error sending RTP packet",INT_TO_POINTER(getSocketErrorCode()));
        }else ortp_warning ("Error sending rtp packet: %s ; socket=%i", getSocketError(), sockfd);
        session-&gt;rtp.send_errno=getSocketErrorCode();
    }else{
        update_sent_bytes(session,error);
    }
    freemsg (m);
    return error;
}
</code></pre>
