Here is a piece of code snippet from tftp
void initsock(int af) {
struct sockaddr_storage s_in;
if (f >= 0)
close(f);
f = socket(af, SOCK_DGRAM, 0);
if (f < 0) {
perror("tftp: socket");
exit(3);
}
printf("protocol family: %d\n", af);
memset(&s_in, 0, sizeof(s_in));
s_in.ss_family = af;
if (bind(f, (struct sockaddr *)&s_in, sizeof (s_in)) < 0) {
perror("tftp: bind");
exit(1);
}
char ip_str[INET6_ADDRSTRLEN] = {0};
inet_ntop(s_in.ss_family, get_in_addr((struct sockaddr *)&s_in), ip_str, sizeof ip_str);
printf("ip address: %s\t port: %d\n", ip_str, get_in_port((struct sockaddr *)&s_in));
}
My question is that why bind() is called here? What's the purpose? The second argument seems empty except for ss_family field, no ip address nor port number.
p.s. Source code from https://packages.ubuntu.com/bionic/tftp