static void udpreceive_read(t_oudpreceive *x, int sockfd) { int i, read = 0; struct sockaddr_in from; socklen_t fromlen = sizeof(from); t_atom output_atom; long addr; unsigned short port; read = recvfrom(sockfd, x->x_msginbuf, MAX_UDP_RECEIVE, 0, (struct sockaddr *)&from, &fromlen); #ifdef DEBUG post("udpreceive_read: read %lu x->x_connectsocket = %d", read, x->x_connectsocket); #endif /* get the sender's ip */ addr = ntohl(from.sin_addr.s_addr); port = ntohs(from.sin_port); x->x_addrbytes[0].a_w.w_float = (addr & 0xFF000000)>>24; x->x_addrbytes[1].a_w.w_float = (addr & 0x0FF0000)>>16; x->x_addrbytes[2].a_w.w_float = (addr & 0x0FF00)>>8; x->x_addrbytes[3].a_w.w_float = (addr & 0x0FF); x->x_addrbytes[4].a_w.w_float = port; outlet_anything(x->x_addrout, gensym("from"), 5L, x->x_addrbytes); if (read < 0) { udpreceive_sock_err(x, "udpreceive_read"); sys_closesocket(x->x_connectsocket); return; } if((read % 4) == 0){ char buf[read]; memcpy(buf, x->x_msginbuf, read); critical_exit(x->lock); omax_util_outletOSC(x->outlet, read, buf); OSC_MEM_INVALIDATE(buf); //oudp_sendData(x, t, x->slipibuf); }else{ critical_exit(x->lock); //object_error((t_object *)x, "bad packet: not a multiple of 4 length"); } /* if (read > 0) { for (i = 0; i < read; ++i) { oudp_decode(x, (unsigned char)x->x_msginbuf[i]); } oudp_decode(x, END); x->x_total_received += read; SETFLOAT(&output_atom, read); outlet_anything(x->x_addrout, gensym("received"), 1, &output_atom); }*/ }
static void udpreceive_read(t_udpreceive *x, int sockfd) { int i, read = 0; struct sockaddr_in6 from; socklen_t fromlen = sizeof(from); t_atom output_atom; long addr; unsigned short port; read = recvfrom(sockfd, x->x_msginbuf, MAX_UDP_RECEIVE, 0, (struct sockaddr *)&from, &fromlen); #ifdef DEBUG post("udpreceive_read: read %lu x->x_connectsocket = %d", read, x->x_connectsocket); #endif char buf[INET6_ADDRSTRLEN]; inet_ntop(AF_INET6, &from.sin6_addr, buf, INET6_ADDRSTRLEN ); t_atom out; out.a_type = A_SYMBOL; out.a_w.w_symbol = gensym(buf); outlet_anything(x->x_addrout, gensym("from"), 1L, &out); /* get the sender's ip */ /* addr = ntohl(from.sin6_addr); port = ntohs(from.sin6_port); x->x_addrbytes[0].a_w.w_float = (addr & 0xFF000000)>>24; x->x_addrbytes[1].a_w.w_float = (addr & 0x0FF0000)>>16; x->x_addrbytes[2].a_w.w_float = (addr & 0x0FF00)>>8; x->x_addrbytes[3].a_w.w_float = (addr & 0x0FF); x->x_addrbytes[4].a_w.w_float = port; outlet_anything(x->x_addrout, gensym("from"), 5L, x->x_addrbytes); */ if (read < 0) { udpreceive_sock_err(x, "udpreceive_read"); sys_closesocket(x->x_connectsocket); return; } if (read > 0) { for (i = 0; i < read; ++i) { /* convert the bytes in the buffer to floats in a list */ x->x_msgoutbuf[i].a_w.w_float = (float)(unsigned char)x->x_msginbuf[i]; } x->x_total_received += read; SETFLOAT(&output_atom, read); outlet_anything(x->x_addrout, gensym("received"), 1, &output_atom); /* send the list out the outlet */ if (read > 1) outlet_list(x->x_msgout, &s_list, read, x->x_msgoutbuf); else outlet_float(x->x_msgout, x->x_msgoutbuf[0].a_w.w_float); } }
static int udpreceive_new_socket(t_udpreceive *x, char *address, int port) { // return nonzero if successful in creating and binding a socket int sockfd; int intarg; int multicast_joined = 0; struct sockaddr_in6 server; struct hostent *hp; #if defined __APPLE__ || defined _WIN32 struct ipv6_mreq mreq; #else struct ipv6_mreqn mreq; #endif if (x->x_connectsocket >= 0) { // close the existing socket first sys_rmpollfn(x->x_connectsocket); sys_closesocket(x->x_connectsocket); } /* create a socket */ sockfd = socket(AF_INET6, SOCK_DGRAM, 0); #ifdef DEBUG post("udpreceive_new: socket %d port %d", sockfd, portno); #endif if (sockfd < 0) { udpreceive_sock_err(x, "udpreceive: socket"); return 0; } server.sin6_family = AF_INET6; if (address[0] == 0) server.sin6_addr = in6addr_any; else { hp = gethostbyname2(address, AF_INET6); if (hp == 0) { pd_error(x, "udpreceive: bad host?\n"); return 0; } //memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length); inet_pton(AF_INET6, address, &server.sin6_addr); } /* enable delivery of all multicast or broadcast (but not unicast) * UDP datagrams to all sockets bound to the same port */ intarg = 1; if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&intarg, sizeof(intarg)) < 0) { udpreceive_sock_err(x, "udpreceive: setsockopt (SO_REUSEADDR) failed"); return 0; } /* assign server port number */ server.sin6_port = htons((u_short)port); if(IN6_IS_ADDR_MULTICAST(&server.sin6_addr)) post("need to impletment mulitcast"); /* if a multicast address was specified, join the multicast group */ /* hop count defaults to 1 so we won't leave the subnet*/ /* if (0xE0000000 == (ntohl(server.sin_addr.s_addr) & 0xF0000000)) { server.sin_addr.s_addr = INADDR_ANY; // first bind the socket to INADDR_ANY if (bind(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0) { udpreceive_sock_err(x, "udpreceive: bind"); sys_closesocket(sockfd); return 0; } // second join the multicast group memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length); #if defined __APPLE__ || defined _WIN32 mreq.imr_multiaddr.s_addr = server.sin_addr.s_addr; mreq.imr_interface.s_addr = INADDR_ANY;// can put a specific local IP address here if host is multihomed #else mreq.imr_multiaddr.s_addr = server.sin_addr.s_addr; mreq.imr_address.s_addr = INADDR_ANY; mreq.imr_ifindex = 0; #endif //__APPLE__ || _WIN32 if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)) < 0) udpreceive_sock_err(x, "udpreceive: setsockopt IP_ADD_MEMBERSHIP"); else { multicast_joined = 1; post ("udpreceive: added to multicast group"); } } else {*/ /* name the socket */ if (bind(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0) { udpreceive_sock_err(x, "udpreceive: bind"); sys_closesocket(sockfd); return 0; } // } x->x_multicast_joined = multicast_joined; x->x_connectsocket = sockfd; x->x_total_received = 0L; sys_addpollfn(x->x_connectsocket, (t_fdpollfn)udpreceive_read, x); return 1; }