void shell(char *name) { setaddress("i"); setaddress("am"); setaddress("arjun"); getaddress("yes"); deladdress("am"); deladdress("i"); setaddress("menon"); getaddress("arjun"); char str[STR_MAX]; puts(name); while(1) { printf("\n$ "); fflush(stdin); fgets(str,STR_MAX,stdin); str[strlen(str)-1] = '\0'; trims(str,' '); trims(str,'\t'); if(!strlen(str)) continue; if(!stricmp(str,"exit")) break; runs(str); } }
SocketPort::SocketPort(SocketService *svc, const IPV6Address &ia, tpport_t port) : Socket(AF_INET6, SOCK_DGRAM, 0) { struct sockaddr_in6 addr; memset(&addr, 0, sizeof(addr)); next = prev = NULL; service = NULL; addr.sin6_family = AF_INET6; addr.sin6_addr = getaddress(ia); addr.sin6_port = htons(port); detect_pending = true; detect_output = false; detect_disconnect = true; if(bind(so, (struct sockaddr *)&addr, (socklen_t)sizeof(addr))) { endSocket(); error(errBindingFailed,"Could not bind socket",socket_errno); return; } state = BOUND; setError(false); if(svc) svc->attach(this); }
void sanitycheck(ni_name mytag) { ni_name master, mastertag; unsigned long masteraddr; interface_list_t *l; int i; if (!getmaster(db_ni, &master, &mastertag)) { /* no master! */ system_log(LOG_ERR, "tag %s: can't determine master's host name", mytag); return; } masteraddr = getaddress(db_ni, master); if (masteraddr == 0) { /* no address for master */ system_log(LOG_ERR, "tag %s: can't determine master's address", mytag); return; } l = sys_interfaces(); if (l == NULL) return; for (i = 0; i < l->count; i++) { if ((l->interface[i].flags & IFF_UP) == 0) continue; if (l->interface[i].flags & IFF_LOOPBACK) continue; checkserves(masteraddr, mastertag, l->interface[i].addr.s_addr, mytag); } sys_interfaces_release(l); }
TCPV6Socket::TCPV6Socket(const IPV6Address &ia, tpport_t port, unsigned backlog, unsigned mss) : Socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP) { struct sockaddr_in6 addr; memset(&addr, 0, sizeof(addr)); addr.sin6_family = AF_INET6; addr.sin6_addr = getaddress(ia); addr.sin6_port = htons(port); #if defined(SO_REUSEADDR) int opt = 1; setsockopt(so, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, (socklen_t)sizeof(opt)); #endif if(bind(so, (struct sockaddr *)&addr, sizeof(addr))) { endSocket(); error(errBindingFailed,(char *)"Could not bind socket",socket_errno); return; } setSegmentSize(mss); if(listen(so, backlog)) { endSocket(); error(errBindingFailed,(char *)"Could not listen on socket",socket_errno); return; } state = BOUND; }
Socket::Error SocketPort::connect(const IPV6Address &ia, tpport_t port) { struct sockaddr_in6 addr; Error rtn = errSuccess; memset(&addr, 0, sizeof(addr)); addr.sin6_family = AF_INET6; addr.sin6_addr = getaddress(ia); addr.sin6_port = htons(port); #ifndef WIN32 long opts = fcntl(so, F_GETFL); fcntl(so, F_SETFL, opts | O_NDELAY); #else u_long opts = 1; ioctlsocket(so,FIONBIO,&opts); #endif // Win32 will crash if you try to connect to INADDR_ANY. if(!memcmp(&addr.sin6_addr, &in6addr_any, sizeof(in6addr_any))) memcpy(&addr.sin6_addr, &in6addr_loopback, sizeof(in6addr_loopback)); if(::connect(so, (struct sockaddr *)&addr, (socklen_t)sizeof(addr))) rtn = connectError(); #ifndef WIN32 fcntl(so, F_SETFL, opts); #else opts = 0; ioctlsocket(so,FIONBIO,&opts); #endif return rtn; }
Socket::Error SocketPort::connect(const IPV4Address &ia, tpport_t port) { struct sockaddr_in addr; Error rtn = errSuccess; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr = getaddress(ia); addr.sin_port = htons(port); #ifndef WIN32 long opts = fcntl(so, F_GETFL); fcntl(so, F_SETFL, opts | O_NDELAY); #else u_long opts = 1; ioctlsocket(so,FIONBIO,&opts); #endif // Win32 will crash if you try to connect to INADDR_ANY. if ( INADDR_ANY == addr.sin_addr.s_addr ) addr.sin_addr.s_addr = INADDR_LOOPBACK; if(::connect(so, (struct sockaddr *)&addr, (socklen_t)sizeof(addr))) rtn = connectError(); #ifndef WIN32 fcntl(so, F_SETFL, opts); #else opts = 0; ioctlsocket(so,FIONBIO,&opts); #endif return rtn; }
SocketPort::SocketPort(SocketService *svc, const IPV6Address &ia, tpport_t port) : Socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP) { #ifdef WIN32 // FIXME: error handling event = CreateEvent(NULL,TRUE,FALSE,NULL); #endif struct sockaddr_in6 addr; memset(&addr, 0, sizeof(addr)); next = prev = NULL; service = NULL; addr.sin6_family = AF_INET6; addr.sin6_addr = getaddress(ia); addr.sin6_port = htons(port); detect_pending = true; detect_output = false; detect_disconnect = true; if(bind(so, (struct sockaddr *)&addr, (socklen_t)sizeof(addr))) { endSocket(); error(errBindingFailed,(char *)"Could not bind socket",socket_errno); return; } state = BOUND; setError(false); if(svc) svc->attach(this); }
inline unsigned long srv_ares(char *host, int *port, char *srv) { int nfds, count, srvh_len; char *srvh; fd_set read_fds, write_fds; struct timeval *tvp, tv; caport = 0; caadr = 0; ca_tmpname = NULL; #ifdef DEBUG printf("!!! ARES query !!!\n"); #endif srvh_len = strlen(host) + strlen(srv) + 2; srvh = malloc(srvh_len); if (srvh == NULL) { printf("error: failed to allocate memory (%i) for ares query\n", srvh_len); exit_code(2); } memset(srvh, 0, srvh_len); strncpy(srvh, srv, strlen(srv)); memcpy(srvh + strlen(srv), ".", 1); strcpy(srvh + strlen(srv) + 1, host); #ifdef DEBUG printf("hostname: '%s', len: %i\n", srvh, srvh_len); #endif ares_query(channel, srvh, CARES_CLASS_C_IN, CARES_TYPE_SRV, cares_callback, (char *) NULL); #ifdef DEBUG printf("after ares_query\n"); #endif /* wait for query to complete */ while (1) { FD_ZERO(&read_fds); FD_ZERO(&write_fds); nfds = ares_fds(channel, &read_fds, &write_fds); if (nfds == 0) break; tvp = ares_timeout(channel, NULL, &tv); count = select(nfds, &read_fds, &write_fds, NULL, tvp); if (count < 0 && errno != EINVAL) { perror("ares select"); exit_code(2); } ares_process(channel, &read_fds, &write_fds); } #ifdef DEBUG printf("end of while\n"); #endif *port = caport; if (caadr == 0 && ca_tmpname != NULL) { caadr = getaddress(ca_tmpname); } if (ca_tmpname != NULL) free(ca_tmpname); free(srvh); return caadr; }
/* tries to take care of a redirection */ void handle_3xx(struct sockaddr_in *tadr) { char *uscheme, *uuser, *uhost, *contact; printf("** received redirect "); if (warning_ext == 1) { printf("from "); warning_extract(rec); printf("\n"); } else printf("\n"); /* we'll try to handle 301 and 302 here, other 3xx are to complex */ regcomp(&(regexps.redexp), "^SIP/[0-9]\\.[0-9] 30[125] ", REG_EXTENDED|REG_NOSUB|REG_ICASE); if (regexec(&(regexps.redexp), rec, 0, 0, 0) == REG_NOERROR) { /* try to find the contact in the redirect */ contact = uri_from_contact(rec); if (contact==NULL) { fprintf(stderr, "error: cannot find Contact in this " "redirect:\n%s\n", rec); exit_code(3, __PRETTY_FUNCTION__, "missing Contact header in reply"); } /* correct our request */ uri_replace(req, contact); new_transaction(req, rep); /* extract the needed information*/ rport = 0; address = 0; parse_uri(contact, &uscheme, &uuser, &uhost, &rport); if (!rport) address = getsrvadr(uhost, &rport, &transport); if (!address) address = getaddress(uhost); if (!address){ fprintf(stderr, "error: cannot determine host " "address from Contact of redirect:" "\n%s\n", rec); exit_code(2, __PRETTY_FUNCTION__, "missing host in Contact header"); } if (!rport) { rport = 5060; } free(contact); if (!outbound_proxy) cdata.connected = set_target(tadr, address, rport, cdata.csock, cdata.connected); } else { fprintf(stderr, "error: cannot handle this redirect:" "\n%s\n", rec); exit_code(2, __PRETTY_FUNCTION__, "unsupported redirect reply"); } }
void get_id(Messenger *m, char *data) { sprintf(data, "[i] ID: "); int offset = strlen(data); int i = 0; uint8_t address[FRIEND_ADDRESS_SIZE]; getaddress(m, address); for (; i < FRIEND_ADDRESS_SIZE; i++) { sprintf(data + 2 * i + offset, "%02X ", address[i]); } }
END_TEST START_TEST(test_messenger_state_saveload_encrypted) { uint8_t addr[FRIEND_ADDRESS_SIZE]; getaddress(m, addr); Messenger *m_temp = new_messenger(TOX_ENABLE_IPV6_DEFAULT); size_t size = messenger_size_encrypted(m); uint8_t buffer[size]; messenger_save_encrypted(m, buffer, "Gentoo", sizeof("Gentoo")); ck_assert_msg(messenger_load_encrypted(m_temp, buffer, size, "Ubuntu", sizeof("Ubuntu")) == -1, "Bad password didn't make the function fail."); ck_assert_msg(messenger_load_encrypted(m_temp, buffer, size, "Gentoo", sizeof("Gentoo")) == 0, "Good password didn't make the function succeed."); uint8_t addr1[FRIEND_ADDRESS_SIZE]; getaddress(m_temp, addr1); ck_assert_msg(memcmp(addr1, addr, FRIEND_ADDRESS_SIZE) == 0, "Didn't load messenger successfully"); kill_messenger(m_temp); }
static inline unsigned long srv_ares(char *host, int *port, char *srv) { int nfds, count, srvh_len; char *srvh; fd_set read_fds, write_fds; struct timeval *tvp, tv; caport = 0; caadr = 0; ca_tmpname = NULL; dbg("starting ARES query\n"); srvh_len = strlen(host) + strlen(srv) + 2; srvh = malloc(srvh_len); if (srvh == NULL) { printf("error: failed to allocate memory (%i) for ares query\n", srvh_len); exit_code(2, __PRETTY_FUNCTION__, "memory allocation failure"); } memset(srvh, 0, srvh_len); strncpy(srvh, srv, strlen(srv)); memcpy(srvh + strlen(srv), ".", 1); strcpy(srvh + strlen(srv) + 1, host); dbg("hostname: '%s', len: %i\n", srvh, srvh_len); ares_query(channel, srvh, CARES_CLASS_C_IN, CARES_TYPE_SRV, cares_callback, (char *) NULL); dbg("ares_query finished, waiting for result...\n"); /* wait for query to complete */ while (1) { FD_ZERO(&read_fds); FD_ZERO(&write_fds); nfds = ares_fds(channel, &read_fds, &write_fds); if (nfds == 0) break; tvp = ares_timeout(channel, NULL, &tv); count = select(nfds, &read_fds, &write_fds, NULL, tvp); if (count < 0 && errno != EINVAL) { perror("ares select"); exit_code(2, __PRETTY_FUNCTION__, "ares DNS resolution failure"); } ares_process(channel, &read_fds, &write_fds); } dbg("ARES answer processed\n"); *port = caport; if (caadr == 0 && ca_tmpname != NULL) { caadr = getaddress(ca_tmpname); } if (ca_tmpname != NULL) free(ca_tmpname); free(srvh); return caadr; }
Socket::Error UDPReceive::connect(const IPV6Host &ia, tpport_t port) { int len = sizeof(peer.ipv6); peer.ipv6.sin6_family = AF_INET6; peer.ipv6.sin6_addr = getaddress(ia); peer.ipv6.sin6_port = htons(port); // Win32 will crash if you try to connect to INADDR_ANY. if(!memcmp(&peer.ipv6.sin6_addr, &in6addr_any, sizeof(in6addr_any))) memcpy(&peer.ipv6.sin6_addr, &in6addr_loopback, sizeof(in6addr_loopback)); if(::connect(so, (sockaddr *)&peer.ipv6, len)) return connectError(); return errSuccess; }
Socket::Error UDPTransmit::cConnect(const IPV4Address &ia, tpport_t port) { int len = sizeof(peer.ipv4); peer.ipv4.sin_family = AF_INET; peer.ipv4.sin_addr = getaddress(ia); peer.ipv4.sin_port = htons(port); // Win32 will crash if you try to connect to INADDR_ANY. if ( INADDR_ANY == peer.ipv4.sin_addr.s_addr ) peer.ipv4.sin_addr.s_addr = INADDR_LOOPBACK; if(::connect(so, (sockaddr *)&peer.ipv4, len)) return connectError(); return errSuccess; }
int main(void) { char *status; float mem; int bat0; char *datetime; char *address; char battext[6]; char *volume; if (!(dpy = XOpenDisplay(NULL))) { fprintf(stderr, "Cannot open display.\n"); return 1; } status = xmalloc(200); for (;;sleep(1)) { datetime = getdatetime(); mem = getmeminfo(); bat0 = getbattery(); address = getaddress(); volume = getvolume(); if (ischarging()) strcpy(battext, "ac:"); else { if (bat0 > 50) strcpy(battext, "bat:"); else if (bat0 < 20) strcpy(battext, "bat:"); else strcpy(battext, "bat:"); } snprintf(status, 200, "|%s|mem:%0.0f%%|%s%d%%|vol:%s|%s|", address, mem, battext, bat0, volume, datetime); free(datetime); free(address); free(volume); setstatus(status); } free(status); XCloseDisplay(dpy); return 0; }
void cmd_myid(ToxWindow *self, Messenger *m, char **args) { char id[FRIEND_ADDRESS_SIZE * 2 + 1] = {0}; size_t i; uint8_t address[FRIEND_ADDRESS_SIZE]; getaddress(m, address); for (i = 0; i < FRIEND_ADDRESS_SIZE; ++i) { char xx[3]; snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff); strcat(id, xx); } wprintw(self->window, "%s\n", id); }
SocketPort::SocketPort(SocketService *svc, const IPV6Host &ih, tpport_t port) : Socket(AF_INET6, SOCK_STREAM, 0) { struct sockaddr_in6 addr; memset(&addr, 0, sizeof(addr)); next = prev = NULL; service = NULL; addr.sin6_family = AF_INET6; addr.sin6_addr = getaddress(ih); addr.sin6_port = htons(port); detect_pending = true; detect_disconnect = true; long opts = fcntl(so, F_GETFL); fcntl(so, F_SETFL, opts | O_NDELAY); int rtn = ::connect(so, (struct sockaddr *)&addr, (socklen_t)sizeof(addr)); if(!rtn) { state = CONNECTED; } else { if(errno == EINPROGRESS) { state = CONNECTING; } else { endSocket(); connectError(); return; } } fcntl(so, F_SETFL, opts); setError(false); detect_output = (state == CONNECTING); if(svc) svc->attach(this); // if(state == CONNECTING) // setDetectOutput(true); }
static ni_status start_service(ni_name tag) { ni_name master; ni_name mastertag; ni_status status; ni_name dbname; struct in_addr inaddr; system_log(LOG_DEBUG, "directory cleanup"); dir_cleanup(tag); dir_getnames(tag, &dbname, NULL, NULL); system_log(LOG_DEBUG, "initializing server"); status = ni_init(dbname, &db_ni); ni_name_free(&dbname); if (status != NI_OK) { system_log(LOG_ERR, "ni_init failed: %s", ni_error(status)); return (status); } system_log(LOG_DEBUG, "checksum = %u", ni_getchecksum(db_ni)); /* "local" is never a clone */ if (strcmp(tag, "local")) { if (getmaster(db_ni, &master, &mastertag)) { inaddr.s_addr = getaddress(db_ni, master); if (!sys_is_my_address(&inaddr)) i_am_clone++; if (!ni_name_match(tag, mastertag)) i_am_clone++; ni_name_free(&master); ni_name_free(&mastertag); } } if (forcedIsRoot == 0) forcedIsRoot = get_forced_root(db_ni); system_log(LOG_DEBUG, "registering tag %s", tag); status = register_it(tag); return (status); }
Socket::Error SocketPort::connect(const IPV6Address &ia, tpport_t port) { struct sockaddr_in6 addr; Error rtn = errSuccess; memset(&addr, 0, sizeof(addr)); addr.sin6_family = AF_INET6; addr.sin6_addr = getaddress(ia); addr.sin6_port = htons(port); long opts = fcntl(so, F_GETFL); fcntl(so, F_SETFL, opts | O_NDELAY); if(!memcmp(&addr.sin6_addr, &in6addr_any, sizeof(in6addr_any))) memcpy(&addr.sin6_addr, &in6addr_loopback, sizeof(in6addr_loopback)); if(::connect(so, (struct sockaddr *)&addr, (socklen_t)sizeof(addr))) rtn = connectError(); fcntl(so, F_SETFL, opts); return rtn; }
bool address_lookup(_addr *res, const char *name, int port) { struct hostent *ent; if (strchr(name, ':')) { /* try ipv6 */ getaddress(res, name, port); return true; } ent = gethostbyname(name); if (!ent) return false; memset(res, 0, sizeof(_addr)); #ifdef HAVE_SIN_LEN ((sockaddr_in *)res)->sin_len = sizeof(_addr); #endif ((sockaddr_in *)res)->sin_family = ent->h_addrtype; ((sockaddr_in *)res)->sin_port = htons(port); memcpy(&((sockaddr_in *)res)->sin_addr, ent->h_addr, ent->h_length); return true; }
/** * Initialize a socket address from a coap URI. * @return a pointer to the address or 0 on failure. */ sockaddr_t* mc_uri_to_address(sockaddr_t* const addr, char* const uri) { sockaddr_t* result; char* host; uint32_t port; /* Skip the scheme. */ char* current = advanceto(uri, ":"); current = advanceby(current, "://"); if (!current) { return 0; } host = getaddress(current); if (!host) { return 0; } if (strlen(host) == 0) { ms_free(host); return 0; } /* Check and compensate for IP v6 []'s if needed. */ if (current && (*current == '[')) current += 2; current += strlen(host); port = getport(current); if (port == 0) { ms_free(host); return 0; } result = mn_sockaddr_inet_init(addr, host, port); ms_free(host); return result; }
int main(int argc, char *argv[]) { FILE *pf; char buff[BUFSIZE]; int c, i, port; unsigned int tsp; char *scheme, *user, *host, *backup; pid_t pid; struct timespec ts; int upp; #ifdef HAVE_GETOPT_LONG int option_index = 0; static struct option l_opts[] = { {"help", 0, 0, 'X'}, {"version", 0, 0, 'V'}, {"filename", 1, 0, 'f'}, {"sip-uri", 1, 0, 's'}, {"traceroute-mode", 0, 0, 'T'}, {"usrloc-mode", 0, 0, 'U'}, {"invite-mode", 0, 0, 'I'}, {"message-mode", 0, 0, 'M'}, {"contact", 1, 0, 'C'}, {"appendix-begin", 1, 0, 'b'}, {"appendix-end", 1, 0, 'e'}, {"sleep", 1, 0, 'o'}, {"expires", 1, 0, 'x'}, {"remove-bindings", 1, 0, 'z'}, {"flood-mode", 0, 0, 'F'}, {"random-mode", 0, 0, 'R'}, {"trash-chars", 1, 0, 't'}, {"local-port", 1, 0, 'l'}, {"remote-port", 1, 0, 'r'}, {"outbound-proxy", 1, 0, 'p'}, {"hostname", 1, 0, 'H'}, {"max-fowards", 1, 0, 'm'}, {"numeric", 0, 0, 'n'}, {"no-via", 0, 0, 'i'}, {"password", 1, 0, 'a'}, {"ignore-redirects", 0, 0, 'd'}, {"verbose", 0, 0, 'v'}, {"extract-ip", 0, 0, 'w'}, {"replace-string", 0, 0, 'g'}, {"replace", 0, 0, 'G'}, {"nagios-code", 0, 0, 'N'}, {"nagios-warn", 1, 0, 'W'}, {"search", 1, 0, 'q'}, {"message-body", 1, 0, 'B'}, {"disposition", 1, 0, 'O'}, {"processes", 1, 0, 'P'}, {"auth-username", 1, 0, 'u'}, {"no-crlf", 0, 0, 'L'}, {"timing", 1, 0, 'A'}, {"symmetric", 0, 0, 'S'}, {"from", 1, 0, 'c'}, {"timeout-factor", 1, 0, 'D'}, {"transport", 1, 0, 'E'}, {"headers", 1, 0, 'j'}, {0, 0, 0, 0} }; #endif /* some initialisation to be shure */ file_b=uri_b=trace=lport=usrloc=flood=verbose=randtrash=trashchar = 0; warning_ext=rand_rem=nonce_count=replace_b=invite=message = 0; sleep_ms=empty_contact=nagios_warn=timing=outbound_proxy=symmetric = 0; namebeg=nameend=maxforw= -1; numeric=via_ins=redirects=fix_crlf=processes = 1; username=password=replace_str=hostname=contact_uri=mes_body = NULL; con_dis=auth_username=from_uri=headers = NULL; scheme = user = host = backup = req = rep = rec = NULL; re = NULL; address= 0; transport=tsp = 0; rport = port = 0; expires_t = USRLOC_EXP_DEF; inv_final = 64 * SIP_T1; memset(buff, 0, BUFSIZE); memset(fqdn, 0, FQDN_SIZE); if (argc==1) { print_help(); } /* lots of command line switches to handle*/ #ifdef HAVE_GETOPT_LONG while ((c=getopt_long(argc, argv, "a:A:b:B:c:C:dD:e:E:f:Fg:GhH:iIj:l:Lm:MnNo:O:p:P:q:r:Rs:St:Tu:UvVwW:x:Xz:", l_opts, &option_index)) != EOF){ #else while ((c=getopt(argc, argv, "a:A:b:B:c:C:dD:e:E:f:Fg:GhH:iIj:l:Lm:MnNo:O:p:P:q:r:Rs:St:Tu:UvVwW:x:z:")) != EOF){ #endif switch(c){ case 'a': if (strlen(optarg) == 1 && STRNCASECMP(optarg, "-", 1) == 0) { password = str_alloc(SIPSAK_MAX_PASSWD_LEN); printf("Please enter the password (max. length %i): ", SIPSAK_MAX_PASSWD_LEN); if (read_stdin(password, SIPSAK_MAX_PASSWD_LEN, 1) == 0) { exit_code(0); } } else { password=str_alloc(strlen(optarg) + 1); strncpy(password, optarg, strlen(optarg)); } break; case 'A': timing=str_to_int(optarg); break; case 'b': namebeg=str_to_int(optarg); break; case 'B': mes_body=str_alloc(strlen(optarg) + 1); strncpy(mes_body, optarg, strlen(optarg)); break; case 'c': backup=str_alloc(strlen(optarg)+1); strncpy(backup, optarg, strlen(optarg)); parse_uri(backup, &scheme, &user, &host, &port); if (scheme == NULL) { fprintf(stderr, "error: missing scheme in From URI\n"); exit_code(2); } else if (user == NULL) { fprintf(stderr, "error: missing username in From URI\n"); exit_code(2); } else if (host == NULL) { fprintf(stderr, "error: missing host in From URI\n"); exit_code(2); } else { from_uri=str_alloc(strlen(optarg)+1); strncpy(from_uri, optarg, strlen(optarg)); } free(backup); break; case 'C': if ((strlen(optarg) == 5 && STRNCASECMP(optarg, "empty", 5) == 0) || (strlen(optarg) == 4 && STRNCASECMP(optarg, "none", 4) == 0)) { empty_contact = 1; } else if (strlen(optarg) == 1 && STRNCASECMP(optarg, "*", 1) == 0) { contact_uri=str_alloc(strlen(optarg)+1); strncpy(contact_uri, optarg, strlen(optarg)); } else { backup=str_alloc(strlen(optarg)+1); strncpy(backup, optarg, strlen(optarg)); parse_uri(backup, &scheme, &user, &host, &port); if (scheme == NULL) { fprintf(stderr, "error: REGISTER Contact uri doesn't not contain " "sip:, sips:, *, or is not empty\n"); exit_code(2); } else if (user == NULL) { fprintf(stderr, "error: missing username in Contact uri\n"); exit_code(2); } else if (host == NULL) { fprintf(stderr, "error: missing host in Contact uri\n"); exit_code(2); } else { contact_uri=str_alloc(strlen(optarg)+1); strncpy(contact_uri, optarg, strlen(optarg)); } free(backup); } break; case 'd': redirects=0; break; case 'D': inv_final = str_to_int(optarg) * SIP_T1; break; case 'e': nameend=str_to_int(optarg); break; case 'E': if (strlen(optarg) == 3 && STRNCASECMP(optarg, "udp", 3) == 0) { transport = SIP_UDP_TRANSPORT; } else if (strlen(optarg) == 3 && STRNCASECMP(optarg, "tcp", 3) == 0) { transport = SIP_TCP_TRANSPORT; } else if (strlen(optarg) == 3 && STRNCASECMP(optarg, "tls", 3) == 0) { fprintf(stderr, "error: TLS is not supported yet, supported values: udp, tcp\n"); exit_code(2); transport = SIP_TLS_TRANSPORT; } else { fprintf(stderr, "error: unsupported transport '%s', supported values: udp, tcp\n", optarg); exit_code(2); } break; case 'F': flood=1; break; case 'f': if (strlen(optarg) != 1 && STRNCASECMP(optarg, "-", 1) != 0) { /* file is opened in binary mode so that the cr-lf is preserved */ pf = fopen(optarg, "rb"); if (!pf){ fprintf(stderr, "error: unable to open the file '%s'.\n", optarg); exit_code(2); } if (fread(buff, 1, sizeof(buff), pf) >= sizeof(buff)){ fprintf(stderr, "error:the file is too big. try files of less " "than %i bytes.\n", BUFSIZE); fprintf(stderr, " or recompile the program with bigger " "BUFSIZE defined.\n"); exit_code(2); } fclose(pf); } else if (strlen(optarg) == 1 && STRNCASECMP(optarg, "-", 1) == 0) { if (read_stdin(&buff[0], sizeof(buff), 0) == 0) { exit_code(0); } } else { fprintf(stderr, "error: unable to handle input file name: %s\n", optarg); exit_code(2); } file_b=1; break; case 'g': replace_str=optarg; break; case 'G': replace_b=1; break; case 'h': print_help(); break; case 'H': hostname=optarg; break; case 'i': via_ins=0; break; case 'I': invite=1; break; case 'j': headers=optarg; break; case 'l': lport=str_to_int(optarg); break; case 'L': fix_crlf=0; break; case 'm': maxforw=str_to_int(optarg); break; case 'M': message=1; break; case 'n': numeric = 0; break; case 'N': exit_mode=EM_NAGIOS; break; case 'o': sleep_ms = 0; if (strlen(optarg) == 4 && STRNCASECMP(optarg, "rand", 4) == 0) { sleep_ms = -2; } else { sleep_ms = str_to_int(optarg); } break; case 'O': con_dis=str_alloc(strlen(optarg) + 1); strncpy(con_dis, optarg, strlen(optarg)); break; case 'p': parse_uri(optarg, &scheme, &user, &host, &rport); if (host == NULL) { fprintf(stderr, "error: missing in host in outbound proxy\n"); exit_code(2); } if (is_ip(host)) { address = getaddress(host); if (transport == 0) transport = SIP_UDP_TRANSPORT; } else { if (!rport) { address = getsrvadr(host, &rport, &tsp); if (tsp != 0) transport = tsp; } if (!address) { address = getaddress(host); if (address && verbose > 1) printf("using A record: %s\n", host); } if (!address){ fprintf(stderr, "error:unable to determine the outbound proxy " "address\n"); exit_code(2); } } outbound_proxy=1; break; case 'P': processes=str_to_int(optarg); break; case 'q': if (re) { /* previously allocated -- free */ regfree(re); } else { /* never tried -- allocate */ re=malloc(sizeof(regex_t)); }; if (!re) { fprintf(stderr, "Error: can't allocate RE\n"); exit_code(2); }; if (regcomp(re, optarg, REG_EXTENDED|REG_ICASE|REG_NEWLINE )!=0) { fprintf(stderr, "Error: compiling RE: %s\n", optarg ); exit_code(2); }; break; case 'r': port = 0; port=str_to_int(optarg); if (rport) { fprintf(stderr, "warning: you are overwritting the destination port with the r argument\n"); } rport = port; break; case 'R': randtrash=1; break; case 's': parse_uri(optarg, &scheme, &user, &host, &port); if (scheme == NULL) { fprintf(stderr, "error: missing scheme in sip uri\n"); exit_code(2); } if (strlen(optarg) == 4 && STRNCASECMP(optarg,"sips",4) == 0){ fprintf(stderr, "error: sips is not supported yet\n"); exit_code(2); } else if (strlen(optarg) != 3 || STRNCASECMP(optarg,"sip",3) != 0){ fprintf(stderr, "error: scheme of sip uri has to be sip\n"); exit_code(2); } if (user != NULL) { username = user; } if (host != NULL) { domainname = host; } else { fprintf(stderr, "error: missing hostname in sip uri\n"); exit_code(2); } if (port && !rport) { rport = port; } if (is_ip(domainname)) { address = getaddress(domainname); if (transport == 0) transport = SIP_UDP_TRANSPORT; } else { if (!rport && !address) { address = getsrvadr(domainname, &rport, &tsp); if (tsp != 0) transport = tsp; } if (!address) { address = getaddress(domainname); if (address && verbose > 1) printf("using A record: %s\n", domainname); } if (!address){ fprintf(stderr, "error:unable to determine the IP address for: %s\n", domainname); exit_code(2); } } if (port != 0) { backup = str_alloc(strlen(domainname)+1+6); snprintf(backup, strlen(domainname)+6, "%s:%i", domainname, port); domainname = backup; } uri_b=1; break; case 'S': fprintf(stderr, "warning: symmetric does not work with a-symmetric servers\n"); symmetric=1; break; case 't': trashchar=str_to_int(optarg); break; case 'T': trace=1; break; case 'U': usrloc=1; break; case 'u': auth_username=str_alloc(strlen(optarg) + 1); strncpy(auth_username, optarg, strlen(optarg)); break; case 'v': verbose++; break; case 'V': printf("sipsak %s by Nils Ohlmeier\n Copyright (C) 2002-2004" " FhG Fokus\n Copyright (C) 2004-2005 Nils Ohlmeier\n", SIPSAK_VERSION); printf(" compiled with DEFAULT_TIMEOUT=%i, FQDN_SIZE=%i", DEFAULT_TIMEOUT, FQDN_SIZE); #ifdef RAW_SUPPORT printf(", RAW_SUPPORT"); #endif #ifdef HAVE_GETOPT_LONG printf(", LONG_OPTS"); #endif #ifdef HAVE_GNUTLS printf(", GNUTLS_MD5"); #else # ifdef HAVE_FULL_OPENSSL printf(", OPENSSL_MD5"); # else printf(", INTERNAL_MD5"); # endif #endif #ifdef HAVE_OPENSSL_SHA1 printf(", OPENSSL_SHA1"); #endif #ifdef HAVE_CARES_H printf(", SRV_SUPPORT(ARES)"); #else # ifdef HAVE_RULI_H printf(", SRV_SUPPORT(RULI)"); # endif #endif #ifdef HAVE_STRCASESTR printf(", STR_CASE_INSENSITIVE"); #endif #ifdef HAVE_STRNCASECMP printf(", CMP_CASE_INSENSITIVE"); #endif printf("\n"); exit_code(0); break; case 'w': warning_ext=1; break; case 'W': nagios_warn = str_to_int(optarg); break; case 'x': expires_t=str_to_int(optarg); break; #ifdef HAVE_GETOPT_LONG case 'X': print_long_help(); break; #endif case 'z': rand_rem=str_to_int(optarg); if (rand_rem < 0 || rand_rem > 100) { fprintf(stderr, "error: z option must between 0 and 100\n"); exit_code(2); } break; default: fprintf(stderr, "error: unknown parameter %c\n", c); exit_code(2); break; } } if (rport == 0) { rport = 5060; } if (rport > 65535 || rport <= 0) { fprintf(stderr, "error: invalid remote port: %i\n", rport); exit_code(2); } if (transport == 0) { transport = SIP_UDP_TRANSPORT; } /* replace LF with CRLF if we read from a file */ if ((file_b) && (fix_crlf)) { insert_cr(buff); } if (headers) { backup = str_alloc(strlen(headers) + 30); // FIXME strcpy(backup, headers); headers = backup; replace_string(headers, "\\n", "\r\n"); backup = headers + strlen(headers) - 1; if (*backup != '\n') { strcpy(backup + 1, "\r\n"); } if (file_b) insert_header(buff, headers, 1); } /* lots of conditions to check */ if (trace) { if (usrloc || flood || randtrash) { fprintf(stderr, "error: trace can't be combined with usrloc, random or " "flood\n"); exit_code(2); } if (!uri_b) { fprintf(stderr, "error: for trace mode a SIPURI is realy needed\n"); exit_code(2); } if (file_b) { fprintf(stderr, "warning: file will be ignored for tracing."); } if (!username) { fprintf(stderr, "error: for trace mode without a file the SIPURI have to " "contain a username\n"); exit_code(2); } if (!via_ins){ fprintf(stderr, "warning: Via-Line is needed for tracing. Ignoring -i\n"); via_ins=1; } if (!warning_ext) { fprintf(stderr, "warning: IP extract from warning activated to be more " "informational\n"); warning_ext=1; } if (maxforw==-1) maxforw=255; } else if (usrloc || invite || message) { if (trace || flood || randtrash) { fprintf(stderr, "error: usrloc can't be combined with trace, random or " "flood\n"); exit_code(2); } if (!username || !uri_b) { fprintf(stderr, "error: for the USRLOC mode you have to give a SIPURI with " "a username\n at least\n"); exit_code(2); } if (namebeg>0 && nameend==-1) { fprintf(stderr, "error: if a starting numbers is given also an ending " "number have to be specified\n"); exit_code(2); } if (invite && message) { fprintf(stderr, "error: invite and message tests are XOR\n"); exit_code(2); } if (!usrloc && invite && !lport) { fprintf(stderr, "warning: Do NOT use the usrloc invite mode without " "registering sipsak before.\n See man page for " "details.\n"); exit_code(2); } if (contact_uri!=NULL) { if (invite || message) { fprintf(stderr, "error: Contact uri is not support for invites or " "messages\n"); exit_code(2); } if (nameend!=-1 || namebeg!=-1) { fprintf(stderr, "warning: ignoring starting or ending number if Contact" " is given\n"); nameend=namebeg=0; } if (rand_rem) { fprintf(stderr, "warning: ignoring -z option when Contact is given\n"); rand_rem=0; } } if (via_ins) { fprintf(stderr, "warning: ignoring -i option when in usrloc mode\n"); via_ins=0; } if (nameend==-1) nameend=0; if (namebeg==-1) namebeg=0; } else if (flood) { if (trace || usrloc || randtrash) { fprintf(stderr, "error: flood can't be combined with trace, random or " "usrloc\n"); exit_code(2); } if (!uri_b) { fprintf(stderr, "error: we need at least a sip uri for flood\n"); exit_code(2); } if (redirects) { fprintf(stderr, "warning: redirects are not expected in flood. " "disableing\n"); redirects=0; } } else if (randtrash) { if (trace || usrloc || flood) { fprintf(stderr, "error: random can't be combined with trace, flood or " "usrloc\n"); exit_code(2); } if (!uri_b) { fprintf(stderr, "error: need at least a sip uri for random\n"); exit_code(2); } if (redirects) { fprintf(stderr, "warning: redirects are not expected in random. " "disableing\n"); redirects=0; } if (verbose) { fprintf(stderr, "warning: random characters may destroy your terminal " "output\n"); } } else if (mes_body) { if (!message) { fprintf(stderr, "warning: to send a message mode (-M) is required. activating\n"); message=1; } if (!uri_b) { fprintf(stderr, "error: need at least a sip uri to send a meesage\n"); exit_code(2); } if (nameend==-1) nameend=0; if (namebeg==-1) namebeg=0; } else { if (!uri_b) { fprintf(stderr, "error: a spi uri is needed at least\n"); exit_code(2); } } switch (transport) { case SIP_TLS_TRANSPORT: transport_str = TRANSPORT_TLS_STR; break; case SIP_TCP_TRANSPORT: transport_str = TRANSPORT_TCP_STR; break; case SIP_UDP_TRANSPORT: transport_str = TRANSPORT_UDP_STR; break; default: fprintf(stderr, "unknown transport: %i\n", transport); exit_code(2); } /* determine our hostname */ get_fqdn(); /* this is not a cryptographic random number generator, but hey this is only a test-tool => should be satisfying*/ srand(time(0) ^ getpid()); if (processes > 1) { if (signal(SIGCHLD , sigchld_handler) == SIG_ERR ) { fprintf(stderr, "error: Could not install SIGCHLD handler\n"); exit_code(2); } } for(i = 0; i < processes - 1; i++) { if ((pid = fork()) < 0) { fprintf(stderr, "error: Cannot fork\n"); exit_code(2); } if (pid == 0){ /* child */ upp = (nameend - namebeg + 1) / processes; namebeg = namebeg + upp * i; nameend = namebeg + upp; shoot(&buff[0], sizeof(buff)); } else { if (lport) { lport++; } } /* Delay execution of children so that the * time of the first transmission gets spread over * the retransmission interval evenly */ ts.tv_sec = 0; ts.tv_nsec = (float)DEFAULT_TIMEOUT / (float)processes * (float)1000 * (float)1000; nanosleep(&ts, 0); } /* here we go...*/ if (processes > 1) { upp = (nameend - namebeg + 1) / processes; namebeg = namebeg + upp * i; nameend = namebeg + upp; } shoot(&buff[0], sizeof(buff)); /* normaly we won't come back here, but to satisfy the compiler */ return 0; }
int main(int argc, char *argv[]) { if (argc < 4 && argc != 2) { printf("usage %s ip port public_key (of the DHT bootstrap node)\n or\n %s Save.bak\n", argv[0], argv[0]); exit(0); } m = initMessenger(); if ( !m ) { fputs("Failed to allocate messenger datastructure\n", stderr); exit(0); } if (argc > 3) { IP_Port bootstrap_ip_port; bootstrap_ip_port.port = htons(atoi(argv[2])); bootstrap_ip_port.ip.i = inet_addr(argv[1]); DHT_bootstrap(m->dht, bootstrap_ip_port, hex_string_to_bin(argv[3])); } else { FILE *file = fopen(argv[1], "rb"); if ( file == NULL ) { return 1; } int read; uint8_t buffer[128000]; read = fread(buffer, 1, 128000, file); printf("Messenger loaded: %i\n", Messenger_load(m, buffer, read)); fclose(file); } m_callback_friendrequest(m, print_request, NULL); m_callback_friendmessage(m, print_message, NULL); printf("OUR ID: "); uint32_t i; uint8_t address[FRIEND_ADDRESS_SIZE]; getaddress(m, address); for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) { if (address[i] < 16) printf("0"); printf("%hhX", address[i]); } setname(m, (uint8_t *)"Anon", 5); char temp_id[128]; printf("\nEnter the address of the friend you wish to add (38 bytes HEX format):\n"); if (scanf("%s", temp_id) != 1) { return 1; } int num = m_addfriend(m, hex_string_to_bin(temp_id), (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); perror("Initialization"); while (1) { uint8_t name[128]; getname(m, num, name); printf("%s\n", name); m_sendmessage(m, num, (uint8_t *)"Test", 5); doMessenger(m); c_sleep(30); FILE *file = fopen("Save.bak", "wb"); if ( file == NULL ) { return 1; } uint8_t *buffer = malloc(Messenger_size(m)); Messenger_save(m, buffer); size_t write_result = fwrite(buffer, 1, Messenger_size(m), file); if (write_result < Messenger_size(m)) { return 1; } free(buffer); fclose(file); } cleanupMessenger(m); }
int main(int argc, char *argv[]) { FILE *pf; char buff[BUFSIZE]; int length, c, i, j; char *delim, *delim2; pid_t pid; struct timespec ts; int upp; #ifdef HAVE_GETOPT_LONG int option_index = 0; static struct option l_opts[] = { {"help", 0, 0, 'X'}, {"version", 0, 0, 'V'}, {"filename", 1, 0, 'f'}, {"sip-uri", 1, 0, 's'}, {"traceroute-mode", 0, 0, 'T'}, {"usrloc-mode", 0, 0, 'U'}, {"invite-mode", 0, 0, 'I'}, {"message-mode", 0, 0, 'M'}, {"contact", 1, 0, 'C'}, {"appendix-begin", 1, 0, 'b'}, {"appendix-end", 1, 0, 'e'}, {"sleep", 1, 0, 'o'}, {"expires", 1, 0, 'x'}, {"remove-bindings", 0, 0, 'z'}, {"flood-mode", 0, 0, 'F'}, {"cseq-max", 1, 0, 'c'}, {"random-mode", 0, 0, 'R'}, {"trash-chars", 1, 0, 't'}, {"local-port", 1, 0, 'l'}, {"remote-port", 1, 0, 'r'}, {"outbound-proxy", 1, 0, 'p'}, {"hostname", 1, 0, 'H'}, {"max-fowards", 1, 0, 'm'}, {"numeric", 0, 0, 'n'}, {"no-via", 0, 0, 'i'}, {"password", 1, 0, 'a'}, {"ignore-redirects", 0, 0, 'd'}, {"verbose", 0, 0, 'v'}, {"extract-ip", 0, 0, 'w'}, {"replace-string", 0, 0, 'g'}, {"replace", 0, 0, 'G'}, {"nagios-code", 0, 0, 'N'}, {"nagios-warn", 1, 0, 'W'}, {"search", 1, 0, 'q'}, {"message-body", 1, 0, 'B'}, {"disposition", 1, 0, 'O'}, {"processes", 1, 0, 'P'}, {"auth-username", 1, 0, 'u'}, {"no-crlf", 0, 0, 'L'}, {"timing", 0, 0, 'A'}, {0, 0, 0, 0} }; #endif /* some initialisation to be shure */ file_b=uri_b=trace=lport=usrloc=flood=verbose=randtrash=trashchar = 0; warning_ext=rand_rem=nonce_count=replace_b=invite=message = 0; sleep_ms=empty_contact=nagios_warn=timing = 0; namebeg=nameend=maxforw= -1; numeric=via_ins=redirects=fix_crlf=processes = 1; username=password=replace_str=hostname=contact_uri=mes_body = NULL; con_dis=auth_username = NULL; re = NULL; address = 0; rport = 5060; expires_t = USRLOC_EXP_DEF; memset(buff, 0, BUFSIZE); memset(confirm, 0, BUFSIZE); memset(ack, 0, BUFSIZE); memset(fqdn, 0, FQDN_SIZE); memset(messusern, 0, FQDN_SIZE); if (argc==1) print_help(); /* lots of command line switches to handle*/ #ifdef HAVE_GETOPT_LONG while ((c=getopt_long(argc, argv, "a:AB:b:c:C:de:f:Fg:GhH:iIl:Lm:MnNo:O:p:P:q:r:Rs:t:Tu:UvVwW:x:z", l_opts, &option_index)) != EOF){ #else while ((c=getopt(argc,argv,"a:AB:b:c:C:de:f:Fg:GhH:iIl:Lm:MnNo:O:p:P:q:r:Rs:t:Tu:UvVwW:x:z")) != EOF){ #endif switch(c){ case 'a': password=malloc(strlen(optarg)); strncpy(password, optarg, strlen(optarg)); *(password+strlen(optarg)) = '\0'; break; case 'A': timing=1; break; case 'b': if ((namebeg=atoi(optarg))==-1) { printf("error: non-numerical appendix begin for the " "username\n"); exit_code(2); } break; case 'B': mes_body=malloc(strlen(optarg)); strncpy(mes_body, optarg, strlen(optarg)); *(mes_body+strlen(optarg)) = '\0'; break; case 'C': if (!strncmp(optarg, "empty", 5) || !strncmp(optarg, "none", 4)) { empty_contact = 1; } else if (((delim=STRSTR(optarg,"sip:"))!=NULL) || ((delim=STRSTR(optarg,"sips:"))!=NULL)) { if (strchr(optarg,'@')<delim) { printf("error: missing '@' in Contact uri\n"); exit_code(2); } else { contact_uri=malloc(strlen(optarg)+1); memset(contact_uri, 0, strlen(optarg)+1); strncpy(contact_uri, optarg, strlen(optarg)); } } else if ((strlen(optarg)==1) && (!strncmp(optarg, "*", 1))) { contact_uri=malloc(strlen(optarg)+1); memset(contact_uri, 0, strlen(optarg)+1); strncpy(contact_uri, optarg, strlen(optarg)); } else{ printf("error: REGISTER Contact uri doesn't not contain " "sip:, *, or is not empty\n"); exit_code(2); } break; case 'c': if ((namebeg=atoi(optarg))==-1) { printf("error: non-numerical CSeq maximum\n"); exit_code(2); } break; case 'd': redirects=0; break; case 'e': if ((nameend=atoi(optarg))==-1) { printf("error: non-numerical appendix end for the " "username\n"); exit_code(2); } break; case 'F': flood=1; break; case 'f': if (strncmp(optarg, "-", 1)) { /* file is opened in binary mode so that the cr-lf is preserved */ pf = fopen(optarg, "rb"); if (!pf){ printf("error: unable to open the file '%s'.\n", optarg); exit_code(2); } length = fread(buff, 1, sizeof(buff), pf); if (length >= sizeof(buff)){ printf("error:the file is too big. try files of less " "than %i bytes.\n", BUFSIZE); printf(" or recompile the program with bigger " "BUFSIZE defined.\n"); exit_code(2); } fclose(pf); } else { for(i = 0; i < BUFSIZE - 1; i++) { j = getchar(); if (j == EOF) break; else buff[i] = j; } length = i; } buff[length] = '\0'; file_b=1; break; case 'g': replace_str=optarg; break; case 'G': replace_b=1; break; case 'h': print_help(); break; case 'H': hostname=optarg; break; case 'i': via_ins=0; break; case 'I': invite=1; break; case 'l': lport=atoi(optarg); if (!lport) { printf("error: non-numerical local port number"); exit_code(2); } break; case 'L': fix_crlf=0; break; case 'm': maxforw=atoi(optarg); if (maxforw==-1) { printf("error: non-numerical number of max-forwards\n"); exit_code(2); } break; case 'M': message=1; break; case 'n': numeric = 0; break; case 'N': exit_mode=EM_NAGIOS; break; case 'o': sleep_ms = 0; if (strncmp(optarg, "rand", 4)==0) { sleep_ms = -2; } else { sleep_ms = atoi(optarg); if (!sleep_ms) { printf("error: non-numerical sleep value\n"); exit_code(2); } } break; case 'O': con_dis=malloc(strlen(optarg)); strncpy(con_dis, optarg, strlen(optarg)); *(con_dis+strlen(optarg)) = '\0'; break; case 'p': address = getaddress(optarg); break; case 'P': processes=atoi(optarg); if (!processes) { printf("error: non-numerical number of processes\n"); exit_code(2); } break; case 'q': if (re) { /* previously allocated -- free */ regfree(re); } else { /* never tried -- allocate */ re=malloc(sizeof(regex_t)); }; if (!re) { fprintf(stderr, "Error: can't allocate RE\n"); exit_code(2); }; if (regcomp(re, optarg, REG_EXTENDED|REG_ICASE|REG_NEWLINE )!=0) { fprintf(stderr, "Error: compiling RE: %s\n", optarg ); exit_code(2); }; break; case 'r': rport=atoi(optarg); if (!rport) { printf("error: non-numerical remote port number\n"); exit_code(2); } break; case 'R': randtrash=1; break; case 's': /* we try to extract as much informationas we can from the uri*/ if (!strncmp(optarg,"sip",3)){ if ((delim=strchr(optarg,':'))!=NULL){ delim++; if ((delim2=strchr(delim,'@'))!=NULL){ username=malloc(delim2-delim+1); strncpy(username, delim, delim2-delim); *(username+(delim2-delim)) = '\0'; delim2++; delim=delim2; } if ((delim2=strchr(delim,':'))!=NULL){ domainname=malloc(strlen(delim)+1); strncpy(domainname, delim, strlen(delim)); *(domainname+strlen(delim)) = '\0'; *delim2 = '\0'; delim2++; rport = atoi(delim2); if (!rport) { printf("error: non-numerical remote port " "number\n"); exit_code(2); } } else { domainname=malloc(strlen(delim)+1); strncpy(domainname, delim, strlen(delim)); *(domainname+strlen(delim)) = '\0'; } if (!address) address = getaddress(delim); if (!address){ printf("error:unable to determine the remote host " "address\n"); exit_code(2); } } else{ printf("error: SIPURI doesn't contain a : ?!\n"); exit_code(2); } } else{ printf("error: SIPURI doesn't not begin with sip\n"); exit_code(2); } uri_b=1; break; break; case 't': trashchar=atoi(optarg); if (!trashchar) { printf("error: non-numerical number of trashed " "character\n"); exit_code(2); } break; case 'T': trace=1; break; case 'U': usrloc=1; break; case 'u': auth_username=malloc(strlen(optarg)); strncpy(auth_username, optarg, strlen(optarg)); *(auth_username+strlen(optarg)) = '\0'; break; case 'v': verbose++; break; case 'V': printf("sipsak %s by Nils Ohlmeier\n Copyright (C) 2002-2004" " FhG Fokus\n Copyright (C) 2004-2005 Nils Ohlmeier\n", SIPSAK_VERSION); printf(" compiled with DEFAULT_RETRYS=%i, DEFAULT_TIMEOUT=%i, FQDN_SIZE=%i", DEFAULT_RETRYS, DEFAULT_TIMEOUT, FQDN_SIZE); #ifdef RAW_SUPPORT printf(", RAW_SUPPORT"); #endif #ifdef HAVE_GETOPT_LONG printf(", LONG_OPTS"); #endif printf("\n"); exit_code(0); break; case 'w': warning_ext=1; break; case 'W': nagios_warn = atoi(optarg); break; case 'x': expires_t=atoi(optarg); break; #ifdef HAVE_GETOPT_LONG case 'X': print_long_help(); break; #endif case 'z': rand_rem=1; break; default: printf("error: unknown parameter %c\n", c); exit_code(2); break; } } /* replace LF with CRLF if we read from a file */ if ((file_b) && (fix_crlf)) { insert_cr(buff); } /* lots of conditions to check */ if (trace) { if (usrloc || flood || randtrash) { printf("error: trace can't be combined with usrloc, random or " "flood\n"); exit_code(2); } if (!uri_b) { printf("error: for trace mode a SIPURI is realy needed\n"); exit_code(2); } if (file_b) { printf("warning: file will be ignored for tracing."); } if (!username) { printf("error: for trace mode without a file the SIPURI have to " "contain a username\n"); exit_code(2); } if (!via_ins){ printf("warning: Via-Line is needed for tracing. Ignoring -i\n"); via_ins=1; } if (!warning_ext) { printf("warning: IP extract from warning activated to be more " "informational\n"); warning_ext=1; } if (maxforw==-1) maxforw=255; } else if (usrloc || invite || message) { if (trace || flood || randtrash) { printf("error: usrloc can't be combined with trace, random or " "flood\n"); exit_code(2); } if (!username || !uri_b) { printf("error: for the USRLOC mode you have to give a SIPURI with " "a username\n at least\n"); exit_code(2); } if (namebeg>0 && nameend==-1) { printf("error: if a starting numbers is given also an ending " "number have to be specified\n"); exit_code(2); } if (invite && message) { printf("error: invite and message tests are XOR\n"); exit_code(2); } if (!usrloc && invite && !lport) { printf("WARNING: Do NOT use the usrloc invite mode without " "registering sipsak before.\n See man page for " "details.\n"); exit_code(2); } if (contact_uri!=NULL) { if (invite || message) { printf("error: Contact uri is not support for invites or " "messages\n"); exit_code(2); } if (nameend!=-1 || namebeg!=-1) { printf("warning: ignoring starting or ending number if Contact" " is given\n"); nameend=namebeg=0; } if (rand_rem) { printf("warning: ignoring -z option when Contact is given\n"); rand_rem=0; } } if (via_ins) { printf("warning: ignoring -i option when in usrloc mode\n"); via_ins=0; } if (nameend==-1) nameend=0; if (namebeg==-1) namebeg=0; } else if (flood) { if (trace || usrloc || randtrash) { printf("error: flood can't be combined with trace, random or " "usrloc\n"); exit_code(2); } if (!uri_b) { printf("error: we need at least a sip uri for flood\n"); exit_code(2); } if (redirects) { printf("warning: redirects are not expected in flood. " "disableing\n"); redirects=0; } } else if (randtrash) { if (trace || usrloc || flood) { printf("error: random can't be combined with trace, flood or " "usrloc\n"); exit_code(2); } if (!uri_b) { printf("error: need at least a sip uri for random\n"); exit_code(2); } if (redirects) { printf("warning: redirects are not expected in random. " "disableing\n"); redirects=0; } if (verbose) { printf("warning: random characters may destroy your terminal " "output\n"); } } else if (mes_body) { if (!message) { printf("warning: to send a message mode (-M) is required. activating\n"); message=1; } if (!uri_b) { printf("error: need at least a sip uri to send a meesage\n"); exit_code(2); } if (nameend==-1) nameend=0; if (namebeg==-1) namebeg=0; } else { if (!uri_b) { printf("error: a spi uri is needed at least\n"); exit_code(2); } } /* determine our hostname */ get_fqdn(); /* this is not a cryptographic random number generator, but hey this is only a test-tool => should be satisfying*/ srand(time(0)); if (processes > 1) { if (signal(SIGCHLD , sigchld_handler) == SIG_ERR ) { printf("error: Could not install SIGCHLD handler\n"); exit_code(2); } } for(i = 0; i < processes - 1; i++) { if ((pid = fork()) < 0) { printf("error: Cannot fork\n"); exit_code(2); } if (pid == 0){ /* child */ upp = (nameend - namebeg + 1) / processes; namebeg = namebeg + upp * i; nameend = namebeg + upp; shoot(buff); } else { if (lport) { lport++; } } /* Delay execution of children so that the * time of the first transmission gets spread over * the retransmission interval evenly */ ts.tv_sec = 0; ts.tv_nsec = (float)DEFAULT_TIMEOUT / (float)processes * (float)1000 * (float)1000; nanosleep(&ts, 0); } /* here we go...*/ if (processes > 1) { upp = (nameend - namebeg + 1) / processes; namebeg = namebeg + upp * i; nameend = namebeg + upp; } shoot(buff); /* normaly we won't come back here, but to satisfy the compiler */ return 0; }
/* * returns a FRIEND_ADDRESS_SIZE byte address to give to others. * Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] * */ void tox_getaddress(void *tox, uint8_t *address) { Messenger *m = tox; getaddress(m, address); }
int main(int argc, char *argv[]) { FILE *pf; char buff[BUFSIZE]; int length, c; char *delim, *delim2; /* some initialisation to be shure */ file_b=uri_b=trace=lport=usrloc=flood=verbose=randtrash=trashchar = 0; numeric=warning_ext=rand_rem=nonce_count=replace_b=invite=message = 0; sleep_ms = 0; namebeg=nameend=maxforw= -1; via_ins=redirects = 1; username=password=replace_str=hostname = NULL; address = 0; rport = 5060; expires_t = USRLOC_EXP_DEF; memset(buff, 0, BUFSIZE); memset(confirm, 0, BUFSIZE); memset(ack, 0, BUFSIZE); memset(fqdn, 0, FQDN_SIZE); memset(messusern, 0, FQDN_SIZE); if (argc==1) print_help(); /* lots of command line switches to handle*/ while ((c=getopt(argc,argv,"a:b:c:de:f:Fg:GhH:iIl:m:Mno:r:Rs:t:TUvVwx:z")) != EOF){ switch(c){ case 'a': password=malloc(strlen(optarg)); strncpy(password, optarg, strlen(optarg)); break; case 'b': if ((namebeg=atoi(optarg))==-1) { printf("error: non-numerical appendix begin for the " "username\n"); exit(2); } break; case 'c': if ((namebeg=atoi(optarg))==-1) { printf("error: non-numerical CSeq maximum\n"); exit(2); } break; case 'd': redirects=0; break; case 'e': if ((nameend=atoi(optarg))==-1) { printf("error: non-numerical appendix end for the " "username\n"); exit(2); } break; case 'F': flood=1; break; case 'f': /* file is opened in binary mode so that the cr-lf is preserved */ pf = fopen(optarg, "rb"); if (!pf){ puts("unable to open the file.\n"); exit(2); } length = fread(buff, 1, sizeof(buff), pf); if (length >= sizeof(buff)){ printf("error:the file is too big. try files of less " "than %i bytes.\n", BUFSIZE); printf(" or recompile the program with bigger " "BUFSIZE defined.\n"); exit(2); } fclose(pf); buff[length] = '\0'; file_b=1; break; case 'g': replace_str=optarg; break; case 'G': replace_b=1; break; case 'h': print_help(); break; case 'H': hostname=optarg; break; case 'i': via_ins=0; break; case 'I': invite=1; break; case 'l': lport=atoi(optarg); if (!lport) { puts("error: non-numerical local port number"); exit(2); } break; case 'm': maxforw=atoi(optarg); if (maxforw==-1) { printf("error: non-numerical number of max-forwards\n"); exit(2); } break; case 'M': message=1; break; case 'n': numeric = 1; break; case 'o': sleep_ms = 0; if (strncmp(optarg, "rand", 4)==0) { sleep_ms = -2; } else { sleep_ms = atoi(optarg); if (!sleep_ms) { printf("error: non-numerical sleep value\n"); exit(2); } } break; case 'r': rport=atoi(optarg); if (!rport) { printf("error: non-numerical remote port number\n"); exit(2); } break; case 'R': randtrash=1; break; case 's': /* we try to extract as much informationas we can from the uri*/ if (!strncmp(optarg,"sip",3)){ if ((delim=strchr(optarg,':'))!=NULL){ delim++; if ((delim2=strchr(delim,'@'))!=NULL){ username=malloc(delim2-delim+1); strncpy(username, delim, delim2-delim); *(username+(delim2-delim)) = '\0'; delim2++; delim=delim2; } if ((delim2=strchr(delim,':'))!=NULL){ domainname=malloc(strlen(delim)+1); strncpy(domainname, delim, strlen(delim)); *(domainname+strlen(delim)) = '\0'; *delim2 = '\0'; delim2++; rport = atoi(delim2); if (!rport) { printf("error: non-numerical remote port " "number\n"); exit(2); } } else { domainname=malloc(strlen(delim)+1); strncpy(domainname, delim, strlen(delim)); *(domainname+strlen(delim)) = '\0'; } address = getaddress(delim); if (!address){ printf("error:unable to determine the remote host " "address\n"); exit(2); } } else{ printf("error: sip:uri doesn't contain a : ?!\n"); exit(2); } } else{ printf("error: sip:uri doesn't not begin with sip\n"); exit(2); } uri_b=1; break; break; case 't': trashchar=atoi(optarg); if (!trashchar) { printf("error: non-numerical number of trashed " "character\n"); exit(2); } break; case 'T': trace=1; break; case 'U': usrloc=1; break; case 'v': verbose++; break; case 'V': printf("sipsak %s by Nils Ohlmeier\n Copyright (C) 2002-2003" " FhG Fokus\n", SIPSAK_VERSION); exit(0); break; case 'w': warning_ext=1; break; case 'x': expires_t=atoi(optarg); break; case 'z': rand_rem=1; break; default: printf("error: unknown parameter %c\n", c); exit(2); break; } } /* lots of conditions to check */ if (trace) { if (usrloc || flood || randtrash) { printf("error: trace can't be combined with usrloc, random or " "flood\n"); exit(2); } if (!uri_b) { printf("error: for trace mode a sip:uri is realy needed\n"); exit(2); } if (file_b) { printf("warning: file will be ignored for tracing."); } if (!username) { printf("error: for trace mode without a file the sip:uir have to " "contain a username\n"); exit(2); } if (!via_ins){ printf("warning: Via-Line is needed for tracing. Ignoring -i\n"); via_ins=1; } if (!warning_ext) { printf("warning: IP extract from warning activated to be more " "informational\n"); warning_ext=1; } if (maxforw==-1) maxforw=255; } else if (usrloc || invite || message) { if (trace || flood || randtrash) { printf("error: usrloc can't be combined with trace, random or " "flood\n"); exit(2); } if (!username || !uri_b) { printf("error: for the USRLOC mode you have to give a sip:uri with " "a username\n at least\n"); exit(2); } if (namebeg>0 && nameend==-1) { printf("error: if a starting numbers is given also an ending " "number have to be specified\n"); exit(2); } if (invite && message) { printf("error: invite and message tests are XOR\n"); exit(2); } if (!usrloc && invite && !lport) { printf("WARNING: Do NOT use the usrloc invite mode without " "registering sipsak before.\n See man page for " "details.\n"); exit(2); } if (via_ins) { via_ins=0; } if (redirects) { printf("warning: redirects are not expected in USRLOC. " "disableing\n"); redirects=0; } if (nameend==-1) nameend=0; if (namebeg==-1) namebeg=0; } else if (flood) { if (trace || usrloc || randtrash) { printf("error: flood can't be combined with trace, random or " "usrloc\n"); exit(2); } if (!uri_b) { printf("error: we need at least a sip uri for flood\n"); exit(2); } if (redirects) { printf("warning: redirects are not expected in flood. " "disableing\n"); redirects=0; } } else if (randtrash) { if (trace || usrloc || flood) { printf("error: random can't be combined with trace, flood or " "usrloc\n"); exit(2); } if (!uri_b) { printf("error: need at least a sip uri for random\n"); exit(2); } if (redirects) { printf("warning: redirects are not expected in random. " "disableing\n"); redirects=0; } if (verbose) { printf("warning: random characters may destroy your terminal " "output\n"); } } else { if (!uri_b) { printf("error: a spi uri is needed at least\n"); exit(2); } /* if (!(username || file_b)) { printf("error: ether a file or an username in the sip uri is " "required\n"); exit(2); }*/ } /* determine our hostname */ get_fqdn(); /* this is not a cryptographic random number generator, but hey this is only a test-tool => should be satisfying*/ srand(time(0)); /* here we go...*/ shoot(buff); /* normaly we won't come back here, but to satisfy the compiler */ return 0; }
/* * returns a FRIEND_ADDRESS_SIZE byte address to give to others. * Format: [client_id (32 bytes)][nospam number (4 bytes)][checksum (2 bytes)] * */ void tox_get_address(const Tox *tox, uint8_t *address) { const Messenger *m = tox; getaddress(m, address); }
void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd) { if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { wclear(self->window); wclear(ctx->history); int x, y; getmaxyx(self->window, y, x); (void) x; wmove(self->window, y - CURS_Y_OFFSET, 0); } else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h")) print_help(ctx); else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) { endwin(); exit(0); } else if (!strncmp(cmd, "/me ", strlen("/me "))) { struct tm *timeinfo = get_time(); char *action = strchr(cmd, ' '); if (action == NULL) { wprintw(self->window, "Invalid syntax.\n"); return; } action++; wattron(ctx->history, COLOR_PAIR(2)); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); wattroff(ctx->history, COLOR_PAIR(2)); uint8_t selfname[MAX_NAME_LENGTH]; int len = getself_name(m, selfname, sizeof(selfname)); char msg[MAX_STR_SIZE - len - 4]; snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t *) selfname, action); wattron(ctx->history, COLOR_PAIR(5)); wprintw(ctx->history, msg); wattroff(ctx->history, COLOR_PAIR(5)); if (m_sendaction(m, ctx->friendnum, (uint8_t *) msg, strlen(msg) + 1) < 0) { wattron(ctx->history, COLOR_PAIR(3)); wprintw(ctx->history, " * Failed to send action\n"); wattroff(ctx->history, COLOR_PAIR(3)); } } else if (!strncmp(cmd, "/status ", strlen("/status "))) { char *status = strchr(cmd, ' '); char *msg; char *status_text; if (status == NULL) { wprintw(ctx->history, "Invalid syntax.\n"); return; } status++; USERSTATUS status_kind; if (!strncmp(status, "online", strlen("online"))) { status_kind = USERSTATUS_NONE; status_text = "ONLINE"; } else if (!strncmp(status, "away", strlen("away"))) { status_kind = USERSTATUS_AWAY; status_text = "AWAY"; } else if (!strncmp(status, "busy", strlen("busy"))) { status_kind = USERSTATUS_BUSY; status_text = "BUSY"; } else { wprintw(ctx->history, "Invalid status.\n"); return; } msg = strchr(status, ' '); if (msg == NULL) { m_set_userstatus(m, status_kind); wprintw(ctx->history, "Status set to: %s\n", status_text); } else { msg++; m_set_userstatus(m, status_kind); m_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1); wprintw(ctx->history, "Status set to: %s, %s\n", status_text, msg); } } else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { char *nick; nick = strchr(cmd, ' '); if (nick == NULL) { wprintw(ctx->history, "Invalid syntax.\n"); return; } nick++; setname(m, (uint8_t *) nick, strlen(nick) + 1); wprintw(ctx->history, "Nickname set to: %s\n", nick); } else if (!strcmp(cmd, "/myid")) { char id[FRIEND_ADDRESS_SIZE * 2 + 1] = {0}; int i; uint8_t address[FRIEND_ADDRESS_SIZE]; getaddress(m, address); for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) { char xx[3]; snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff); strcat(id, xx); } wprintw(ctx->history, "%s\n", id); } else if (strcmp(ctx->line, "/close") == 0) { int f_num = ctx->friendnum; delwin(ctx->linewin); del_window(self); disable_chatwin(f_num); } else wprintw(ctx->history, "Invalid command.\n"); }
Socket::Error UDPSocket::join(const IPV4Multicast &ia,int InterfaceIndex) { join(Socket::address(getaddress(ia)), InterfaceIndex); }
int main(int argc, char *argv[]) { /* let user override default by cmdline */ uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled); if (argvoffset < 0) exit(1); /* with optional --ipvx, now it can be 1-4 arguments... */ if ((argc != argvoffset + 2) && (argc != argvoffset + 4)) { printf("Usage: %s [--ipv4|--ipv6] ip port public_key (of the DHT bootstrap node)\n", argv[0]); printf("or\n"); printf(" %s [--ipv4|--ipv6] Save.bak (to read Save.bak as state file)\n", argv[0]); exit(0); } m = initMessenger(ipv6enabled); if ( !m ) { fputs("Failed to allocate messenger datastructure\n", stderr); exit(0); } if (argc == argvoffset + 4) { uint16_t port = htons(atoi(argv[argvoffset + 2])); uint8_t *bootstrap_key = hex_string_to_bin(argv[argvoffset + 3]); int res = DHT_bootstrap_from_address(m->dht, argv[argvoffset + 1], ipv6enabled, port, bootstrap_key); free(bootstrap_key); if (!res) { printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]); exit(1); } } else { FILE *file = fopen(argv[argvoffset + 1], "rb"); if ( file == NULL ) { printf("Failed to open \"%s\" - does it exist?\n", argv[argvoffset + 1]); return 1; } int read; uint8_t buffer[128000]; read = fread(buffer, 1, 128000, file); printf("Messenger loaded: %i\n", Messenger_load(m, buffer, read)); fclose(file); } m_callback_friendrequest(m, print_request, NULL); m_callback_friendmessage(m, print_message, NULL); printf("OUR ID: "); uint32_t i; uint8_t address[FRIEND_ADDRESS_SIZE]; getaddress(m, address); for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) { if (address[i] < 16) printf("0"); printf("%hhX", address[i]); } setname(m, (uint8_t *)"Anon", 5); char temp_id[128]; printf("\nEnter the address of the friend you wish to add (38 bytes HEX format):\n"); if (scanf("%s", temp_id) != 1) { return 1; } int num = m_addfriend(m, hex_string_to_bin(temp_id), (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); perror("Initialization"); while (1) { uint8_t name[128]; getname(m, num, name); printf("%s\n", name); m_sendmessage(m, num, (uint8_t *)"Test", 5); doMessenger(m); c_sleep(30); FILE *file = fopen("Save.bak", "wb"); if ( file == NULL ) { return 1; } uint8_t *buffer = malloc(Messenger_size(m)); Messenger_save(m, buffer); size_t write_result = fwrite(buffer, 1, Messenger_size(m), file); if (write_result < Messenger_size(m)) { return 1; } free(buffer); fclose(file); } cleanupMessenger(m); }