struct rtp_recorder_t* rtp_recorder_create(crypt_aes_p crypt, bool disable_audio, audio_queue_p audio_queue, struct sockaddr* local_end_point, struct sockaddr* remote_end_point, uint16_t remote_control_port, uint16_t remote_timing_port) { struct rtp_recorder_t* rr = (struct rtp_recorder_t*)malloc(sizeof(struct rtp_recorder_t)); bzero(rr, sizeof(struct rtp_recorder_t)); rr->crypt = crypt; rr->disable_audio = disable_audio; rr->audio_queue = audio_queue; rr->timer_mutex = mutex_create(); rr->timer_cond = condition_create(); rr->remote_control_end_point = sockaddr_copy(remote_end_point); rr->remote_timing_end_point = sockaddr_copy(remote_end_point); sockaddr_set_port(rr->remote_control_end_point, remote_control_port); sockaddr_set_port(rr->remote_timing_end_point, remote_timing_port); rr->streaming_socket = _rtp_recorder_create_socket(rr, "Straming socket", local_end_point, remote_end_point); rr->control_socket = _rtp_recorder_create_socket(rr, "Control socket", local_end_point, remote_end_point); rr->timing_socket = _rtp_recorder_create_socket(rr, "Timing socket", local_end_point, remote_end_point); return rr; }
uint32_t StunClient_startSTUNTrace(STUN_CLIENT_DATA* clientData, void* userCtx, const struct sockaddr* serverAddr, const struct sockaddr* baseAddr, bool useRelay, const char* ufrag, const char* password, uint8_t ttl, StunMsgId transactionId, uint32_t sockhandle, STUN_SENDFUNC sendFunc, STUNCB stunCbFunc, DiscussData* discussData) /*NULL if * none*/ { StunBindReqStruct m; STUN_TRANSACTION_DATA trans; StunMessage stunMsg; uint8_t stunBuff[STUN_MAX_PACKET_SIZE]; uint32_t len; memset( &m, 0, sizeof(m) ); m.userCtx = userCtx; sockaddr_copy( (struct sockaddr*)&m.serverAddr, serverAddr ); sockaddr_copy( (struct sockaddr*)&m.baseAddr, baseAddr ); m.useRelay = useRelay; strncpy(m.ufrag, ufrag, sizeof(m.ufrag) - 1); strncpy(m.password, password, sizeof(m.password) - 1); m.ttl = ttl; m.transactionId = transactionId; m.sockhandle = sockhandle; m.sendFunc = sendFunc; m.discussData = discussData; m.addSoftware = false; /* callback and data (owned by caller) */ m.stunCbFunc = stunCbFunc; m.stuntrace = true; StoreStunBindReq(&trans, &m); BuildStunBindReq(&trans, &stunMsg); StunClientMain(clientData, STUNCLIENT_CTX_UNKNOWN, STUN_SIGNAL_BindReq, (uint8_t*)&m); len = stunlib_encodeMessage(&stunMsg, (uint8_t*)stunBuff, STUN_MAX_PACKET_SIZE, (unsigned char*)password, /* md5key */ password ? strlen(password) : 0, /* keyLen */ NULL); return len; }
int32_t StunClient_startBindTransaction(STUN_CLIENT_DATA* clientData, void* userCtx, const struct sockaddr* serverAddr, const struct sockaddr* baseAddr, int proto, bool useRelay, const char* ufrag, const char* password, uint32_t peerPriority, bool useCandidate, bool iceControlling, uint64_t tieBreaker, StunMsgId transactionId, uint32_t sockhandle, STUN_SENDFUNC sendFunc, STUNCB stunCbFunc, DiscussData* discussData) { StunBindReqStruct m; if (clientData == NULL) { return STUNCLIENT_CTX_UNKNOWN; } memset( &m, 0, sizeof(m) ); m.userCtx = userCtx; sockaddr_copy( (struct sockaddr*)&m.serverAddr, serverAddr ); sockaddr_copy( (struct sockaddr*)&m.baseAddr, baseAddr ); strncpy(m.ufrag, ufrag, sizeof(m.ufrag) - 1); strncpy(m.password, password, sizeof(m.password) - 1); m.proto = proto; m.useRelay = useRelay; m.peerPriority = peerPriority; m.useCandidate = useCandidate; m.iceControlling = iceControlling; m.tieBreaker = tieBreaker; m.transactionId = transactionId; m.sockhandle = sockhandle; m.sendFunc = sendFunc; m.discussData = discussData; m.addSoftware = true; /* callback and data (owned by caller) */ m.stunCbFunc = stunCbFunc; StunClientMain(clientData, STUNCLIENT_CTX_UNKNOWN, STUN_SIGNAL_BindReq, (uint8_t*)&m); return 0; }
static void ICMPRespCallback(STUN_TRANSACTION_DATA* trans, const struct sockaddr* srcAddr) { STUN_CLIENT_DATA* client = trans->client; char ip_str [SOCKADDR_MAX_STRLEN]; StunCallBackData_T res; memset( &res, 0, sizeof (StunCallBackData_T) ); memcpy( &res.msgId, &trans->stunBindReq.transactionId, sizeof(StunMsgId) ); res.stunResult = StunResult_ICMPResp; res.ICMPtype = trans->ICMPtype; res.ttl = trans->ttl; res.rtt = getRTTvalue(trans); res.retransmits = trans->retransmits; sockaddr_copy( (struct sockaddr*)&res.srcAddr, srcAddr ); StunPrint( client->logUserData, client->Log_cb, StunInfoCategory_Info, "<STUNCLIENT:%02d> ICMPResp from src: %s", trans->inst, sockaddr_toString( (struct sockaddr*) &res.srcAddr, ip_str, SOCKADDR_MAX_STRLEN, true ) ); if (trans->stunBindReq.stunCbFunc) { (trans->stunBindReq.stunCbFunc)(trans->stunBindReq.userCtx, &res); } }
static void sendPacket(void* ctx, int sockfd, const uint8_t* buf, int len, const struct sockaddr* addr, int proto, bool useRelay, uint8_t ttl) { (void) ctx; (void) sockfd; (void) len; (void) proto; (void) useRelay; char addr_str[SOCKADDR_MAX_STRLEN]; /* find the transaction id so we can use this in the simulated resp */ memcpy(&LastTransId, &buf[8], STUN_MSG_ID_SIZE); memcpy(&LastTransId, &buf[8], STUN_MSG_ID_SIZE); sockaddr_copy( (struct sockaddr*)&LastAddress, addr ); sockaddr_toString(addr, addr_str, SOCKADDR_MAX_STRLEN, true); LastTTL = ttl; }
void StunClient_HandleIncResp(STUN_CLIENT_DATA* clientData, const StunMessage* msg, const struct sockaddr* srcAddr) { if (clientData == NULL) { return; } for (int i = 0; i < MAX_STUN_TRANSACTIONS; i++) { STUN_TRANSACTION_DATA* trans = &clientData->data[i]; if ( trans->inUse && TransIdIsEqual(&msg->msgHdr.id, &trans->stunBindReq.transactionId) ) { StunRespStruct m; gettimeofday(&trans->stop[trans->retransmits], NULL); memcpy( &m.stunRespMessage, msg, sizeof(m.stunRespMessage) ); sockaddr_copy( (struct sockaddr*)&m.srcAddr, srcAddr ); StunClientMain(clientData, i, StunMsgToInternalStunSig(msg), (void*)&m); return; } } StunPrint(clientData->logUserData, clientData->Log_cb, StunInfoCategory_Trace, "<STUNCLIENT> no instance with transId, discarding, msgType %d\n ", msg->msgHdr.msgType); }
void StunTraceCallBack(void* userCtx, StunTraceCallBackData_T* data) { (void) userCtx; if (data->nodeAddr == NULL) { sockaddr_copy( (struct sockaddr*)&LastHopAddr, data->nodeAddr ); } else { sockaddr_copy( (struct sockaddr*)&LastHopAddr, data->nodeAddr ); } Done = data->done; EndOfTrace = data->traceEnd; }
struct sockaddr * sockaddr_dup(const struct sockaddr *src, int flags) { struct sockaddr *dst; if ((dst = sockaddr_alloc(src->sa_family, src->sa_len, flags)) == NULL) return NULL; return sockaddr_copy(dst, dst->sa_len, src); }
void TurnStatusCallBack(void *ctx, TurnCallBackData_T *retData) { struct turn_allocation *turnResult = (struct turn_allocation *)ctx; if ( retData->turnResult == TurnResult_AllocOk ){ sockaddr_copy((struct sockaddr *)&turnResult->activeTurnServerAddr, (struct sockaddr *)&retData->TurnResultData.AllocResp.activeTurnServerAddr); sockaddr_copy((struct sockaddr *)&turnResult->rflxAddr, (struct sockaddr *)&retData->TurnResultData.AllocResp.srflxAddr); sockaddr_copy((struct sockaddr *)&turnResult->relAddrIPv4, (struct sockaddr *)&retData->TurnResultData.AllocResp.relAddrIPv4); sockaddr_copy((struct sockaddr *)&turnResult->relAddrIPv6, (struct sockaddr *)&retData->TurnResultData.AllocResp.relAddrIPv6); }else if (retData->turnResult == TurnResult_CreatePermissionOk) { turnResult->turnPerm.ok = true; }else if (retData->turnResult == TurnResult_RelayReleaseComplete){ memset(&turnResult->activeTurnServerAddr, 0,sizeof(struct sockaddr_storage)); //memset(&turnResult->hostAddr, 0,sizeof(struct sockaddr_storage)); memset(&turnResult->rflxAddr, 0,sizeof(struct sockaddr_storage)); memset(&turnResult->relAddrIPv4, 0,sizeof(struct sockaddr_storage)); memset(&turnResult->relAddrIPv6, 0,sizeof(struct sockaddr_storage)); memset(&turnResult->turnPerm, 0,sizeof(struct turn_permissions)); } turnResult->update_turninfo(); }
static void BindRespCallback(STUN_TRANSACTION_DATA* trans, const struct sockaddr* srcAddr) { STUN_CLIENT_DATA* client = trans->client; char ip_str [SOCKADDR_MAX_STRLEN]; StunCallBackData_T res; memset( &res, 0, sizeof (StunCallBackData_T) ); memcpy( &res.msgId, &trans->stunBindReq.transactionId, sizeof(StunMsgId) ); res.stunResult = StunResult_BindOk; sockaddr_copy( (struct sockaddr*)&res.rflxAddr, (struct sockaddr*)&trans->rflxAddr ); sockaddr_copy( (struct sockaddr*)&res.srcAddr, srcAddr ); sockaddr_copy( (struct sockaddr*)&res.dstBaseAddr, (struct sockaddr*)&trans->stunBindReq.baseAddr ); /* So did we loose a packet, or got an answer to the first response?*/ res.rtt = getRTTvalue(trans); res.ttl = trans->stunBindReq.ttl; StunPrint( client->logUserData, client->Log_cb, StunInfoCategory_Info, "<STUNCLIENT:%02d> BindResp from src: %s", trans->inst, sockaddr_toString( (struct sockaddr*) &res.srcAddr, ip_str, SOCKADDR_MAX_STRLEN, true ) ); if (trans->stunBindReq.stunCbFunc) { (trans->stunBindReq.stunCbFunc)(trans->stunBindReq.userCtx, &res); } }
struct sockaddr * sockaddr_externalize(struct sockaddr *dst, socklen_t socklen, const struct sockaddr *src) { struct domain *dom; dom = pffinddomain(src->sa_family); if (dom != NULL && dom->dom_sockaddr_externalize != NULL) return (*dom->dom_sockaddr_externalize)(dst, socklen, src); return sockaddr_copy(dst, socklen, src); }
struct sockaddr * sockaddr_in6_externalize(struct sockaddr *dst, socklen_t socklen, const struct sockaddr *src) { struct sockaddr_in6 *sin6; sin6 = satosin6(sockaddr_copy(dst, socklen, src)); if (sin6 == NULL || sa6_recoverscope(sin6) != 0) return NULL; return dst; }
void StunClient_HandleICMP(STUN_CLIENT_DATA* clientData, const struct sockaddr* srcAddr, uint32_t ICMPtype) { if (clientData == NULL) { return; } /* Todo: Test if this is for me.. */ StunPrint(clientData->logUserData, clientData->Log_cb, StunInfoCategory_Trace, "<STUNTRACE> StunClient_HandleICMP: Got ICMP type: %i\n ", ICMPtype); if ( isTimeExceeded(ICMPtype, srcAddr->sa_family) || isDstUnreachable(ICMPtype,srcAddr->sa_family) ) { for (int i = 0; i < MAX_STUN_TRANSACTIONS; i++) { STUN_TRANSACTION_DATA* trans = &clientData->data[i]; if ( trans->inUse && TransIdIsEqual(&clientData->traceResult.currStunMsgId, &trans->stunBindReq.transactionId) ) { StunRespStruct m; gettimeofday(&trans->stop[trans->retransmits], NULL); /* memcpy(&m.stunRespMessage, msg, sizeof(m.stunRespMessage)); */ sockaddr_copy( (struct sockaddr*)&m.srcAddr, srcAddr ); m.ICMPtype = ICMPtype; m.ttl = clientData->traceResult.currentTTL; StunClientMain(clientData, i, STUN_SIGNAL_ICMPResp, (void*)&m); return; } } } else { StunPrint(clientData->logUserData, clientData->Log_cb, StunInfoCategory_Trace, "<STUNTRACE> StunClient_HandleICMP: Ignoring ICMP Type, nothing to do\n ", ICMPtype); } }
static int SendRawStun(int sockfd, uint8_t *buf, int len, struct sockaddr *addr, bool useRelay) { char addr_str[SOCKADDR_MAX_STRLEN]; /* find the transaction id so we can use this in the simulated resp */ memcpy(&LastTransId, &buf[8], STUN_MSG_ID_SIZE); sockaddr_copy((struct sockaddr *)&LastAddress, addr); sockaddr_toString(addr, addr_str, SOCKADDR_MAX_STRLEN, true); //printf("Sendto: '%s'\n", addr_str); }
struct rtp_socket_t* rtp_socket_create(const char* name, struct sockaddr* allowed_remote_end_point) { struct rtp_socket_t* rs = (struct rtp_socket_t*)malloc(sizeof(struct rtp_socket_t)); bzero(rs, sizeof(struct rtp_socket_t)); if (allowed_remote_end_point != NULL) rs->allowed_remote_end_point = sockaddr_copy(allowed_remote_end_point); if (name) { rs->name = (char*)malloc(strlen(name) + 1); strcpy(rs->name, name); } rs->mutex = mutex_create(); return rs; }
END_TEST START_TEST (sockaddr_IPv4_copy) { struct sockaddr_storage copy; sockaddr_copy((struct sockaddr *)©, (const struct sockaddr *)sockaddr_IPv4_1); fail_unless( sockaddr_alike((struct sockaddr *)©, (struct sockaddr *)sockaddr_IPv4_1), "copy failed"); fail_if( sockaddr_alike((struct sockaddr *)©, (struct sockaddr *)sockaddr_IPv4_2), "copy failed"); }
void socket_connect(struct socket_t* s, struct sockaddr* end_point) { if (!s->is_connected && !s->is_udp) { if (s->socket <= 0) s->socket = socket(end_point->sa_family, SOCK_STREAM, IPPROTO_TCP); if (s->socket <= 0) log_message(LOG_ERROR, "Socket creation error: %s", strerror(errno)); if (s->remote_end_point != NULL) sockaddr_destroy(s->remote_end_point); s->remote_end_point = sockaddr_copy(end_point); s->receive_thread = thread_create(_socket_connect, s); } }
bool socket_bind(struct socket_t* s, struct sockaddr* end_point) { assert(end_point != NULL); if (s->local_end_point != NULL) { sockaddr_destroy(s->local_end_point); s->local_end_point = NULL; } struct sockaddr* ep = sockaddr_copy(end_point); s->local_end_point = ep; if (s->socket < 0) { s->socket = socket(ep->sa_family, (s->is_udp ? SOCK_DGRAM : SOCK_STREAM), (s->is_udp ? IPPROTO_UDP : IPPROTO_TCP)); if (s->socket < 0) { log_message(LOG_ERROR, "Socket creation error: %s", strerror(errno)); return false; } if (sockaddr_is_ipv6(ep)) { int32_t on = 1; setsockopt(s->socket, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)); } } if (!s->is_udp) { int so_reuseaddr = 1; setsockopt(s->socket, SOL_SOCKET, SO_REUSEADDR, &so_reuseaddr, sizeof(int)); } if (bind(s->socket, ep, ep->sa_len) == 0) return true; return false; }
rtp_socket_p _rtp_recorder_create_socket(struct rtp_recorder_t* rr, const char* name, struct sockaddr* local_end_point, struct sockaddr* remote_end_point) { rtp_socket_p ret = rtp_socket_create(name, remote_end_point); unsigned short p; for (p = 6000 ; p < 6100 ; p++) { struct sockaddr* ep = sockaddr_copy(local_end_point); sockaddr_set_port(ep, p); if (rtp_socket_setup(ret, ep)) { rtp_socket_set_data_received_callback(ret, _rtp_recorder_socket_data_received_callback, rr); log_message(LOG_INFO, "Setup socket on port %u", p); sockaddr_destroy(ep); return ret; } sockaddr_destroy(ep); } log_message(LOG_ERROR, "Unable to bind socket."); rtp_socket_destroy(ret); return NULL; }
void _socket_receive_loop(void* ctx) { struct socket_t* s = (struct socket_t*)ctx; mutex_lock(s->mutex); _socket_set_loop_name(s, "Receive Loop"); s->is_connected = true; void* buffer = NULL; size_t buffer_size = 0; size_t write_pos = 0; ssize_t processed = 0; ssize_t read = 0; do { if (buffer_size - write_pos < 16384) { buffer_size += 16384; buffer = realloc(buffer, buffer_size); } if (s->is_udp) { struct sockaddr_storage remote_addr; socklen_t remote_addr_len = sizeof(struct sockaddr_storage); mutex_unlock(s->mutex); read = recvfrom(s->socket, buffer + write_pos, buffer_size - write_pos, 0, (struct sockaddr*) &remote_addr, &remote_addr_len); mutex_lock(s->mutex); if (s->remote_end_point != NULL) { sockaddr_destroy(s->remote_end_point); s->remote_end_point = NULL; } s->remote_end_point = sockaddr_copy((struct sockaddr*) &remote_addr); } else { mutex_unlock(s->mutex); read = recv(s->socket, buffer + write_pos, buffer_size - write_pos, 0); mutex_lock(s->mutex); } if (read > 0) { write_pos += read; processed = write_pos; if (s->callbacks.receive != NULL) { mutex_unlock(s->mutex); processed = s->callbacks.receive(s, buffer, write_pos, s->remote_end_point, s->callbacks.ctx.receive); mutex_lock(s->mutex); } if (processed > 0) { memcpy(buffer, buffer + processed, write_pos - processed); write_pos -= processed; } } } while (read > 0 && processed >= 0); if (buffer != NULL) free(buffer); mutex_unlock(s->mutex); socket_close(s); }
int revname(int *lookup, struct sockaddr_storage *addr, char *target, size_t target_size, int rvnfd) { struct rvn rpkt; int br; struct sockaddr_un su; socklen_t fl; fd_set sockset; struct timeval tv; int sstat = 0; memset(target, 0, target_size); if (*lookup) { if (rvnfd > 0) { su.sun_family = AF_UNIX; strcpy(su.sun_path, IPTSOCKNAME); rpkt.type = RVN_REQUEST; sockaddr_copy(&rpkt.addr, addr); sendto(rvnfd, &rpkt, sizeof(struct rvn), 0, (struct sockaddr *) &su, sizeof(su.sun_family) + strlen(su.sun_path)); fl = sizeof(su.sun_family) + strlen(su.sun_path); do { tv.tv_sec = 10; tv.tv_usec = 0; FD_ZERO(&sockset); FD_SET(rvnfd, &sockset); do { sstat = select(rvnfd + 1, &sockset, NULL, NULL, &tv); } while ((sstat < 0) && (errno == EINTR)); if (FD_ISSET(rvnfd, &sockset)) br = recvfrom(rvnfd, &rpkt, sizeof(struct rvn), 0, (struct sockaddr *) &su, &fl); else br = -1; } while ((br < 0) && (errno == EINTR)); if (br < 0) { sockaddr_ntop(addr, target, target_size); printipcerr(); *lookup = 0; return RESOLVED; } strncpy(target, rpkt.fqdn, target_size - 1); return (rpkt.ready); } else { struct hostent *he = sockaddr_gethostbyaddr(addr); if (he == NULL) { sockaddr_ntop(addr, target, target_size); } else { strncpy(target, he->h_name, target_size - 1); } return RESOLVED; } } else { sockaddr_ntop(addr, target, target_size); return RESOLVED; } return NOTRESOLVED; }
int StunTrace_startTrace(STUN_CLIENT_DATA* clientData, void* userCtx, const struct sockaddr* toAddr, const struct sockaddr* fromAddr, uint32_t sockhandle, const char* ufrag, const char* password, uint32_t numTraces, STUN_TRACECB traceCbFunc, STUN_SENDFUNC sendFunc) { if (clientData == NULL) { return 0; } if (!sendFunc || !toAddr) { return 0; } struct hiutResult* result; result = &clientData->traceResult; result->currentTTL = MAX_TTL; result->userCtx = userCtx; stunlib_createId(&result->currStunMsgId); result->stunCtx = clientData; /* Fill inn the hiut struct so we get something back in the CB */ /* TODO: Fix the struct so we do not store information twice!! */ sockaddr_copy( (struct sockaddr*)&clientData->traceResult.localAddr, fromAddr ); sockaddr_copy( (struct sockaddr*)&clientData->traceResult.remoteAddr, toAddr ); result->user_max_ttl = 40; result->user_start_ttl = 1; result->wait_ms = 0; result->max_recuring = numTraces; result->user_paralell_traces = 0; result->path_max_ttl = 255; result->num_traces = 1; result->traceCb = traceCbFunc; result->sendFunc = sendFunc; result->sockfd = sockhandle; strncpy(result->username, ufrag, sizeof(result->username) - 1); strncpy(result->password, password, sizeof(result->password) - 1); StunClient_startSTUNTrace(result->stunCtx, result, toAddr, fromAddr, false, result->username, result->password, result->currentTTL, result->currStunMsgId, result->sockfd, result->sendFunc, StunStatusCallBack, NULL); return 1; }
struct othptabent *add_othp_entry(struct othptable *table, struct pkt_hdr *pkt, struct sockaddr_storage *saddr, struct sockaddr_storage *daddr, int is_ip, int protocol, char *packet2, char *ifname, int *rev_lookup, int rvnfd, int logging, FILE *logfile, int fragment) { struct othptabent *new_entry; struct othptabent *temp; new_entry = xmallocz(sizeof(struct othptabent)); new_entry->is_ip = is_ip; new_entry->fragment = fragment; if (options.mac || !is_ip) { if (pkt->pkt_hatype == ARPHRD_ETHER) { convmacaddr((char *) pkt->ethhdr->h_source, new_entry->smacaddr); convmacaddr((char *) pkt->ethhdr->h_dest, new_entry->dmacaddr); } else if (pkt->pkt_hatype == ARPHRD_FDDI) { convmacaddr((char *) pkt->fddihdr->saddr, new_entry->smacaddr); convmacaddr((char *) pkt->fddihdr->daddr, new_entry->dmacaddr); } } if (is_ip) { sockaddr_copy(&new_entry->saddr, saddr); sockaddr_copy(&new_entry->daddr, daddr); revname(rev_lookup, saddr, new_entry->s_fqdn, sizeof(new_entry->s_fqdn), rvnfd); revname(rev_lookup, daddr, new_entry->d_fqdn, sizeof(new_entry->d_fqdn), rvnfd); if (!fragment) { if (protocol == IPPROTO_ICMP) { new_entry->un.icmp.type = ((struct icmphdr *) packet2)->type; new_entry->un.icmp.code = ((struct icmphdr *) packet2)->code; } else if (protocol == IPPROTO_ICMPV6) { new_entry->un.icmp6.type = ((struct icmp6_hdr *) packet2)->icmp6_type; new_entry->un.icmp6.code = ((struct icmp6_hdr *) packet2)->icmp6_code; } else if (protocol == IPPROTO_UDP) { servlook(ntohs(((struct udphdr *) packet2)->source), IPPROTO_UDP, new_entry->un.udp.s_sname, 10); servlook(ntohs(((struct udphdr *) packet2)->dest), IPPROTO_UDP, new_entry->un.udp.d_sname, 10); } else if (protocol == IPPROTO_OSPFIGP) { new_entry->un.ospf.type = ((struct ospfhdr *) packet2)->ospf_type; new_entry->un.ospf.area = ntohl(((struct ospfhdr *) packet2)-> ospf_areaid.s_addr); inet_ntop(AF_INET, &((struct ospfhdr *)packet2)->ospf_routerid, new_entry->un.ospf.routerid, sizeof(new_entry->un.ospf.routerid)); } } } else { new_entry->linkproto = pkt->pkt_hatype; if (protocol == ETH_P_ARP) { new_entry->un.arp.opcode = ((struct arp_hdr *) packet2)->ar_op; memcpy(&(new_entry->un.arp.src_ip_address), &(((struct arp_hdr *) packet2)->ar_sip), 4); memcpy(&(new_entry->un.arp.dest_ip_address), &(((struct arp_hdr *) packet2)->ar_tip), 4); } else if (protocol == ETH_P_RARP) { new_entry->un.rarp.opcode = ((struct arphdr *) packet2)->ar_op; memcpy(&(new_entry->un.rarp.src_mac_address), &(((struct arp_hdr *) packet2)->ar_sha), 6); memcpy(&(new_entry->un.rarp.dest_mac_address), &(((struct arp_hdr *) packet2)->ar_tha), 6); } } new_entry->protocol = protocol; strcpy(new_entry->iface, ifname); new_entry->pkt_length = pkt->pkt_len; if (table->head == NULL) { new_entry->prev_entry = NULL; table->head = new_entry; table->firstvisible = new_entry; } /* * Max number of entries in the lower window is 512. Upon reaching * this figure, oldest entries are thrown out. */ if (table->count == 512) { if (table->firstvisible == table->head) { wscrl(table->othpwin, 1); printothpentry(table, table->lastvisible->next_entry, table->oimaxy - 1, logging, logfile); table->firstvisible = table->firstvisible->next_entry; table->lastvisible = table->lastvisible->next_entry; } temp = table->head; table->head = table->head->next_entry; table->head->prev_entry = NULL; free(temp); } else table->count++; if (table->tail != NULL) { new_entry->prev_entry = table->tail; table->tail->next_entry = new_entry; } table->tail = new_entry; new_entry->next_entry = NULL; table->lastpos++; new_entry->index = table->lastpos; if (table->count <= table->oimaxy) { table->lastvisible = new_entry; printothpentry(table, new_entry, table->count - 1, logging, logfile); } else if (table->lastvisible == table->tail->prev_entry) { wscrl(table->othpwin, 1); table->firstvisible = table->firstvisible->next_entry; table->lastvisible = table->tail; printothpentry(table, new_entry, table->oimaxy - 1, logging, logfile); } return new_entry; }
int createLocalUDPSocket(int ai_family, const struct sockaddr *localIp, struct sockaddr *hostaddr, uint16_t port) { int sockfd; int rv; int yes = 1; struct addrinfo hints, *ai, *p; char addr[SOCKADDR_MAX_STRLEN]; char service[8]; sockaddr_toString(localIp, addr, sizeof addr, false); //itoa(port, service, 10); snprintf(service, 8, "%d", port); //snprintf(service, 8, "%d", 3478); // get us a socket and bind it memset(&hints, 0, sizeof hints); hints.ai_family = ai_family; hints.ai_socktype = SOCK_DGRAM; hints.ai_flags = AI_NUMERICHOST | AI_ADDRCONFIG; if ((rv = getaddrinfo(addr, service, &hints, &ai)) != 0) { fprintf(stderr, "selectserver: %s ('%s')\n", gai_strerror(rv), addr); exit(1); } for (p = ai; p != NULL; p = p->ai_next) { if (sockaddr_isAddrAny(p->ai_addr) ){ //printf("Ignoring any\n"); continue; } if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("client: socket"); continue; } if (bind(sockfd, p->ai_addr, p->ai_addrlen) < 0) { printf("Bind failed\n"); close(sockfd); continue; } if (localIp != NULL){ struct sockaddr_storage ss; socklen_t len = sizeof(ss); if (getsockname(sockfd, (struct sockaddr *)&ss, &len) == -1){ perror("getsockname"); } else{ if (ss.ss_family == AF_INET) { ((struct sockaddr_in *)p->ai_addr)->sin_port = ((struct sockaddr_in *)&ss)->sin_port; }else{ ((struct sockaddr_in6 *)p->ai_addr)->sin6_port = ((struct sockaddr_in6 *)&ss)->sin6_port; } } sockaddr_copy(hostaddr, p->ai_addr); //printf("Bound to: '%s'\n", // sockaddr_toString(localIp, addr, sizeof(addr), true)); } break; } return sockfd; }
struct tcptableent *addentry(struct tcptable *table, struct sockaddr_storage *saddr, struct sockaddr_storage *daddr, int protocol, char *ifname, int *rev_lookup, int rvnfd) { struct tcptableent *new_entry; struct closedlist *ctemp; /* * Allocate and attach a new node if no closed entries found */ if (table->closedentries == NULL) { new_entry = xmalloc(sizeof(struct tcptableent)); new_entry->oth_connection = xmalloc(sizeof(struct tcptableent)); new_entry->oth_connection->oth_connection = new_entry; if (table->head == NULL) { new_entry->prev_entry = NULL; table->head = new_entry; table->firstvisible = new_entry; } if (table->tail != NULL) { table->tail->next_entry = new_entry; new_entry->prev_entry = table->tail; } table->lastpos++; new_entry->index = table->lastpos; table->lastpos++; new_entry->oth_connection->index = table->lastpos; table->tail = new_entry->oth_connection; new_entry->next_entry = new_entry->oth_connection; new_entry->next_entry->prev_entry = new_entry; new_entry->next_entry->next_entry = NULL; if (new_entry->oth_connection->index <= table->firstvisible->index + (table->imaxy - 1)) table->lastvisible = new_entry->oth_connection; else if (new_entry->index <= table->firstvisible->index + (table->imaxy - 1)) table->lastvisible = new_entry; new_entry->reused = new_entry->oth_connection->reused = 0; table->count++; rate_alloc(&new_entry->rate, 5); rate_alloc(&new_entry->oth_connection->rate, 5); print_tcp_num_entries(table); } else { /* * If we reach this point, we're allocating off the list of closed * entries. In this case, we take the top entry, let the new_entry * variable point to whatever the top is pointing to. The new_entry's * oth_connection also points to the reused entry's oth_connection */ new_entry = table->closedentries->closedentry; new_entry->oth_connection = table->closedentries->pair; ctemp = table->closedentries; table->closedentries = table->closedentries->next_entry; free(ctemp); /* * Mark the closed list's tail as NULL if we use the last entry * in the list to prevent a dangling reference. */ if (table->closedentries == NULL) table->closedtail = NULL; new_entry->reused = new_entry->oth_connection->reused = 1; /* * Delete the old hash entries for this reallocated node; */ del_tcp_hash_node(table, new_entry); del_tcp_hash_node(table, new_entry->oth_connection); } /* * Fill in address fields with raw IP addresses */ sockaddr_copy(&new_entry->saddr, saddr); sockaddr_copy(&new_entry->oth_connection->daddr, saddr); sockaddr_copy(&new_entry->daddr, daddr); sockaddr_copy(&new_entry->oth_connection->saddr, daddr); new_entry->protocol = protocol; /* * Initialize count fields */ new_entry->pcount = new_entry->bcount = 0; new_entry->win = new_entry->psize = 0; new_entry->timedout = new_entry->oth_connection->timedout = 0; new_entry->oth_connection->pcount = new_entry->oth_connection->bcount = 0; new_entry->oth_connection->win = new_entry->oth_connection->psize = 0; /* * Store interface name */ strcpy(new_entry->ifname, ifname); strcpy(new_entry->oth_connection->ifname, ifname); /* * Zero out MAC address fields */ memset(new_entry->smacaddr, 0, sizeof(new_entry->smacaddr)); memset(new_entry->oth_connection->smacaddr, 0, sizeof(new_entry->oth_connection->smacaddr)); new_entry->stat = new_entry->oth_connection->stat = 0; new_entry->s_fstat = revname(rev_lookup, &new_entry->saddr, new_entry->s_fqdn, sizeof(new_entry->s_fqdn), rvnfd); new_entry->d_fstat = revname(rev_lookup, &new_entry->daddr, new_entry->d_fqdn, sizeof(new_entry->d_fqdn), rvnfd); /* set port service names (where applicable) */ servlook(sockaddr_get_port(saddr), IPPROTO_TCP, new_entry->s_sname, 10); servlook(sockaddr_get_port(daddr), IPPROTO_TCP, new_entry->d_sname, 10); strcpy(new_entry->oth_connection->s_sname, new_entry->d_sname); strcpy(new_entry->oth_connection->d_sname, new_entry->s_sname); strcpy(new_entry->oth_connection->d_fqdn, new_entry->s_fqdn); strcpy(new_entry->oth_connection->s_fqdn, new_entry->d_fqdn); new_entry->oth_connection->s_fstat = new_entry->d_fstat; new_entry->oth_connection->d_fstat = new_entry->s_fstat; if (new_entry->index < new_entry->oth_connection->index) { new_entry->half_bracket = ACS_ULCORNER; new_entry->oth_connection->half_bracket = ACS_LLCORNER; } else { new_entry->half_bracket = ACS_LLCORNER; new_entry->oth_connection->half_bracket = ACS_ULCORNER; } new_entry->inclosed = new_entry->oth_connection->inclosed = 0; new_entry->finack = new_entry->oth_connection->finack = 0; new_entry->finsent = new_entry->oth_connection->finsent = 0; new_entry->partial = new_entry->oth_connection->partial = 0; new_entry->spanbr = new_entry->oth_connection->spanbr = 0; new_entry->conn_starttime = new_entry->oth_connection->conn_starttime = time(NULL); rate_init(&new_entry->rate); rate_init(&new_entry->oth_connection->rate); /* * Mark flow rate start time and byte counter for flow computation * if the highlight bar is on either flow of the new connection. */ if (table->barptr == new_entry) { new_entry->starttime = time(NULL); new_entry->spanbr = 0; } else if (table->barptr == new_entry->oth_connection) { new_entry->oth_connection->starttime = time(NULL); new_entry->oth_connection->spanbr = 0; } /* * Add entries to hash table */ add_tcp_hash_entry(table, new_entry); add_tcp_hash_entry(table, new_entry->oth_connection); return new_entry; }