#include "rawSend.h" extern int sock[2]; u_long md_32(char *string, int length) { MD_CTX context; union { char c[16]; u_long x[4]; } digest; u_long r; int i; MDInit (&context); MDUpdate (&context, string, length); MDFinal ((unsigned char *)&digest, &context); r = 0; for (i = 0; i < 3; i++) { r ^= digest.x[i]; } return r; } /* md_32 */ /* * Return random unsigned 32-bit quantity. Use 'type' argument if you * need to generate several different values in close succession. */ u_int32 random32(int type) { struct { int type; struct timeval tv; clock_t cpu; pid_t pid; u_long hid; uid_t uid; gid_t gid; struct utsname name; } s; gettimeofday(&s.tv, 0); uname(&s.name); s.type = type; s.cpu = clock(); s.pid = getpid(); s.hid = gethostid(); s.uid = getuid(); s.gid = getgid(); return md_32((char *)&s, sizeof(s)); } /* random32 */ u_int16 SendFrame(u_int16 start_seq, u_int32 ts, u_int32 ssrc, u_int8 *jpeg_data, int len, u_int8 x, u_int8 y, int width, int height) { rtp_hdr_t rtphdr; struct jpeghdr jpghdr; u_int8 packet_buf[PACKET_SIZE]; u_int8 *ptr; int bytes_left = len; int data_len; /* Initialize RTP header */ rtphdr.version = 2; rtphdr.p = 0; rtphdr.x = 0; rtphdr.cc = 0; rtphdr.m = 0; rtphdr.pt = RTP_SVM_RAW; rtphdr.seq = start_seq; #ifdef DEBUG printf("SEQ # at initialization; %d \n", rtphdr.seq); #endif rtphdr.ts = htonl(ts); rtphdr.ssrc = htonl(ssrc); /* Initialize JPEG header */ //jpghdr.type =0; jpghdr.off = 0; jpghdr.x = x/2; jpghdr.y = y/2; jpghdr.width = width/2; jpghdr.height = height/2; while (bytes_left > 0) { ptr = packet_buf + RTP_HDR_SZ; jpghdr.off = htonl(jpghdr.off); /*convert offset in hdr to network order, copy to packet*/ memcpy(ptr, &jpghdr, sizeof(jpghdr)); jpghdr.off = ntohl(jpghdr.off); ptr += sizeof(jpghdr); data_len = PACKET_SIZE - (ptr - packet_buf); #ifdef DEBUG printf("data length is %d \n", data_len); #endif if (data_len >= bytes_left) { data_len = bytes_left; rtphdr.m = 1; } rtphdr.seq = htons(rtphdr.seq); #ifdef DEBUG printf("SEQ # for network byte order is; %d \n", rtphdr.seq); #endif memcpy(packet_buf, &rtphdr, RTP_HDR_SZ); memcpy(ptr, jpeg_data + jpghdr.off, data_len); if(((ptr-packet_buf)+data_len) && send(sock[0], packet_buf, (ptr - packet_buf) + data_len, 0)<0) perror("write"); jpghdr.off += data_len; #ifdef DEBUG printf("header offset is %d \n", jpghdr.off); #endif bytes_left -= data_len; rtphdr.seq = ntohs(rtphdr.seq); #ifdef DEBUG printf("SEQ # for host order is; %d \n", rtphdr.seq); #endif rtphdr.seq++; #ifdef DEBUG printf("SEQ # after increament in host is; %d \n", rtphdr.seq); #endif } return rtphdr.seq; } int GetSendSocekt(char *host, struct sockaddr_in *sin, unsigned char *ttl, char* argv0 ) { int i; if (hpt(host, (struct sockaddr *)sin, ttl) < 0) return -1; /* create/connect sockets */ for (i = 0; i < 2; i++) { sock[i] = socket(PF_INET, SOCK_DGRAM, 0); #ifdef DEBUG printf("sock[%d] is %d\n", i, sock[i]); #endif if (sock[i] < 0) { perror("socket"); exit(1); } sin->sin_port = htons(ntohs(sin->sin_port) + i); #ifdef DEBUG printf("sock[%d] port is %d\n", i, ntohs(sin->sin_port)); printf("sin_addr in network order is %u\n", sin->sin_addr.s_addr); printf("sin_addr in host order is %x\n", ntohl(sin->sin_addr.s_addr)); printf("network order IP in class_D? %d\n", IN_CLASSD(sin->sin_addr.s_addr)); printf("host IP in class_D? %d\n", IN_CLASSD(ntohl(sin->sin_addr.s_addr))); #endif if (connect(sock[i], (struct sockaddr *)sin, sizeof(*sin)) < 0) { perror("connect"); exit(1); } // notify_set_socket(sock[i], 1); if (IN_CLASSD(ntohl((sin->sin_addr.s_addr))) && (setsockopt(sock[i], IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)) { perror("IP_MULTICAST_TTL"); exit(1);} } return 0; } double tdbl(struct timeval *a) { return a->tv_sec + a->tv_usec/1e6; }